106 lines
2.5 KiB
Go
106 lines
2.5 KiB
Go
package items
|
|
|
|
import (
|
|
"git.aiterp.net/stufflog3/stufflog3/internal/genutils"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type Group struct {
|
|
Label string `json:"label,omitempty"`
|
|
List []GroupReference `json:"list,omitempty"`
|
|
}
|
|
|
|
type GroupOptions struct {
|
|
HideCreated bool `json:"hideCreated,omitempty"`
|
|
HideScheduled bool `json:"hideScheduled,omitempty"`
|
|
HideAcquired bool `json:"hideAcquired,omitempty"`
|
|
}
|
|
|
|
type GroupReference struct {
|
|
Index int `json:"idx"`
|
|
Event string `json:"event"`
|
|
Time string `json:"time"`
|
|
|
|
sortKey string
|
|
}
|
|
|
|
func GenerateGroups(items []Result, opts GroupOptions) []Group {
|
|
groups := make(map[string]Group, 16)
|
|
|
|
// TODO: Get the scope from somewhere else, if necessary.
|
|
|
|
for i, item := range items {
|
|
if !opts.HideCreated && (item.AcquiredTime == nil || item.AcquiredTime.After(item.CreatedTime)) {
|
|
addItem(groups, i, "created", item.CreatedTime, tz)
|
|
}
|
|
if !opts.HideScheduled && item.ScheduledDate != nil {
|
|
addItem(groups, i, "scheduled", item.ScheduledDate.WithTimeAndLocation(23, 59, 59, tz), tz)
|
|
}
|
|
if !opts.HideAcquired && item.AcquiredTime != nil {
|
|
addItem(groups, i, "acquired", *item.AcquiredTime, tz)
|
|
}
|
|
}
|
|
|
|
for _, group := range groups {
|
|
sort.Slice(group.List, func(i, j int) bool {
|
|
return group.List[j].sortKey < group.List[i].sortKey
|
|
})
|
|
}
|
|
|
|
groupSlice := genutils.MapValues(groups)
|
|
sort.Slice(groupSlice, func(i, j int) bool {
|
|
return groupSlice[j].Label < groupSlice[i].Label
|
|
})
|
|
|
|
return groupSlice
|
|
}
|
|
|
|
func addItem(groups map[string]Group, index int, event string, eventDate time.Time, tz *time.Location) {
|
|
groupDate := eventDate.In(tz).Add(-time.Minute * 300)
|
|
eventDate = eventDate.In(tz)
|
|
|
|
groupKey := groupDate.Format("2006-01-02")
|
|
var sortKey string
|
|
if event == "scheduled" {
|
|
sortKey = eventDate.Format("2006-01-02") + " 23:59:59.999"
|
|
} else {
|
|
sortKey = eventDate.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
// Why, Go, must you be so.
|
|
group, ok := groups[groupKey]
|
|
if !ok {
|
|
group = Group{
|
|
Label: groupDate.Format("2006-01-02 (Monday)"),
|
|
List: make([]GroupReference, 0, 16),
|
|
}
|
|
}
|
|
|
|
for i, ref := range group.List {
|
|
if ref.Index == index {
|
|
group.List = append(group.List[:i], group.List[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
|
|
group.List = append(group.List, GroupReference{
|
|
Index: index,
|
|
Event: event,
|
|
Time: eventDate.Format("15:04"),
|
|
sortKey: sortKey,
|
|
})
|
|
groups[groupKey] = group
|
|
}
|
|
|
|
var tz *time.Location
|
|
|
|
func init() {
|
|
var err error
|
|
|
|
tz, err = time.LoadLocation("Europe/Oslo")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|