blob: 9b254e8cc947854d3dfcd5ea60aad4a302267802 [file] [log] [blame]
alanlxl30f15bd2020-08-11 21:26:12 +10001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "federated/utils.h"
6
7#include <string>
8#include <vector>
9
alanlxl9d26c1c2020-08-21 13:42:36 +100010#include <base/strings/stringprintf.h>
11
alanlxl30f15bd2020-08-11 21:26:12 +100012namespace federated {
13
14namespace {
15using chromeos::federated::mojom::ExamplePtr;
16using chromeos::federated::mojom::FloatList;
17using chromeos::federated::mojom::Int64List;
18using chromeos::federated::mojom::ValueList;
19} // namespace
20
alanlxl9d26c1c2020-08-21 13:42:36 +100021// TODO(alanlxl): just random numbers, need a discussion
22constexpr size_t kMaxStreamingExampleCount = 4000;
23constexpr size_t kMinExampleCount = 1;
24
25constexpr char kSessionStartedState[] = "started";
26constexpr char kSessionStoppedState[] = "stopped";
27constexpr char kUserDatabasePath[] = "/run/daemon-store/federated";
28constexpr char kDatabaseFileName[] = "examples.db";
29
30// Get the database file path with the given sanitized_username.
31base::FilePath GetDatabasePath(const std::string& sanitized_username) {
32 return base::FilePath(base::StringPrintf("%s/%s/%s", kUserDatabasePath,
33 sanitized_username.c_str(),
34 kDatabaseFileName));
35}
36
alanlxl30f15bd2020-08-11 21:26:12 +100037tensorflow::Example ConvertToTensorFlowExampleProto(const ExamplePtr& example) {
38 tensorflow::Example tf_example;
39 auto& feature = *tf_example.mutable_features()->mutable_feature();
40
41 for (const auto& iter : example->features->feature) {
42 if (iter.second->which() == ValueList::Tag::INT64_LIST) {
43 std::vector<int64_t>& value_list = iter.second->get_int64_list()->value;
44 *feature[iter.first].mutable_int64_list()->mutable_value() = {
45 value_list.begin(), value_list.end()};
46 } else if (iter.second->which() == ValueList::Tag::FLOAT_LIST) {
47 std::vector<double>& value_list = iter.second->get_float_list()->value;
48 *feature[iter.first].mutable_float_list()->mutable_value() = {
49 value_list.begin(), value_list.end()};
50 } else if (iter.second->which() == ValueList::Tag::STRING_LIST) {
51 std::vector<std::string>& value_list =
52 iter.second->get_string_list()->value;
53 *feature[iter.first].mutable_bytes_list()->mutable_value() = {
54 value_list.begin(), value_list.end()};
55 }
56 }
57 return tf_example;
58}
59
60} // namespace federated