Skip to content

Commit 39c5370

Browse files
author
umgnunes
committed
Re-organize decay structure
1 parent 7f96766 commit 39c5370

File tree

2 files changed

+118
-72
lines changed

2 files changed

+118
-72
lines changed

include/event_batch/global_decay.hpp

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,11 @@
99
#include <cstdint>
1010
#include <utility>
1111

12+
#include "event_batch/types.hpp"
1213
#include "event_batch/utils.hpp"
1314

1415
namespace event_batch
1516
{
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-
8717
/**
8818
* @brief Global decay estimator.
8919
*
@@ -138,6 +68,62 @@ class GlobalDecay
13868
*/
13969
~GlobalDecay() = default;
14070

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+
141127
/**
142128
* @brief Estimates the global decay one event at a time.
143129
*
@@ -149,7 +135,23 @@ class GlobalDecay
149135
void
150136
operator()(Event event)
151137
{
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+
153155
handle_decay_(event_to_decay_(event, decay_.decay, decay_.n_decay,
154156
decay_.t_decay, decay_.rate));
155157
}

include/event_batch/types.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,50 @@ struct __attribute__((__packed__)) Event
4242
*/
4343
uint16_t p;
4444
};
45+
46+
/**
47+
* @brief Event decay structure.
48+
*/
49+
struct Decay
50+
{
51+
/**
52+
* @brief Previous timestamp \f$[\text{microseconds}]\f$.
53+
*/
54+
uint64_t t;
55+
/**
56+
* @brief Event decay in \f$[0,1]\f$.
57+
*/
58+
float decay;
59+
/**
60+
* @brief Auxiliary variable that counts the incoming number of events.
61+
*/
62+
float n_decay;
63+
/**
64+
* @brief Auxiliary variable that estimates the event time decay
65+
* \f$[\text{microseconds}]\f$.
66+
*/
67+
float t_decay;
68+
/**
69+
* @brief Estimated event rate \f$[\text{events}/\text{microseconds}]\f$.
70+
*/
71+
float rate;
72+
73+
/**
74+
* @brief Resets the context.
75+
*
76+
* @param t_decay_first Initial time rate assumption to bootstrap the rate
77+
* estimator \f$[\text{microseconds}]\f$.
78+
*/
79+
void
80+
reset(const uint64_t t_decay_first)
81+
{
82+
t = 0;
83+
decay = 1;
84+
n_decay = 0;
85+
t_decay = t_decay_first;
86+
rate = 0;
87+
}
88+
};
4589
} // namespace event_batch
4690

4791
#endif // EVENT_BATCH_TYPES_HPP

0 commit comments

Comments
 (0)