Skip to content

Instantly share code, notes, and snippets.

@isoraqathedh
Last active February 27, 2020 16:28
Show Gist options
  • Save isoraqathedh/df7fc82d6064d1021d78c771ad6f96b9 to your computer and use it in GitHub Desktop.
Save isoraqathedh/df7fc82d6064d1021d78c771ad6f96b9 to your computer and use it in GitHub Desktop.
;; n.b. do not set the number of days to be above 14,
;; as that is the point when archiving kicks in
;; and so the dates are not accurate.
(let* ((days* (if (boundp 'days) days 14))
(cutoff (* days* 24 3600)) ; Days in seconds
(cutoff* (/ cutoff 60)) ; and in minutes
(fmt "%.2f")
(cutoff-time (time-add (current-time) (- cutoff)))
(dat ())
(grand-total 0)
(sources `((work "Work" . ,(agenda-work-clocktable-files))
(life "Life" . ,(org-agenda-files))
(sleep "Sleep" . (,(concat org-directory "/maint/sleep.org"))))))
(labels ((counted-minutes (element)
(let ((timestamp (org-element-property :value element)))
(if (and
(eql 'closed (org-element-property :status element))
(time-less-p
cutoff-time
(date-to-time
(format "%d-%02d-%02dT%02d:%02d:00"
(org-element-property :year-end timestamp)
(org-element-property :month-end timestamp)
(org-element-property :day-end timestamp)
(org-element-property :hour-end timestamp)
(org-element-property :minute-end timestamp)))))
(org-duration-to-minutes (org-element-property :duration element))
0)))
(sum-clocks-with-key (key files)
(cons key (loop for file-name in files
when file-name
sum (with-current-buffer (find-buffer-visiting file-name)
(reduce #'+ (org-element-map
(org-element-parse-buffer) 'clock
#'counted-minutes))))))
(format-row (header key total)
(let ((amount (typecase key
(symbol (cdr (assoc key dat)))
(number key))))
(list header
(org-duration-from-minutes amount)
(floor amount)
(format fmt (/ amount total 0.01))
(format fmt (/ amount cutoff* 0.01))))))
(dolist (i sources)
(add-to-list 'dat (sum-clocks-with-key (car i) (cddr i))))
(setq grand-total (reduce #'+ dat :key #'cdr))
`(("Type" "Total" "Mins" "% clocked" "% period")
hline
,@(mapcar (lambda (r)
(format-row (second r) (first r) grand-total))
sources)
hline
,(format-row "Total" grand-total grand-total)
,(format-row "Period" cutoff* grand-total))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment