File: query_clusters_state.cc

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 (324 lines) | stat: -rw-r--r-- 13,197 bytes parent folder | download | duplicates (6)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/history_clusters/core/query_clusters_state.h"

#include <algorithm>
#include <set>

#include "base/memory/ref_counted_delete_on_sequence.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_types.h"
#include "components/history_clusters/core/features.h"
#include "components/history_clusters/core/history_clusters_service.h"
#include "components/history_clusters/core/history_clusters_service_task.h"
#include "components/history_clusters/core/history_clusters_util.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"

namespace history_clusters {

namespace {

QueryClustersFilterParams GetFilterParamsFromFlags(const std::string& query) {
  QueryClustersFilterParams filter_params;
  // Journeys launched Synced visits by default in early 2024.
  filter_params.include_synced_visits = true;
  filter_params.group_clusters_by_content = false;

  // If there is a query, we do not want to apply any filtering.
  if (!query.empty()) {
    return filter_params;
  }

  // Only set special filter params if the zero state filtering flag is applied.
  if (!ShouldUseNavigationContextClustersFromPersistence()) {
    return filter_params;
  }

  filter_params.is_search_initiated = true;
  filter_params.is_shown_on_prominent_ui_surfaces = true;
  // TODO(b/277528165): Apply category filtering only for eligible users.
  return filter_params;
}

}  // namespace

// Helper class that lives and is destroyed on the `sequenced_task_runner`,
// although it's created on the main thread. It allows us to store state that
// is only accessed on `sequenced_task_runner` that persists between batches.
class QueryClustersState::PostProcessor
    : public base::RefCountedDeleteOnSequence<PostProcessor> {
 public:
  PostProcessor(scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner,
                const std::string query)
      : base::RefCountedDeleteOnSequence<PostProcessor>(sequenced_task_runner),
        query_(query) {}
  PostProcessor(const PostProcessor& other) = delete;
  PostProcessor& operator=(const PostProcessor& other) = delete;

  std::vector<history::Cluster> PostProcess(
      std::vector<history::Cluster> clusters) {
    ApplySearchQuery(query_, clusters);
    CullNonProminentOrDuplicateClusters(query_, clusters,
                                        &seen_single_visit_cluster_urls_);

    // Only sort after we figured out what we are showing.
    SortClusters(&clusters);

    // We have to do this AFTER applying the search query, because applying the
    // search query re-scores matching visits to promote them above non-matching
    // visits. Show 1-visit clusters only in query mode.
    CullVisitsThatShouldBeHidden(clusters,
                                 /*is_zero_query_state=*/query_.empty());
    // Do this AFTER we cull the low scoring visits, so those visits don't get
    // their related searches coalesced onto the cluster level.
    CoalesceRelatedSearches(clusters);
    return clusters;
  }

 private:
  friend class base::RefCountedDeleteOnSequence<PostProcessor>;
  friend class base::DeleteHelper<PostProcessor>;

  // Ref-counted object should only be deleted via ref-counting.
  ~PostProcessor() = default;

  const std::string query_;

  // URLs of single-visit non-prominent clusters we've already seen.
  std::set<GURL> seen_single_visit_cluster_urls_;
};

QueryClustersState::QueryClustersState(
    base::WeakPtr<HistoryClustersService> service,
    history::HistoryService* history_service,
    const std::string& query,
    base::Time begin_time,
    bool recluster)
    : service_(service),
      history_service_(history_service),
      query_(query),
      begin_time_(begin_time),
      filter_params_(GetFilterParamsFromFlags(query)),
      recluster_(recluster),
      post_processing_task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
          {base::MayBlock(), base::TaskPriority::USER_VISIBLE})),
      post_processing_state_(
          base::MakeRefCounted<PostProcessor>(post_processing_task_runner_,
                                              query)) {}

QueryClustersState::~QueryClustersState() = default;

