Skip to content

Commit 8327765

Browse files
authored
Move timestamp in data points to the end of the time interval (#128)
* Move timestamp to the end of the time interval Signed-off-by: RaulSanchez <raul@eprosima.com> * Fix get_data tests Signed-off-by: RaulSanchez <raul@eprosima.com>
1 parent b2a3618 commit 8327765

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/cpp/detail/data_aggregation.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct IDataAggregator
7979
for (auto& it = *iterators.first; it != *iterators.second; ++it)
8080
{
8181
// Find and check the bin corresponding to the sample timestamp
82-
Timestamp ts = it.get_timestamp();
82+
Timestamp ts = it.get_timestamp() + interval_;
8383
auto index = (ts - data_[0].first) / interval_;
8484
assert((index >= 0) && static_cast<size_t>(index) < data_.size());
8585

@@ -152,8 +152,8 @@ struct IDataAggregator
152152
// Fill each bin with the initial value and the initial timestamp of the bin
153153
do
154154
{
155-
data_.emplace_back(t_from, initial_value);
156155
t_from += interval_;
156+
data_.emplace_back(t_from, initial_value);
157157
bins--;
158158
} while (bins > 0);
159159
}

test/unittest/StatisticsBackend/GetDataTests.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ TEST_P(get_data_no_data_tests, no_data)
131131

132132
std::vector<StatisticsData> result;
133133

134+
// The number of bins is also used for the time interval so the time interval correspoding to each data point is
135+
// exactly 1 second.
136+
uint16_t bins = 10;
137+
134138
if (entity2.is_valid())
135139
{
136140
result = StatisticsBackend::get_data(
@@ -148,12 +152,12 @@ TEST_P(get_data_no_data_tests, no_data)
148152
data_type,
149153
std::vector<EntityId>(1, entity1),
150154
std::vector<EntityId>(1, entity2),
151-
10,
155+
bins,
152156
Timestamp(),
153-
Timestamp() + std::chrono::seconds(10),
157+
Timestamp() + std::chrono::seconds(bins),
154158
statistic);
155159

156-
ASSERT_EQ(10, result.size());
160+
ASSERT_EQ(bins, result.size());
157161
}
158162
else
159163
{
@@ -170,18 +174,21 @@ TEST_P(get_data_no_data_tests, no_data)
170174
result = StatisticsBackend::get_data(
171175
data_type,
172176
std::vector<EntityId>(1, entity1),
173-
10,
177+
bins,
174178
Timestamp(),
175-
Timestamp() + std::chrono::seconds(10),
179+
Timestamp() + std::chrono::seconds(bins),
176180
statistic);
177181

178-
ASSERT_EQ(10, result.size());
182+
ASSERT_EQ(bins, result.size());
179183
}
180184

181185
for (size_t i = 0; i < result.size(); ++i)
182186
{
183187
ASSERT_TRUE(std::isnan(result[i].second));
184-
ASSERT_EQ(Timestamp() + std::chrono::seconds(i), result[i].first);
188+
// The timestamp of each datapoint is the timestamp correspoding to the end of the time interval. For this
189+
// tests the time interval is configure to be 1 second as the number of bins is equal to the time interval in
190+
// seconds.
191+
ASSERT_EQ(Timestamp() + std::chrono::seconds(1) + std::chrono::seconds(i), result[i].first);
185192
}
186193
}
187194

@@ -481,7 +488,7 @@ void fill_expected_result (
481488
for (uint16_t i = 0; i < nbins; ++i)
482489
{
483490
StatisticsData data;
484-
data.first = start + (bin_size * i);
491+
data.first = start + bin_size + (bin_size * i);
485492
data.second = count ? 0 : std::numeric_limits<double>::quiet_NaN();
486493
expected.push_back(data);
487494
}

0 commit comments

Comments
 (0)