File: result_refresh_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 4,338 bytes parent folder | download | duplicates (11)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_SELECTION_RESULT_REFRESH_MANAGER_H_
#define COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_SELECTION_RESULT_REFRESH_MANAGER_H_

#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ref.h"
#include "components/segmentation_platform/internal/database/cached_result_writer.h"
#include "components/segmentation_platform/internal/scheduler/execution_service.h"
#include "components/segmentation_platform/internal/selection/segment_result_provider.h"
#include "components/segmentation_platform/public/result.h"

namespace segmentation_platform {
struct Config;
class SegmentResultProvider;

// ResultRefreshManager runs on every startup, ensuring that model results are
// valid both in database and prefs. Responsible for two things.
// 1. Getting model result from database or by running the model and updating
// datab.
// 2. Call to update prefs with the latest model result, if prefs results are
// invalid.
class ResultRefreshManager {
 public:
  ResultRefreshManager(const ConfigHolder* config_holder,
                       CachedResultWriter* cached_result_writer,
                       const PlatformOptions& platform_options);

  ~ResultRefreshManager();

  // Disallow copy/assign.
  ResultRefreshManager(ResultRefreshManager&) = delete;
  ResultRefreshManager& operator=(ResultRefreshManager&) = delete;

  // This method initialises the class with result providers.
  void Initialize(std::map<std::string, std::unique_ptr<SegmentResultProvider>>
                      result_providers,
                  ExecutionService* execution_service);

  // This refreshes model results. At startup, it refreshes model results after
  // `kModelInitializationTimeoutMs` delay. It does the following when result
  // for a model is required.
  // 1. Try to get model result from database.
  // 2. If present in database, update it in prefs if prefs result is invalid or
  // expired.
  // 3. Else, try to get result by running the model and also save computed
  // scores in db and update it in prefs if prefs result is invalid or
  // expired.
  void RefreshModelResults(bool is_startup);

  // This is triggered when model info is updated. This ensures model execution,
  // updating prefs and database if required on model update. If delay isn't
  // executed, it won't do anything and wait for RefreshModelResult to execute
  // the model.
  void OnModelUpdated(proto::SegmentInfo* segment_info);

 private:
  // Tells about the state of delay before model execution starts.
  enum DelayState {
    DELAY_NOT_HIT = 0,   // Delay isn't introduced yet.
    DELAY_EXECUTED = 1,  // Delay is executed completely.
  };

  // Method that internally execute all the models one by one and get result for
  // them.
  void RefreshModelResultsInternal();

  // Gives result for the model based on `run_model`. If `run_model` is false,
  // tries to get the result from database, else tries to get the result by
  // executing model. It also saves to the result to database after model
  // execution.
  void GetCachedResultOrRunModel(const Config* config);

  void OnGetCachedResultOrRunModel(
      SegmentResultProvider* segment_result_provider,
      const Config* config,
      std::unique_ptr<SegmentResultProvider::SegmentResult> result);

  // Configs for all registered clients.
  const raw_ptr<const ConfigHolder> config_holder_;

  // Stores `SegmentResultProvider` for all clients.
  std::map<std::string, std::unique_ptr<SegmentResultProvider>>
      result_providers_;

  // Delegate to write results for all clients to prefs if previous results are
  // not present or invalid.
  const raw_ptr<CachedResultWriter, DanglingUntriaged> cached_result_writer_;

  // Execution Service
  raw_ptr<ExecutionService> execution_service_{nullptr};

  // Platform options indicating whether to force refresh results or not.
  const PlatformOptions platform_options_;

  DelayState delay_state_{DelayState::DELAY_NOT_HIT};

  base::WeakPtrFactory<ResultRefreshManager> weak_ptr_factory_{this};
};

}  // namespace segmentation_platform

#endif  // COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_SELECTION_RESULT_REFRESH_MANAGER_H_