mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
refactor: use cross join instead of subquery for populating summary items (see #350)
This commit is contained in:
parent
e6441f124c
commit
179042f81b
2
go.mod
2
go.mod
@ -29,7 +29,7 @@ require (
|
|||||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
|
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
|
||||||
gorm.io/driver/mysql v1.3.3
|
gorm.io/driver/mysql v1.3.3
|
||||||
gorm.io/driver/postgres v1.3.3
|
gorm.io/driver/postgres v1.3.4
|
||||||
gorm.io/driver/sqlite v1.3.1
|
gorm.io/driver/sqlite v1.3.1
|
||||||
gorm.io/gorm v1.23.4
|
gorm.io/gorm v1.23.4
|
||||||
)
|
)
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package repositories
|
package repositories
|
||||||
|
|
||||||
import (
|
import (
|
||||||
datastructure "github.com/duke-git/lancet/v2/datastructure/set"
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
"github.com/muety/wakapi/models"
|
"github.com/muety/wakapi/models"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ func (r *SummaryRepository) GetAll() ([]*models.Summary, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.populateItems(summaries); err != nil {
|
if err := r.populateItems(summaries, []clause.Interface{}); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,17 +41,26 @@ func (r *SummaryRepository) Insert(summary *models.Summary) error {
|
|||||||
|
|
||||||
func (r *SummaryRepository) GetByUserWithin(user *models.User, from, to time.Time) ([]*models.Summary, error) {
|
func (r *SummaryRepository) GetByUserWithin(user *models.User, from, to time.Time) ([]*models.Summary, error) {
|
||||||
var summaries []*models.Summary
|
var summaries []*models.Summary
|
||||||
if err := r.db.
|
|
||||||
Where(&models.Summary{UserID: user.ID}).
|
queryConditions := []clause.Interface{
|
||||||
Where("from_time >= ?", from.Local()).
|
clause.Where{Exprs: r.db.Statement.BuildCondition("user_id = ?", user.ID)},
|
||||||
Where("to_time <= ?", to.Local()).
|
clause.Where{Exprs: r.db.Statement.BuildCondition("from_time >= ?", from.Local())},
|
||||||
Order("from_time asc").
|
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
|
// branch summaries are currently not persisted, as only relevant in combination with project filter
|
||||||
Find(&summaries).Error; err != nil {
|
if err := q.Find(&summaries).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.populateItems(summaries); err != nil {
|
if err := r.populateItems(summaries, queryConditions); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,28 +87,29 @@ func (r *SummaryRepository) DeleteByUser(userId string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// inplace
|
// inplace
|
||||||
func (r *SummaryRepository) populateItems(summaries []*models.Summary) error {
|
func (r *SummaryRepository) populateItems(summaries []*models.Summary, conditions []clause.Interface) error {
|
||||||
summaryMap := map[uint]*models.Summary{}
|
|
||||||
summaryIds := datastructure.NewSet[uint]()
|
|
||||||
for _, s := range summaries {
|
|
||||||
if s.NumHeartbeats == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
summaryMap[s.ID] = s
|
|
||||||
summaryIds.Add(s.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
var items []*models.SummaryItem
|
var items []*models.SummaryItem
|
||||||
|
|
||||||
if err := r.db.
|
summaryMap := slice.GroupWith[*models.Summary, uint](summaries, func(s *models.Summary) uint {
|
||||||
Model(&models.SummaryItem{}).
|
return s.ID
|
||||||
Where("summary_id in ?", summaryIds.Values()).
|
})
|
||||||
Find(&items).Error; err != nil {
|
|
||||||
|
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 {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
l := summaryMap[item.SummaryID].ItemsByType(item.Type)
|
l := summaryMap[item.SummaryID][0].ItemsByType(item.Type)
|
||||||
*l = append(*l, item)
|
*l = append(*l, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user