~pepe/neil

ref: a372855fb2a6c85e46f9e3cdebff92895f318135 neil/neil/guides.janet -rw-r--r-- 5.5 KiB
a372855f — Josef Pospíšil Use flatten 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
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
(use eleanor bearimy/utils marble manisha jff/scorer)

(defn name-has-match [q]
  (if (empty? q)
    (fn [&] true)
    (fn [i] (has-match q (. (i :uuid) " " (i :name))))))

(defn match-n-sort [s]
  (if (empty? s)
    (fn n-sort[d]
      (->> d
           (map |[$ (or ($ :timestamp) (- ($ :order)))])
           (sort-by |(- ($ 1)))))
    (fn match-n-sort [d]
      (->>
        (seq [t :in d
              :let [n (. (t :uuid) " " (t :name))
                    sc (score s n)]
              :when (and sc (> sc score-min))]
          [t sc])
        (sort-by |(- ($ 1)))))))

(defn stamp [&opt now]
  (default now (os/time))
  (def t
    (case (type now)
      :number now
      (:epoch now)))
  {:timestamp t})

(defn load [brush id] (:soak-ident brush id))

(defn tasks-by [brush project]
  (def project (load brush project))
  [:clients (project :client)
   :projects (project :uuid)
   :tasks])

(defn next-id [brush]
  (def nid (inc (:soak brush :counter)))
  (:paint brush nid :counter)
  (. nid))

(defn populate [data]
  (def now (os/time))
  (merge
    {:created now
     :state "active"}
    data
    (stamp now)))

(defn save [brush what & path]
  (def w (populate what))
  (unless (w :uuid)
    (put w :uuid (next-id brush))
    (when (and (= (last path) :tasks)
               (not (w :work-intervals)))
      (put w :work-intervals @[])
      (:paint brush (w :uuid) :task/last)))
  (:paint brush w
          ;(array/concat @[] path (w :uuid)))
  (:dry brush)
  (w :uuid))

(defn retrieve [brush path]
  (:soak brush (guide ;path)))

(def all-clients [:clients values])

(def all-projects-table [:clients values (>: :projects) merged])

(def all-projects [;all-clients (>: :projects)
                   flatvals])

(def all-tasks [;all-projects (>: :tasks) (>Y present?) flatvals])

(defn state= [state] (fn _state= [i] (= (i :state) state)))

(def active? (>Y (state= "active")))

(defn sort-by-timestamp [ds]
  (sort-by (fn [i] (- (or (i :pin) (i :timestamp)))) ds))

(defn workeds [wis]
  (def ic (length wis))
  (def ts (os/time))
  (if (pos? ic)
    (. ic "x "
            (format-interval
              (reduce
                (fn [a w]
                  (+ a (- (get w :end ts)
                          (w :start)))) 0 wis)))))

(defn task-with-project-name [project-name task]
  (def wis (task :work-intervals))
  (def ts (task :timestamp))
  (merge task
         {:label (. "#" (task :uuid) " " (task :name))
          :intervals-count wis
          :project-name project-name
          :date-time (format-date-time ts)
          :human-ts (human ts)
          :worked (workeds wis)}))

(defn task-for-project [task _]
  (def wis (task :work-intervals))
  (def ts (task :timestamp))
  (merge task
         {:label (. "#" (task :uuid) " " (task :name))
          :intervals-count wis
          :date-time (format-date-time ts)
          :human-ts (human ts)
          :worked (workeds wis)}))

(defn task-with-project-name-view [task [projects]]
  (task-with-project-name (get-in projects [(task :project) :name]) task))

(defn tasks-with-filter [brush filtr &opt limt]
  (default limt math/inf)
  (retrieve brush [;all-clients (>: :projects)
                   (<- merged) flatvals
                   (>: :tasks) (>Y present?) flatvals
                   (>Y filtr) sort-by-timestamp (>n limt)
                   (<o> task-with-project-name-view)
                   <x]))


(defn clients-ts-sorted [brush]
  (retrieve brush [;all-clients sort-by-timestamp]))

(defn projects-ts-sorted [brush]
  (retrieve brush [;all-projects sort-by-timestamp]))

(defn- project-with-client-view [project [clients]]
  (merge project
         {:name (. "" (project :name)
                   " $" (get-in clients [(project :client) :abbrev]))}))

(defn projects-with-client [brush]
  (retrieve
    brush
    [:clients (<-) values
     (>: :projects) flatvals
     (<o> project-with-client-view) <x
     sort-by-timestamp]))

(defn project-tasks-with-filter [project filtr]
  ((guide :tasks values (>Y filtr)
          (<o> task-for-project)
          sort-by-timestamp) project))

(defn knife [& ts]
  (fn [item]
    (case (length ts)
      1 (>= item ;ts)
      2 (<= (ts 0) item (ts 1)))))

(defn state-or-not-canceled [state]
  (if state
    (state= state)
    (fn [s] (not (= (s :state) "canceled")))))

(defn projects-or-all [brush project]
  (if project [;(tasks-by brush project) flatten] all-tasks))

(defn worked-tasks [brush project state & ts]
  (:soak brush
         (guide ;(projects-or-all brush project)
                (>Y (state-or-not-canceled state))
                (>Y |((knife (ts 0)) ($ :timestamp)))
                (>Y present?)
                (>Y (=> :work-intervals (>: :start)
                        (>?? some (knife ;ts)))))))

(defn list-worked [brush project state & ts]
  (:soak brush
         (guide ;(projects-or-all brush project)
                (>Y (state-or-not-canceled state))
                (>Y |((knife (ts 0)) ($ :timestamp)))
                (>: :work-intervals) (>Y present?)
                flatten (>Y (=> :start (knife ;ts))))))

(defn running-task [brush]
  (-?>> (:soak brush :task/running)
        (load brush)))

(defn active-tasks-match [q]
  [;all-clients (>: :projects)
   (<- merged) flatvals
   (>: :tasks) (>Y present?) flatvals
   active? (>Y (name-has-match q)) (match-n-sort q)
   (limit 20) (>: 0) (<o> task-with-project-name-view)
   <x])

(defn active-projects-match [q]
  [;all-projects active? (>Y (name-has-match q))
   (match-n-sort q) (limit 20) (>: 0)])

(defn in-future? [e] (and e (> e (os/time))))