blob: f69d6e5538aaa44c9107cb230720b1f96b5ef804 [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/select_first_aggregation_procedure.h"
#include <memory>
#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.h"
#include "src/logger/project_context_factory.h"
#include "src/registry/report_definition.pb.h"
namespace cobalt::local_aggregation {
class SelectFirstAggregationProcedureTest : public testing::TestAggregationProcedure {
protected:
logger::EventRecord GetEventRecord(uint32_t metric_id) {
return logger::EventRecord(GetProjectContext(), metric_id);
}
std::unique_ptr<AggregationProcedure> GetProcedure(uint32_t metric_id, uint32_t report_index) {
return std::make_unique<SelectFirstAggregationProcedure>(GetMetricDef(metric_id),
GetReportDef(metric_id, report_index));
}
};
TEST_F(SelectFirstAggregationProcedureTest, UpdateAggregate1DayReport) {
uint32_t metric_id = kOccurrenceMetricMetricId;
uint32_t report_index = kOccurrenceMetricUniqueDeviceCountsSelectFirstReport1DayReportIndex;
auto procedure = GetProcedure(metric_id, report_index);
const uint32_t kDayIndex = 10000;
auto record = GetEventRecord(metric_id);
record.event()->set_day_index(kDayIndex);
uint32_t kNumEventCodes = 2;
ReportAggregate aggregate;
// Add events for 2 different event vectors: {1} and {2}.
AddOccurrenceEventsForDay(kNumEventCodes, kDayIndex, procedure.get(), &aggregate);
// Check that |aggregate| was updated for the first event vector but not the second.
ASSERT_EQ(aggregate.daily().by_day_index().count(kDayIndex), 1u);
ASSERT_EQ(aggregate.daily().by_day_index().at(kDayIndex).by_event_code(0).event_codes(0), 1);
ASSERT_TRUE(
aggregate.daily().by_day_index().at(kDayIndex).by_event_code(0).data().at_least_once());
}
TEST_F(SelectFirstAggregationProcedureTest, GenerateObservation1DayReport) {
uint32_t metric_id = kOccurrenceMetricMetricId;
uint32_t report_index = kOccurrenceMetricUniqueDeviceCountsSelectFirstReport1DayReportIndex;
auto procedure = GetProcedure(metric_id, report_index);
const uint32_t kDayIndex = 10000;
util::TimeInfo time_info;
time_info.day_index = kDayIndex;
const uint32_t kNumEventCodes = 2;
ReportAggregate report_aggregate;
// Add events for 2 different event vectors: {1} and {2}.
AddOccurrenceEventsForDay(kNumEventCodes, kDayIndex, procedure.get(), &report_aggregate);
auto status_or_observation = procedure->GenerateObservation(time_info, &report_aggregate);
ASSERT_TRUE(status_or_observation.ok());
auto observation = std::move(status_or_observation.ValueOrDie());
// Check that |observation| includes the first event vector but not the second.
ASSERT_TRUE(observation->has_integer());
const auto &integer_obs = observation->integer();
ASSERT_EQ(integer_obs.values_size(), 1u);
ASSERT_EQ(integer_obs.values(0).event_codes(0), 1u);
// Check that obsolete aggregates were cleaned up.
EXPECT_EQ(report_aggregate.daily().by_day_index().count(kDayIndex), 0u);
}
TEST_F(SelectFirstAggregationProcedureTest, GenerateObservation7DaysReport) {
uint32_t metric_id = kOccurrenceMetricMetricId;
uint32_t report_index = kOccurrenceMetricUniqueDeviceCountsSelectFirstReport7DaysReportIndex;
auto procedure = GetProcedure(metric_id, report_index);
const uint32_t kDayIndex = 10000;
util::TimeInfo time_info;
time_info.day_index = kDayIndex;
const uint32_t kNumEventCodes = 2;
ReportAggregate report_aggregate;
// Add events for 2 different event vectors: {1} and {2}, for each of the 7 days in the window
// ending on |kDayIndex|.
for (uint32_t day = kDayIndex; day > kDayIndex - 7; --day) {
AddOccurrenceEventsForDay(kNumEventCodes, day, procedure.get(), &report_aggregate);
}
auto status_or_observation = procedure->GenerateObservation(time_info, &report_aggregate);
ASSERT_TRUE(status_or_observation.ok());
auto observation = std::move(status_or_observation.ValueOrDie());
// Check that |observation| includes the first event vector but not the second.
ASSERT_TRUE(observation->has_integer());
const auto &integer_obs = observation->integer();
ASSERT_EQ(integer_obs.values_size(), 1u);
ASSERT_EQ(integer_obs.values(0).event_codes(0), 1u);
// Check that obsolete aggregates were cleaned up.
EXPECT_EQ(report_aggregate.daily().by_day_index().count(kDayIndex - 6), 0u);
for (uint32_t day = kDayIndex; day > kDayIndex - 6; --day) {
EXPECT_EQ(report_aggregate.daily().by_day_index().count(day), 1u);
}
}
} // namespace cobalt::local_aggregation