| // 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 <chrono> |
| |
| #include "src/lib/util/datetime_util.h" |
| #include "src/local_aggregation_1.1/local_aggregation.pb.h" |
| #include "src/registry/metric_definition.pb.h" |
| |
| namespace cobalt::local_aggregation { |
| |
| using TimeInfo = util::TimeInfo; |
| |
| BackfillManager::BackfillManager(uint32_t total_backfill_window) |
| : backstop_days_(total_backfill_window > 0 ? total_backfill_window - 1 : 0) {} |
| |
| std::vector<TimeInfo> BackfillManager::CalculateBackfill(TimeInfo start_time, TimeInfo end_time, |
| MetricDefinition::TimeZonePolicy time_zone, |
| bool is_daily) const { |
| uint32_t backstop_day = end_time.day_index - backstop_days_; |
| if (end_time.day_index < backstop_days_) { |
| backstop_day = 0; |
| } |
| uint32_t backstop_hour = util::DayIndexToHourId(backstop_day); |
| |
| std::vector<TimeInfo> backfill; |
| if (is_daily) { |
| uint32_t start_day_index = std::max(start_time.day_index + 1, backstop_day); |
| |
| for (uint32_t day_index = start_day_index; day_index <= end_time.day_index; day_index++) { |
| backfill.push_back(TimeInfo::FromDayIndex(day_index)); |
| } |
| } else { |
| uint32_t start_hour_id = std::max(start_time.hour_id + 1, backstop_hour); |
| int64_t start_epoch = util::DayIndexToUnixSeconds(backstop_day); |
| int64_t end_epoch = util::DayIndexToUnixSeconds(end_time.day_index + 1); |
| for (int64_t epoch = start_epoch; epoch < end_epoch; epoch += util::kNumUnixSecondsPerHour) { |
| uint32_t current_hour_id = util::TimeToHourId( |
| std::chrono::system_clock::to_time_t(util::FromUnixSeconds(epoch)), time_zone); |
| |
| if (current_hour_id >= start_hour_id && current_hour_id <= end_time.hour_id) { |
| backfill.push_back(TimeInfo::FromHourId(current_hour_id)); |
| } |
| } |
| } |
| |
| return backfill; |
| } |
| |
| } // namespace cobalt::local_aggregation |