2019-05-05 23:36:49 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
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"
|
2019-05-05 23:36:49 +03:00
|
|
|
"github.com/n1try/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
|
|
|
)
|
|
|
|
|
2019-05-06 01:40:41 +03:00
|
|
|
const TableHeartbeat = "heartbeat"
|
2019-05-05 23:36:49 +03:00
|
|
|
|
|
|
|
type HeartbeatService struct {
|
2019-05-16 23:53:03 +03:00
|
|
|
Cofnig *models.Config
|
|
|
|
Db *gorm.DB
|
2019-05-05 23:36:49 +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).
|
|
|
|
Where("time <= ?", to).
|
|
|
|
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
|
|
|
}
|