void QueryClustersState::LoadNextBatchOfClusters(ResultCallback callback) {
  if (!service_)
    return;

  auto query_clusters_callback = &QueryClustersState::OnGotRawClusters;
  if (!query_.empty() && history_service_ &&
      base::FeatureList::IsEnabled(kSearchesFindUngroupedVisits)) {
    // If there's a search query, we also want to first tack on ungrouped
    // visits.
    query_clusters_callback = &QueryClustersState::GetUngroupedVisits;
  }

  base::TimeTicks query_start_time = base::TimeTicks::Now();
  query_clusters_task_ = service_->QueryClusters(
      ClusteringRequestSource::kJourneysPage, filter_params_, begin_time_,
      continuation_params_, recluster_,
      base::BindOnce(query_clusters_callback, weak_factory_.GetWeakPtr(),
                     query_start_time, std::move(callback)));
}

void QueryClustersState::GetUngroupedVisits(
    base::TimeTicks query_start_time,
    ResultCallback callback,
    std::vector<history::Cluster> clusters,
    QueryClustersContinuationParams new_continuation_params) {
  DCHECK(history_service_);

  // Find ungrouped visits within the timespan bounded by the batch of clusters
  // we have just received.
  history::QueryOptions options;
  options.end_time = continuation_params_.continuation_time;
  options.begin_time = new_continuation_params.continuation_time;

  // No need to use BrowsingHistoryService, because history is now fully synced.
  history_service_->GetAnnotatedVisits(
      options,
      /*compute_redirect_chain_start_properties=*/false,
      /*get_unclustered_visits_only=*/true,
      base::BindOnce(&QueryClustersState::OnGotUngroupedVisits,
                     weak_factory_.GetWeakPtr(), query_start_time,
                     std::move(callback), clusters, new_continuation_params),
      &history_task_tracker_);
}

void QueryClustersState::OnGotUngroupedVisits(
    base::TimeTicks query_start_time,
    ResultCallback callback,
    std::vector<history::Cluster> clusters,
    QueryClustersContinuationParams new_continuation_params,
    std::vector<history::AnnotatedVisit> ungrouped_visits) {
  // Load all the visits in `clusters` into `seen_visits` using similarity key.
  for (auto& cluster : clusters) {
    for (auto& visit : cluster.visits) {
      seen_visits_for_deduping_ungrouped_visits_.insert(SimilarVisit(visit));
    }
  }

  std::vector<history::ClusterVisit> unique_ungrouped_visits;
  for (auto& visit : ungrouped_visits) {
    history::ClusterVisit cluster_visit;
    cluster_visit.annotated_visit = visit;
    if (!visit.content_annotations.search_normalized_url.is_empty()) {
      cluster_visit.normalized_url =
          visit.content_annotations.search_normalized_url;
      cluster_visit.url_for_deduping = cluster_visit.normalized_url;
    } else {
      cluster_visit.normalized_url = visit.url_row.url();
      cluster_visit.url_for_deduping =
          ComputeURLForDeduping(cluster_visit.url_for_deduping);
    }

    auto [ignored_iterator, inserted] =
        seen_visits_for_deduping_ungrouped_visits_.insert(
            SimilarVisit(cluster_visit));
    if (inserted) {
      // Fill in these fields here to avoid doing so unless we're inserting this
      // visit.
      cluster_visit.url_for_display =
          ComputeURLForDisplay(cluster_visit.normalized_url);
      // Give every fake cluster visit a generic 1.0 score to ensure visibility.
      cluster_visit.score = 1.0;

      unique_ungrouped_visits.push_back(std::move(cluster_visit));
    }
  }

  if (!unique_ungrouped_visits.empty()) {
    history::Cluster ungrouped_cluster;
    ungrouped_cluster.visits = std::move(unique_ungrouped_visits);
    // Setting this for correctness, but should have no effect since the user
    // should be searching.
    ungrouped_cluster.should_show_on_prominent_ui_surfaces = false;
    ungrouped_cluster.label_source =
        history::Cluster::LabelSource::kUngroupedVisits;
    ungrouped_cluster.label = l10n_util::GetStringUTF16(
            IDS_HISTORY_CLUSTERS_CLUSTER_LABEL_OTHER_MATCHING_VISITS);
    ungrouped_cluster.raw_label = ungrouped_cluster.label;
    clusters.push_back(std::move(ungrouped_cluster));
  }

  OnGotRawClusters(query_start_time, std::move(callback), std::move(clusters),
                   std::move(new_continuation_params));
}

