update db and added weight
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2023-04-16 17:08:40 +07:00
parent 3f9447f0b8
commit 09fe5f2324
7 changed files with 86 additions and 25 deletions

2
go.mod
View File

@ -1,6 +1,6 @@
module valera module valera
go 1.18 go 1.20
require ( require (
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible

View File

@ -2,6 +2,7 @@ package db
import ( import (
"context" "context"
"fmt"
"log" "log"
"time" "time"
@ -152,7 +153,7 @@ func (db *DB) SetPause(chatID int64, duration time.Duration) error {
return err return err
} }
func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error) { func (db *DB) GetStatBetween(chatID int64, startTime time.Time, endTime time.Time) (map[string]float64, error) {
ctx := context.Background() ctx := context.Background()
if err := db.AddChat(chatID); err != nil { if err := db.AddChat(chatID); err != nil {
@ -165,8 +166,16 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error
ctx, ctx,
bson.M{ bson.M{
"chat_id": chatID, "chat_id": chatID,
"created_at": bson.M{"$gt": t}, "created_at": bson.M{
"$gte": startTime,
"$lt": endTime,
}, },
},
options.Find().SetSort(
bson.M{
"created_at": 1,
},
),
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -176,6 +185,7 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error
if err := cursor.Decode(&result); err != nil { if err := cursor.Decode(&result); err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(result)
res[result.Name] += float64(result.Count) res[result.Name] += float64(result.Count)
} }
if err := cursor.Err(); err != nil { if err := cursor.Err(); err != nil {
@ -186,8 +196,16 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error
ctx, ctx,
bson.M{ bson.M{
"chat_id": chatID, "chat_id": chatID,
"created_at": bson.M{"$gt": t}, "created_at": bson.M{
"$gte": startTime,
"$lt": endTime,
}, },
},
options.Find().SetSort(
bson.M{
"created_at": 1,
},
),
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -207,8 +225,16 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error
ctx, ctx,
bson.M{ bson.M{
"chat_id": chatID, "chat_id": chatID,
"created_at": bson.M{"$gt": t}, "created_at": bson.M{
"$gte": startTime,
"$lt": endTime,
}, },
},
options.Find().SetSort(
bson.M{
"created_at": 1,
},
),
) )
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1,14 +1,36 @@
package times package times
import "time" import (
"time"
)
var (
loc *time.Location
)
func init() {
loc, _ = time.LoadLocation("Asia/Novosibirsk")
}
func GetNow() time.Time {
t := time.Now().In(loc)
return t
}
func GetStartDay() time.Time { func GetStartDay() time.Time {
loc, _ := time.LoadLocation("Asia/Novosibirsk") t := GetNow()
t := time.Now().In(loc)
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc) t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
return t return t
} }
func GetStartDayMinus(days int) time.Time {
t := GetStartDay()
for i := 0; i < days; i++ {
t = t.Add(-24 * time.Hour)
}
return t
}
func GetStartWeek() time.Time { func GetStartWeek() time.Time {
t := GetStartDay() t := GetStartDay()
t = t.Add(-6 * 24 * time.Hour) t = t.Add(-6 * 24 * time.Hour)

View File

@ -2,11 +2,12 @@ package clear_bot_state
import ( import (
"net/http" "net/http"
"valera/internal/db"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api" tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/states"
) )
var names = []string{ var names = []string{

View File

@ -4,12 +4,13 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api" tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
) )
var names = []string{ var names = []string{
@ -39,7 +40,7 @@ func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo) error {
log.Println(err) log.Println(err)
return nil return nil
} }
stat, err := s.dataBase.GetStatAfter(chatInfo.ChatID, times.GetStartDay()) stat, err := s.dataBase.GetStatBetween(chatInfo.ChatID, times.GetStartDay(), times.GetNow())
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil return nil

View File

@ -4,13 +4,13 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
"valera/internal/db"
"valera/internal/states"
"valera/internal/modules/times"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api" tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
) )
var names = []string{ var names = []string{
@ -81,7 +81,7 @@ func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Req
func (s *statBotState) sendStatToChatAfter(chatID int64, prefix string, t time.Time) error { func (s *statBotState) sendStatToChatAfter(chatID int64, prefix string, t time.Time) error {
msgText := prefix msgText := prefix
stat, err := s.dataBase.GetStatAfter(chatID, t) stat, err := s.dataBase.GetStatBetween(chatID, t, times.GetNow())
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,11 +6,13 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"valera/internal/db"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api" tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
) )
var names = []string{ var names = []string{
@ -44,7 +46,16 @@ func (s *weightBotState) Do(text string, chatInfo *db.ChatInfo) error {
log.Println(err) log.Println(err)
return nil return nil
} }
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("%vкг, записал.", weight))) stat, err := s.dataBase.GetStatBetween(chatInfo.ChatID, times.GetStartDayMinus(1), times.GetStartDay())
if err != nil {
log.Println(err)
return nil
}
dWeight := 0.0
if stat["Вес кг"] != 0 {
dWeight = weight - stat["Вес кг"]
}
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("%vкг, записал.\nИзменение веса: %vкг", weight, dWeight)))
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone) return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone)
} }