| // 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/select_most_common_aggregation_procedure.h" |
| |
| #include "src/lib/statusor/status_macros.h" |
| #include "src/pb/observation.pb.h" |
| #include "src/registry/packed_event_codes.h" |
| |
| namespace cobalt::local_aggregation { |
| |
| SelectMostCommonAggregationProcedure::SelectMostCommonAggregationProcedure( |
| const MetricDefinition &metric, const ReportDefinition &report) |
| : AggregationProcedure(metric, report) {} |
| |
| void SelectMostCommonAggregationProcedure::UpdateAggregateData( |
| const logger::EventRecord &event_record, AggregateData *aggregate_data, |
| EventCodeAggregate * /*aggregate*/) { |
| // TODO(fxbug.dev/53775): Handle the case where event_record is malformed. |
| aggregate_data->set_count(aggregate_data->count() + |
| event_record.event()->occurrence_event().count()); |
| } |
| |
| std::string SelectMostCommonAggregationProcedure::DebugString() const { |
| return "SELECT_MOST_COMMON"; |
| } |
| |
| lib::statusor::StatusOr<std::unique_ptr<Observation>> |
| SelectMostCommonAggregationProcedure::GenerateSingleObservation( |
| const logger::Encoder *encoder, const std::vector<EventCodeAggregate *> &aggregates, |
| const std::set<uint32_t> &event_vectors) { |
| std::vector<std::tuple<std::vector<uint32_t>, int64_t>> data; |
| if (event_vectors.empty()) { |
| return encoder->EncodeIntegerObservation(data); |
| } |
| data.reserve(1); |
| |
| uint32_t max_count = 0; |
| uint32_t selected_event_vector = -1; |
| |
| for (const uint32_t packed_event_vector : event_vectors) { |
| uint32_t event_vector_count = 0; |
| |
| for (const EventCodeAggregate *aggregate : aggregates) { |
| auto it = aggregate->by_event_code().find(packed_event_vector); |
| if (it == aggregate->by_event_code().end()) { |
| continue; |
| } |
| event_vector_count += it->second.count(); |
| } |
| if (event_vector_count >= max_count) { |
| selected_event_vector = packed_event_vector; |
| max_count = event_vector_count; |
| } |
| } |
| |
| data.emplace_back(std::make_tuple(config::UnpackEventCodes(selected_event_vector), 1)); |
| |
| return encoder->EncodeIntegerObservation(data); |
| } |
| |
| } // namespace cobalt::local_aggregation |