Let sleep tracking be switched off
Settings gets a Track sleep switch, stored on the synced profile as sleepOff (negative, so a profile from before it, or an old client that doesn't send it, means tracking). Off hides everything marked data-sleep-only (the sleep log buttons, the Sleep tab, the Sleep/Awake tiles, Last sleep and the Sleep & wake list) plus the bar timer. The sleep state also reads as unknown, so a leftover sleep-start can't dim the log buttons or run a sleep rail through every day's history. The reminder scheduler treats the state as unknown too. Otherwise a leftover sleep-start would mute every other rule for good, or a leftover sleep-end would fire the sleep rule every interval. Nothing logged is deleted.
This commit is contained in:
1 parent
f98137097c
commit
a01d74e9d5
7 files changed
+176
-32
No files matched your search
+24
-8
@@ -84,7 +84,12 @@ type Config struct {
|
||||
// PedigreeID is the dog's SKK chip or registration number. When set, the app
|
||||
// unlocks the pedigree view and looks this dog up; empty means no pedigree.
|
||||
PedigreeID string `json:"pedigreeId"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
// SleepOff means sleep isn't being logged: the app hides its sleep buttons,
|
||||
// timer and charts, and reminders stop reading the puppy as awake or asleep.
|
||||
// Negative so that the zero value, and a client too old to send the field,
|
||||
// both mean "tracking", which is what every profile was before it existed.
|
||||
SleepOff bool `json:"sleepOff"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type ConfigStore struct {
|
||||
@@ -100,8 +105,8 @@ func (cs *ConfigStore) get(userID string) Config {
|
||||
// One profile row per user. A missing row is the pre-configuration state,
|
||||
// so a zero-value Config is the right answer.
|
||||
err := cs.db.QueryRow(
|
||||
`SELECT name, birthday, pedigree_id, updated FROM config WHERE user_id = ?`, userID,
|
||||
).Scan(&c.Name, &c.Birthday, &c.PedigreeID, &c.UpdatedAt)
|
||||
`SELECT name, birthday, pedigree_id, sleep_off, updated FROM config WHERE user_id = ?`, userID,
|
||||
).Scan(&c.Name, &c.Birthday, &c.PedigreeID, &c.SleepOff, &c.UpdatedAt)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
log.Printf("config get: %v", err)
|
||||
}
|
||||
@@ -111,19 +116,20 @@ func (cs *ConfigStore) get(userID string) Config {
|
||||
// merge applies an incoming config for one user with last-write-wins by
|
||||
// UpdatedAt and returns the resulting stored config (which the caller sends back).
|
||||
func (cs *ConfigStore) merge(userID string, in Config) (Config, error) {
|
||||
// Name/birthday/updated are last-write-wins: the incoming row replaces the
|
||||
// Name/birthday/sleep_off/updated are last-write-wins: the incoming row replaces the
|
||||
// stored one only when strictly newer. The pedigree id is stickier — an empty
|
||||
// incoming value never clears a stored one, so a clock race between devices
|
||||
// can't drop it; when both are set, the newer profile's id wins with the rest.
|
||||
_, err := cs.db.Exec(`
|
||||
INSERT INTO config (user_id, name, birthday, pedigree_id, updated)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
INSERT INTO config (user_id, name, birthday, pedigree_id, sleep_off, updated)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(user_id) DO UPDATE SET
|
||||
name = excluded.name, birthday = excluded.birthday,
|
||||
pedigree_id = CASE WHEN excluded.pedigree_id != '' THEN excluded.pedigree_id ELSE config.pedigree_id END,
|
||||
sleep_off = excluded.sleep_off,
|
||||
updated = excluded.updated
|
||||
WHERE excluded.updated > config.updated`,
|
||||
userID, in.Name, in.Birthday, in.PedigreeID, in.UpdatedAt)
|
||||
userID, in.Name, in.Birthday, in.PedigreeID, in.SleepOff, in.UpdatedAt)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
@@ -468,6 +474,7 @@ func openDB(path string) (*sql.DB, error) {
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
birthday TEXT NOT NULL DEFAULT '',
|
||||
pedigree_id TEXT NOT NULL DEFAULT '',
|
||||
sleep_off INTEGER NOT NULL DEFAULT 0,
|
||||
updated INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
@@ -653,6 +660,15 @@ func migrateSchema(db *sql.DB) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
hasSleepOff, err := columnExists(db, "config", "sleep_off")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !hasSleepOff {
|
||||
if _, err := db.Exec(`ALTER TABLE config ADD COLUMN sleep_off INTEGER NOT NULL DEFAULT 0`); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1029,7 +1045,7 @@ func main() {
|
||||
case http.MethodGet:
|
||||
writeConfig(configStore.get(userID(r)))
|
||||
case http.MethodPut, http.MethodPost:
|
||||
// The profile (name, birthday, pedigree id) is the owner's to set.
|
||||
// The profile (name, birthday, pedigree id, sleep tracking) is the owner's to set.
|
||||
// The GET above stays open — a guest needs the name and birthday to
|
||||
// render the header at all.
|
||||
if isGuest(r) {
|
||||
|
||||
+16
-1
@@ -188,10 +188,25 @@ type sleepState struct {
|
||||
since int64 // ms epoch of that boundary
|
||||
}
|
||||
|
||||
// sleepStateFor reads that boundary for one user. An account that has switched
|
||||
// sleep tracking off has no sleep state at all,
|
||||
// whatever boundaries are left in its log. Reading the last one anyway would
|
||||
// leave the puppy "asleep" from the day tracking stopped, with every other
|
||||
// rule silenced for good — or "awake" since then, with the sleep rule firing
|
||||
// every interval forever.
|
||||
func (sch *Scheduler) sleepStateFor(userID string) (sleepState, error) {
|
||||
var off bool
|
||||
err := sch.db.QueryRow(`SELECT sleep_off FROM config WHERE user_id = ?`, userID).Scan(&off)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return sleepState{}, err
|
||||
}
|
||||
if off {
|
||||
return sleepState{}, nil
|
||||
}
|
||||
|
||||
var typ string
|
||||
var at int64
|
||||
err := sch.db.QueryRow(`
|
||||
err = sch.db.QueryRow(`
|
||||
SELECT type, at FROM events
|
||||
WHERE user_id = ? AND deleted = 0 AND type IN ('sleep-start', 'sleep-end')
|
||||
ORDER BY at DESC, updated DESC LIMIT 1`, userID).Scan(&typ, &at)
|
||||
|
||||
@@ -188,6 +188,46 @@ func TestSleepStateMatchesLatestBoundary(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSleepStateIgnoredWhenTrackingIsOff(t *testing.T) {
|
||||
sch := testScheduler(t)
|
||||
now := int64(1_700_000_000_000)
|
||||
configs := newConfigStore(sch.db)
|
||||
|
||||
// Tracking stopped while the log still says asleep.
|
||||
addEvent(t, sch, "u1", "pee", now-90*minute)
|
||||
addEvent(t, sch, "u1", "sleep-start", now-600*minute)
|
||||
if _, err := configs.merge("u1", Config{Name: "Rex", SleepOff: true, UpdatedAt: now}); err != nil {
|
||||
t.Fatalf("merge: %v", err)
|
||||
}
|
||||
if got := configs.get("u1"); !got.SleepOff {
|
||||
t.Fatal("sleepOff did not round-trip through the config store")
|
||||
}
|
||||
s, err := sch.sleepStateFor("u1")
|
||||
if err != nil {
|
||||
t.Fatalf("sleepStateFor: %v", err)
|
||||
}
|
||||
if s.state != "" {
|
||||
t.Errorf("state = %q, want none while tracking is off", s.state)
|
||||
}
|
||||
|
||||
// So an overdue pee rule fires rather than waiting for a wake-up that will
|
||||
// never be logged, and the sleep rule stays quiet.
|
||||
if _, ok, err := sch.due("u1", Reminder{Kind: "pee", Enabled: true, IntervalMin: 60}, 0, now, s); err != nil || !ok {
|
||||
t.Errorf("pee: fired = %v (err %v), want a fire", ok, err)
|
||||
}
|
||||
if _, ok, err := sch.due("u1", Reminder{Kind: "sleep", Enabled: true, IntervalMin: 45}, 0, now, s); err != nil || ok {
|
||||
t.Errorf("sleep: fired = %v (err %v), want no fire", ok, err)
|
||||
}
|
||||
|
||||
// Switching it back on reads the log again.
|
||||
if _, err := configs.merge("u1", Config{Name: "Rex", UpdatedAt: now + 1}); err != nil {
|
||||
t.Fatalf("merge: %v", err)
|
||||
}
|
||||
if s, err := sch.sleepStateFor("u1"); err != nil || s.state != "asleep" {
|
||||
t.Errorf("tracking back on: state = %q (err %v), want asleep", s.state, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemindersStoreDefaultsAndClamping(t *testing.T) {
|
||||
sch := testScheduler(t)
|
||||
|
||||
|
||||
Reference in new issue
Block a user