~pepe/neil

aa575d0655aaae631d3effd1f1a4c01e79786b3a — Josef Pospíšil 2 months ago d6e4cbb
Refactor loop-interval-tasks
2 files changed, 22 insertions(+), 33 deletions(-)

M neil/watch/stats/init.janet
M neil/watch/stats/utils.janet
M neil/watch/stats/init.janet => neil/watch/stats/init.janet +12 -31
@@ 16,47 16,28 @@
(defn interval [brush]
  (fn [{:params {:interval i :project project :state state}}]
    (def [projects clients] (retrieve brush [:clients (collect) (in-all :projects) merged]))
    (def tdy (today))
    (capout
      (case i
        "today"
        (utils/loop-tasks (worked-tasks brush project state (:epoch (today)))
                          (>= (w :start) (:epoch (today)))
                          clients projects)
        (utils/loop-interval-tasks (:epoch tdy))
        "yesterday"
        (utils/loop-tasks (worked-tasks brush project state (:epoch (yesterday)) (:epoch (today)))
                          (and
                            (>= (w :start) (:epoch (yesterday)))
                            (<= (w :end) (:epoch (today))))
                          clients projects)
        (utils/loop-interval-tasks (:epoch (yesterday)) (:epoch tdy))
        "current-week"
        (utils/loop-tasks (worked-tasks brush project state (:epoch (current-week-start)))
                          (>= (w :start) (:epoch (current-week-start)))
                          clients projects)
        (utils/loop-interval-tasks (:epoch (current-week-start)))
        "last-week"
        (utils/loop-tasks (worked-tasks brush project state (:epoch (last-week-start)) (:epoch (current-week-start)))
                          (and
                            (>= (w :start) (:epoch (last-week-start)))
                            (<= (w :end) (:epoch (current-week-start))))
                          clients projects)
        (utils/loop-interval-tasks (:epoch (last-week-start))
                                   (:epoch (current-week-start)))
        "current-month"
        (utils/loop-tasks (worked-tasks brush project state (:epoch (current-month-start)))
                          (>= (w :start) (:epoch (current-month-start)))
                          clients projects)
        (utils/loop-interval-tasks (:epoch (current-month-start)))
        "last-month"
        (utils/loop-tasks (worked-tasks brush project state (:epoch (last-month-start)) (:epoch (current-month-start)))
                          (and
                            (>= (w :start) (:epoch (last-month-start)))
                            (<= (w :end) (:epoch (current-month-start))))
                          clients projects)
        (utils/loop-interval-tasks (:epoch (last-month-start))
                                   (:epoch (current-month-start)))
        "current-year"
        (utils/loop-tasks (worked-tasks brush project state (:epoch (current-year-start)))
                          (>= (w :start) (:epoch (current-year-start)))
                          clients projects)
        (utils/loop-interval-tasks (:epoch (current-year-start)))
        "last-year"
        (utils/loop-tasks (worked-tasks brush project state (:epoch (start-of-year 1)) (:epoch (current-year-start)))
                          (and (>= (w :start) (:epoch (start-of-year 1)))
                               (<= (w :end) (:epoch (current-year-start))))
                          clients projects)))))
        (utils/loop-interval-tasks (:epoch (start-of-year 1))
                                   (:epoch (current-year-start)))))))

(defn filter [brush]
  (fn [{:body body}]

M neil/watch/stats/utils.janet => neil/watch/stats/utils.janet +10 -2
@@ 18,10 18,18 @@
          :intervals-count cnt
          :one-go (and (= (length wi) 1) (= st "completed"))))))

(defmacro loop-interval-tasks [& epochs]
  (def redseqcond (case (length epochs)
                    1 ~(>= (w :start) ,(first epochs))
                    2 ~(and
                          (>= (w :start) ,(first epochs))
                          (<= (w :end) ,(last epochs)))))
  ~(utils/loop-tasks (worked-tasks brush project state ,;epochs)
                     ,redseqcond
                     clients projects))

(defmacro loop-tasks-csv [coll redseqcond]
  ~(loop [{:uuid i :name n
           :work-intervals wi} :in ,coll
          :let [iwi (seq [w :in wi :when ,redseqcond] w)]]
     (,csv-line/render :name n :work-intervals iwi)))