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:
Alexander Heldt committed 2026-11-12 10:27:03 +00:00
1 parent f98137097c
commit a01d74e9d5
7 files changed
+176 -32

No files matched your search

+24 -8
View File
@@ -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) {