This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
/
aggregation.cljc
154 lines (115 loc) · 4.94 KB
/
aggregation.cljc
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
(ns onyx.windowing.aggregation
(:refer-clojure :exclude [min max count conj]))
(defn set-value-aggregation-apply-log [window state v]
;; Log command is not needed for single transition type
v)
(defn conj-aggregation-apply-log [window state v]
;; Log command is not needed for single transition type
(clojure.core/conj state v))
(defn conj-aggregation-fn-init [window]
[])
(defn conj-aggregation-fn [window segment]
;; Log command is not needed for single transition type
segment)
(defn conj-super-aggregation [window state-1 state-2]
(into state-1 state-2))
(def ^:export conj
{:aggregation/init conj-aggregation-fn-init
:aggregation/create-state-update conj-aggregation-fn
:aggregation/apply-state-update conj-aggregation-apply-log
:aggregation/super-aggregation-fn conj-super-aggregation})
(defn collect-by-key-aggregation-fn-init [window]
{})
(defn collect-by-key-aggregation-apply-log [window state segment]
(let [k (get segment (second (:window/aggregation window)))]
(update state k (fnil clojure.core/conj #{}) segment)))
(defn collect-by-key-aggregation-fn [window segment]
segment)
(defn collect-by-key-super-aggregation [window state-1 state-2]
(merge state-1 state-2))
(def ^:export collect-by-key
{:aggregation/init collect-by-key-aggregation-fn-init
:aggregation/create-state-update collect-by-key-aggregation-fn
:aggregation/apply-state-update collect-by-key-aggregation-apply-log
:aggregation/super-aggregation-fn collect-by-key-super-aggregation})
(defn collect-key-value-aggregation-apply-log [window state v]
(clojure.core/conj state v))
(defn collect-key-value-aggregation-fn-init [window]
[])
(defn collect-key-value-aggregation-fn [window segment]
(get segment (second (:window/aggregation window))))
(defn collect-key-value-super-aggregation [window state-1 state-2]
(merge state-1 state-2))
(def ^:export collect-key-value
{:aggregation/init collect-key-value-aggregation-fn-init
:aggregation/create-state-update collect-key-value-aggregation-fn
:aggregation/apply-state-update collect-key-value-aggregation-apply-log
:aggregation/super-aggregation-fn collect-key-value-super-aggregation})
(defn sum-aggregation-fn-init [window]
0)
(defn sum-create-state-update-fn [window segment]
(let [k (second (:window/aggregation window))]
(get segment k)))
(defn sum-aggregation-function [_ state v]
(+ state v))
(defn sum-super-aggregation [window state-1 state-2]
(+ state-1 state-2))
(def ^:export sum
{:aggregation/init sum-aggregation-fn-init
:aggregation/create-state-update sum-create-state-update-fn
:aggregation/apply-state-update sum-aggregation-function
:aggregation/super-aggregation-fn sum-super-aggregation})
(defn count-aggregation-fn-init [window]
0)
(defn count-aggregation-apply-fn [window state _]
(inc state))
(defn count-aggregation-create-state-update [_ _])
(defn count-super-aggregation [window state-1 state-2]
(+ state-1 state-2))
(def ^:export count
{:aggregation/init count-aggregation-fn-init
:aggregation/create-state-update count-aggregation-create-state-update
:aggregation/apply-state-update count-aggregation-apply-fn
:aggregation/super-aggregation-fn count-super-aggregation})
(defn min-aggregation-create-fn [window segment]
(let [k (second (:window/aggregation window))]
(get segment k)))
(defn min-aggregation-apply-fn [window state v]
(clojure.core/min state v))
(defn min-super-aggregation [window state-1 state-2]
(clojure.core/min state-1 state-2))
(def ^:export min
{:aggregation/create-state-update min-aggregation-create-fn
:aggregation/apply-state-update min-aggregation-apply-fn
:aggregation/super-aggregation-fn min-super-aggregation})
(defn max-aggregation-fn [window segment]
(let [k (second (:window/aggregation window))]
(get segment k)))
(defn max-create-aggregation-apply-fn [window state v]
(clojure.core/max state v))
(defn max-super-aggregation [window state-1 state-2]
(clojure.core/max state-1 state-2))
(def ^:export max
{:aggregation/create-state-update max-aggregation-fn
:aggregation/apply-state-update max-create-aggregation-apply-fn
:aggregation/super-aggregation-fn max-super-aggregation})
(defn average-aggregation-fn-init [window]
{:sum 0 :n 0})
(defn average-aggregation-apply-fn [window state v]
(let [sum (+ (:sum state) v)
n (inc (:n state))]
{:n n :sum sum :average (/ sum n)}))
(defn average-aggregation-create-fn [window segment]
(let [k (second (:window/aggregation window))]
(get segment k)))
(defn average-super-aggregation [window state-1 state-2]
(let [n* (+ (:n state-1) (:n state-2))
sum* (+ (:sum state-1) (:sum state-2))]
{:n n*
:sum sum*
:average (/ sum* n*)}))
(def ^:export average
{:aggregation/init average-aggregation-fn-init
:aggregation/create-state-update average-aggregation-create-fn
:aggregation/apply-state-update average-aggregation-apply-fn
:aggregation/super-aggregation-fn average-super-aggregation})