Files
2026-12-02 19:47:35 +01:00

121 lines
3.3 KiB
Go

package tracking
import (
"math/bits"
"time"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
)
const (
seedEffectiveFrom = "2000-01-01"
seedHoursPerWeek = 40.0
seedWorkdaysMask = 31
seedTimezone = "UTC"
)
func seedDefaultSettings(e *core.RecordEvent) error {
count, err := e.App.CountRecords("settings_history", dbx.HashExp{"owner": e.Record.Id})
if err == nil && count == 0 {
var rec *core.Record
rec, err = newRecord(e.App, "settings_history", e.Record.Id)
if err == nil {
rec.Set("effective_from", seedEffectiveFrom)
rec.Set("hours_per_week", seedHoursPerWeek)
rec.Set("workdays_mask", seedWorkdaysMask)
rec.Set("timezone", seedTimezone)
bump(rec, time.Now())
err = e.App.Save(rec)
}
}
if err != nil {
// The user is created regardless; it just starts without settings.
e.App.Logger().Error("seeding default settings failed", "owner", e.Record.Id, "error", err)
}
return e.Next()
}
func settingsAt(tx core.App, owner, dayKey string) (*core.Record, error) {
rows, err := tx.FindRecordsByFilter("settings_history",
liveFilter+" && effective_from <= {:day}",
"-effective_from,-id",
1, 0,
dbx.Params{"owner": owner, "day": dayKey},
)
if err != nil {
return nil, err
}
if len(rows) == 0 {
return nil, nil
}
return rows[0], nil
}
// location returns the timezone of the settings effective on dayKey, or UTC when
// there are none or they cannot be read. A failed lookup is logged because UTC
// is only a guess at what the owner meant.
func location(tx core.App, owner, dayKey string) *time.Location {
set, err := settingsAt(tx, owner, dayKey)
if err != nil {
tx.Logger().Warn("cannot read the settings for the timezone, using UTC",
"owner", owner, "dayKey", dayKey, "error", err)
return time.UTC
}
if set == nil {
return time.UTC
}
timezone := set.GetString("timezone")
loc, err := time.LoadLocation(timezone)
if err != nil {
tx.Logger().Warn("unknown timezone in the settings, using UTC",
"owner", owner, "dayKey", dayKey, "timezone", timezone, "error", err)
return time.UTC
}
return loc
}
// weekExpectedMs adds up the expected time of the workdays of the week. Each day
// counts towards the settings effective on it, so a week in which the settings
// change is only partly computed with the new hours.
func weekExpectedMs(tx core.App, owner string, dayKeys []string) (int64, error) {
var expectedMs int64
for _, dayKey := range dayKeys {
ms, err := dayExpectedMsAt(tx, owner, dayKey)
if err != nil {
return 0, err
}
expectedMs += ms
}
return expectedMs, nil
}
// dayExpectedMsAt returns the expected daily working time of the settings effective
// on dayKey, or 0 on a non-workday or without settings.
func dayExpectedMsAt(tx core.App, owner, dayKey string) (int64, error) {
set, err := settingsAt(tx, owner, dayKey)
if err != nil || set == nil {
return 0, err
}
if !isWorkday(set, parseDayKey(dayKey).Weekday()) {
return 0, nil
}
return dayExpectedMs(set), nil
}
func dayExpectedMs(set *core.Record) int64 {
days := bits.OnesCount(uint(workdaysMask(set)))
if days == 0 {
return 0
}
return int64(set.GetFloat("hours_per_week")*3_600_000) / int64(days)
}
func isWorkday(set *core.Record, wd time.Weekday) bool {
return workdaysMask(set)&(1<<((int(wd)+6)%7)) != 0
}
func workdaysMask(set *core.Record) int {
return set.GetInt("workdays_mask")
}