Commit e83383e0 authored by Becca Hughes's avatar Becca Hughes Committed by Commit Bot

[Media Feeds] Add aggregate watchtime histogram

Add a histogram to collect aggregate watchtime for
all media feeds so we can see the distribution of
watchtimes.

BUG=1110373

Change-Id: I472cff204e299efabe9f0b90949b14ca3d0fd912
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2432590Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarTommy Steimel <steimel@chromium.org>
Commit-Queue: Becca Hughes <beccahughes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#811817}
parent ced7bf89
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/metrics/histogram_functions.h" #include "base/metrics/histogram_functions.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/task/post_task.h"
#include "base/task/task_traits.h" #include "base/task/task_traits.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "base/time/clock.h" #include "base/time/clock.h"
...@@ -137,6 +138,9 @@ class CookieChangeListener : public network::mojom::CookieChangeListener { ...@@ -137,6 +138,9 @@ class CookieChangeListener : public network::mojom::CookieChangeListener {
} // namespace } // namespace
const char MediaFeedsService::kAggregateWatchtimeHistogramName[] =
"Media.Feeds.AggregateWatchtime";
const char MediaFeedsService::kSafeSearchResultHistogramName[] = const char MediaFeedsService::kSafeSearchResultHistogramName[] =
"Media.Feeds.SafeSearch.Result"; "Media.Feeds.SafeSearch.Result";
...@@ -175,6 +179,20 @@ MediaFeedsService::MediaFeedsService(Profile* profile) ...@@ -175,6 +179,20 @@ MediaFeedsService::MediaFeedsService(Profile* profile)
base::BindOnce(&MediaFeedsService::FetchTopMediaFeeds, base::BindOnce(&MediaFeedsService::FetchTopMediaFeeds,
weak_factory_.GetWeakPtr(), base::OnceClosure())); weak_factory_.GetWeakPtr(), base::OnceClosure()));
} }
// Wrapping in PostTask is needed to avoid a crash in the tests.
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&MediaFeedsService::RecordFeedWatchtimes,
weak_factory_.GetWeakPtr()));
}
void MediaFeedsService::RecordFeedWatchtimes() {
GetMediaHistoryService()->GetMediaFeeds(
media_history::MediaHistoryKeyedService::GetMediaFeedsRequest::
CreateTopFeedsForFetch(std::numeric_limits<unsigned>::max(),
base::TimeDelta()),
base::BindOnce(&MediaFeedsService::OnGotFeedsForMetrics,
weak_factory_.GetWeakPtr()));
} }
// static // static
...@@ -677,4 +695,13 @@ void MediaFeedsService::EnsureCookieObserver() { ...@@ -677,4 +695,13 @@ void MediaFeedsService::EnsureCookieObserver() {
base::Unretained(this))); base::Unretained(this)));
} }
void MediaFeedsService::OnGotFeedsForMetrics(
std::vector<media_feeds::mojom::MediaFeedPtr> feeds) {
for (const auto& feed : feeds) {
base::UmaHistogramCustomTimes(kAggregateWatchtimeHistogramName,
*feed->aggregate_watchtime, base::TimeDelta(),
base::TimeDelta::FromHours(1), 60);
}
}
} // namespace media_feeds } // namespace media_feeds
...@@ -43,6 +43,7 @@ class CookieChangeListener; ...@@ -43,6 +43,7 @@ class CookieChangeListener;
class MediaFeedsService : public KeyedService { class MediaFeedsService : public KeyedService {
public: public:
static const char kAggregateWatchtimeHistogramName[];
static const char kSafeSearchResultHistogramName[]; static const char kSafeSearchResultHistogramName[];
// Time to wait between background fetch delayed tasks. // Time to wait between background fetch delayed tasks.
...@@ -114,6 +115,8 @@ class MediaFeedsService : public KeyedService { ...@@ -114,6 +115,8 @@ class MediaFeedsService : public KeyedService {
void EnsureCookieObserver(); void EnsureCookieObserver();
void RecordFeedWatchtimes();
private: private:
friend class MediaFeedsServiceTest; friend class MediaFeedsServiceTest;
...@@ -170,6 +173,9 @@ class MediaFeedsService : public KeyedService { ...@@ -170,6 +173,9 @@ class MediaFeedsService : public KeyedService {
void OnDiscoveredFeed(); void OnDiscoveredFeed();
void OnGotFeedsForMetrics(
std::vector<media_feeds::mojom::MediaFeedPtr> feeds);
// Settings related to fetching a feed in the background. // Settings related to fetching a feed in the background.
struct BackgroundFetchFeedSettings { struct BackgroundFetchFeedSettings {
// Whether this feed should be fetched now. // Whether this feed should be fetched now.
......
...@@ -2410,4 +2410,28 @@ TEST_F(MediaFeedsServiceTest, FetchTopMediaFeeds_DisableAutoSelection) { ...@@ -2410,4 +2410,28 @@ TEST_F(MediaFeedsServiceTest, FetchTopMediaFeeds_DisableAutoSelection) {
MediaFeedsFetcher::kFetchSizeKbHistogramName, 15, 1); MediaFeedsFetcher::kFetchSizeKbHistogramName, 15, 1);
} }
TEST_F(MediaFeedsServiceTest, AggregateWatchtimeHistogram) {
base::HistogramTester histogram_tester;
task_environment()->RunUntilIdle();
const GURL feed_url("https://www.google.com/feed");
GetMediaFeedsService()->DiscoverMediaFeed(feed_url);
WaitForDB();
content::MediaPlayerWatchTime watch_time(feed_url, feed_url.GetOrigin(),
base::TimeDelta::FromMinutes(30),
base::TimeDelta(), true, true);
GetMediaHistoryService()->SavePlayback(watch_time);
WaitForDB();
GetMediaFeedsService()->RecordFeedWatchtimes();
WaitForDB();
histogram_tester.ExpectUniqueTimeSample(
MediaFeedsService::kAggregateWatchtimeHistogramName,
base::TimeDelta::FromMinutes(30), 1);
}
} // namespace media_feeds } // namespace media_feeds
...@@ -83,6 +83,9 @@ struct MediaFeed { ...@@ -83,6 +83,9 @@ struct MediaFeed {
// The result of safe search checking this media feed. // The result of safe search checking this media feed.
SafeSearchResult safe_search_result; SafeSearchResult safe_search_result;
// The aggregate watchtime for the media feed.
mojo_base.mojom.TimeDelta? aggregate_watchtime;
}; };
// Contains details about the user signed into the website. // Contains details about the user signed into the website.
......
...@@ -245,6 +245,7 @@ std::vector<media_feeds::mojom::MediaFeedPtr> MediaHistoryFeedsTable::GetRows( ...@@ -245,6 +245,7 @@ std::vector<media_feeds::mojom::MediaFeedPtr> MediaHistoryFeedsTable::GetRows(
// LEFT JOIN mediaFeed. This means there should be a row for each origin // LEFT JOIN mediaFeed. This means there should be a row for each origin
// and if there is a media feed that will be included. // and if there is a media feed that will be included.
sql.push_back( sql.push_back(
",origin.aggregate_watchtime_audio_video_s "
"FROM origin " "FROM origin "
"LEFT JOIN mediaFeed " "LEFT JOIN mediaFeed "
"ON origin.id = mediaFeed.origin_id"); "ON origin.id = mediaFeed.origin_id");
...@@ -448,6 +449,11 @@ std::vector<media_feeds::mojom::MediaFeedPtr> MediaHistoryFeedsTable::GetRows( ...@@ -448,6 +449,11 @@ std::vector<media_feeds::mojom::MediaFeedPtr> MediaHistoryFeedsTable::GetRows(
feed->reset_token = ProtoToUnguessableToken(token); feed->reset_token = ProtoToUnguessableToken(token);
} }
if (top_feeds) {
feed->aggregate_watchtime =
base::TimeDelta::FromSeconds(statement.ColumnInt64(20));
}
feeds.push_back(std::move(feed)); feeds.push_back(std::move(feed));
// If we are returning top feeds then we should apply a limit here. // If we are returning top feeds then we should apply a limit here.
......
...@@ -1751,6 +1751,17 @@ reviews. Googlers can read more about this at go/gwsq-gerrit. ...@@ -1751,6 +1751,17 @@ reviews. Googlers can read more about this at go/gwsq-gerrit.
</summary> </summary>
</histogram> </histogram>
<histogram name="Media.Feeds.AggregateWatchtime" units="ms"
expires_after="2021-06-07">
<owner>beccahughes@chromium.org</owner>
<owner>media-dev@chromium.org</owner>
<summary>
Records the aggregate watchtime for all Media Feeds on startup. The
aggregate watchtime is the total watchtime for a website recorded across
sessions. It is also cleared every 90 days.
</summary>
</histogram>
<histogram name="Media.Feeds.Feed.ReadResult" enum="MediaFeedReadResult" <histogram name="Media.Feeds.Feed.ReadResult" enum="MediaFeedReadResult"
expires_after="2021-03-07"> expires_after="2021-03-07">
<owner>beccahughes@chromium.org</owner> <owner>beccahughes@chromium.org</owner>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment