9
9
#include < cstdint>
10
10
#include < utility>
11
11
12
+ #include " event_batch/types.hpp"
12
13
#include " event_batch/utils.hpp"
13
14
14
15
namespace event_batch
15
16
{
16
- /* *
17
- * @brief Event decay structure.
18
- */
19
- struct __attribute__ ((__packed__)) Decay
20
- {
21
- /* *
22
- * @brief Previous timestamp \f$[\text{microseconds}]\f$.
23
- */
24
- uint64_t t;
25
- /* *
26
- * @brief Event decay in \f$[0,1]\f$.
27
- */
28
- float decay;
29
- /* *
30
- * @brief Auxiliary variable that counts the incoming number of events.
31
- */
32
- float n_decay;
33
- /* *
34
- * @brief Auxiliary variable that estimates the event time decay
35
- * \f$[\text{microseconds}]\f$.
36
- */
37
- float t_decay;
38
- /* *
39
- * @brief Estimated event rate \f$[\text{events}/\text{microseconds}]\f$.
40
- */
41
- float rate;
42
-
43
- /* *
44
- * @brief Estimates the event decay.
45
- *
46
- * This method estimates the decay by estimating the event stream activity.
47
- *
48
- * @param t_cur Current timestamp \f$[\text{microseconds}]\f$.
49
- */
50
- void
51
- operator ()(const uint64_t t_cur)
52
- {
53
- decay = static_cast <float >(1 );
54
- const float t_diff = (t_cur > t) ? static_cast <float >(t_cur - t) : 0 ;
55
- if (t_diff > 0 )
56
- {
57
- decay /=
58
- static_cast <float >(1e-6 ) * t_diff * n_decay + static_cast <float >(1 );
59
-
60
- n_decay *= decay;
61
- t_decay = decay * t_decay + t_diff;
62
-
63
- t = t_cur;
64
- }
65
- ++n_decay;
66
-
67
- rate = n_decay / t_decay;
68
- }
69
-
70
- /* *
71
- * @brief Resets the context.
72
- *
73
- * @param t_decay_first Initial time rate assumption to bootstrap the rate
74
- * estimator \f$[\text{microseconds}]\f$.
75
- */
76
- void
77
- reset (const uint64_t t_decay_first)
78
- {
79
- t = 0 ;
80
- decay = 1 ;
81
- n_decay = 0 ;
82
- t_decay = t_decay_first;
83
- rate = 0 ;
84
- }
85
- };
86
-
87
17
/* *
88
18
* @brief Global decay estimator.
89
19
*
@@ -138,6 +68,62 @@ class GlobalDecay
138
68
*/
139
69
~GlobalDecay () = default ;
140
70
71
+ /* *
72
+ * @brief Returns the current timestamp \f$[\text{microseconds}]\f$.
73
+ *
74
+ * @return Current timestamp \f$[\text{microseconds}]\f$.
75
+ */
76
+ uint64_t
77
+ t () const
78
+ {
79
+ return decay_.t ;
80
+ }
81
+
82
+ /* *
83
+ * @brief Returns the current decay.
84
+ *
85
+ * @return Current decay.
86
+ */
87
+ float
88
+ decay () const
89
+ {
90
+ return decay_.decay ;
91
+ }
92
+
93
+ /* *
94
+ * @brief Returns the count of the incoming number of events.
95
+ *
96
+ * @return Count of the incoming number of events.
97
+ */
98
+ float
99
+ n_decay () const
100
+ {
101
+ return decay_.n_decay ;
102
+ }
103
+
104
+ /* *
105
+ * @brief Returns the event time decay \f$[\text{microseconds}]\f$.
106
+ *
107
+ * @return Event time decay \f$[\text{microseconds}]\f$.
108
+ */
109
+ float
110
+ t_decay () const
111
+ {
112
+ return decay_.t_decay ;
113
+ }
114
+
115
+ /* *
116
+ * @brief Returns the current event rate
117
+ * \f$[\text{events}/\text{microseconds}]\f$.
118
+ *
119
+ * @return Current event rate \f$[\text{events}/\text{microseconds}]\f$.
120
+ */
121
+ float
122
+ rate () const
123
+ {
124
+ return decay_.rate ;
125
+ }
126
+
141
127
/* *
142
128
* @brief Estimates the global decay one event at a time.
143
129
*
@@ -149,7 +135,23 @@ class GlobalDecay
149
135
void
150
136
operator ()(Event event)
151
137
{
152
- decay_ (event.t );
138
+ decay_.decay = static_cast <float >(1 );
139
+ const float t_diff =
140
+ (event.t > decay_.t ) ? static_cast <float >(event.t - decay_.t ) : 0 ;
141
+ if (t_diff > 0 )
142
+ {
143
+ decay_.decay /= static_cast <float >(1e-6 ) * t_diff * decay_.n_decay +
144
+ static_cast <float >(1 );
145
+
146
+ decay_.n_decay *= decay_.decay ;
147
+ decay_.t_decay = decay_.decay * decay_.t_decay + t_diff;
148
+
149
+ decay_.t = event.t ;
150
+ }
151
+ ++decay_.n_decay ;
152
+
153
+ decay_.rate = decay_.n_decay / decay_.t_decay ;
154
+
153
155
handle_decay_ (event_to_decay_ (event, decay_.decay , decay_.n_decay ,
154
156
decay_.t_decay , decay_.rate ));
155
157
}
0 commit comments