29 KiB
The Chronometrist Manual
- How to…
- Explanation
- Reference
- Legend of currently-used time formats
- chronometrist-common.el
- chronometrist-custom.el
- chronometrist-diary-view.el
- chronometrist.el
- chronometrist-events.el
- chronometrist-migrate.el
- chronometrist-plist-pp.el
- chronometrist-queries.el
- chronometrist-report-custom.el
- chronometrist-report.el
- chronometrist-key-values.el
- chronometrist-statistics-custom.el
- chronometrist-statistics.el
- chronometrist-time.el
- chronometrist-timer.el
- chronometrist-goal
- chronometrist-sexp
The structure of this manual was inspired by https://documentation.divio.com/
How to…
How to set up Emacs to contribute
All of these are optional, but recommended for the best experience.
- Use nameless-mode for easier reading of Emacs Lisp code, and
- Use visual-fill-column-mode to soft-wrap lines in Org/Markdown files.
org-indent-mode
(for Org files) and adaptive-prefix-mode (for Markdown and other files) will further enhance the experience. - Get the sources from https://github.com/contrapunctus-1/chronometrist and read this manual in the Org format (doc/manual.org), so links to identifiers can take you to their location in the source.
-
Install Cask to easily byte-compile and test the project. From the project root, you can now run
cask
to install the project dependencies in a sandboxcask exec buttercup -L . --traceback pretty
to run tests.
Explanation
Design goals
-
Don't make assumptions about the user's profession
- e.g. timeclock seems to assume you're using it for a 9-to-5/contractor job
-
Incentivize use
- Hooks allow the time tracker to automate tasks and become a useful part of your workflow
-
Make it easy to edit data using existing, familiar tools
- We don't use an SQL database, where changing a single field is tricky 1
- We use a text file containing s-expressions (easy for humans to read and write)
- We use ISO-8601 for timestamps (easy for humans to read and write) rather than UNIX epoch time
- Reduce human errors in tracking
- Have a useful, informative, interactive interface
- Support mouse and keyboard use equally [1] I still have doubts about this. Having SQL as a query language would be very useful in perusing the stored data. Maybe we should have tried to create a companion mode to edit SQL databases interactively?
Terminology
For lack of a better term, events are how we refer to time intervals. They are stored as plists; each contains at least a :name "<name>"
, a :start "<iso-timestamp>"
, and (except in case of an ongoing task) a :stop "<iso-timestamp>"
.
Project overview
Chronometrist has three components, and each has a file containing major mode definitions and user-facing commands.
All three of these use (info "(elisp)Tabulated List Mode")
. Each of them also contains a "-print-non-tabular" function, which prints the non-tabular parts of the buffer.
Each of them has a corresponding -custom
file, which contain the Customize group and custom variable definitions for user-facing variables -
chronometrist-common.el contains definitions common to all components.
All three components use timers to keep their buffers updated. chronometrist-timer.el contains all timer-related code.
Note - sometimes, when hacking or dealing with errors, timers may result in subtle bugs which are very hard to debug. Using chronometrist-force-restart-timer
or restarting Emacs can fix them, so try that as a first sanity check.
Chronometrist
Optimization
It is of great importance that Chronometrist be responsive -
- A responsive program is more likely to be used; recall our design goal of 'incentivizing use'.
- Being an Emacs program, freezing the UI for any human-noticeable length of time is unacceptable - it prevents the user from working on anything in their environment.
Thus, I have considered various optimization strategies, and so far implemented two.
Prevent excess creation of file watchers
One of the earliest 'optimizations' of great importance turned out to simply be a bug - turns out, if you run an identical call to file-notify-add-watch
twice, you create two file watchers and your callback will be called twice. We were creating a file watcher each time the chronometrist command was run. 🤦 This was causing humongous slowdowns each time the file changed. 😅
- It was fixed in v0.2.2 by making the watch creation conditional, using
chronometrist--fs-watch
to store the watch object.
Preserve hash table state for some commands
NOTE - this has been replaced with a more general optimization - see next section.
The next one was released in v0.5. Till then, any time the chronometrist-file
was modified, we'd clear the chronometrist-events
hash table and read data into it again. The reading itself is nearly-instant, even with ~2 years' worth of data 1 (it uses Emacs' read
, after all), but the splitting of midnight-spanning events is the real performance killer.
After the optimization…
- Two backend functions (
chronometrist-sexp-new
andchronometrist-sexp-replace-last
) were modified to set a flag (chronometrist--inhibit-read-p
) before saving the file. - If this flag is non-nil,
chronometrist-refresh-file
skips the expensive calls tochronometrist-events-populate
,chronometrist-tasks-from-table
, andchronometrist-tags-history-populate
, and resets the flag. -
Instead, the aforementioned backend functions modify the relevant variables -
chronometrist-events
,chronometrist-task-list
, andchronometrist-tags-history
- via…chronometrist-events-add
/chronometrist-events-replace-last
chronometrist-task-list-add
, andchronometrist-tags-history-add
/chronometrist-tags-history-replace-last
, respectively.
There are still some operations which chronometrist-refresh-file
runs unconditionally - which is to say there is scope for further optimization, if or when required.
Determine type of change made to file
Most changes, whether made through user-editing or by Chronometrist commands, happen at the end of the file. We try to detect the kind of change made - whether the last expression was modified, removed, or whether a new expression was added to the end - and make the corresponding change to chronometrist-events
, instead of doing a full parse again (chronometrist-events-populate
). The increase in responsiveness has been significant.
When chronometrist-refresh-file
is run by the file system watcher, it uses chronometrist-file-hash
to assign indices and a hash to chronometrist--file-state
. The next time the file changes, chronometrist-file-change-type
compares this state to the current state of the file to determine the type of change made.
Midnight-spanning events
A unique problem in working with Chronometrist, one I had never foreseen, was tasks which start on one day and end on another. These mess up data consumption (especially interval calculations and acquiring data for a specific date) in all sorts of unforeseen ways.
There are a few different approaches of dealing with them. (Currently, Chronometrist uses #3.)
Check the code of the first event of the day (timeclock format)
- Advantage - very simple to detect
- Disadvantage - "in" and "out" events must be represented separately
Split them at the file level
- Advantage - operation is performed only once for each such event + simpler data-consuming code + reduced post-parsing load.
-
What happens when the user changes their day-start-time? The split-up events are now split wrongly, and the second event may get split again. Possible solutions -
- Add function to check if, for two events A and B, the :stop of A is the same as the :start of B, and that all their other tags are identical. Then we can re-split them according to the new day-start-time.
- Add a :split tag to split events. It can denote that the next event was originally a part of this one.
-
Re-check and update the file when the day-start-time changes.
- Possible with
add-variable-watcher
or:custom-set
in Customize (thanks bpalmer)
- Possible with
Split them at the hash-table-level
Handled by chronometrist-sexp-events-populate
- Advantage - simpler data-consuming code.
Split them at the data-consumer level (e.g. when calculating time for one day/getting events for one day)
- Advantage - reduced repetitive post-parsing load.
Point restore behaviour
After hacking, always test for and ensure the following -
- Toggling the buffer via
chronometrist=/=chronometrist-report=/=chronometrist-statistics
should preserve point - The timer function should preserve point when the buffer is current
- The timer function should preserve point when the buffer is not current, but is visible in another window
- The next/previous week keys and buttons should preserve point.
chronometrist-report date range logic
A quick description, starting from the first time chronometrist-report
is run in an Emacs session -
- We get the current date as a ts struct
(chronometrist-date)
. - The variable
chronometrist-report-week-start-day
stores the day we consider the week to start with. The default is "Sunday". We check if the date from #2 is on the week start day, else decrement it till we are, using(chronometrist-report-previous-week-start)
. - We store the date from #3 in the global variable
chronometrist-report--ui-date
. - By counting up from
chronometrist-report--ui-date
, we get dates for the days in the next 7 days using(chronometrist-report-date->dates-in-week)
. We store them inchronometrist-report--ui-week-dates
. The dates inchronometrist-report--ui-week-dates
are what is finally used to query the data displayed in the buffer. - To get data for the previous/next weeks, we decrement/increment the date in
chronometrist-report--ui-date
by 7 days and repeat the above process (via(chronometrist-report-previous-week)=/=(chronometrist-report-next-week)
).
Tags and Key-Values
chronometrist-key-values.el deals with adding additional information to events, in the form of key-values and tags.
Key-values are stored as plist keywords and values. The user can add any keywords except :name
, :tags
, :start
, and :stop
. 2 Values can be any readable Lisp values.
Similarly, tags are stored using a :tags (<tag>*)
keyword-value pair. The tags themselves (the elements of the list) can be any readable Lisp value.
User input
The entry points are chronometrist-kv-add
and chronometrist-tags-add
. The user adds these to the desired hooks, and they prompt the user for tags/key-values.
Both have corresponding functions to create a prompt -
chronometrist-kv-add
's way of reading key-values from the user is somewhat different from most Emacs prompts - it creates a new buffer, and uses the minibuffer to alternatingly ask for keys and values in a loop. Key-values are inserted into the buffer as the user enters/selects them. The user can break out of this loop with an empty input (the keys to accept an empty input differ between completion systems, so we try to let the user know about them using chronometrist-kv-completion-quit-key
). After exiting the loop, they can edit the key-values in the buffer, and use the commands chronometrist-kv-accept
to accept the key-values (which uses chronometrist-append-to-last
to add them to the last plist in chronometrist-file
) or chronometrist-kv-reject
to discard them.
History
All prompts suggest past user inputs. These are queried from three history hash tables -
Each of these has a corresponding function to clear it and fill it with values -
Reference
Legend of currently-used time formats
ts
ts.el struct
- Used by nearly all internal functions
iso-timestamp
"YYYY-MM-DDTHH:MM:SSZ"
- Used in the s-expression file format
- Read by chronometrist-sexp-events-populate
- Used in the plists in the chronometrist-events hash table values
iso-date
"YYYY-MM-DD"
- Used as hash table keys in chronometrist-events - can't use ts structs for keys, you'd have to make a hash table predicate which uses ts=
seconds
integer seconds as duration
- Used for most durations
- May be changed to floating point to allow larger durations. The minimum range of `most-positive-fixnum` is 536870911, which seems to be enough to represent durations of 17 years.
- Used for update intervals (chronometrist-update-interval, chronometrist-change-update-interval)
minutes
integer minutes as duration
- Used for goals (chronometrist-goals-list, chronometrist-get-goal) - minutes seems like the ideal unit for users to enter
list-duration
(hours minute seconds)
- Only returned by chronometrist-seconds-to-hms, called by chronometrist-format-time
chronometrist-common.el
- Variable - chronometrist-empty-time-string
- Variable - chronometrist-date-re
- Variable - chronometrist-time-re-ui
- Internal Variable - chronometrist–task-list
- Internal Variable - chronometrist–fs-watch
- Function - chronometrist-current-task ()
-
Function - chronometrist-format-time (seconds &optional (blank " "))
- seconds -> "hⓂ️s"
- Function - chronometrist-common-file-empty-p (file)
- Function - chronometrist-common-clear-buffer (buffer)
- Function - chronometrist-format-keybinds (command map &optional firstonly)
-
Function - chronometrist-events->ts-pairs (events)
- (plist …) -> ((ts . ts) …)
-
Function - chronometrist-ts-pairs->durations (ts-pairs)
- ((ts . ts) …) -> seconds
-
Function - chronometrist-previous-week-start (ts)
- ts -> ts
chronometrist-custom.el
- Custom variable - chronometrist-file
- Custom variable - chronometrist-buffer-name
- Custom variable - chronometrist-hide-cursor
- Custom variable - chronometrist-update-interval
- Custom variable - chronometrist-activity-indicator
- Custom variable - chronometrist-day-start-time
chronometrist-diary-view.el
- Variable - chronometrist-diary-buffer-name
- Internal Variable - chronometrist-diary–current-date
- Function - chronometrist-intervals-on (date)
- Function - chronometrist-diary-tasks-reasons-on (date)
- Function - chronometrist-diary-refresh (&optional ignore-auto noconfirm date)
- Major Mode - chronometrist-diary-view-mode
- Command - chronometrist-diary-view (&optional date)
chronometrist.el
- Internal Variable - chronometrist–task-history
- Internal Variable - chronometrist–point
- Keymap - chronometrist-mode-map
- Command - chronometrist-open-log (&optional button)
- Function - chronometrist-common-create-file ()
-
Function - chronometrist-task-active? (task)
- String -> Boolean
- Function - chronometrist-use-goals? ()
- Function - chronometrist-run-transformers (transformers arg)
- Function - chronometrist-activity-indicator ()
- Function - chronometrist-entries ()
- Function - chronometrist-task-at-point ()
- Function - chronometrist-goto-last-task ()
- Function - chronometrist-print-keybind (command &optional description firstonly)
- Function - chronometrist-print-non-tabular ()
- Function - chronometrist-goto-nth-task (n)
- Function - chronometrist-refresh (&optional ignore-auto noconfirm)
- Internal Variable - chronometrist–file-state
- Function - chronometrist-file-hash (&optional start end hash)
- Function - chronometrist-read-from (position)
- Function - chronometrist-file-change-type (state)
- Function - chronometrist-refresh-file (fs-event)
- Command - chronometrist-query-stop ()
- Command - chronometrist-in (task &optional _prefix)
- Command - chronometrist-out (&optional _prefix)
- Variable - chronometrist-before-in-functions
- Variable - chronometrist-after-in-functions
- Variable - chronometrist-before-out-functions
- Variable - chronometrist-after-out-functions
- Function - chronometrist-run-functions-and-clock-in (task)
- Function - chronometrist-run-functions-and-clock-out (task)
- Keymap - chronometrist-mode-map
- Major Mode - chronometrist-mode
- Function - chronometrist-toggle-task-button (button)
- Function - chronometrist-add-new-task-button (button)
- Command - chronometrist-toggle-task (&optional prefix inhibit-hooks)
- Command - chronometrist-toggle-task-no-hooks (&optional prefix)
- Command - chronometrist-add-new-task ()
- Command - chronometrist (&optional arg)
chronometrist-events.el
-
Variable - chronometrist-events
- keys - iso-date
-
Function - chronometrist-day-start (timestamp)
- iso-timestamp -> encode-time
-
Function - chronometrist-file-clean ()
- commented out, unused
- Function - chronometrist-events-maybe-split (event)
- Function - chronometrist-events-populate ()
- Function - chronometrist-tasks-from-table ()
- Function - chronometrist-events-add (plist)
- Function - chronometrist-events-replace-last (plist)
-
Function - chronometrist-events-subset (start end)
- ts ts -> hash-table
chronometrist-migrate.el
- Variable - chronometrist-migrate-table
- Function - chronometrist-migrate-populate (in-file)
- Function - chronometrist-migrate-timelog-file->sexp-file (&optional in-file out-file)
- Function - chronometrist-migrate-check ()
chronometrist-plist-pp.el
- Variable - chronometrist-plist-pp-keyword-re
- Variable - chronometrist-plist-pp-whitespace-re
- Function - chronometrist-plist-pp-longest-keyword-length ()
- Function - chronometrist-plist-pp-buffer-keyword-helper ()
- Function - chronometrist-plist-pp-buffer ()
- Function - chronometrist-plist-pp-to-string (object)
- Function - chronometrist-plist-pp (object &optional stream)
chronometrist-queries.el
-
Function - chronometrist-last ()
- -> plist
-
Function - chronometrist-task-time-one-day (task &optional (ts (ts-now)))
- String &optional ts -> seconds
-
Function - chronometrist-active-time-one-day (&optional ts)
- &optional ts -> seconds
- Function - chronometrist-statistics-count-active-days (task &optional (table chronometrist-events))
- Function - chronometrist-task-events-in-day (task ts)
chronometrist-report-custom.el
- Custom variable - chronometrist-report-buffer-name
- Custom variable - chronometrist-report-week-start-day
- Custom variable - chronometrist-report-weekday-number-alist
chronometrist-report.el
- Internal Variable - chronometrist-report–ui-date
- Internal Variable - chronometrist-report–ui-week-dates
- Internal Variable - chronometrist-report–point
- Function - chronometrist-report-date ()
-
Function - chronometrist-report-date->dates-in-week (first-date-in-week)
- ts-1 -> (ts-1 … ts-7)
- Function - chronometrist-report-date->week-dates ()
- Function - chronometrist-report-entries ()
- Function - chronometrist-report-print-keybind (command &optional description firstonly)
- Function - chronometrist-report-print-non-tabular ()
- Function - chronometrist-report-refresh (&optional _ignore-auto _noconfirm)
- Function - chronometrist-report-refresh-file (_fs-event)
- Keymap - chronometrist-report-mode-map
- Major Mode - chronometrist-report-mode
- Function - chronometrist-report (&optional keep-date)
- Function - chronometrist-report-previous-week (arg)
- Function - chronometrist-report-next-week (arg)
chronometrist-key-values.el
- Internal Variable - chronometrist–tag-suggestions
- Internal Variable - chronometrist–value-suggestions
- Function - chronometrist-plist-remove (plist &rest keys)
- Function - chronometrist-maybe-string-to-symbol (list)
- Function - chronometrist-maybe-symbol-to-string (list)
- Function - chronometrist-append-to-last (tags plist)
- Variable - chronometrist-tags-history
- Function - chronometrist-history-prep (key history-table)
- Function - chronometrist-tags-history-populate (task history-table file)
- Function - chronometrist-key-history-populate (task history-table file)
- Function - chronometrist-value-history-populate (history-table file)
- Function - chronometrist-tags-history-add (plist)
- Function - chronometrist-tags-history-combination-strings (task)
- Function - chronometrist-tags-history-individual-strings (task)
- Function - chronometrist-tags-prompt (task &optional initial-input)
- Function - chronometrist-tags-add (&rest args)
- Custom Variable - chronometrist-kv-buffer-name
- Variable - chronometrist-key-history
- Variable - chronometrist-value-history
- Keymap - chronometrist-kv-read-mode-map
- Major Mode - chronometrist-kv-read-mode
- Function - chronometrist-kv-completion-quit-key ()
- Function - chronometrist-string-has-whitespace-p (string)
- Function - chronometrist-key-prompt (used-keys)
- Function - chronometrist-value-prompt (key)
- Function - chronometrist-value-insert (value)
- Function - chronometrist-kv-add (&rest args)
- Command - chronometrist-kv-accept ()
- Command - chronometrist-kv-reject ()
- Internal Variable - chronometrist–skip-detail-prompts
- Function - chronometrist-skip-query-prompt (task)
- Function - chronometrist-skip-query-reset (_task)
chronometrist-statistics-custom.el
- Custom variable - chronometrist-statistics-buffer-name
chronometrist-statistics.el
- Internal Variable - chronometrist-statistics–ui-state
- Internal Variable - chronometrist-statistics–point
-
Function - chronometrist-statistics-count-average-time-spent (task &optional (table chronometrist-events))
- string &optional hash-table -> seconds
- Function - chronometrist-statistics-entries-internal (table)
- Function - chronometrist-statistics-entries ()
- Function - chronometrist-statistics-print-keybind (command &optional description firstonly)
- Function - chronometrist-statistics-print-non-tabular ()
- Function - chronometrist-statistics-refresh (&optional ignore-auto noconfirm)
- Keymap - chronometrist-statistics-mode-map
- Major Mode - chronometrist-statistics-mode
- Command - chronometrist-statistics (&optional preserve-state)
- Command - chronometrist-statistics-previous-range (arg)
- Command - chronometrist-statistics-next-range (arg)
chronometrist-time.el
-
Function - chronometrist-iso-timestamp->ts (timestamp)
- iso-timestamp -> ts
-
Function - chronometrist-iso-date->ts (date)
- iso-date -> ts
-
Function - chronometrist-date (&optional (ts (ts-now)))
- &optional ts -> ts (with time 00:00:00)
- Function - chronometrist-format-time-iso8601 (&optional unix-time)
- Function - chronometrist-midnight-spanning-p (start-time stop-time)
-
Function - chronometrist-seconds-to-hms (seconds)
- seconds -> list-duration
-
Function - chronometrist-interval (event)
- event -> duration
chronometrist-timer.el
- Internal Variable - chronometrist–timer-object
- Function - chronometrist-timer ()
- Command - chronometrist-stop-timer ()
- Command - chronometrist-maybe-start-timer (&optional interactive-test)
- Command - chronometrist-force-restart-timer ()
- Command - chronometrist-change-update-interval (arg)
chronometrist-goal
- Internal Variable - chronometrist-goal–timers-list
- Custom Variable - chronometrist-goal-list nil
- Function - chronometrist-goal-run-at-time (time repeat function &rest args)
-
Function - chronometrist-goal-seconds->alert-string (seconds)
- seconds -> string
-
Function - chronometrist-goal-approach-alert (task goal spent)
- string minutes minutes
-
Function - chronometrist-goal-complete-alert (task goal spent)
- string minutes minutes
-
Function - chronometrist-goal-exceed-alert (task goal spent)
- string minutes minutes
-
Function - chronometrist-goal-no-goal-alert (task goal spent)
- string minutes minutes
-
Custom Variable - chronometrist-goal-alert-functions
- each function is passed - string minutes minutes
-
Function - chronometrist-goal-get (task &optional (goal-list chronometrist-goal-list))
- String &optional List -> minutes
- Function - chronometrist-goal-run-alert-timers (task)
- Function - chronometrist-goal-stop-alert-timers (&optional _task)
- Function - chronometrist-goal-on-file-change ()
chronometrist-sexp
- Custom variable - chronometrist-sexp-pretty-print-function
- Macro - chronometrist-sexp-in-file (file &rest body)
- Macro - chronometrist-loop-file (for expr in file &rest loop-clauses)
- Function - chronometrist-sexp-open-log ()
- Function - chronometrist-sexp-between (&optional (ts-beg (chronometrist-date)) (ts-end (ts-adjust 'day +1 (chronometrist-date))))
- Function - chronometrist-sexp-query-till (&optional (date (chronometrist-date)))
-
Function - chronometrist-sexp-last ()
- -> plist
- Function - chronometrist-sexp-current-task ()
- Function - chronometrist-sexp-events-populate ()
- Function - chronometrist-sexp-create-file ()
- Function - chronometrist-sexp-new (plist &optional (buffer (find-file-noselect chronometrist-file)))
- Function - chronometrist-sexp-delete-list (&optional arg)
- Function - chronometrist-sexp-replace-last (plist)
- Command - chronometrist-sexp-reindent-buffer ()
As indicated by exploratory work in the parsimonious-reading
branch, where I made a loop to only read
and collect s-expressions from the file. It was near-instant…until I added event splitting to it.
To remove this restriction, I had briefly considered making a keyword called :user
, whose value would be another plist containing all user-defined keyword-values. But in practice, this hasn't been a big enough issue yet to justify the work.