-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-chronos.el
1254 lines (1116 loc) · 52.3 KB
/
org-chronos.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; org-chronos.el --- Org-based time management -*- lexical-binding: t -*-
;; Copyright (C) 2021 Akira Komamura
;; Author: Akira Komamura <akira.komamura@gmail.com>
;; Version: 0.1
;; Package-Requires: ((emacs "27.1") (dash "2.18") (org-ql "0.6") (ts "0.2") (org "9.4"))
;; Keywords: outlines hypermedia
;; URL: https://github.com/akirak/org-chronos
;; This file is not part of GNU Emacs.
;;; License:
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; FIXME
;;; Code:
(require 'dash)
(require 'org-ql)
(require 'org-element)
(require 'org-clock)
(require 'eieio)
(require 'org-chronos-utils)
(defgroup org-chronos
nil
"A time-tracking framework for Org."
:group 'org
:group 'org-clock)
;;;; Constants
(defconst org-chronos-json-version "0.1.1")
;;;; Structs
(cl-defstruct org-chronos-clock-range
"Structure that represents a certain period of time."
;; Start time of the range as ts
start
;; End time of the range as ts
end
;; Duration string as in Org, i.e. HH:MM
duration-string
;; Duration in minutes as a number
duration-minutes)
(cl-defstruct org-chronos-log-note
type timestamp attributes comment)
(cl-defstruct org-chronos-heading-element
"Structure that holds information on a heading with clock entries."
marker olp tags category properties todo-state link log-notes clock-entries
closed created)
;;;; Custom variables
(defcustom org-chronos-logged-properties nil
"List of properties that should be recorded."
:group 'org-chronos
:type '(repeat (choice string
(cons string plist))))
(defcustom org-chronos-show-property-summary t
"Whether to produce summary tables for `org-chronos-logged-properties'."
:group 'org-chronos
:type 'boolean)
(defcustom org-chronos-log-dblock-defaults
(list :span 'day :files #'org-agenda-files
:sections "groups,property-groups,entries,closed,created")
"Default parameters of the Org dynamic block."
:group 'org-chronos
:type 'plist)
(defcustom org-chronos-scan-containing-file nil
"Whether to include the file that contains the dblock.
When this variable is non-nil, the dynamic block adds the
containing file to the source files."
:group 'org-chronos
:type 'boolean)
(defcustom org-chronos-annotate-links t
"Whether to decorate headline names with links.
Note that a link is produced on every entry having clock
entries. This may lead to generating IDs if you have turned on
`org-id-link-to-org-use-id'.
The value can also be a function that takes no argument and
returns a boolean value."
:group 'org-chronos
:type '(choice boolean function))
(defcustom org-chronos-trim-headline 25
"Maximimal length of headlines in Org dynamic block output."
:group 'org-chronos
:type '(choice number null))
(defcustom org-chronos-tag-groups nil
"List of tags used to group headings."
:group 'org-chronos
:type '(repeat string))
(defcustom org-chronos-ignored-categories nil
"List of categories that are excluded from the input data.
When this variable is set to a list of Org categories, items that
belong to one of the categories are excluded from statistics
and the output."
:group 'org-chronos
:type '(repeat string))
(defcustom org-chronos-clock-threshold nil
"Threshold of duration to display an item in blocks."
:group 'org-chronos
:type '(choice null number))
(defcustom org-chronos-duration-format 'h:mm
"Custom format used in this package.
If this variable is non-nil, it is used as the format for
converting durations to strings in this package.
If it is nil, the default value is used.
See `org-duration-format' for possible values of this variable."
:group 'org-chronos
:type 'sexp)
(defcustom org-chronos-auto-export nil
"Whether to export the log data on every evaluation."
:group 'org-chronos
:type 'boolean)
(defcustom org-chronos-export-root-directory nil
"Destination directory of exporting.
Files are saved to subdirectories of this directory based on the
time span."
:group 'org-chronos
:type 'directory)
(defcustom org-chronos-creation-time-property nil
"Name of an Org property used to track creation time of an entry.
Values of the property must be an inactive timestamp."
:group 'org-chronos
:type '(choice string null))
(defcustom org-chronos-reduce-nested-entries t
"Whether to exclude nested entries from certain views in the report."
:group 'org-chronos
:type 'boolean)
(defcustom org-chronos-json-formatter 'jq
"Formatter applied to exported to JSON files."
:group 'org-chronos
:type '(choice (const :tag "jq" jq)
null))
(defcustom org-chronos-jq-executable "jq"
"Executable of jq.
See `org-chronos-json-formatter'."
:group 'org-chronos
:type 'file)
;;;; Log object
(defclass org-chronos-log ()
((span :initarg :span
:type (or null (member day week month)))
(snapshot-time :initarg :snapshot-time
:type ts)
(start :initarg :start
:type ts)
(end :initarg :end
:type (or null ts))
(files :initarg :files
:type list)
(data :type list)))
;;;; Parsing
(defsubst org-chronos--closed-clock-p (clock)
"Return t is CLOCK is closed."
(and (eq 'clock (org-element-type clock))
(eq 'closed (org-element-property :status clock))))
(defun org-chronos--timestamp-start-ts (timestamp)
"Return the start time of TIMESTAMP as a ts object."
(ts-fill
(make-ts :year (org-element-property :year-start timestamp)
:month (org-element-property :month-start timestamp)
:day (org-element-property :day-start timestamp)
:hour (org-element-property :hour-start timestamp)
:minute (org-element-property :minute-start timestamp)
:second 0)))
(defun org-chronos--timestamp-end-ts (timestamp)
"Return the end time of TIMESTAMP as a ts object."
(ts-fill
(make-ts :year (org-element-property :year-end timestamp)
:month (org-element-property :month-end timestamp)
:day (org-element-property :day-end timestamp)
:hour (org-element-property :hour-end timestamp)
:minute (org-element-property :minute-end timestamp)
:second 0)))
(defun org-chronos--parse-clock-line ()
"Return `org-chronos-clock-range' from the current clock line."
(let ((clock (org-element-clock-parser (line-end-position))))
(when (org-chronos--closed-clock-p clock)
(let ((timestamp (org-element-property :value clock))
(duration-string (org-element-property :duration clock)))
(make-org-chronos-clock-range
:start (org-chronos--timestamp-start-ts timestamp)
:end (org-chronos--timestamp-end-ts timestamp)
:duration-string duration-string
:duration-minutes (org-duration-to-minutes duration-string))))))
(defmacro org-chronos--with-logbook (&rest progn)
"Evaluate PROGN with the buffer narrowed to the logbook, if any."
`(save-excursion
(org-back-to-heading)
(end-of-line 1)
(let* ((content-end (org-entry-end-position))
(logbook-end (save-excursion
(re-search-forward org-logbook-drawer-re
content-end t))))
(when logbook-end
(goto-char (car (match-data)))
(save-restriction
(narrow-to-region (point) logbook-end)
,@progn)))))
(defun org-chronos--entry-creation-time ()
"Return the creation time of the entry.
This looks up the value of `org-chronos-creation-time-property'
if set, and converts it to a ts value."
(when org-chronos-creation-time-property
(-some->> (org-entry-get nil org-chronos-creation-time-property)
(ts-parse-org)
(ts-update))))
(defun org-chronos--clock-entries-on-heading ()
"Return clock entries on the current Org heading after the point."
(org-chronos--with-logbook
(let (entries)
(while (re-search-forward org-clock-line-re nil t)
(when-let (range (org-chronos--parse-clock-line))
(push range entries)))
entries)))
(defun org-chronos--parse-logbook ()
"Return clock entries on the current Org heading after the point."
(org-chronos--with-logbook
(condition-case err
(let (clock-entries
log-notes)
(catch 'finish
(while (not (eobp))
(cond
((looking-at org-clock-line-re)
(when-let (range (org-chronos--parse-clock-line))
(push range clock-entries))
(forward-line))
((looking-at (rx (* space) "- "))
(push (org-chronos--parse-log-note-heading)
log-notes))
((looking-at org-clock-drawer-end-re)
(throw 'finish t))
(t
(beginning-of-line 2)))))
(list :clock-entries clock-entries
:log-notes log-notes))
(error (error "Error while parsing logbook at %d in %s: %s"
(point)
(buffer-file-name)
err)))))
(defun org-chronos--log-note-heading-to-re (pattern)
"Convert a log note pattern to a regular expression.
PATTERN should be the cdr of one of the items in
`org-log-note-headings'."
(--> pattern
(replace-regexp-in-string
(rx "%" (? "-") (* (any digit)) (any "td"))
(replace-regexp-in-string
"\\\\" "\\\\\\\\"
(org-re-timestamp 'inactive))
it t)
(replace-regexp-in-string
(rx "%" (? "-") (* (any digit)) (any "TD"))
(replace-regexp-in-string
"\\\\" "\\\\\\\\"
(org-re-timestamp 'active))
it t)
(replace-regexp-in-string
(rx "%" (? "-") (* (any digit)) (any "sS"))
(replace-regexp-in-string
"\\\\" "\\\\\\\\"
(rx (* space)
(? "\""
(regexp org-todo-regexp)
;; %s and %S can be a timestamp as well.
;; I haven't found actual use of such a pattern,
;; but I have to support it in the future.
;;
;; (or (regexp org-todo-regexp)
;; (regexp (org-re-timestamp 'inactive)))
"\"")
(* space)))
it t)
(concat (rx (* space) "- ") it)))
(defun org-chronos--parse-log-note-heading ()
"Parse a log note heading in the buffer.
This function parses a log note at point and returns an object of
`org-chronos-log-note' type."
(catch 'finish
(pcase-dolist (`(,type . ,pattern) org-log-note-headings)
(unless (string-empty-p pattern)
(let ((re (org-chronos--log-note-heading-to-re pattern)))
(when (looking-at re)
(goto-char (nth 1 (match-data)))
(let* ((placeholders (s-match-strings-all
(rx "%" (? "-") (* (any digit))
;; TODO: Add %u and %U
(group (any "sStTdD")))
pattern))
(match-strings (-map (pcase-lambda (`(,begin ,end))
(when (and begin end)
(buffer-substring-no-properties begin end)))
(-partition-all 2 (match-data))))
(note-data (org-chronos--analyse-log-note-data
placeholders
(cdr match-strings)))
(timestamp-ts (org-chronos--ts-from-decoded-time
(org-parse-time-string
(alist-get 'timestamp note-data))))
(comment-start (when (looking-at (rx (* space) "\\\\"))
(re-search-forward (rx (* space) "\\\\\n" (* space)))))
(comment (if comment-start
(progn
(re-search-forward (rx (or (regexp org-clock-line-re)
(and bol (* space) "- ")
(regexp org-clock-drawer-end-re)))
nil t)
(beginning-of-line 1)
(buffer-substring-no-properties comment-start (1- (point))))
(beginning-of-line 2)
nil)))
(throw 'finish
(make-org-chronos-log-note
:type type
:timestamp timestamp-ts
:attributes note-data
:comment comment)))))))))
(defun org-chronos--analyse-log-note-data (placeholders match-strings)
"Given the parsing result, return an alist of data.
Both PLACEHOLDERS and MATCH-STRINGS must be a list that have an
equal number of items."
(->> (-zip (--map (nth 1 it) placeholders) match-strings)
(-map (pcase-lambda (`(,c . ,s))
(cl-labels
((parse-todo
(raw)
(save-match-data
(when (and raw
(string-match
(rx bol "\"" (group (+ anything)) "\"" eol)
raw))
(match-string 1 raw)))))
(pcase c
("t" (cons 'timestamp s))
("T" (cons 'timestamp s))
("d" (cons 'timestamp s))
("D" (cons 'timestamp s))
("s" (cons 'old-todo (parse-todo s)))
("S" (cons 'new-todo (parse-todo s)))))))))
(defun org-chronos--search-headings-with-clock (files from to)
"Search headings with clock entries in a given time range.
FIXME: FILES, FROM, and TO."
(let (result)
(dolist (file files)
(with-current-buffer (or (find-buffer-visiting file)
(find-file-noselect file))
(org-save-outline-visibility t
(org-with-wide-buffer
(org-show-all)
(push (org-ql-select (current-buffer)
`(ts-inactive :from ,from :to ,to)
:action
`(let* ((closed (progn
;; We are at the headline at the beginning,
;; so move on to the next line.
(beginning-of-line 2)
(when (looking-at org-closed-time-regexp)
(re-search-forward org-ts-regexp-inactive)
(ts-parse-org (match-string 0)))))
;; Ignore values that are not within the range.
(closed (when (and closed
(ts-in ,from ,to closed))
closed))
(logbook (org-chronos--parse-logbook))
(clock-entries (-filter (lambda (x)
(ts-in ,from ,to (org-chronos-clock-range-start x)))
(plist-get logbook :clock-entries)))
(created (org-chronos--entry-creation-time))
;; Ignore values that are not within the range.
(created (when (and created
(ts-in ,from ,to created))
created))
(log-notes (-filter (lambda (x)
(ts-in ,from ,to (org-chronos-log-note-timestamp x)))
(plist-get logbook :log-notes))))
(when (or clock-entries log-notes created closed)
(make-org-chronos-heading-element
:marker (point-marker)
:link (when (if (functionp org-chronos-annotate-links)
(funcall org-chronos-annotate-links)
org-chronos-annotate-links)
(save-excursion
(org-store-link nil 'interactive)
(pop org-stored-links)))
:closed closed
:created created
:olp (org-get-outline-path t t)
:tags (org-get-tags)
:category (org-get-category)
:properties (org-chronos--collect-properties)
:todo-state (org-get-todo-state)
:log-notes log-notes
:clock-entries clock-entries))))
result)))))
(->> (-flatten-n 1 (nreverse result))
(-filter #'org-chronos--meaningful-element-p))))
(defun org-chronos--collect-properties ()
"Return an alist of property values specified in `org-chronos-logged-properties'."
(->> org-chronos-logged-properties
(-map (lambda (x)
(pcase x
((pred stringp)
(cons x (org-entry-get nil x)))
(`(,name . ,plist)
(cons name (org-entry-get nil name
(plist-get plist :inherit)
(plist-get plist :literal-nil)))))))
(-filter #'cdr)))
(defun org-chronos--meaningful-element-p (element)
"Check if ELEMENT will be meaningful in the report."
;; Since clocks may not be contained in the heading but in
;; children, you have to exclude headings without clock entries
;; during the period.
(and (org-chronos-heading-element-p element)
(not (member (org-chronos-heading-element-category element)
org-chronos-ignored-categories))))
;;;; Generic functions
(cl-defgeneric org-chronos--sum-minutes (x)
"Return the clock sum on X in minutes.")
(cl-defmethod org-chronos--sum-minutes ((x org-chronos-clock-range))
"Return the clock sum on X in minutes."
(org-chronos-clock-range-duration-minutes x))
(cl-defmethod org-chronos--sum-minutes ((x org-chronos-heading-element))
"Return the clock sum on X in minutes."
(->> (org-chronos-heading-element-clock-entries x)
(-map #'org-chronos--sum-minutes)
(-sum)))
;;;; Grouping
(defun org-chronos--group-elements-by-tag (elements)
"Group ELEMENTS by tags defined in `org-chronos-tag-groups'."
(let ((entries-1 (copy-sequence elements))
result)
(dolist (tag org-chronos-tag-groups)
(-let (((group-entries rest) (--separate
(-contains-p (org-chronos-heading-element-tags it)
tag)
entries-1)))
(push (cons tag group-entries) result)
(setq entries-1 rest)))
(when entries-1
(push (cons nil entries-1) result))
(nreverse result)))
(defun org-chronos--group-elements-by-category (elements)
"Group ELEMENTS by categories."
(-group-by #'org-chronos-heading-element-category elements))
(defun org-chronos--group-elements-by-property (property elements)
"Group elements by a property.
This uses the values of PROPERTY to group ELEMENTS."
(->> elements
(-group-by (lambda (x)
(->> (org-chronos-heading-element-properties x)
(assoc property)
(cdr))))
;; Ignore nil groups
(-filter #'car)))
;;;; Org output functions
(cl-defun org-chronos--write-elements-as-org-table (elements
&key grouped
range-format
todo-state
show-total)
"Insert elements to the buffer as an Org table.
ELEMENTS is a list of `org-chronos-heading-element'.
GROUPED, RANGE-FORMAT, TODO-STATE, and SHOW-TOTAL specifies
output options."
(let* ((columns (-non-nil (list (when grouped '(group "Group"))
'(name "Task")
'(duration "Duration")
(when range-format '(start "Start"))
(when range-format '(end "End"))
(when todo-state '(todo "State")))))
(hline (concat "|-" (string-join (-repeat (1- (length columns)) "-+-"))
"-|\n"))
(total 0))
;; Header
(insert "| " (mapconcat (-partial #'nth 1) columns " | ") "\n")
(insert hline)
;; Body
(cl-labels
((write-element-row
(x &optional group)
(insert "| "
(mapconcat (lambda (column)
(pcase (car column)
('group
group)
('name
(if-let (link (org-chronos-heading-element-link x))
(org-link-make-string (car link)
(org-chronos--trim-string-at-length
(org-chronos--escape-link-description
(nth 1 link))
org-chronos-trim-headline))
(org-chronos--trim-string-at-length
(org-chronos--escape-link-description
(-last-item (org-chronos-heading-element-olp x)))
org-chronos-trim-headline)))
('duration
(org-duration-from-minutes (org-chronos--sum-minutes x)
org-chronos-duration-format))
('start
(ts-format range-format
(org-chronos--start-of-clocks
(org-chronos-heading-element-clock-entries x))))
('end
(ts-format range-format
(org-chronos--end-of-clocks
(org-chronos-heading-element-clock-entries x))))
('todo
(or (org-chronos-heading-element-todo-state x)
""))))
columns
" | ")
" |\n")
(cl-incf total (org-chronos--sum-minutes x))))
(if grouped
(pcase-dolist (`(,group . ,group-elements) elements)
(dolist (x group-elements)
(write-element-row x group)))
(dolist (x elements)
(write-element-row x))))
;; Footer (optional)
(when show-total
(insert hline)
(let ((n (-find-index (lambda (column) (eq 'duration (car column))) columns)))
(insert "| "
(string-join (-repeat (1- n) " | "))
" *Total* | "
(org-duration-from-minutes total org-chronos-duration-format)
(string-join (-repeat (1- (- (length columns) n)) " | "))
" |\n")))
(delete-char -1)
(org-table-align)))
(defun org-chronos--trim-string-at-length (string max-length)
"Trim STRING at MAX-LENGTH."
(if (and max-length
(> (length string) max-length))
(concat (substring string 0 (- max-length 4)) "...")
string))
(defsubst org-chronos--org-table-hline (columns)
"Return a separator for a table with COLUMNS."
(concat "|-"
(apply #'concat (-repeat (cl-etypecase columns
(list (length columns))
(number columns))
"-+-"))
"-|\n"))
(defsubst org-chronos--write-org-table-row (cells)
"Insert CELLS to the buffer as an Org table row."
(declare (indent 0))
(insert "| " (string-join cells " | ") " |\n"))
(cl-defun org-chronos--write-group-sums-as-org-table (groups group-type
&key show-percents)
"Insert groups to the buffer as an Org table.
FIXME: GROUPS, GROUP-TYPE, and SHOW-PERCENTS."
(let* ((columns (-non-nil (list group-type
'duration
(when show-percents
'percent))))
(hline (org-chronos--org-table-hline columns))
(groups-with-sums (->> (-map (pcase-lambda (`(,group . ,elements))
(list group
elements
(-sum (-map #'org-chronos--sum-minutes elements))))
groups)
(-sort (-on #'> (-partial #'nth 2)))))
(total (->> groups-with-sums
(--map (nth 2 it))
(-sum))))
;; header
(org-chronos--write-org-table-row
(-map (lambda (column)
(cl-case column
(tag "Tag")
(category "Category")
(duration "Sum")
(percent "%")
(otherwise (format "%s" column))))
columns))
(insert hline)
;; body
(pcase-dolist (`(,group ,_elements ,sum) groups-with-sums)
(when (> sum 0)
(org-chronos--write-org-table-row
(-map (lambda (column)
(cl-case column
(duration (org-duration-from-minutes sum org-chronos-duration-format))
(percent (format "%.0f %%" (/ (* 100 sum) total)))
(otherwise (or group "-"))))
columns))))
;; footer
(insert hline)
(org-chronos--write-org-table-row
(-map (lambda (column)
(cl-case column
(duration (org-duration-from-minutes total org-chronos-duration-format))
(percent "100 %")
(otherwise "*Total*")))
columns))
(delete-char -1)
(org-table-align)))
(cl-defgeneric org-chronos--write-org (obj)
"Write OBJ as Org into the buffer.")
(cl-defgeneric org-chronos--write-org-null-p (obj)
"Return non-nil if OBJ has no entries.")
(cl-defstruct org-chronos-composite-view
views)
(cl-defmethod org-chronos--write-org ((obj org-chronos-composite-view))
"Write OBJ as Org into the buffer."
(let (not-first)
(dolist (view (org-chronos-composite-view-views obj))
(unless (org-chronos--write-org-null-p view)
(when not-first
(insert "\n\n"))
(org-chronos--write-org view)
(setq not-first t)))))
(cl-defmethod org-chronos--write-org-null-p ((obj org-chronos-composite-view))
"Return non-nil if OBJ has no entries."
(-all-p #'org-chronos--write-org-null-p
(org-chronos-composite-view-views obj)))
(cl-defstruct org-chronos-entry-view
items-or-groups grouped time-format todo-state show-total)
(defun org-chronos-entry-view-clocked-items-or-groups (x)
"Return items that have clock entries in X."
(if (org-chronos-entry-view-grouped x)
(->> (org-chronos-entry-view-items-or-groups x)
(-map (pcase-lambda (`(,group . ,items))
(cons group
(-filter #'org-chronos-heading-element-clock-entries items))))
(-filter #'cdr))
(->> (org-chronos-entry-view-items-or-groups x)
(-filter #'org-chronos-heading-element-clock-entries))))
(cl-defmethod org-chronos--write-org ((obj org-chronos-entry-view))
"Write OBJ as Org into the buffer."
(org-chronos--write-elements-as-org-table
(org-chronos-entry-view-clocked-items-or-groups obj)
:grouped (org-chronos-entry-view-grouped obj)
:range-format (org-chronos-entry-view-time-format obj)
:todo-state (org-chronos-entry-view-todo-state obj)
:show-total (org-chronos-entry-view-show-total obj)))
(cl-defmethod org-chronos--write-org-null-p ((obj org-chronos-entry-view))
"Return non-nil if OBJ has no entries."
(null (org-chronos-entry-view-clocked-items-or-groups obj)))
(cl-defstruct org-chronos-closed-items-view items-or-groups grouped time-format)
(defun org-chronos-closed-items-view-closed-items (x)
"Return items that have clock entries in X."
(->> (if (org-chronos-closed-items-view-grouped x)
(->> (org-chronos-closed-items-view-items-or-groups x)
(-map (pcase-lambda (`(,group . ,items))
(--map (cons group it) items)))
(-flatten-n 1))
(--map (cons nil it) (org-chronos-closed-items-view-items-or-groups x)))
(--map (when-let (closed (org-chronos-heading-element-closed (cdr it)))
(cons closed it)))
(-non-nil)
(org-chronos--reduce-nested-entries)))
(cl-defmethod org-chronos--write-org ((obj org-chronos-closed-items-view))
"Write OBJ as Org into the buffer."
(let ((time-format (org-chronos-closed-items-view-time-format obj))
(grouped (org-chronos-closed-items-view-grouped obj)))
(insert "(Closed items)\n"
(mapconcat (pcase-lambda (`(,time ,group . ,element))
(format "- %s %s%s %s (%s in sum) \\ %s"
(ts-format time-format time)
(org-chronos-heading-element-todo-state element)
(if grouped (concat " " group) (group) "")
(if-let (link (org-chronos-heading-element-link element))
(apply #'org-link-make-string link)
(-last-item (org-chronos-heading-element-olp element)))
(org-with-point-at (org-chronos-heading-element-marker element)
(org-duration-from-minutes (org-clock-sum-current-item)))
(org-format-outline-path
(nreverse (-butlast (org-chronos-heading-element-olp element)))
nil nil " \\ ")))
(->> (org-chronos-closed-items-view-closed-items obj)
(-sort (-on #'ts< #'car)))
"\n"))))
(cl-defmethod org-chronos--write-org-null-p ((obj org-chronos-closed-items-view))
"Return non-nil if OBJ has no entries."
(null (org-chronos-closed-items-view-closed-items obj)))
(cl-defstruct org-chronos-created-items-view items-or-groups grouped time-format)
(defun org-chronos-created-items-view-created-items (x)
"Return items that have clock entries in X."
(->> (if (org-chronos-created-items-view-grouped x)
(->> (org-chronos-created-items-view-items-or-groups x)
(-map (pcase-lambda (`(,group . ,items))
(--map (cons group it) items)))
(-flatten-n 1))
(--map (cons nil it) (org-chronos-created-items-view-items-or-groups x)))
(--map (when-let (created (org-chronos-heading-element-created (cdr it)))
;; Don't report items that were closed during the period.
(unless (or (org-chronos-heading-element-closed (cdr it))
(member "ARCHIVE" (org-chronos-heading-element-tags (cdr it)))
;; Don't report items in the same buffer as the evaluation context.
(equal (current-buffer)
(marker-buffer (org-chronos-heading-element-marker (cdr it)))))
(cons created it))))
(-non-nil)
(org-chronos--reduce-nested-entries)))
(cl-defmethod org-chronos--write-org ((obj org-chronos-created-items-view))
"Write OBJ as Org into the buffer."
(let ((grouped (org-chronos-created-items-view-grouped obj)))
(insert "(New items)\n"
(mapconcat (pcase-lambda (`(,_ ,group . ,element))
(format "- %s%s \\ %s"
(if (and grouped group) (concat group " ") "")
(if-let (link (org-chronos-heading-element-link element))
(apply #'org-link-make-string link)
(-last-item (org-chronos-heading-element-olp element)))
(org-format-outline-path
(nreverse (-butlast (org-chronos-heading-element-olp element)))
nil
nil
" \\ ")))
(->> (org-chronos-created-items-view-created-items obj)
(-sort (-on #'ts< #'car)))
"\n"))))
(cl-defmethod org-chronos--write-org-null-p ((obj org-chronos-created-items-view))
"Return non-nil if OBJ has no entries."
(null (org-chronos-created-items-view-created-items obj)))
(cl-defstruct org-chronos-group-statistic-view
group-type groups show-percents)
(cl-defmethod org-chronos--write-org ((obj org-chronos-group-statistic-view))
"Write OBJ as Org into the buffer."
(org-chronos--write-group-sums-as-org-table
(org-chronos-group-statistic-view-groups obj)
(org-chronos-group-statistic-view-group-type obj)
:show-percents (org-chronos-group-statistic-view-show-percents obj)))
(cl-defmethod org-chronos--write-org-null-p ((obj org-chronos-group-statistic-view))
"Return non-nil if OBJ has no entries."
(-all-p (-compose #'null #'cdr) (org-chronos-group-statistic-view-groups obj)))
(defun org-chronos--reduce-nested-entries (items)
"If necessary, exclude nested entries from ITEMS."
(if org-chronos-reduce-nested-entries
(->> (--group-by (nth 1 it) items)
(-map #'cdr)
(-map (lambda (entries-in-group)
(->> (--group-by (marker-buffer
(org-chronos-heading-element-marker (cddr it)))
entries-in-group)
(-map #'cdr)
(-map #'org-chronos--reduce-by-olp))))
(-flatten-n 2))
items))
(defun org-chronos--reduce-by-olp (items)
"Check olps in ITEMS and eliminate nested entries."
(let (result)
(dolist (group-by-depth (->> items
(--map (cons (org-chronos-heading-element-olp (cddr it)) it))
(-group-by (-compose #'length #'car))
(-sort (-on #'< #'car))
(-map #'cdr)))
(dolist (item group-by-depth)
(unless (--find (-is-prefix-p (car it) (car item)) result)
(push item result))))
(-map #'cdr result)))
;;;; Exporting
(cl-defun org-chronos--export-log-to-json (log out-file
&key groups group-type)
"Export log data to a JSON file.
LOG must be an object of `org-chronos-log' type.
It write the data of the log object to OUT-FILE in JSON.
Optionally, it can take a list of GROUPS and its GROUP-TYPE."
(cl-check-type log org-chronos-log)
(with-temp-buffer
(insert (json-serialize
(org-chronos--build-object-for-json
:span (oref log span)
:start (oref log start)
:end (oref log end)
:elements (oref log data)
:group-type group-type
:groups groups
:closed (org-chronos--log-closed-p log)
:files (oref log files))))
(cl-case org-chronos-json-formatter
(jq (when (or (file-executable-p org-chronos-jq-executable)
(executable-find org-chronos-jq-executable))
(unless (zerop (call-process-region
(point-min) (point-max)
org-chronos-jq-executable
nil (list (current-buffer) nil)
"."))
(error "Error returned while running jq on the exported JSON")))))
(write-region (point-min) (point-max) out-file)))
(cl-defun org-chronos--build-object-for-json (&key span start end elements
group-type groups
closed
files)
"Build an alist that should be converted to json for exporting.
See `org-chronos--export-log-to-json' for the following parameters:
SPAN, START, END, ELEMENTS, GROUP-TYPE, GROUPS, CLOSED, FILES."
`((version . ,org-chronos-json-version)
(closed . ,(if closed t :false))
(source . ((files . ,(apply #'vector files))))
(range . ((span . ,(symbol-name span))
(start . ,(ts-format start))
(end . ,(ts-format end))))
(meta . ((groups . ,(if groups
`((,group-type
. ,(apply #'vector
(-map (pcase-lambda (`(,group . ,_elements))
group)
groups))))
:null))
(properties . ,(if org-chronos-logged-properties
(apply #'vector
(-map (lambda (p)
(pcase p
((pred stringp) p)
(`(,name . ,_) name)))
org-chronos-logged-properties))
:null))))
(data . ((headings . ,(apply #'vector
(-map #'org-chronos--json-serializable-object
elements)))))))
(cl-defgeneric org-chronos--json-serializable-object (x)
"Convert X to an object that can be serialized to JSON.")
(cl-defmethod org-chronos--json-serializable-object ((x org-chronos-heading-element))
"Convert X to an object that can be serialized to JSON."
(let ((marker (org-chronos-heading-element-marker x))
(olp (-map #'org-link-display-format (org-chronos-heading-element-olp x)))
(tags (org-chronos-heading-element-tags x))
(category (org-chronos-heading-element-category x))
(properties (org-chronos-heading-element-properties x))
(todo-state (org-chronos-heading-element-todo-state x))
(link (org-chronos-heading-element-link x))
(log-notes (org-chronos-heading-element-log-notes x))
(closed (org-chronos-heading-element-closed x))
(created (org-chronos-heading-element-created x))
(clock-entries (org-chronos-heading-element-clock-entries x)))
`((buffer . ,(buffer-name (marker-buffer marker)))
(position . ,(marker-position marker))
(title . ,(-last-item olp))
(outline . ,(apply #'vector olp))
(tags . ,(apply #'vector tags))
(properties . ,(-map (pcase-lambda (`(,key . ,value))
(cons (intern key) value))
properties))
(link . ,(car link))
(category . ,(or category :null))
(todo . ,(or todo-state :null))
(closed . ,(if closed (ts-format closed) :null))
(created . ,(if created (ts-format created) :null))
(log-notes . ,(apply #'vector
(-map #'org-chronos--json-serializable-object
log-notes)))
(clocks . ,(apply #'vector
(-map #'org-chronos--json-serializable-object
clock-entries))))))
(cl-defmethod org-chronos--json-serializable-object ((x org-chronos-log-note))
"Convert X to an object that can be serialized to JSON."
`((time . ,(ts-format (org-chronos-log-note-timestamp x)))
(type . ,(symbol-name (org-chronos-log-note-type x)))
(attributes . ,(org-chronos-log-note-attributes x))
(comment . ,(or (org-chronos-log-note-comment x) :null))))
(cl-defmethod org-chronos--json-serializable-object ((x org-chronos-clock-range))
"Convert X to an object that can be serialized to JSON."
(let ((start (org-chronos-clock-range-start x))
(end (org-chronos-clock-range-end x))
(duration (org-chronos-clock-range-duration-minutes x)))
`((start . ,(ts-format start))
(end . ,(ts-format end))
(duration . ,(ceiling duration)))))
;;;; Other utility functions
(defun org-chronos--start-of-clocks (range-list)
"Return the first start time of many clock events.
RANGE-LIST should be a list of `org-chronos-clock-range' objects."
(->> (-map #'org-chronos-clock-range-start range-list)
(-sort #'ts<)
(car)))
(defun org-chronos--end-of-clocks (range-list)
"Return the last end time of many clock events.
RANGE-LIST should be a list of `org-chronos-clock-range' objects."
(->> (-map #'org-chronos-clock-range-end range-list)