blob: a488ee73f7d963de469b9e746253956bb5547a8b [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 "src/local_aggregation_1_1/aggregation_procedures/aggregation_procedure.h"
#include <gtest/gtest.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"
namespace cobalt::local_aggregation {
class AggregationProcedureTest : public testing::TestAggregationProcedure {};
TEST_F(AggregationProcedureTest, GetWorks) {
auto count_aggregation_procedure = GetProcedureFor(
kOccurrenceMetricMetricId, kOccurrenceMetricFleetwideOccurrenceCountsReportReportIndex);
ASSERT_NE(count_aggregation_procedure, nullptr);
EXPECT_EQ(count_aggregation_procedure->DebugString(), "COUNT");
EXPECT_TRUE(count_aggregation_procedure->IsValidEventType(Event::TypeCase::kOccurrenceEvent));
count_aggregation_procedure = nullptr;
count_aggregation_procedure = GetProcedureFor(kOccurrenceMetricMetricId,
kOccurrenceMetricHourlyDeviceHistogramsReportIndex);
ASSERT_NE(count_aggregation_procedure, nullptr);
EXPECT_EQ(count_aggregation_procedure->DebugString(), "COUNT");
EXPECT_TRUE(count_aggregation_procedure->IsValidEventType(Event::TypeCase::kOccurrenceEvent));
auto sum_and_count_aggregation_procedure =
GetProcedureFor(kIntegerMetricMetricId, kIntegerMetricFleetwideMeansReportReportIndex);
ASSERT_NE(sum_and_count_aggregation_procedure, nullptr);
EXPECT_EQ(sum_and_count_aggregation_procedure->DebugString(), "SUM_AND_COUNT");
EXPECT_TRUE(
sum_and_count_aggregation_procedure->IsValidEventType(Event::TypeCase::kIntegerEvent));
auto at_least_aggregation_procedure = GetProcedureFor(
kOccurrenceMetricMetricId, kOccurrenceMetricUniqueDeviceCountsReport1DayReportIndex);
ASSERT_NE(at_least_aggregation_procedure, nullptr);
EXPECT_EQ(at_least_aggregation_procedure->DebugString(), "AT_LEAST_ONCE");
EXPECT_TRUE(at_least_aggregation_procedure->IsValidEventType(Event::TypeCase::kOccurrenceEvent));
}
TEST_F(AggregationProcedureTest, UpdateAggregate) {
auto count_aggregation_procedure = GetProcedureFor(
kOccurrenceMetricMetricId, kOccurrenceMetricFleetwideOccurrenceCountsReportReportIndex);
uint64_t event_vector_buffer_max = GetEventVectorBufferMax();
ASSERT_GE(event_vector_buffer_max, 0u);
uint32_t kHourId = 10000;
ReportAggregate aggregate;
uint64_t num_events = event_vector_buffer_max + 1;
AddOccurrenceEventsForHour(num_events, kHourId, count_aggregation_procedure.get(), &aggregate);
ASSERT_EQ(aggregate.hourly().by_hour_id().count(kHourId), 1u);
ASSERT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code_size(),
event_vector_buffer_max);
for (uint32_t i = 0; i < event_vector_buffer_max; ++i) {
EXPECT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code(i).event_codes(0), i + 1);
EXPECT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code(i).data().count(), i + 1);
}
}
TEST_F(AggregationProcedureTest, GenerateObservation) {
auto at_least_once_aggregation_procedure = GetProcedureFor(
kOccurrenceMetricMetricId, kOccurrenceMetricUniqueDeviceCountsReport7DaysReportIndex);
uint32_t event_vector_buffer_max = GetEventVectorBufferMax();
ASSERT_GE(event_vector_buffer_max, 0u);
uint32_t kFinalDayIndex = 10000;
uint32_t kFirstDayIndex = 10000 - 6;
ReportAggregate aggregate;
uint32_t kFirstDayNumEvents = event_vector_buffer_max - 1;
AddOccurrenceEventsForDay(kFirstDayNumEvents, kFirstDayIndex,
at_least_once_aggregation_procedure.get(), &aggregate);
uint32_t kSecondDayNumEvents = event_vector_buffer_max + 1;
AddOccurrenceEventsForDay(kSecondDayNumEvents, kFirstDayIndex + 1,
at_least_once_aggregation_procedure.get(), &aggregate);
auto observation_or = GenerateObservation(util::TimeInfo::FromDayIndex(kFinalDayIndex),
at_least_once_aggregation_procedure.get(), &aggregate);
ASSERT_TRUE(observation_or.ok());
auto observation = observation_or.ConsumeValueOrDie();
ASSERT_EQ(observation->integer().values_size(), event_vector_buffer_max);
for (uint32_t i = 0; i < event_vector_buffer_max; ++i) {
ASSERT_EQ(observation->integer().values(i).event_codes(0), i + 1);
}
}
} // namespace cobalt::local_aggregation