Documentation
¶
Index ¶
- func Watch(d *DB) <-chan struct{}
- type DB
- func (d *DB) Close() error
- func (d *DB) CreateClient(name string, hourlyRate float64) (*model.Client, error)
- func (d *DB) CreateProject(clientID int64, name string) (*model.Project, error)
- func (d *DB) DeleteClient(id int64) error
- func (d *DB) DeleteEntry(id int64) error
- func (d *DB) DeleteProject(id int64) error
- func (d *DB) DeleteTaskEntries(projectID int64, task string) (int64, error)
- func (d *DB) EnsureUncategorizedProject() (int64, error)
- func (d *DB) GetClient(id int64) (*model.Client, error)
- func (d *DB) GetProject(id int64) (*model.Project, error)
- func (d *DB) GetRunningEntry() (*model.Entry, error)
- func (d *DB) ListClients() ([]*model.Client, error)
- func (d *DB) ListEntries(projectID int64, dateFrom, dateTo string, includeRunning bool) ([]*model.Entry, error)
- func (d *DB) ListFinishedEntriesOverlapping(from, to time.Time) ([]*model.Entry, error)
- func (d *DB) ListProjects(clientID int64) ([]*model.Project, error)
- func (d *DB) ListRecentTasks(limit int) ([]*model.RecentTask, error)
- func (d *DB) Path() string
- func (d *DB) ReportByProject(dateFrom, dateTo string) ([]*model.ReportRow, error)
- func (d *DB) ResolveOrCreateProject(clientID int64, clientName string, projectID int64, projectName string) (int64, error)
- func (d *DB) SetEntryInvoiced(id int64, invoiced bool) error
- func (d *DB) SetEntryPaid(id int64, paid bool) error
- func (d *DB) StartEntry(projectID int64, task string) (*model.Entry, error)
- func (d *DB) StopAllRunning() error
- func (d *DB) StopEntry(id int64) error
- func (d *DB) UpdateClient(c *model.Client) error
- func (d *DB) UpdateEntry(e *model.Entry) error
- func (d *DB) UpdateProject(p *model.Project) error
- func (d *DB) UpdateTaskEntries(fromProjectID int64, fromTask string, toProjectID int64, toTask string) (int64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Watch ¶
func Watch(d *DB) <-chan struct{}
Watch starts watching the filesystem for external changes to the database -- a write from another process (another "notch" CLI invocation, or the aliasOS/Omarchy bar-widget plugin, both of which are just short-lived CLI calls with no way to otherwise notify a long-running consumer) -- and returns a channel that receives a value shortly after, a fast trigger for a long-running consumer (the TUI) to reload rather than wait for its next poll tick. Mirrors internal/theme's own Watch(): same reasoning, same debounce, same "nil channel on failure, caller falls back to polling" contract.
Getting this right took two wrong turns, both confirmed with inotifywait against a real WAL-mode DB:
- Watching "<path>-wal" (or the directory containing it, catching -wal along with everything else): every notch invocation, reads included, opens/checkpoints/closes that file, deleting and recreating it each time. A watch on it fires on *reads* too, and since d's own reload after a fire itself reads the DB, that's a self-triggering loop -- a "poll as fast as possible" watch, not the efficient one this is meant to be.
- Watching "<path>" itself instead: a genuine write only produces a MODIFY event on it once SQLite checkpoints the WAL back in, and SQLite only auto-checkpoints on the *last* connection to the file closing -- which never happens while d itself (the long-running consumer's own connection, e.g. the TUI's) stays open. So this fixed the self-triggering problem but then almost never fired during the one scenario Watch exists for: the consumer running with an open connection, watching for someone else's write.
The actual fix doesn't try to infer "did the DB change" from *which* file got touched or how -- it asks SQLite directly, via `PRAGMA data_version` on d's own connection. That pragma is specifically designed for this: it changes the instant any *other* connection commits a write (no checkpoint required -- confirmed by querying it on an already-open connection immediately after a separate connection's write), and it does not change on reads, from this connection or any other. So the directory watch below still exists (still watching "<path>", "-wal" and "-shm" together, whichever a given write happens to touch) purely as a low-cost "something might have happened, go check" trigger; the data_version comparison is what actually decides whether to notify the caller, making a spurious wake from a pure read a no-op instead of a self-triggering reload.
Returns nil if the OS watcher couldn't be started, the directory doesn't exist (e.g. a NOTCH_DB pointed somewhere never created), or the initial data_version read fails; callers should treat a nil channel as one that never fires and keep their own poll fallback.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func (*DB) CreateClient ¶
func (*DB) CreateProject ¶
func (*DB) DeleteClient ¶
func (*DB) DeleteEntry ¶
DeleteEntry deletes one entry, and errors if there's no entry with that ID (say, another process already deleted it).
func (*DB) DeleteProject ¶
func (*DB) DeleteTaskEntries ¶ added in v0.10.0
DeleteTaskEntries deletes every entry for one (project, task) pairing -- the same grouping ListRecentTasks reports -- including a running one, and returns how many were removed. Same-named tasks on other projects are a different pairing and are left alone.
func (*DB) EnsureUncategorizedProject ¶
EnsureUncategorizedProject returns (or creates) a catch-all "Uncategorized / General" project for untagged timers -- e.g. a quick-add that only specifies a task, no client/project. Moved here (from what was internal/ui/views' unexported ensureUncategorizedProject) alongside ResolveOrCreateProject, for the same reason: the CLI needs it too, not just the TUI's own new-timer form.
func (*DB) ListEntries ¶
func (d *DB) ListEntries(projectID int64, dateFrom, dateTo string, includeRunning bool) ([]*model.Entry, error)
ListEntries lists entries with optional filters. Pass zero values to skip a filter. dateFrom/dateTo format: "2006-01-02"
func (*DB) ListFinishedEntriesOverlapping ¶ added in v0.10.0
ListFinishedEntriesOverlapping returns the finished entries whose time span overlaps [from, to), newest first -- including one that started before from, which ListEntries' start-date filter would miss.
func (*DB) ListRecentTasks ¶
func (d *DB) ListRecentTasks(limit int) ([]*model.RecentTask, error)
ListRecentTasks returns the most recently-used distinct (project, task) pairings, most recent first, each with its accumulated duration across every finished entry that used that exact task string on that project -- for a picker that wants to resume a specific named task (e.g. "Meeting" on "Website") rather than just start a bare project. Like ReportByProject, running entries are excluded from the total; a task that's never been finished before won't appear here until it has.
func (*DB) Path ¶
Path returns the filesystem path this DB was opened from -- e.g. for Watch, which needs it but isn't itself a DB method (it has to work before/without an open connection, and doesn't want one).
func (*DB) ReportByProject ¶
ReportByProject returns aggregated hours/earnings grouped by client + project, with individual entries attached to each row for drill-down display.
func (*DB) ResolveOrCreateProject ¶
func (d *DB) ResolveOrCreateProject(clientID int64, clientName string, projectID int64, projectName string) (int64, error)
ResolveOrCreateProject finds or creates the client+project and returns the project ID. Returns 0 if both clientName and projectName are blank (uncategorized). Moved here (from what was internal/ui/views' unexported resolveProject) so the CLI ("notch start --client-name/ --project-name") and the TUI's own new-timer form share one implementation instead of the CLI needing its own copy or an import of the ui/views package.
func (*DB) SetEntryInvoiced ¶
SetEntryInvoiced marks one or more entries as invoiced/uninvoiced.
func (*DB) SetEntryPaid ¶
SetEntryPaid marks one or more entries as paid/unpaid.
func (*DB) StopAllRunning ¶
func (*DB) UpdateTaskEntries ¶ added in v0.10.0
func (d *DB) UpdateTaskEntries(fromProjectID int64, fromTask string, toProjectID int64, toTask string) (int64, error)
UpdateTaskEntries re-files every entry of one (project, task) pairing under another -- a rename, a move to a different project, or both -- and returns how many entries changed. If the target pairing already has entries, the two simply merge into one ListRecentTasks row.