2019-05-05 23:36:49 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2020-09-29 19:55:07 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
2020-11-01 18:56:36 +03:00
|
|
|
"github.com/muety/wakapi/repositories"
|
2019-05-09 01:07:38 +03:00
|
|
|
"time"
|
2019-05-05 23:36:49 +03:00
|
|
|
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2019-05-05 23:36:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type HeartbeatService struct {
|
2020-11-01 22:14:10 +03:00
|
|
|
config *config.Config
|
2020-11-08 12:12:49 +03:00
|
|
|
repository repositories.IHeartbeatRepository
|
|
|
|
languageMappingSrvc ILanguageMappingService
|
2019-05-05 23:36:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
func NewHeartbeatService(heartbeatRepo repositories.IHeartbeatRepository, languageMappingService ILanguageMappingService) *HeartbeatService {
|
2020-05-24 18:32:26 +03:00
|
|
|
return &HeartbeatService{
|
2020-11-01 22:14:10 +03:00
|
|
|
config: config.Get(),
|
|
|
|
repository: heartbeatRepo,
|
|
|
|
languageMappingSrvc: languageMappingService,
|
2020-05-24 18:32:26 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
func (srv *HeartbeatService) Insert(heartbeat *models.Heartbeat) error {
|
|
|
|
return srv.repository.InsertBatch([]*models.Heartbeat{heartbeat})
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:40:03 +03:00
|
|
|
func (srv *HeartbeatService) InsertBatch(heartbeats []*models.Heartbeat) error {
|
2020-11-01 18:56:36 +03:00
|
|
|
return srv.repository.InsertBatch(heartbeats)
|
2019-05-05 23:36:49 +03:00
|
|
|
}
|
2019-05-09 01:07:38 +03:00
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
func (srv *HeartbeatService) CountByUser(user *models.User) (int64, error) {
|
|
|
|
return srv.repository.CountByUser(user)
|
|
|
|
}
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
func (srv *HeartbeatService) GetAllWithin(from, to time.Time, user *models.User) ([]*models.Heartbeat, error) {
|
2020-11-01 22:14:10 +03:00
|
|
|
heartbeats, err := srv.repository.GetAllWithin(from, to, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return srv.augmented(heartbeats, user.ID)
|
2019-05-09 01:07:38 +03:00
|
|
|
}
|
2019-10-10 00:26:28 +03:00
|
|
|
|
2021-02-06 02:31:30 +03:00
|
|
|
func (srv *HeartbeatService) GetLatestByOriginAndUser(origin string, user *models.User) (*models.Heartbeat, error) {
|
|
|
|
return srv.repository.GetLatestByOriginAndUser(origin, user)
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
func (srv *HeartbeatService) GetFirstByUsers() ([]*models.TimeByUser, error) {
|
|
|
|
return srv.repository.GetFirstByUsers()
|
2019-10-10 00:26:28 +03:00
|
|
|
}
|
2020-03-09 19:30:23 +03:00
|
|
|
|
|
|
|
func (srv *HeartbeatService) DeleteBefore(t time.Time) error {
|
2020-11-01 18:56:36 +03:00
|
|
|
return srv.repository.DeleteBefore(t)
|
2020-03-09 19:30:23 +03:00
|
|
|
}
|
|
|
|
|
2020-11-01 22:14:10 +03:00
|
|
|
func (srv *HeartbeatService) augmented(heartbeats []*models.Heartbeat, userId string) ([]*models.Heartbeat, error) {
|
|
|
|
languageMapping, err := srv.languageMappingSrvc.ResolveByUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range heartbeats {
|
|
|
|
heartbeats[i].Augment(languageMapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
return heartbeats, nil
|
|
|
|
}
|