| // 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/aggregation_procedures/sum_and_count_aggregation_procedure.h" |
| |
| #include <tuple> |
| |
| #include "src/lib/statusor/status_macros.h" |
| #include "src/local_aggregation_1.1/aggregation_procedures/aggregation_procedure.h" |
| #include "src/logging.h" |
| #include "src/pb/observation.pb.h" |
| #include "src/registry/packed_event_codes.h" |
| |
| namespace cobalt::local_aggregation { |
| |
| void SumAndCountAggregationProcedure::UpdateAggregateData(const logger::EventRecord& event_record, |
| AggregateData* aggregate_data, |
| EventCodeAggregate* /*aggregate*/) { |
| SumAndCount* sum_and_count = aggregate_data->mutable_sum_and_count(); |
| sum_and_count->set_count(sum_and_count->count() + 1); |
| sum_and_count->set_sum(sum_and_count->sum() + event_record.event()->integer_event().value()); |
| } |
| |
| lib::statusor::StatusOr<std::unique_ptr<Observation>> |
| SumAndCountAggregationProcedure::GenerateHourlyObservation(const logger::Encoder* encoder, |
| EventCodeAggregate* aggregate) { |
| std::vector<std::tuple<std::vector<uint32_t>, int64_t, uint32_t>> data; |
| data.reserve(aggregate->by_event_code_size()); |
| |
| for (auto [packed_event_codes, event_code_data] : aggregate->by_event_code()) { |
| data.emplace_back(std::make_tuple(config::UnpackEventCodes(packed_event_codes), |
| event_code_data.sum_and_count().sum(), |
| event_code_data.sum_and_count().count())); |
| } |
| |
| if (data.empty()) { |
| return {nullptr}; |
| } |
| return encoder->EncodeSumAndCountObservation(data); |
| } |
| |
| std::string SumAndCountAggregationProcedure::DebugString() const { return "SUM_AND_COUNT"; } |
| |
| } // namespace cobalt::local_aggregation |