~pepe/neil

ref: 6476e53ca409e8660ab2579477527f2ba7a0a582 neil/neil/guides.janet -rw-r--r-- 8.4 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
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
# Code used for navigating and transforming
# the brush. Plus some utilities.
(use eleanor bearimy/utils marble manisha fzy)

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

(defn sort-by-timestamp
  ```
  Sorts `ds` by the `:timestamp` member.
  ```
  [ds]
  (sort-by (fn [i] (- (or (i :pin) (i :timestamp)))) ds))

(def state-weights
  @{"active" 2 "completed" 1})

(defn sort-by-state-timestamp
  ```
  Sorts `ds` by the `:timestamp` and `state-weights` member.
  ```
  [ds]
  (sort-by (fn [i]
             (- (* (or (i :pin) (i :timestamp))
                   (state-weights (i :state))))) ds))

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

(defn match-n-sort
  ```
  Constructs function that will match and sort its `d` argument.
  against `s` query.
  All the members of `d` must have `:name` and `:uuid` keys if `s`
  is not empty or `:timestamp` or `:order` if it is empty.
  ```
  [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 next-id
  ```
  Gets next id by counter from `brush`.
  ```
  [brush]
  (def nid (inc (:soak brush :counter)))
  (:paint brush nid :counter)
  (. nid))

(defn load
  ```
  Loads instance with `id` from `brush`.
  ```
  [brush id] (:soak-ident brush id))

(defn stamp
  ```
  Constructs table with timestamp of `now`
  under `:timestamp` key.
  ```
  [&opt now]
  (default now (os/time))
  (def t
    (case (type now)
      :number now
      (:epoch now)))
  {:timestamp t})

(defn populate
  ```
  Populates `data` with basic info.
  ```
  [data]
  (def now (os/time))
  (merge
    {:created now :state "active"}
    data (stamp now)))


(defn save
  ```
  Saves `data` of type `what` on optional `path`.
  ```
  [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))

(defmacro retrieve
  ```
  Retrieves by `path` from `brush`.
  ```
  [brush path]
  ~(:soak ,brush (guide ;,path)))

(defn tasks-by
  ```
  Constructs the guide path for all tasks of the `project`.
  ```
  [brush project]
  (def project (load brush project))
  [:clients (project :client)
   :projects (project :uuid)
   :tasks])

(def all-clients
  ```
  Path to all clients.
  ```
  [:clients values])

(def all-projects-table
  ```
  Path to all projects in one table.
  ```
  [;all-clients (>: :projects) merged])

(def all-projects
  ```
  Path to all projects.
  ```
  [;all-clients (>: :projects) flatvals])

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

(defn workeds
  ```
  Constructs string with count and lenght of intervals in `wis`.
  ```
  [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
  ```
  Construct new table from `task` with `projects-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
  ```
  Construsts new table from `task` with computed
  fields from its work intervals.
  ```
  [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
  ```
  View for `task` with project name from `projects`.
  ```
  [task [projects]]
  (task-with-project-name (get-in projects [(task :project) :name]) task))

(defn all-tasks-with-project-name
  ```
  Retrieves all tasks with project name set
  ```
  [brush]
  (retrieve brush [;all-clients (>: :projects)
                   (<- merged) flatvals
                   (>: :tasks) (>Y present?)
                   flatvals sort-by-timestamp
                   (<o> task-with-project-name-view)
                   <x]))

(defn tasks-with-filter
  ```
  Retrieves all the tasks with given `filter` and optional `limt`.
  ```
  [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 last-ten-active-tasks
  ```
  Retrieves last ten active tasks from `brush`.
  ```
  [brush]
  (tasks-with-filter brush (state= "active") 10))

(defn last-ran-task
  ```
  Loads last ran task from `brush`.
  ```
  [brush]
  (load brush (:soak brush :task/last-ran)))

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

(defn projects-ts-sorted
  ```
  Retrieves all projects sorted by `:timestamp`.
  ```
  [brush]
  (retrieve brush [;all-projects (>Y active?) sort-by-timestamp]))

(defn projects-ts-state-sorted
  ```
  Retrieves all projects sorted by `:timestamp` and `:state`.
  ```
  [brush]
  (retrieve brush [;all-projects sort-by-state-timestamp]))

(defn project-with-client-view
  ```
  Contructs new table from `project` with the `:name` containing
  its client name.
  ```
  [project [clients]]
  (merge project
         {:name (. "" (project :name)
                   " $" (get-in clients [(project :client) :abbrev]))}))

(defn projects-with-client
  ```
  Retrieve all projects with their client.
  ```
  [brush]
  (retrieve
    brush
    [:clients (<-) values
     (>: :projects) flatvals (>Y active?)
     (<o> project-with-client-view) <x
     sort-by-timestamp]))

(defn project-tasks-with-filter
  ```
  Retrieves all tasks from `project` filtered by `filtr`.
  ```
  [project filtr]
  ((=> :tasks values (>Y filtr)
       (<o> task-for-project)
       sort-by-timestamp) project))

(defn state-or-not-canceled
  ```
  Contstructs function with `state` predicate
  or not canceled state predicate.
  ```
  [state]
  (if state
    (state= state)
    (fn [s] (not ((state= "canceled") s)))))

(defn projects-or-all
  ```
  Returns all tasks for `project` or all if it is not set
  ```
  [brush project]
  (if project [;(tasks-by brush project) flatten] all-tasks))

(defn worked-tasks
  ```
  Returns all tasks that were worked on for the optional
  `project` and `state`.
  ```
  [brush project state & ts]
  (retrieve
    brush
    [;(projects-or-all brush project)
     (>Y (state-or-not-canceled state))
     (>Y (timestamp-appraiser (ts 0)))
     (>Y present?)
     (>Y (??? {:work-intervals (>?? some (start-appraiser ts))}))]))

(defn list-worked
  ```
  Retrieves list of all worked tasks for optional `projects` and `state`.
  ```
  [brush project state & ts]
  (retrieve
    brush
    [;(projects-or-all brush project)
     (>Y (state-or-not-canceled state))
     (>Y (timestamp-appraiser (ts 0)))
     (>: :work-intervals) flatvals
     (>Y (start-appraiser ts))]))

(defn running-task
  ```
  Retrieves the running task.
  ```
  [brush]
  (-?>> (:soak brush :task/running)
        (load brush)))

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

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