2020-11-01 18:56:36 +03:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
2022-04-13 01:05:18 +03:00
|
|
|
datastructure "github.com/duke-git/lancet/v2/datastructure/set"
|
2020-11-01 18:56:36 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-01 22:14:10 +03:00
|
|
|
"gorm.io/gorm"
|
2020-11-01 18:56:36 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SummaryRepository struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSummaryRepository(db *gorm.DB) *SummaryRepository {
|
|
|
|
return &SummaryRepository{db: db}
|
|
|
|
}
|
|
|
|
|
2021-04-18 11:59:13 +03:00
|
|
|
func (r *SummaryRepository) GetAll() ([]*models.Summary, error) {
|
|
|
|
var summaries []*models.Summary
|
2021-04-19 21:36:37 +03:00
|
|
|
if err := r.db.
|
|
|
|
Order("from_time asc").
|
2022-01-02 15:39:20 +03:00
|
|
|
// branch summaries are currently not persisted, as only relevant in combination with project filter
|
2021-04-19 21:36:37 +03:00
|
|
|
Find(&summaries).Error; err != nil {
|
2021-04-18 11:59:13 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-17 13:08:40 +03:00
|
|
|
|
|
|
|
if err := r.populateItems(summaries); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-18 11:59:13 +03:00
|
|
|
return summaries, nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
func (r *SummaryRepository) Insert(summary *models.Summary) error {
|
|
|
|
if err := r.db.Create(summary).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SummaryRepository) GetByUserWithin(user *models.User, from, to time.Time) ([]*models.Summary, error) {
|
|
|
|
var summaries []*models.Summary
|
|
|
|
if err := r.db.
|
|
|
|
Where(&models.Summary{UserID: user.ID}).
|
2021-04-25 15:15:18 +03:00
|
|
|
Where("from_time >= ?", from.Local()).
|
|
|
|
Where("to_time <= ?", to.Local()).
|
2020-11-01 18:56:36 +03:00
|
|
|
Order("from_time asc").
|
2022-01-02 15:39:20 +03:00
|
|
|
// branch summaries are currently not persisted, as only relevant in combination with project filter
|
2020-11-01 18:56:36 +03:00
|
|
|
Find(&summaries).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-17 13:08:40 +03:00
|
|
|
|
|
|
|
if err := r.populateItems(summaries); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
return summaries, nil
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
func (r *SummaryRepository) GetLastByUser() ([]*models.TimeByUser, error) {
|
|
|
|
var result []*models.TimeByUser
|
|
|
|
r.db.Model(&models.User{}).
|
|
|
|
Select("users.id as user, max(to_time) as time").
|
|
|
|
Joins("left join summaries on users.id = summaries.user_id").
|
|
|
|
Group("user").
|
|
|
|
Scan(&result)
|
|
|
|
return result, nil
|
2020-11-01 18:56:36 +03:00
|
|
|
}
|
2020-11-06 19:09:41 +03:00
|
|
|
|
|
|
|
func (r *SummaryRepository) DeleteByUser(userId string) error {
|
|
|
|
if err := r.db.
|
|
|
|
Where("user_id = ?", userId).
|
|
|
|
Delete(models.Summary{}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-06 19:20:26 +03:00
|
|
|
}
|
2022-03-17 13:08:40 +03:00
|
|
|
|
|
|
|
// inplace
|
|
|
|
func (r *SummaryRepository) populateItems(summaries []*models.Summary) error {
|
|
|
|
summaryMap := map[uint]*models.Summary{}
|
2022-04-13 01:05:18 +03:00
|
|
|
summaryIds := datastructure.NewSet[uint]()
|
|
|
|
for _, s := range summaries {
|
2022-03-17 13:08:40 +03:00
|
|
|
if s.NumHeartbeats == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
summaryMap[s.ID] = s
|
2022-04-13 01:05:18 +03:00
|
|
|
summaryIds.Add(s.ID)
|
2022-03-17 13:08:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var items []*models.SummaryItem
|
|
|
|
|
|
|
|
if err := r.db.
|
|
|
|
Model(&models.SummaryItem{}).
|
2022-04-13 01:05:18 +03:00
|
|
|
Where("summary_id in ?", summaryIds.Values()).
|
2022-03-17 13:08:40 +03:00
|
|
|
Find(&items).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
l := summaryMap[item.SummaryID].ItemsByType(item.Type)
|
|
|
|
*l = append(*l, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|