Fix divison by zero in workload overview report

When today's work hours are used up, show some sensible value instead of “-nan%”.
This commit is contained in:
Daniel Borchmann 2024-10-06 11:48:48 +02:00
parent fa6bd0e713
commit 344dde2821
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625

View File

@ -810,28 +810,34 @@ PARAMS is a property list of the following parameters:
;; Compute workload report for each date and record the total time; ;; Compute workload report for each date and record the total time;
(let ((total-work-hours 0)) (let ((total-work-hours 0))
(dolist (interval-end-date date-range) (dolist (interval-end-date date-range)
(let* ((total-time-duration (car ;; XXX: this might be slow, try to reduce the calls to (let* ((total-time-duration (car
;; `db/org-planned-tasks-in-range'. ;; XXX: repeatedly calling `db/org-planned-tasks-in-range might
;; be slow, try to reduce the numer of calls
(db/org-planned-tasks-in-range (db/org-planned-tasks-in-range
;; Set start date to nil to also include tasks scheduled or ;; Set start date to nil to also include tasks scheduled or
;; deadlined before `start-date', as those are also still open ;; deadlined before `start-date', as those are also still open
;; and need to be done somewhen. ;; and need to be done somewhen.
nil nil
interval-end-date interval-end-date
org-ql-match))) org-ql-match))))
(utilization (* (/ (org-duration-to-minutes total-time-duration)
(cl-incf total-work-hours (cl-incf total-work-hours
(org-duration-to-minutes (org-duration-to-minutes
(funcall work-hours interval-end-date)))) (funcall work-hours interval-end-date)))
100)))
(insert (format "| [%s] | %s | %s | %s |\n" (insert (format "| [%s] | %s | %s | %s |\n"
interval-end-date interval-end-date
total-time-duration total-time-duration
(org-duration-from-minutes total-work-hours) (org-duration-from-minutes total-work-hours)
;; XXX: the following code might better to into a spearate function to
;; increase comprehension
(let* ((total-time-minutes (org-duration-to-minutes total-time-duration)))
(cond
((zerop total-time-minutes) "0.00%")
((zerop total-work-hours) "*Inf*")
(t (let ((utilization (* (/ total-time-minutes total-work-hours) 100)))
(if (<= 80 utilization) (if (<= 80 utilization)
;; When utilization is above 80%, mark entry in bold ;; When utilization is above 80%, mark entry in bold
(format "*%.2f%%*" utilization) (format "*%.2f%%*" utilization)
(format "%.2f%%" utilization))))))) (format "%.2f%%" utilization)))))))))))
(insert "|--|") (insert "|--|")
(org-table-align))) (org-table-align)))