~pepe/neil

ref: 6476e53ca409e8660ab2579477527f2ba7a0a582 neil/neil/utils.janet -rw-r--r-- 2.2 KiB
6476e53c — Josef Pospíšil Add some actions tooltips 5 months ago
                                                                                
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
# Main utility for the whole application.
(import manisha :prefix "" :export true)
(import bearimy :prefix "" :export true)
(import marble :prefix "" :export true)
(import bearimy/utils :prefix "" :export true)

(import /neil/guides :prefix "" :export true)

(defn fresh
  ```
  Updates timestamp of `what`.
  ```
  [what]
  (merge-into what (stamp)))

(defn stamp-ident
  ```
  Timestamps the instance with `uuid`.
  ```
  [brush uuid]
  (-> (load brush uuid) fresh))

(defn valid?
  ```
  Checks if the `what` is valid new instance.
  ```
  [what data]
  (case what
    :task (new-task-appraiser data)
    :project (new-project-appraiser data)
    :client (new-client-appraiser data)))

(defn sum-intervals
  ```
  Sums all the intervals in `wis`.
  ```
  [wis]
  (reduce
    (fn [t wi]
      (def e (or (wi :end) (os/time)))
      (+ t (- e (wi :start))))
    0 wis))

(defn stamps
  ```
  Creates the table with all the timestamps used in stats.
  ```
  []
  (def [tts ydts tdats wts lwts twats mts lmts tmats yts lyts]
    (map |(:epoch $)
         @[;(map |(days-ago $) (range 3))
           ;(map |(start-of-week (- $)) (range 3))
           ;(map |(start-of-month (- $)) (range 3))
           ;(map |(start-of-year (- $)) (range 2))]))
  @{:today [tts] :yesterday [ydts tts] :two-days-ago [tdats ydts]
    :week [wts] :last-week [lwts wts] :two-weeks-ago [twats lwts]
    :month [mts] :last-month [lmts mts] :two-months-ago [tmats lmts]
    :year [yts] :last-year [lyts yts]})

(defn work-intervals-sums
  ```
  Sums all intervals, optionaly for `project` and `state`, for usage in stats.
  ```
  [brush &opt project state]
  (if (or (not project)
          (present? ((load brush project) :tasks)))
    (map-vals |(-> (list-worked brush project state ;$)
                   sum-intervals
                   format-interval)
              (stamps))))

(defn today-time
  ```
  Computes the time spent working today.
  ```
  [brush]
  (->> (:epoch (today))
       (list-worked brush nil nil)
       sum-intervals))

(defmacro conn-fiber
  ```
  Convenience macro for constructing fiber, handling connection,
  which is also set as `dyn`
  ```
  [handling]
  ~(fiber/new
     (fn [conn] (setdyn :conn conn) (,handling conn)) :tp))