2020-11-01 18:56:36 +03:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
2022-04-18 17:59:46 +03:00
|
|
|
"github.com/duke-git/lancet/v2/slice"
|
2020-11-01 18:56:36 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-01 22:14:10 +03:00
|
|
|
"gorm.io/gorm"
|
2022-04-18 17:59:46 +03:00
|
|
|
"gorm.io/gorm/clause"
|
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
|
|
|
|
2022-04-18 17:59:46 +03:00
|
|
|
if err := r.populateItems(summaries, []clause.Interface{}); err != nil {
|
2022-03-17 13:08:40 +03:00
|
|
|
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
|
2022-04-18 17:59:46 +03:00
|
|
|
|
|
|
|
queryConditions := []clause.Interface{
|
|
|
|
clause.Where{Exprs: r.db.Statement.BuildCondition("user_id = ?", user.ID)},
|
|
|
|
clause.Where{Exprs: r.db.Statement.BuildCondition("from_time >= ?", from.Local())},
|
|
|
|
clause.Where{Exprs: r.db.Statement.BuildCondition("to_time <= ?", to.Local())},
|
|
|
|
}
|
|
|
|
|
|
|
|
q := r.db.Model(&models.Summary{}).
|
|
|
|
Order("from_time asc")
|
|
|
|
|
|
|
|
for _, c := range queryConditions {
|
|
|
|
q.Statement.AddClause(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// branch summaries are currently not persisted, as only relevant in combination with project filter
|
|
|
|
if err := q.Find(&summaries).Error; err != nil {
|
2020-11-01 18:56:36 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-17 13:08:40 +03:00
|
|
|
|
2022-04-18 17:59:46 +03:00
|
|
|
if err := r.populateItems(summaries, queryConditions); err != nil {
|
2022-03-17 13:08:40 +03:00
|
|
|
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
|
2022-04-18 17:59:46 +03:00
|
|
|
func (r *SummaryRepository) populateItems(summaries []*models.Summary, conditions []clause.Interface) error {
|
2022-03-17 13:08:40 +03:00
|
|
|
var items []*models.SummaryItem
|
|
|
|
|
2022-04-18 17:59:46 +03:00
|
|
|
summaryMap := slice.GroupWith[*models.Summary, uint](summaries, func(s *models.Summary) uint {
|
|
|
|
return s.ID
|
|
|
|
})
|
|
|
|
|
|
|
|
q := r.db.Model(&models.SummaryItem{}).
|
|
|
|
Select("summary_items.*").
|
|
|
|
Joins("cross join summaries").
|
|
|
|
Where("summary_items.summary_id = summaries.id").
|
|
|
|
Where("num_heartbeats > ?", 0)
|
|
|
|
|
|
|
|
for _, c := range conditions {
|
|
|
|
q.Statement.AddClause(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := q.Find(&items).Error; err != nil {
|
2022-03-17 13:08:40 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
2022-04-18 17:59:46 +03:00
|
|
|
l := summaryMap[item.SummaryID][0].ItemsByType(item.Type)
|
2022-03-17 13:08:40 +03:00
|
|
|
*l = append(*l, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|