add value graph
This commit is contained in:
72
bot.go
72
bot.go
@@ -9,6 +9,10 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gonum.org/v1/plot"
|
||||
"gonum.org/v1/plot/plotter"
|
||||
"gonum.org/v1/plot/plotutil"
|
||||
"gonum.org/v1/plot/vg"
|
||||
tb "gopkg.in/tucnak/telebot.v2"
|
||||
)
|
||||
|
||||
@@ -60,6 +64,21 @@ func (b *Bot) SendChat(chatID int64, text string) {
|
||||
logErrorDebug(err, "Bot.SendChat()")
|
||||
}
|
||||
|
||||
func (b *Bot) SendChatImage(chatID int64, image string) {
|
||||
opt := tb.SendOptions{
|
||||
ParseMode: tb.ModeDefault,
|
||||
}
|
||||
|
||||
ch := tb.Chat{
|
||||
ID: chatID,
|
||||
}
|
||||
|
||||
photo := &tb.Photo{File: tb.FromDisk(image)}
|
||||
|
||||
_, err := b.bot.Send(&ch, photo, &opt)
|
||||
logErrorDebug(err, "Bot.SendChatImage()")
|
||||
}
|
||||
|
||||
func (b *Bot) BotHandlers() {
|
||||
|
||||
b.bot.Handle("/pause", botPause)
|
||||
@@ -86,6 +105,8 @@ func (b *Bot) BotHandlers() {
|
||||
b.bot.Handle("/take", botTake)
|
||||
b.bot.Handle("/transfer", botTransfer)
|
||||
|
||||
b.bot.Handle("/value", botGraphValue)
|
||||
|
||||
b.bot.Handle(tb.OnPhoto, botPhoto)
|
||||
b.bot.Handle(tb.OnChannelPost, botChannelPost)
|
||||
b.bot.Handle(tb.OnQuery, botQuery)
|
||||
@@ -860,6 +881,57 @@ func botRegister(m *tb.Message) {
|
||||
return
|
||||
}
|
||||
|
||||
func botGraphValue(m *tb.Message) {
|
||||
var vals map[uint8]plotter.XYs
|
||||
vals = make(map[uint8]plotter.XYs)
|
||||
for coID, dStats := range cfg.Stats {
|
||||
if cfg.CompanyIsRegistered(coID) {
|
||||
vals[coID] = make(plotter.XYs, 0)
|
||||
for dStr, stat := range dStats {
|
||||
d, err := time.Parse("20060102", dStr)
|
||||
logErrorDebug(err, "botGraphValue : time.Parse")
|
||||
if err != nil {
|
||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("time.Parse : %s", err))
|
||||
return
|
||||
}
|
||||
dateFloat := float64(d.Year()) + float64(d.Month()-1)/12
|
||||
valueFloat := float64(stat.CompanyValueLastQuarter)
|
||||
pt := plotter.XY{
|
||||
X: dateFloat,
|
||||
Y: valueFloat,
|
||||
}
|
||||
vals[coID] = append(vals[coID], pt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p := plot.New()
|
||||
p.Title.Text = "Company Values"
|
||||
p.X.Label.Text = "Year"
|
||||
p.Y.Label.Text = "Value"
|
||||
|
||||
for coID, xys := range vals {
|
||||
cc := cfg.GetCompanyClient(coID)
|
||||
err := plotutil.AddLinePoints(p, cc.Username, xys)
|
||||
logErrorDebug(err, "Stats.ValueGraph : plotutil.AddLinePoints")
|
||||
if err != nil {
|
||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("plotutil.AddLinePoints : %s", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err := p.Save(4*vg.Inch, 4*vg.Inch, "/app/data/points.png")
|
||||
logErrorDebug(err, "Stats.ValueGraph : plot.Save")
|
||||
if err != nil {
|
||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("plot.Save : %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
bot.SendChatImage(m.Chat.ID, "/app/data/points.png")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func PrintText(m *tb.Message) {
|
||||
logInfoDebug("[%d] %s(%d) | %s(%d) : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user