| // Copyright 2019 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/aggregation_utils.h" |
| |
| #include <optional> |
| |
| #include "src/lib/util/status_builder.h" |
| #include "src/logging.h" |
| |
| namespace cobalt::local_aggregation { |
| |
| OnDeviceAggregationWindow MakeDayWindow(int num_days) { |
| OnDeviceAggregationWindow window; |
| window.set_days(static_cast<AggregationDays>(num_days)); |
| return window; |
| } |
| |
| OnDeviceAggregationWindow MakeHourWindow(int num_hours) { |
| OnDeviceAggregationWindow window; |
| window.set_hours(static_cast<AggregationHours>(num_hours)); |
| return window; |
| } |
| |
| std::tuple<Status, int64_t> GetUpdatedAggregate( |
| ReportDefinition::OnDeviceAggregationType aggregation_type, |
| std::optional<int64_t> stored_aggregate, int64_t new_value) { |
| if (!stored_aggregate.has_value()) { |
| return std::make_tuple(Status::OK, new_value); |
| } |
| |
| switch (aggregation_type) { |
| case ReportDefinition::SUM: |
| return std::make_tuple(Status::OK, new_value + stored_aggregate.value()); |
| case ReportDefinition::MAX: |
| return std::make_tuple(Status::OK, std::max(new_value, stored_aggregate.value())); |
| case ReportDefinition::MIN: |
| return std::make_tuple(Status::OK, std::min(new_value, stored_aggregate.value())); |
| default: |
| return std::make_tuple( |
| util::StatusBuilder(StatusCode::INVALID_ARGUMENT, "Unexpected aggregation type") |
| .WithContexts(aggregation_type) |
| .LogError() |
| .Build(), |
| 0); |
| } |
| } |
| } // namespace cobalt::local_aggregation |