-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimeline.cpp
More file actions
280 lines (230 loc) · 8.75 KB
/
Copy pathtimeline.cpp
File metadata and controls
280 lines (230 loc) · 8.75 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "db/timeline.hpp"
#include <exception>
#include <fstream>
#include <functional>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
namespace henhouse::db
{
const offset_type ADD_BUCKET_BACK_LIMIT = 60;
/**
* This is the main function to compute the partial sums given previous bucket.
* It turns the current non-summed bucket into a summed bucket.
*
* current.value is assumed to be set to the count in that bucket.
*
* It computes partial sum(X) and partial sum(X^2) up the current bucket.
*/
void propogate(data_item prev, data_item& current)
{
const auto v = current.value;
current.integral = prev.integral + v;
current.second_integral = prev.second_integral + (v * v);
}
//Adds a count c to the current bucket and updates the partial sum
//values of the current bucket.
void update_current(data_item prev, data_item& current, count_type c)
{
current.value += c;
propogate(prev, current);
}
/**
*
* Mean is computing as the (running sum of x) / N.
* In addition, variance requires maintaining the running sum of x^2
*
* mean = sum(x) / N
* mean of squared x = sum(x^2) / N
*
* variance = (sum(x^2) / N) - (sum(x) / N )^2
* = (sum(x^2) / N) - mean^2
* = (mean of squared x) - mean^2
* = (mean of squared x) - (mean squared)
*/
diff_result diff_buckets(
const time_type ta, //time from
const time_type tb, //time to
const time_type resolution, //resolution of time buckets
const offset_type index_offset,
const data_item a, //bucket from
const data_item b, //bucket to
const count_type n) //number of buckets
{
REQUIRE_GREATER(resolution, 0);
REQUIRE_GREATER(n, 0);
//Sum here is the values added within
const auto sum = b.integral - a.integral;
const auto second_sum = b.second_integral - a.second_integral;
const auto mean = static_cast<mean_type>(sum) / n;
const auto mean_squared = mean * mean;
const auto second_mean = static_cast<mean_type>(second_sum) / n;
const auto variance = second_mean - mean_squared;
return diff_result
{
ta,
tb,
resolution,
index_offset,
sum,
mean,
variance,
n,
a,
b,
};
}
bool timeline::put(time_type t, count_type c)
{
//We already have data, let's add and index new point.
if(index.size() > 0)
{
const auto last_range = index.cend() - 1;
//don't add if time is before last range
if(t >= last_range->time)
{
//get last position only because we want to keep
//a specific performance profile. This is a deliberate limitation.
auto p = index.find_pos_from_range(t, last_range, index.cend());
const auto pos = p.pos + p.offset;
//bucket is current or in the past, no need to index.
if(pos < data.size())
{
//if we are too far back in the range, skip it,
//otherwise propogate the values up.
//This limitation is to keep performance predictable for
//inserts while providing a buffer for slow inserters
//to catch up.
if(data.size() - pos < ADD_BUCKET_BACK_LIMIT)
{
const auto prev = pos > 0 ? data[pos - 1] : data_item{0, 0, 0};
update_current(prev, data[pos], c);
for(auto p = pos + 1; p < data.size(); p++)
propogate(data[p-1], data[p]);
}
else return false;
}
//if we move beyond end, append data
else
{
const auto last_pos = data.size() - 1;
const auto prev = data[last_pos];
//don't compute integral and second_integral
//because propogate will overwrite
data_item current{c, 0, 0};
propogate(prev, current);
data.push_back(current);
//skip if we have no gaps, otherwise index.
auto new_pos = last_pos + 1;
if(pos == new_pos) return true;
//index position
const auto resolution = index.meta().resolution;
CHECK_GREATER(resolution, 0);
const auto aliased_time = p.time + (p.offset * resolution);
index_item index_entry = {aliased_time, new_pos};
CHECK_LESS_EQUAL(aliased_time, t);
index.push_back(index_entry);
}
}
else return false;
}
//We have an empty timeline, let's add initial data point and index it.
else
{
CHECK_EQUAL(data.size(), 0);
data_item v{c, c, c * c};
data.push_back(v);
index_item i = {t, 0};
index.push_back(i);
}
return true;
}
summary_result timeline::summary() const
{
const auto resolution = index.meta().resolution;
CHECK_GREATER(resolution, 0);
if(index.empty()) return summary_result{0,0,resolution, 0,0,0,0};
REQUIRE(!data.empty());
const auto front = index.front();
const auto back = index.back();
//time of first bucket
const auto from = front.time;
//compute time of last bucket
CHECK_GREATER(data.size(), back.pos);
auto last_buckets = data.size() - back.pos;
auto to = back.time + (last_buckets * resolution);
CHECK_GREATER(to, from);
count_type n = (to - from) / resolution;
//if we have one bucket then first is empty data item
auto first_bucket = data_item{0,0,0};
auto last_bucket = data.back();
//diff the two buckets
auto diff = diff_buckets(from, to, resolution, 0, first_bucket, last_bucket, n);
return summary_result
{
from,
to,
resolution,
diff.sum,
diff.mean,
diff.variance,
n
};
}
void clamp(pos_result& r, std::size_t size)
{
REQUIRE_LESS(r.pos, size);
const auto pos = r.pos + r.offset;
if(pos < size) return;
r.offset = size - r.pos - 1;
ENSURE_RANGE(r.pos + r.offset, 0, size);
}
get_result timeline::get(time_type t, const offset_type index_offset) const
{
auto p = index.find_pos(t, index_offset);
clamp(p, data.size());
// zero out data before beginning of collection
const bool before_beginning = t < p.time;
const auto dat = before_beginning ? data_item{0,0,0} : data[p.pos + p.offset];
return get_result
{
p.index_offset,
t,
p.time,
p.pos,
p.offset,
dat
};
}
diff_result timeline::diff(time_type a, time_type b, const offset_type index_offset) const
{
const auto resolution = index.meta().resolution;
CHECK_GREATER(resolution, 0);
if(a > b) std::swap(a,b);
if(data.size() == 0) return diff_result{ a, b, resolution, 0, 0, 0, 0, 0, {0}, {0}};
auto ar = get(a, index_offset);
auto br = get(b, index_offset);
b = std::max(br.query_time, br.range_time);
a = std::min(ar.query_time, b);
const auto time_diff = b - a;
auto n = time_diff / resolution;
if(n == 0) return diff_result{ a, b, resolution, 0, 0, 0, 0, 0, ar.value, br.value};
CHECK_GREATER(n , 0);
CHECK_LESS_EQUAL(ar.index_offset, br.index_offset);
return diff_buckets(a, b, resolution, ar.index_offset, ar.value, br.value, n);
}
timeline from_directory(const std::string& path, const time_type resolution)
{
REQUIRE(!path.empty());
REQUIRE_GREATER(resolution, 0);
fs::create_directory(path);
if(!fs::is_directory(path))
throw std::runtime_error{"path " + path + " is not a directory"};
fs::path root = path;
timeline t;
fs::path idx_data = root / "_.i";
t.index = std::move(index_type{idx_data, resolution});
fs::path cdata = root / "_.d";
t.data = std::move(data_type{cdata, DATA_SIZE});
return t;
}
}