void QueryClustersState::OnGotRawClusters(
    base::TimeTicks query_start_time,
    ResultCallback callback,
    std::vector<history::Cluster> clusters,
    QueryClustersContinuationParams new_continuation_params) {
  // Post-process the clusters (expensive task) on an anonymous thread to
  // prevent janks.
  base::ElapsedTimer post_processing_timer;  // Create here to time the task.

  size_t clusters_from_backend_count = clusters.size();
  post_processing_task_runner_->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&PostProcessor::PostProcess, post_processing_state_,
                     std::move(clusters)),
      base::BindOnce(
          &QueryClustersState::OnGotClusters, weak_factory_.GetMutableWeakPtr(),
          std::move(post_processing_timer), clusters_from_backend_count,
          query_start_time, std::move(callback), new_continuation_params));
}

void QueryClustersState::OnGotClusters(
    base::ElapsedTimer post_processing_timer,
    size_t clusters_from_backend_count,
    base::TimeTicks query_start_time,
    ResultCallback callback,
    QueryClustersContinuationParams new_continuation_params,
    std::vector<history::Cluster> clusters) {
  base::UmaHistogramTimes("History.Clusters.ProcessClustersDuration",
                          post_processing_timer.Elapsed());

  if (clusters_from_backend_count > 0) {
    // Log the percentage of clusters that get filtered (e.g., 100 - % of
    // clusters that remain).
    base::UmaHistogramCounts100(
        "History.Clusters.PercentClustersFilteredByQuery",
        static_cast<int>(100 - (clusters.size() /
                                (1.0 * clusters_from_backend_count) * 100)));
  }

  continuation_params_ = new_continuation_params;

  // In case no clusters came back, recursively ask for more here. We do this
  // to fulfill the mojom contract where we always return at least one cluster,
  // or we exhaust History. We don't do this in the service because of task
  // tracker lifetime difficulty. In practice, this only happens when the user
  // has a search query that doesn't match any of the clusters in the "page".
  // https://crbug.com/1263728
  //
  // This is distinct from the "tall monitor" case because the page may already
  // be full of clusters. In that case, the WebUI would not know to make another
  // request for clusters.
  if (clusters.empty() && !new_continuation_params.exhausted_all_visits) {
    LoadNextBatchOfClusters(std::move(callback));
    return;
  }

  // This feels like it belongs in `PostProcessor`, but this operates on the
  // main thread, because the data needs to live on the main thread. Doing it
  // on the task runner requires making heap copies, which probably costs more
  // than just doing this simple computation on the main thread.
  UpdateUniqueRawLabels(clusters);

  size_t clusters_size = clusters.size();
  bool is_continuation = number_clusters_sent_to_page_ > 0;
  std::move(callback).Run(query_, std::move(clusters),
                          !new_continuation_params.exhausted_all_visits,
                          is_continuation);

  number_clusters_sent_to_page_ += clusters_size;

  // Log metrics after delivering the results to the page.
  base::TimeDelta service_latency = base::TimeTicks::Now() - query_start_time;
  base::UmaHistogramTimes("History.Clusters.ServiceLatency", service_latency);
}

void QueryClustersState::UpdateUniqueRawLabels(
    const std::vector<history::Cluster>& clusters) {
  // Skip this computation when there's a search query.
  if (!query_.empty())
    return;

  for (const auto& cluster : clusters) {
    if (!cluster.raw_label)
      return;

    const auto& raw_label_value = cluster.raw_label.value();
    // Warning: N^2 algorithm below. If this ends up scaling poorly, it can be
    // optimized by adding a map that tracks which labels have been seen
    // already.
    auto it = std::ranges::find(raw_label_counts_so_far_, raw_label_value,
                                &LabelCount::first);
    if (it == raw_label_counts_so_far_.end()) {
      it = raw_label_counts_so_far_.insert(it,
                                           std::make_pair(raw_label_value, 0));
    }
    it->second++;
  }
}

}  // namespace history_clusters