File: view_demotion.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 (166 lines) | stat: -rw-r--r-- 5,959 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
// 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.

#include "components/feed/core/v2/view_demotion.h"

#include <algorithm>
#include <map>
#include <ostream>
#include <tuple>
#include <vector>

#include "base/check_op.h"
#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "components/feed/core/proto/v2/store.pb.h"
#include "components/feed/core/v2/config.h"
#include "components/feed/core/v2/feed_store.h"
#include "components/feed/core/v2/feed_stream.h"
#include "components/feed/core/v2/feedstore_util.h"
#include "components/feed/feed_feature_list.h"

namespace feed {
namespace {
base::TimeDelta kMaxViewAge = base::Hours(72);

bool IsEnabled(FeedStream& feed_stream) {
  return base::FeatureList::IsEnabled(kFeedSignedOutViewDemotion) &&
         !feed_stream.IsSignedIn();
}
}  // namespace

namespace internal {
DocViewDigest CreateDigest(std::vector<feedstore::DocView> all_views) {
  const base::Time now = base::Time::Now();
  const base::Time too_old = now - kMaxViewAge;
  DocViewDigest digest;

  // Remove all views that are too old.
  std::erase_if(all_views, [&](const feedstore::DocView& view) {
    base::Time view_time =
        feedstore::FromTimestampMillis(view.view_time_millis());
    if (view_time > now || view_time < too_old) {
      digest.old_doc_views.push_back(view);
      return true;
    }
    return false;
  });

  // Note that all_views will be sorted by the datastore key, which is prefixed
  // with the docid, and therefore we can assume that all views for a given
  // docid are adjacent in this list. We take advantage of this here.
  for (const feedstore::DocView& view : all_views) {
    if (digest.doc_view_counts.empty() ||
        digest.doc_view_counts.back().docid != view.docid()) {
      digest.doc_view_counts.push_back(DocViewCount{view.docid(), 1});
    } else {
      ++digest.doc_view_counts.back().view_count;
    }
  }

  // If there are too many recent entries, remove docids which have been viewed
  // least recently.
  if (digest.doc_view_counts.size() > GetFeedConfig().max_docviews_to_send) {
    // Get the most recent view time for each doc, sort by time and select the
    // docids to remove.
    size_t remove_count =
        digest.doc_view_counts.size() - GetFeedConfig().max_docviews_to_send;

    base::UmaHistogramCounts100(
        "ContentSuggestions.Feed.DroppedDocumentViewCount", remove_count);

    std::vector<std::pair<int64_t, uint64_t>> latest_view_to_docid;
    for (const feedstore::DocView& view : all_views) {
      if (latest_view_to_docid.empty() ||
          latest_view_to_docid.back().second != view.docid()) {
        latest_view_to_docid.emplace_back(view.view_time_millis(),
                                          view.docid());
      } else {
        auto& view_time = latest_view_to_docid.back().first;
        view_time = std::max(view_time, view.view_time_millis());
      }
    }

    std::ranges::sort(latest_view_to_docid);

    std::vector<uint64_t> docids_to_remove;
    docids_to_remove.reserve(remove_count);
    for (auto iter = latest_view_to_docid.begin();
         iter != latest_view_to_docid.begin() + remove_count; ++iter) {
      docids_to_remove.push_back(iter->second);
    }

    // Finally, remove the old docids, and add to old_doc_views.
    base::flat_set<uint64_t> docids_to_remove_set(std::move(docids_to_remove));
    std::erase_if(digest.doc_view_counts, [&](const DocViewCount& view_count) {
      return docids_to_remove_set.contains(view_count.docid);
    });
    for (const feedstore::DocView& view : all_views) {
      if (docids_to_remove_set.contains(view.docid())) {
        digest.old_doc_views.push_back(view);
      }
    }
    DCHECK_EQ(digest.doc_view_counts.size(),
              GetFeedConfig().max_docviews_to_send);
  }

  base::UmaHistogramCounts100(
      "ContentSuggestions.Feed.DocumentViewSendCount100",
      digest.doc_view_counts.size());

  base::UmaHistogramCounts1000(
      "ContentSuggestions.Feed.DocumentViewSendCount1000",
      digest.doc_view_counts.size());
  return digest;
}
}  // namespace internal

bool DocViewCount::operator==(const DocViewCount& rhs) const {
  return std::tie(docid, view_count) == std::tie(rhs.docid, rhs.view_count);
}

void ReadDocViewDigestIfEnabled(
    FeedStream& feed_stream,
    base::OnceCallback<void(DocViewDigest)> callback) {
  if (!IsEnabled(feed_stream)) {
    std::move(callback).Run({});
    return;
  }
  auto callback_wrapper = [](base::OnceCallback<void(DocViewDigest)> callback,
                             std::vector<feedstore::DocView> all_views) {
    std::move(callback).Run(internal::CreateDigest(std::move(all_views)));
  };
  feed_stream.GetStore().ReadDocViews(
      base::BindOnce(callback_wrapper, std::move(callback)));
}

void RemoveOldDocViews(const DocViewDigest& doc_view_digest,
                       FeedStore& feed_store) {
  feed_store.RemoveDocViews(doc_view_digest.old_doc_views);
}

void WriteDocViewIfEnabled(FeedStream& feed_stream, uint64_t docid) {
  if (!IsEnabled(feed_stream)) {
    return;
  }
  feed_stream.GetStore().WriteDocView(feedstore::CreateDocView(docid));
}

DocViewDigest::DocViewDigest() = default;
DocViewDigest::~DocViewDigest() = default;
DocViewDigest::DocViewDigest(const DocViewDigest&) = default;
DocViewDigest::DocViewDigest(DocViewDigest&&) = default;
DocViewDigest& DocViewDigest::operator=(const DocViewDigest&) = default;
DocViewDigest& DocViewDigest::operator=(DocViewDigest&&) = default;

std::ostream& operator<<(std::ostream& os, const DocViewCount& doc_view_count) {
  return os << "DocViewCount{docid: " << doc_view_count.docid
            << " count: " << doc_view_count.view_count << " }";
}

}  // namespace feed