Line data Source code
1 4 : (ns matchscore.utils.cron
2 : (:require
3 : [clojure.string :as str]
4 : [taoensso.telemere :as t]
5 : [tick.core :as tick])
6 : (:import
7 : [java.security MessageDigest]
8 : (java.sql Timestamp)
9 : (java.time Instant)
10 : [java.time Instant]
11 : [java.util Date]
12 : [org.quartz CronExpression]))
13 :
14 :
15 16 : (def TICK-DT-FORMATTER (tick/formatter "yyyy-MM-dd'T'HH:mm:ssXXX"))
16 :
17 :
18 4 : (defn sha256
19 : "Generates a SHA-256 hash from a string."
20 : [s]
21 10 : (let [digest (.digest (MessageDigest/getInstance "SHA-256") (.getBytes (str s) "UTF-8"))]
22 2 : (str/join
23 2 : (map
24 1 : (fn [b]
25 6 : (format "%02x" (bit-and b 0xff)))
26 1 : digest))))
27 :
28 :
29 4 : (defn hash-datetime-pair
30 : "Generates a SHA-256 hash from a pair of datetimes."
31 : [[dt-start dt-end]]
32 7 : (let [start-str (str (tick/format TICK-DT-FORMATTER dt-start))
33 6 : end-str (str (tick/format TICK-DT-FORMATTER dt-end))
34 5 : daterange-str (str start-str "-" end-str)]
35 3 : (sha256 daterange-str)))
36 :
37 :
38 4 : (defn cron-to-datetimes
39 : "Expands a cron expression into a list of datetimes between start and end times, localized to UTC."
40 : [cron-expr
41 : start-datetime
42 : end-datetime
43 : &
44 : {:keys [max-count zone]
45 : :or {zone "Europe/Kyiv"}}]
46 3 : (let [cron (CronExpression. cron-expr)
47 3 : start (tick/instant start-datetime)
48 3 : end (tick/instant end-datetime)
49 3 : input-zone (tick/zone zone)
50 3 : utc-zone (tick/zone "UTC")]
51 3 : (loop [current-time start
52 1 : result []
53 : count 0]
54 16 : (if (or (tick/>= current-time end) (and max-count (>= count max-count)))
55 1 : result
56 5 : (let [next-time (.getNextValidTimeAfter cron (Date/from current-time))
57 5 : next-instant (if next-time (tick/instant (.toInstant next-time)) nil)]
58 9 : (if (and next-instant (tick/<= next-instant end))
59 1 : (recur
60 : next-instant
61 : (conj result (tick/in (tick/in next-instant input-zone) utc-zone))
62 : (inc count))
63 1 : result))))))
64 :
65 :
66 4 : (defn cron-pairs
67 : "Returns vector of paired datetime ranges from start and end cron expressions."
68 : [cron-start-expr
69 : cron-end-expr
70 : start-time
71 : end-time
72 : &
73 : {:keys [max-count zone with-hash]
74 : :or {zone "Europe/Kyiv"
75 : with-hash false}}]
76 1 : (let [dt-starts
77 9 : (cron-to-datetimes cron-start-expr start-time end-time :max-count max-count :zone zone)
78 9 : dt-ends (cron-to-datetimes cron-end-expr start-time end-time :max-count max-count :zone zone)
79 5 : pairs (mapv vector dt-starts dt-ends)]
80 2 : (if with-hash
81 2 : (mapv
82 1 : (fn [pair]
83 6 : (conj pair (hash-datetime-pair pair)))
84 1 : pairs)
85 1 : pairs)))
86 :
87 :
88 : ;; Transform schedule data to cron expressions
89 4 : (defn transform-to-cron
90 : [input-map]
91 2 : (let [{:keys [start-hour end-hour day-of-the-week]} input-map
92 6 : cron-start (str "0 0 " start-hour " ? * " day-of-the-week)
93 6 : cron-end (str "0 0 " end-hour " ? * " day-of-the-week)
94 4 : now (Timestamp/from (Instant/now))
95 5 : end-of-2050 (Timestamp/from (Instant/parse "2050-12-31T23:59:59Z"))]
96 5 : (->
97 1 : input-map
98 2 : (assoc :cron-start cron-start
99 1 : :cron-end cron-end
100 1 : :cron-start-datetime now
101 1 : :cron-end-datetime end-of-2050))))
102 :
103 :
104 4 : (comment
105 : (let [cron-start-expr "0 0 10 ? * mon"
106 : cron-end-expr "0 0 12 ? * mon"
107 : ;start-time (tick/instant (tick/at (tick/new-date 2025 10 20)
108 : ;(tick/new-time 0 0)))
109 : ;end-time (tick/instant (tick/at (tick/new-date 2025 11 10)
110 : ;(tick/new-time 0 0)))
111 : start-time (tick/instant (tick/at (tick/new-date 2025 8 20) (tick/new-time 0 0)))
112 : end-time (tick/instant (tick/at (tick/new-date 2025 10 10) (tick/new-time 0 0)))
113 : pairs
114 : (cron-pairs cron-start-expr cron-end-expr start-time end-time :max-count 5 :with-hash true)]
115 : (doseq [pair pairs]
116 : (when (= (count pair) 3)
117 : (let [[dt-start dt-end hash] pair] (t/log! :info (str dt-start dt-end "Hash:" hash)))))))
|