blob: 90ad48927f41fe3a2a3d421072aac7ac89eccb4b [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.
#ifndef COBALT_SRC_PB_METADATA_BUILDER_H_
#define COBALT_SRC_PB_METADATA_BUILDER_H_
#include "src/lib/util/clock.h"
#include "src/lib/util/consistent_proto_store.h"
#include "src/lib/util/datetime_util.h"
#include "src/lib/util/protected_fields.h"
#include "src/logger/project_context.h"
#include "src/pb/observation_batch.pb.h"
#include "src/pb/system_data_history.pb.h"
#include "src/registry/metric_definition.pb.h"
#include "src/registry/report_definition.pb.h"
#include "src/system_data/system_data.h"
namespace cobalt {
class MetadataBuilder {
public:
MetadataBuilder(system_data::SystemDataInterface &system_data,
std::string system_profile_cache_dir, util::FileSystem &fs)
: MetadataBuilder(system_data, nullptr, std::move(system_profile_cache_dir), fs) {}
MetadataBuilder(system_data::SystemDataInterface &system_data,
util::ValidatedClockInterface *validated_clock,
std::string system_profile_cache_dir, util::FileSystem &fs);
// GetSystemProfile retrieves the full SystemProfile from the time-based storage, as selected by
// the SystemProfileSelectionPolicy.
[[nodiscard]] SystemProfile GetSystemProfile(const MetricDefinition &metric,
const ReportDefinition &report,
util::TimeInfo time_info) const;
// Build constructs an ObservationMetadata instance based on the time-based SystemProfile selected
// by the SystemProfileSelectionPolicy.
//
// Note: This only works with SystemProfileSelectionPolicies SELECT_FIRST and SELECT_LAST.
[[nodiscard]] std::unique_ptr<ObservationMetadata> Build(const logger::MetricRef &metric,
const ReportDefinition &report,
util::TimeInfo time_info) const;
// BuildFromProfile constructs an ObservationMetadata instance based on the provided
// SystemProfile.
static std::unique_ptr<ObservationMetadata> BuildFromProfile(const logger::MetricRef &metric,
const ReportDefinition &report,
uint32_t day_index,
const SystemProfile &profile);
// SnapshotSystemData signals to MetadataBuilder that the SystemProfile may have changed, and
// should be snapshotted.
void SnapshotSystemData();
// CleanupBefore is used to clean up SystemProfile snapshots that are no longer useful.
void CleanupBefore(std::chrono::hours hours_ago);
// CurrentSystemProfile returns a SystemProfile that has been filtered by the SystemProfileFields
// in the given ReportDefinition.
SystemProfile CurrentSystemProfile(const ReportDefinition &report) const;
static void FilteredSystemProfile(const ReportDefinition &report, const SystemProfile &profile_in,
SystemProfile *profile_out);
private:
void FilteredSystemProfile(const ReportDefinition &report, SystemProfile *profile_out) const;
void WriteHistory(util::ProtectedFields<SystemProfileHistory>::LockedFieldsPtr history);
util::ProtectedFields<SystemProfileHistory> system_profile_history_;
system_data::SystemDataInterface &system_data_;
std::unique_ptr<util::AlwaysAccurateClock> owned_clock_;
util::ValidatedClockInterface *validated_clock_;
std::unique_ptr<util::ConsistentProtoStore> history_store_;
};
} // namespace cobalt
#endif // COBALT_SRC_PB_METADATA_BUILDER_H_