| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "src/local_aggregation_1_1/backfill_manager.h" |
| |
| #include <memory> |
| |
| #include <gtest/gtest.h> |
| |
| #include "src/lib/util/datetime_util.h" |
| #include "src/local_aggregation_1_1/aggregation_procedures/aggregation_procedure.h" |
| |
| namespace cobalt::local_aggregation { |
| |
| using TimeInfo = util::TimeInfo; |
| |
| std::vector<uint32_t> ToHourIds(std::vector<TimeInfo> times) { |
| std::vector<uint32_t> output; |
| std::transform(times.begin(), times.end(), std::back_inserter(output), |
| [](TimeInfo time) { return time.hour_id; }); |
| return output; |
| } |
| |
| std::vector<uint32_t> ToDayIndices(std::vector<TimeInfo> times) { |
| std::vector<uint32_t> output; |
| std::transform(times.begin(), times.end(), std::back_inserter(output), |
| [](TimeInfo time) { return time.day_index; }); |
| return output; |
| } |
| |
| TEST(BackfillManager, HourlyBackfill) { |
| BackfillManager manager(2); |
| |
| const uint32_t kStartHourId = 10001; |
| const uint32_t kNumHoursToGenerate = 5; |
| |
| std::vector<uint32_t> hour_sequence = ToHourIds( |
| manager.CalculateBackfill(TimeInfo::FromHourId(kStartHourId), |
| TimeInfo::FromHourId(kStartHourId + (kNumHoursToGenerate * 2)), |
| MetricDefinition_TimeZonePolicy_UTC, false)); |
| |
| ASSERT_EQ(hour_sequence, std::vector<uint32_t>({10003, 10005, 10007, 10009, 10011})); |
| } |
| |
| TEST(BackfillManager, HourlyBackstopDayAligned) { |
| BackfillManager manager(1); |
| const uint32_t kStartDayIndex = 100; |
| const uint32_t kStartHourId = (kStartDayIndex * util::kHourIdsPerDay) + 1; |
| // Last hour ID of the day |
| const uint32_t kEndHourId = kStartHourId + (util::kHourIdsPerDay * 2) - 2; |
| |
| std::vector<uint32_t> hour_sequence = ToHourIds(manager.CalculateBackfill( |
| TimeInfo::FromHourId(kStartHourId), TimeInfo::FromHourId(kEndHourId), |
| MetricDefinition_TimeZonePolicy_UTC, false)); |
| |
| ASSERT_EQ(hour_sequence.size(), 24); |
| ASSERT_EQ(*hour_sequence.rbegin(), kEndHourId); |
| } |
| |
| TEST(BackfillManager, HourlyBackstopDayUnaligned) { |
| BackfillManager manager(1); |
| const uint32_t kStartDayIndex = 100; |
| const uint32_t kStartHourId = (kStartDayIndex * util::kHourIdsPerDay) + 1; |
| // 4 Hours before the end of the day |
| const uint32_t kEndHourId = kStartHourId + (util::kHourIdsPerDay * 2) - 10; |
| |
| std::vector<uint32_t> hour_sequence = ToHourIds(manager.CalculateBackfill( |
| TimeInfo::FromHourId(kStartHourId), TimeInfo::FromHourId(kEndHourId), |
| MetricDefinition_TimeZonePolicy_UTC, false)); |
| |
| ASSERT_EQ(*hour_sequence.rbegin(), kEndHourId); |
| ASSERT_EQ(hour_sequence.size(), 20); |
| } |
| |
| TEST(BackfillManager, DailyBackfill) { |
| BackfillManager manager(5); |
| |
| const uint32_t kStartDayIndex = 10001; |
| const uint32_t kNumDaysToGenerate = 5; |
| |
| std::vector<uint32_t> day_sequence = ToDayIndices( |
| manager.CalculateBackfill(TimeInfo::FromDayIndex(kStartDayIndex), |
| TimeInfo::FromDayIndex(kStartDayIndex + kNumDaysToGenerate), |
| MetricDefinition_TimeZonePolicy_UTC, true)); |
| |
| ASSERT_EQ(day_sequence, std::vector<uint32_t>({10002, 10003, 10004, 10005, 10006})); |
| } |
| |
| TEST(BackfillManager, DailyBackstop) { |
| BackfillManager manager(10); |
| |
| const uint32_t kStartDayIndex = 100; |
| const uint32_t kEndDayIndex = 120; |
| |
| std::vector<uint32_t> day_sequence = ToDayIndices(manager.CalculateBackfill( |
| TimeInfo::FromDayIndex(kStartDayIndex), TimeInfo::FromDayIndex(kEndDayIndex), |
| MetricDefinition_TimeZonePolicy_UTC, true)); |
| |
| ASSERT_EQ(day_sequence.size(), 10); |
| ASSERT_EQ(*day_sequence.rbegin(), kEndDayIndex); |
| } |
| |
| } // namespace cobalt::local_aggregation |