2019-05-05 23:36:49 +03:00
|
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
2020-03-09 19:30:23 +03:00
|
|
|
|
"github.com/jasonlvhit/gocron"
|
2020-09-29 19:55:07 +03:00
|
|
|
|
"github.com/muety/wakapi/config"
|
2020-03-31 13:22:17 +03:00
|
|
|
|
"github.com/muety/wakapi/utils"
|
2020-03-09 19:30:23 +03:00
|
|
|
|
"log"
|
2019-05-09 01:07:38 +03:00
|
|
|
|
"time"
|
2019-05-05 23:36:49 +03:00
|
|
|
|
|
2019-05-11 18:49:56 +03:00
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-03-31 13:22:17 +03:00
|
|
|
|
"github.com/muety/wakapi/models"
|
2019-05-11 18:49:56 +03:00
|
|
|
|
gormbulk "github.com/t-tiger/gorm-bulk-insert"
|
2019-05-05 23:36:49 +03:00
|
|
|
|
)
|
|
|
|
|
|
2020-03-09 19:30:23 +03:00
|
|
|
|
const (
|
|
|
|
|
TableHeartbeat = "heartbeat"
|
|
|
|
|
cleanUpInterval = time.Duration(aggregateIntervalDays) * 2 * 24 * time.Hour
|
|
|
|
|
)
|
2019-05-05 23:36:49 +03:00
|
|
|
|
|
|
|
|
|
type HeartbeatService struct {
|
2020-09-29 19:55:07 +03:00
|
|
|
|
Config *config.Config
|
2019-05-16 23:53:03 +03:00
|
|
|
|
Db *gorm.DB
|
2019-05-05 23:36:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 18:32:26 +03:00
|
|
|
|
func NewHeartbeatService(db *gorm.DB) *HeartbeatService {
|
|
|
|
|
return &HeartbeatService{
|
2020-09-29 19:55:07 +03:00
|
|
|
|
Config: config.Get(),
|
2020-05-24 18:32:26 +03:00
|
|
|
|
Db: db,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-20 16:28:55 +03:00
|
|
|
|
|
2019-05-17 09:40:03 +03:00
|
|
|
|
func (srv *HeartbeatService) InsertBatch(heartbeats []*models.Heartbeat) error {
|
2019-05-11 18:49:56 +03:00
|
|
|
|
var batch []interface{}
|
2019-05-17 09:40:03 +03:00
|
|
|
|
for _, h := range heartbeats {
|
2019-05-19 20:49:27 +03:00
|
|
|
|
batch = append(batch, *h)
|
2019-05-05 23:36:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 18:49:56 +03:00
|
|
|
|
if err := gormbulk.BulkInsert(srv.Db, batch, 3000); err != nil {
|
2019-05-05 23:36:49 +03:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-05-09 01:07:38 +03:00
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
|
func (srv *HeartbeatService) GetAllWithin(from, to time.Time, user *models.User) ([]*models.Heartbeat, error) {
|
2019-05-17 09:40:03 +03:00
|
|
|
|
var heartbeats []*models.Heartbeat
|
2019-05-11 18:49:56 +03:00
|
|
|
|
if err := srv.Db.
|
|
|
|
|
Where(&models.Heartbeat{UserID: user.ID}).
|
2019-05-19 20:49:27 +03:00
|
|
|
|
Where("time >= ?", from).
|
2020-10-16 13:00:20 +03:00
|
|
|
|
Where("time < ?", to).
|
2019-05-20 20:46:41 +03:00
|
|
|
|
Order("time asc").
|
2019-05-19 20:49:27 +03:00
|
|
|
|
Find(&heartbeats).Error; err != nil {
|
2019-05-11 18:49:56 +03:00
|
|
|
|
return nil, err
|
2019-05-09 01:07:38 +03:00
|
|
|
|
}
|
2019-05-17 09:40:03 +03:00
|
|
|
|
return heartbeats, nil
|
2019-05-09 01:07:38 +03:00
|
|
|
|
}
|
2019-10-10 00:26:28 +03:00
|
|
|
|
|
2019-10-11 09:00:02 +03:00
|
|
|
|
// Will return *models.Heartbeat object with only user_id and time fields filled
|
2019-10-10 00:26:28 +03:00
|
|
|
|
func (srv *HeartbeatService) GetFirstUserHeartbeats(userIds []string) ([]*models.Heartbeat, error) {
|
|
|
|
|
var heartbeats []*models.Heartbeat
|
|
|
|
|
if err := srv.Db.
|
|
|
|
|
Table("heartbeats").
|
|
|
|
|
Select("user_id, min(time) as time").
|
|
|
|
|
Where("user_id IN (?)", userIds).
|
|
|
|
|
Group("user_id").
|
|
|
|
|
Scan(&heartbeats).Error; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return heartbeats, nil
|
|
|
|
|
}
|
2020-03-09 19:30:23 +03:00
|
|
|
|
|
|
|
|
|
func (srv *HeartbeatService) DeleteBefore(t time.Time) error {
|
|
|
|
|
if err := srv.Db.
|
|
|
|
|
Where("time <= ?", t).
|
|
|
|
|
Delete(models.Heartbeat{}).Error; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (srv *HeartbeatService) CleanUp() error {
|
2020-09-12 00:24:51 +03:00
|
|
|
|
refTime := utils.StartOfToday().Add(-cleanUpInterval)
|
2020-03-09 19:30:23 +03:00
|
|
|
|
if err := srv.DeleteBefore(refTime); err != nil {
|
|
|
|
|
log.Printf("Failed to clean up heartbeats older than %v – %v\n", refTime, err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
log.Printf("Successfully cleaned up heartbeats older than %v\n", refTime)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (srv *HeartbeatService) ScheduleCleanUp() {
|
|
|
|
|
srv.CleanUp()
|
|
|
|
|
|
|
|
|
|
gocron.Every(1).Day().At("02:30").Do(srv.CleanUp)
|
|
|
|
|
<-gocron.Start()
|
|
|
|
|
}
|