1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "components/segmentation_platform/internal/database/test_segment_info_database.h"
#include <optional>
#include "base/containers/contains.h"
#include "components/segmentation_platform/internal/metadata/metadata_writer.h"
#include "components/segmentation_platform/internal/proto/model_prediction.pb.h"
#include "components/segmentation_platform/public/proto/types.pb.h"
namespace segmentation_platform::test {
TestSegmentInfoDatabase::TestSegmentInfoDatabase()
: SegmentInfoDatabase(nullptr, nullptr) {}
TestSegmentInfoDatabase::~TestSegmentInfoDatabase() = default;
void TestSegmentInfoDatabase::Initialize(SuccessCallback callback) {
std::move(callback).Run(true);
}
void TestSegmentInfoDatabase::GetSegmentInfoForSegments(
const base::flat_set<SegmentId>& segment_ids,
MultipleSegmentInfoCallback callback) {
auto result = std::make_unique<SegmentInfoDatabase::SegmentInfoList>();
for (const auto& pair : segment_infos_) {
if (pair.second.model_source() != ModelSource::DEFAULT_MODEL_SOURCE &&
base::Contains(segment_ids, pair.first)) {
result->emplace_back(pair.first, &pair.second);
}
}
std::move(callback).Run(std::move(result));
}
std::unique_ptr<SegmentInfoDatabase::SegmentInfoList>
TestSegmentInfoDatabase::GetSegmentInfoForBothModels(
const base::flat_set<SegmentId>& segment_ids) {
auto result = std::make_unique<SegmentInfoDatabase::SegmentInfoList>();
for (const auto& pair : segment_infos_) {
if (base::Contains(segment_ids, pair.first)) {
result->emplace_back(pair.first, &pair.second);
}
}
return result;
}
const SegmentInfo* TestSegmentInfoDatabase::GetCachedSegmentInfo(
SegmentId segment_id,
ModelSource model_source) {
for (const auto& pair : segment_infos_) {
if (segment_id == pair.first &&
model_source == pair.second.model_source()) {
return &pair.second;
}
}
return nullptr;
}
void TestSegmentInfoDatabase::UpdateSegment(
SegmentId segment_id,
ModelSource model_source,
std::optional<proto::SegmentInfo> segment_info,
SuccessCallback callback) {
if (segment_info.has_value()) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
info->CopyFrom(segment_info.value());
} else {
// Delete the segment.
auto new_end = std::remove_if(
segment_infos_.begin(), segment_infos_.end(),
[segment_id,
model_source](const std::pair<SegmentId, proto::SegmentInfo>& pair) {
return (pair.first == segment_id &&
pair.second.model_source() == model_source);
});
segment_infos_.erase(new_end, segment_infos_.end());
}
std::move(callback).Run(true);
}
void TestSegmentInfoDatabase::SaveSegmentResult(
SegmentId segment_id,
ModelSource model_source,
std::optional<proto::PredictionResult> result,
SuccessCallback callback) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
if (!result.has_value()) {
info->clear_prediction_result();
} else {
info->mutable_prediction_result()->Swap(&result.value());
}
std::move(callback).Run(true);
}
void TestSegmentInfoDatabase::SaveTrainingData(SegmentId segment_id,
ModelSource model_source,
const proto::TrainingData& data,
SuccessCallback callback) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
info->add_training_data()->CopyFrom(data);
std::move(callback).Run(true);
}
void TestSegmentInfoDatabase::GetTrainingData(SegmentId segment_id,
ModelSource model_source,
TrainingRequestId request_id,
bool delete_from_db,
TrainingDataCallback callback) {
// TODO(ritikagup) : Try replacing it with GetCachedSegmentInfo.
proto::SegmentInfo* segment_info = nullptr;
for (auto& pair : segment_infos_) {
if (pair.first == segment_id &&
pair.second.model_source() == model_source) {
segment_info = &pair.second;
break;
}
}
std::optional<proto::TrainingData> result;
if (segment_info == nullptr) {
std::move(callback).Run(result);
return;
}
for (int i = 0; i < segment_info->training_data_size(); i++) {
if (segment_info->training_data(i).request_id() ==
request_id.GetUnsafeValue()) {
result = segment_info->training_data(i);
if (delete_from_db) {
segment_info->mutable_training_data()->DeleteSubrange(i, 1);
}
break;
}
}
std::move(callback).Run(result);
}
void TestSegmentInfoDatabase::AddUserActionFeature(
SegmentId segment_id,
const std::string& name,
uint64_t bucket_count,
uint64_t tensor_length,
proto::Aggregation aggregation,
ModelSource model_source) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
MetadataWriter writer(info->mutable_model_metadata());
MetadataWriter::UMAFeature feature{
.signal_type = proto::SignalType::USER_ACTION,
.name = name.c_str(),
.bucket_count = bucket_count,
.tensor_length = tensor_length,
.aggregation = aggregation,
.accepted_enum_ids = nullptr};
MetadataWriter::UMAFeature features[] = {feature};
writer.AddUmaFeatures(features, 1);
}
void TestSegmentInfoDatabase::AddHistogramValueFeature(
SegmentId segment_id,
const std::string& name,
uint64_t bucket_count,
uint64_t tensor_length,
proto::Aggregation aggregation,
ModelSource model_source) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
MetadataWriter writer(info->mutable_model_metadata());
MetadataWriter::UMAFeature feature{
.signal_type = proto::SignalType::HISTOGRAM_VALUE,
.name = name.c_str(),
.bucket_count = bucket_count,
.tensor_length = tensor_length,
.aggregation = aggregation,
.accepted_enum_ids = nullptr};
MetadataWriter::UMAFeature features[] = {feature};
writer.AddUmaFeatures(features, 1);
}
void TestSegmentInfoDatabase::AddHistogramEnumFeature(
SegmentId segment_id,
const std::string& name,
uint64_t bucket_count,
uint64_t tensor_length,
proto::Aggregation aggregation,
const std::vector<int32_t>& accepted_enum_ids,
ModelSource model_source) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
MetadataWriter writer(info->mutable_model_metadata());
MetadataWriter::UMAFeature feature{
.signal_type = proto::SignalType::HISTOGRAM_ENUM,
.name = name.c_str(),
.bucket_count = bucket_count,
.tensor_length = tensor_length,
.aggregation = aggregation,
.enum_ids_size = accepted_enum_ids.size(),
.accepted_enum_ids = accepted_enum_ids.data()};
MetadataWriter::UMAFeature features[] = {feature};
writer.AddUmaFeatures(features, 1);
}
void TestSegmentInfoDatabase::AddSqlFeature(
SegmentId segment_id,
const MetadataWriter::SqlFeature& feature,
ModelSource model_source) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
MetadataWriter writer(info->mutable_model_metadata());
writer.AddSqlFeature(feature);
}
void TestSegmentInfoDatabase::AddPredictionResult(SegmentId segment_id,
float score,
base::Time timestamp,
ModelSource model_source) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
proto::PredictionResult* result = info->mutable_prediction_result();
result->clear_result();
result->add_result(score);
result->set_timestamp_us(
timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds());
}
void TestSegmentInfoDatabase::AddDiscreteMapping(
SegmentId segment_id,
const float mappings[][2],
int num_pairs,
const std::string& discrete_mapping_key,
ModelSource model_source) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
auto* discrete_mappings_map =
info->mutable_model_metadata()->mutable_discrete_mappings();
auto& discrete_mappings = (*discrete_mappings_map)[discrete_mapping_key];
for (int i = 0; i < num_pairs; i++) {
auto* pair = mappings[i];
auto* entry = discrete_mappings.add_entries();
entry->set_min_result(pair[0]);
entry->set_rank(pair[1]);
}
}
void TestSegmentInfoDatabase::SetBucketDuration(SegmentId segment_id,
uint64_t bucket_duration,
proto::TimeUnit time_unit,
ModelSource model_source) {
proto::SegmentInfo* info = FindOrCreateSegment(segment_id, model_source);
info->mutable_model_metadata()->set_bucket_duration(bucket_duration);
info->mutable_model_metadata()->set_time_unit(time_unit);
}
proto::SegmentInfo* TestSegmentInfoDatabase::FindOrCreateSegment(
SegmentId segment_id,
ModelSource model_source) {
// TODO(ritikagup) : Try replacing it with GetCachedSegmentInfo.
proto::SegmentInfo* info = nullptr;
for (auto& pair : segment_infos_) {
if (pair.first == segment_id &&
pair.second.model_source() == model_source) {
info = &pair.second;
break;
}
}
if (info == nullptr) {
segment_infos_.emplace_back(segment_id, proto::SegmentInfo());
info = &segment_infos_.back().second;
info->set_segment_id(segment_id);
info->set_model_source(model_source);
}
return info;
}
} // namespace segmentation_platform::test
|