blob: abc5529701bfea05f13fb6cc479e687a0d03cf83 [file] [log] [blame]
// 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 <gtest/gtest.h>
#include "src/lib/util/datetime_util.h"
#include "src/local_aggregation_1_1/aggregation_procedures/aggregation_procedure.h"
#include "src/local_aggregation_1_1/aggregation_procedures/testing/test_aggregation_procedure.h"
#include "src/local_aggregation_1_1/testing/test_registry.cb.h"
#include "src/logger/project_context_factory.h"
#include "src/pb/metadata_builder.h"
namespace cobalt::local_aggregation {
class CountAggregationProcedureTest : public testing::TestAggregationProcedure {};
TEST_F(CountAggregationProcedureTest, UpdateAggregateWorks) {
uint32_t metric_id = kOccurrenceMetricMetricId;
auto procedure =
GetProcedureFor(metric_id, kOccurrenceMetricFleetwideOccurrenceCountsReportReportIndex);
ReportAggregate aggregate;
const uint32_t kHourId = 1;
const uint32_t kNumEventCodes = 100;
AddOccurrenceEventsForHour(kNumEventCodes, kHourId, procedure.get(), &aggregate);
ASSERT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code_size(), kNumEventCodes);
for (uint32_t i = 0; i < kNumEventCodes; ++i) {
ASSERT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code(i).event_codes(0), i + 1);
ASSERT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code(i).data().count(), i + 1);
}
}
TEST_F(CountAggregationProcedureTest, UpdateAggregateWorksCountAsInt) {
uint32_t metric_id = kOccurrenceMetricMetricId;
auto procedure = GetProcedureFor(metric_id, kOccurrenceMetricHourlyDeviceHistogramsReportIndex);
ReportAggregate aggregate;
const uint32_t kHourId = 1;
const uint32_t kNumEventCodes = 100;
AddOccurrenceEventsForHourWithCount(kNumEventCodes, 5, kHourId, procedure.get(), &aggregate);
ASSERT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code_size(), kNumEventCodes);
for (uint32_t i = 0; i < kNumEventCodes; ++i) {
ASSERT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code(i).event_codes(0), i + 1);
ASSERT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code(i).data().count(),
(i + 1) * 5);
}
}
TEST_F(CountAggregationProcedureTest, GenerateObservationWorks) {
uint32_t metric_id = kOccurrenceMetricMetricId;
uint32_t report_index = kOccurrenceMetricFleetwideOccurrenceCountsReportReportIndex;
auto procedure = GetProcedureFor(metric_id, report_index);
ReportAggregate aggregate;
const uint32_t kNumEventCodes = 10;
ASSERT_GE(GetReportDef(metric_id, report_index).event_vector_buffer_max(), kNumEventCodes);
const uint32_t kEndHourId = 11;
for (auto hour_id = 1; hour_id <= kEndHourId; hour_id += 2) {
AddOccurrenceEventsForHour(kNumEventCodes, hour_id, procedure.get(), &aggregate);
}
util::TimeInfo info;
info.hour_id = kEndHourId;
auto observation_or = GenerateObservation(info, procedure.get(), &aggregate);
ASSERT_TRUE(observation_or.ok());
auto observation = observation_or.ConsumeValueOrDie();
// Should only generate for kEndHourId
ASSERT_TRUE(observation);
ASSERT_EQ(observation->integer().values_size(), kNumEventCodes);
for (const auto& value : observation->integer().values()) {
ASSERT_EQ(value.event_codes(0), value.value());
}
ASSERT_EQ(aggregate.hourly().by_hour_id_size(), 0);
}
TEST_F(CountAggregationProcedureTest, GenerateObservationWorksCountAsInt) {
uint32_t metric_id = kOccurrenceMetricMetricId;
uint32_t report_index = kOccurrenceMetricHourlyDeviceHistogramsReportIndex;
auto procedure = GetProcedureFor(metric_id, report_index);
ReportAggregate aggregate;
const uint32_t kNumEventCodes = 10;
ASSERT_GE(GetReportDef(metric_id, report_index).event_vector_buffer_max(), kNumEventCodes);
const uint32_t kEndHourId = 11;
for (auto hour_id = 1; hour_id <= kEndHourId; hour_id += 2) {
AddOccurrenceEventsForHour(kNumEventCodes, hour_id, procedure.get(), &aggregate);
}
util::TimeInfo info;
info.hour_id = kEndHourId;
auto observation_or = GenerateObservation(info, procedure.get(), &aggregate);
ASSERT_TRUE(observation_or.ok());
auto observation = observation_or.ConsumeValueOrDie();
// Should only generate for kEndHourId
ASSERT_TRUE(observation);
ASSERT_EQ(observation->integer().values_size(), kNumEventCodes);
for (const auto& value : observation->integer().values()) {
ASSERT_EQ(value.event_codes(0), value.value());
}
ASSERT_EQ(aggregate.hourly().by_hour_id_size(), 0);
}
} // namespace cobalt::local_aggregation