add week stat
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
c3d00b5dec
commit
d1f2c359a2
|
@ -125,17 +125,13 @@ func (db *DB) SetPause(chatID int64, duration time.Duration) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) GetStat(chatID int64) (map[string]int, error) {
|
func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]int, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
if err := db.AddChat(chatID); err != nil {
|
if err := db.AddChat(chatID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
|
||||||
t := time.Now().In(loc)
|
|
||||||
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
|
|
||||||
|
|
||||||
res := map[string]int{}
|
res := map[string]int{}
|
||||||
|
|
||||||
cursor, err := db.workoutsColection.Find(
|
cursor, err := db.workoutsColection.Find(
|
||||||
|
|
|
@ -3,6 +3,7 @@ package stat_bot_state
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
"valera/internal/db"
|
"valera/internal/db"
|
||||||
"valera/internal/states"
|
"valera/internal/states"
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ import (
|
||||||
|
|
||||||
var names = []string{
|
var names = []string{
|
||||||
"/stat",
|
"/stat",
|
||||||
|
"/stat_week",
|
||||||
}
|
}
|
||||||
|
|
||||||
type statBotState struct {
|
type statBotState struct {
|
||||||
|
@ -31,7 +33,14 @@ func (s *statBotState) Do(text string, chatInfo *db.ChatInfo, username string) e
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
s.sendStatToChat(chatInfo.ChatID, "")
|
if text == "/stat" {
|
||||||
|
s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за сегодня:\n", s.getStartDay())
|
||||||
|
}
|
||||||
|
|
||||||
|
if text == "/stat_week" {
|
||||||
|
s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за неделю:\n", s.getStartWeek())
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,13 +55,21 @@ func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Req
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, chatID := range chats {
|
for _, chatID := range chats {
|
||||||
if err := s.sendStatToChat(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\n"); err != nil {
|
t := s.getStartDay()
|
||||||
fmt.Println(err)
|
if err := s.sendStatToChatAfter(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\nРезультаты за сегодня:\n", s.getStartDay()); err != nil {
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
|
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if t.Weekday() == time.Sunday {
|
||||||
|
if err := s.sendStatToChatAfter(chatID, "Результаты за неделю:\n", s.getStartWeek()); err != nil {
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
@ -60,15 +77,28 @@ func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Req
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statBotState) sendStatToChat(chatID int64, prefix string) error {
|
func (s *statBotState) sendStatToChatAfter(chatID int64, prefix string, t time.Time) error {
|
||||||
stat, err := s.dataBase.GetStat(chatID)
|
msgText := prefix
|
||||||
|
stat, err := s.dataBase.GetStatAfter(chatID, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
msgText := prefix + "Результаты за сегодня:\n"
|
|
||||||
for k, v := range stat {
|
for k, v := range stat {
|
||||||
msgText += fmt.Sprintf("- %s: %d\n", k, v)
|
msgText += fmt.Sprintf("- %s: %d\n", k, v)
|
||||||
}
|
}
|
||||||
_, _ = s.bot.Send(tgbot.NewMessage(chatID, msgText))
|
_, _ = s.bot.Send(tgbot.NewMessage(chatID, msgText))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *statBotState) getStartDay() time.Time {
|
||||||
|
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
||||||
|
t := time.Now().In(loc)
|
||||||
|
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *statBotState) getStartWeek() time.Time {
|
||||||
|
t := s.getStartDay()
|
||||||
|
t = t.Add(-6 * 24 * time.Hour)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue