xref: /aosp_15_r20/external/federated-compute/fcp/client/opstats/opstats_example_store.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef FCP_CLIENT_OPSTATS_OPSTATS_EXAMPLE_STORE_H_
17 #define FCP_CLIENT_OPSTATS_OPSTATS_EXAMPLE_STORE_H_
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "fcp/client/engine/example_iterator_factory.h"
24 #include "fcp/client/log_manager.h"
25 #include "fcp/client/opstats/opstats_db.h"
26 #include "fcp/client/opstats/opstats_logger.h"
27 #include "fcp/client/simple_task_environment.h"
28 #include "fcp/protos/plan.pb.h"
29 
30 namespace fcp {
31 namespace client {
32 namespace opstats {
33 inline static constexpr char kOpStatsCollectionUri[] = "internal:/opstats";
34 inline static constexpr char kPopulationName[] = "population_name";
35 inline static constexpr char kSessionName[] = "session_name";
36 inline static constexpr char kTaskName[] = "task_name";
37 inline static constexpr char kEventsEventType[] = "events-event_type";
38 inline static constexpr char kEventsTimestampMillis[] = "events-timestamp";
39 inline static constexpr char kDatasetStatsUri[] = "dataset_stats-uri";
40 inline static constexpr char kDatasetStatsNumExamplesRead[] =
41     "dataset_stats-num_examples_read";
42 inline static constexpr char kDatasetStatsNumBytesRead[] =
43     "dataset_stats-num_bytes_read";
44 inline static constexpr char kErrorMessage[] = "error_message";
45 inline static constexpr char kRetryWindowDelayMinMillis[] =
46     "retry_window-delay_min";
47 inline static constexpr char kRetryWindowDelayMaxMillis[] =
48     "retry_window-delay_max";
49 inline static constexpr char kChunkingLayerBytesDownloaded[] =
50     "chunking_layer_bytes_downloaded";
51 inline static constexpr char kChunkingLayerBytesUploaded[] =
52     "chunking_layer_bytes_uploaded";
53 inline static constexpr char kNetworkDuration[] = "network_duration";
54 inline static constexpr char kEarliestTrustWorthyTimeMillis[] =
55     "earliest_trustworthy_time";
56 
57 class OpStatsExampleIteratorFactory
58     : public fcp::client::engine::ExampleIteratorFactory {
59  public:
OpStatsExampleIteratorFactory(OpStatsLogger * op_stats_logger,LogManager * log_manager,bool opstats_last_successful_contribution_criteria)60   OpStatsExampleIteratorFactory(
61       OpStatsLogger* op_stats_logger, LogManager* log_manager,
62       bool opstats_last_successful_contribution_criteria)
63       : op_stats_logger_(op_stats_logger),
64         log_manager_(log_manager),
65         opstats_last_successful_contribution_criteria_(
66             opstats_last_successful_contribution_criteria) {}
67 
68   bool CanHandle(const google::internal::federated::plan::ExampleSelector&
69                      example_selector) override;
70 
ShouldCollectStats()71   bool ShouldCollectStats() override { return true; }
72 
73   absl::StatusOr<std::unique_ptr<ExampleIterator>> CreateExampleIterator(
74       const google::internal::federated::plan::ExampleSelector&
75           example_selector) override;
76 
77  private:
78   OpStatsLogger* op_stats_logger_;
79   LogManager* log_manager_;
80   bool opstats_last_successful_contribution_criteria_;
81 };
82 
83 }  // namespace opstats
84 }  // namespace client
85 }  // namespace fcp
86 
87 #endif  // FCP_CLIENT_OPSTATS_OPSTATS_EXAMPLE_STORE_H_
88