Commit 6768cb5c authored by Brandon Wylie's avatar Brandon Wylie Committed by Commit Bot

[EOC] Contextual suggestions debugging reporter

This debugging reporter will cache metrics events that will be used
for debugging purposes. The reporter is currently configured to cache
5 items (controlled by a static variable BUFFER_SIZE).

Bug: 843251
Change-Id: I09a63200db01819df56474c831ad9260f75d9432
Reviewed-on: https://chromium-review.googlesource.com/1056413
Commit-Queue: Brandon Wylie <wylieb@chromium.org>
Reviewed-by: default avatarPatrick Noland <pnoland@chromium.org>
Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561583}
parent d4eaf259
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "components/image_fetcher/core/image_fetcher_impl.h" #include "components/image_fetcher/core/image_fetcher_impl.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/ntp_snippets/contextual/contextual_content_suggestions_service.h" #include "components/ntp_snippets/contextual/contextual_content_suggestions_service.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_debugging_reporter.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_features.h" #include "components/ntp_snippets/contextual/contextual_suggestions_features.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_fetcher_impl.h" #include "components/ntp_snippets/contextual/contextual_suggestions_fetcher_impl.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_reporter.h" #include "components/ntp_snippets/contextual/contextual_suggestions_reporter.h"
...@@ -112,7 +113,9 @@ ContextualContentSuggestionsServiceFactory::BuildServiceInstanceFor( ...@@ -112,7 +113,9 @@ ContextualContentSuggestionsServiceFactory::BuildServiceInstanceFor(
request_context.get()), request_context.get()),
pref_service, contextual_suggestions_database.get()); pref_service, contextual_suggestions_database.get());
auto reporter_provider = std::make_unique< auto reporter_provider = std::make_unique<
contextual_suggestions::ContextualSuggestionsReporterProvider>(); contextual_suggestions::ContextualSuggestionsReporterProvider>(
std::make_unique<
contextual_suggestions::ContextualSuggestionsDebuggingReporter>());
auto* service = new ContextualContentSuggestionsService( auto* service = new ContextualContentSuggestionsService(
std::move(contextual_suggestions_fetcher), std::move(contextual_suggestions_fetcher),
std::move(cached_image_fetcher), std::move(cached_image_fetcher),
......
...@@ -54,6 +54,8 @@ static_library("ntp_snippets") { ...@@ -54,6 +54,8 @@ static_library("ntp_snippets") {
"contextual/contextual_suggestion.h", "contextual/contextual_suggestion.h",
"contextual/contextual_suggestions_composite_reporter.cc", "contextual/contextual_suggestions_composite_reporter.cc",
"contextual/contextual_suggestions_composite_reporter.h", "contextual/contextual_suggestions_composite_reporter.h",
"contextual/contextual_suggestions_debugging_reporter.cc",
"contextual/contextual_suggestions_debugging_reporter.h",
"contextual/contextual_suggestions_features.cc", "contextual/contextual_suggestions_features.cc",
"contextual/contextual_suggestions_features.h", "contextual/contextual_suggestions_features.h",
"contextual/contextual_suggestions_fetch.cc", "contextual/contextual_suggestions_fetch.cc",
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "components/ntp_snippets/category_info.h" #include "components/ntp_snippets/category_info.h"
#include "components/ntp_snippets/content_suggestion.h" #include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/contextual/contextual_suggestion.h" #include "components/ntp_snippets/contextual/contextual_suggestion.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_debugging_reporter.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_fetcher.h" #include "components/ntp_snippets/contextual/contextual_suggestions_fetcher.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_reporter.h" #include "components/ntp_snippets/contextual/contextual_suggestions_reporter.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_test_utils.h" #include "components/ntp_snippets/contextual/contextual_suggestions_test_utils.h"
...@@ -107,8 +108,11 @@ class ContextualContentSuggestionsServiceTest : public testing::Test { ...@@ -107,8 +108,11 @@ class ContextualContentSuggestionsServiceTest : public testing::Test {
std::unique_ptr<FakeContextualSuggestionsFetcher> fetcher = std::unique_ptr<FakeContextualSuggestionsFetcher> fetcher =
std::make_unique<FakeContextualSuggestionsFetcher>(); std::make_unique<FakeContextualSuggestionsFetcher>();
fetcher_ = fetcher.get(); fetcher_ = fetcher.get();
auto debugging_reporter = std::make_unique<
contextual_suggestions::ContextualSuggestionsDebuggingReporter>();
auto reporter_provider = std::make_unique< auto reporter_provider = std::make_unique<
contextual_suggestions::ContextualSuggestionsReporterProvider>(); contextual_suggestions::ContextualSuggestionsReporterProvider>(
std::move(debugging_reporter));
source_ = std::make_unique<ContextualContentSuggestionsService>( source_ = std::make_unique<ContextualContentSuggestionsService>(
std::move(fetcher), std::move(fetcher),
std::make_unique<FakeCachedImageFetcher>(&pref_service_), std::make_unique<FakeCachedImageFetcher>(&pref_service_),
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ntp_snippets/contextual/contextual_suggestions_debugging_reporter.h"
#include <algorithm>
namespace {
static constexpr int CACHE_SIZE = 5;
} // namespace
namespace contextual_suggestions {
ContextualSuggestionsDebuggingReporter::
ContextualSuggestionsDebuggingReporter() = default;
ContextualSuggestionsDebuggingReporter::
~ContextualSuggestionsDebuggingReporter() = default;
const std::vector<ContextualSuggestionsDebuggingEvent>&
ContextualSuggestionsDebuggingReporter::GetEvents() const {
return events_;
}
void ContextualSuggestionsDebuggingReporter::SetupForPage(
const std::string& url,
ukm::SourceId source_id) {
current_event_ = ContextualSuggestionsDebuggingEvent();
current_event_.url = url;
}
void ContextualSuggestionsDebuggingReporter::RecordEvent(
ContextualSuggestionsEvent event) {
switch (event) {
case FETCH_DELAYED:
case FETCH_REQUESTED:
case FETCH_ERROR:
case FETCH_SERVER_BUSY:
case FETCH_BELOW_THRESHOLD:
case FETCH_EMPTY:
case FETCH_COMPLETED:
return;
case UI_PEEK_REVERSE_SCROLL:
current_event_.sheet_peeked = true;
return;
case UI_OPENED:
current_event_.sheet_opened = true;
return;
case UI_DISMISSED_WITHOUT_OPEN:
case UI_DISMISSED_AFTER_OPEN:
current_event_.sheet_closed = true;
return;
case SUGGESTION_DOWNLOADED:
current_event_.any_suggestion_downloaded = true;
return;
case SUGGESTION_CLICKED:
current_event_.any_suggestion_taken = true;
return;
case UI_CLOSED_OBSOLETE:
NOTREACHED() << "Obsolete event, do not use!";
return;
default:
NOTREACHED() << "Unexpected event, not correctly handled!";
return;
}
}
void ContextualSuggestionsDebuggingReporter::Flush() {
// Check if we've already sent an event with this url to the cache. If so,
// remove it before adding another one.
const std::string current_url = current_event_.url;
std::remove_if(events_.begin(), events_.end(),
[current_url](ContextualSuggestionsDebuggingEvent event) {
return current_url == event.url;
});
events_.push_back(current_event_);
// If the cache is too large, then remove the least recently used.
if (events_.size() > CACHE_SIZE)
events_.erase(events_.begin());
}
} // namespace contextual_suggestions
\ No newline at end of file
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_NTP_SNIPPETS_CONTEXTUAL_CONTEXTUAL_SUGGESTIONS_DEBUGGING_REPORTER_H_
#define COMPONENTS_NTP_SNIPPETS_CONTEXTUAL_CONTEXTUAL_SUGGESTIONS_DEBUGGING_REPORTER_H_
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_reporter.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
namespace contextual_suggestions {
// Models the events being reported to the debugging reporter.
// TODO(wylieb): Timestamp events as they happen.
struct ContextualSuggestionsDebuggingEvent {
// Name of the url.
std::string url;
// Whether any suggestion was downloaded.
bool any_suggestion_downloaded = false;
// Whether any suggestion was taken.
bool any_suggestion_taken = false;
// Whether the sheet was closed.
bool sheet_closed = false;
// Whether the sheet was ever opened.
bool sheet_opened = false;
// Whether the sheet ever peeked.
bool sheet_peeked = false;
};
// Reporter specialized for caching information for debugging purposes.
class ContextualSuggestionsDebuggingReporter
: public ContextualSuggestionsReporter {
public:
ContextualSuggestionsDebuggingReporter();
~ContextualSuggestionsDebuggingReporter() override;
// Get all events currently in the buffer.
const std::vector<ContextualSuggestionsDebuggingEvent>& GetEvents() const;
// ContextualSuggestionsReporter
void SetupForPage(const std::string& url, ukm::SourceId source_id) override;
void RecordEvent(
contextual_suggestions::ContextualSuggestionsEvent event) override;
void Flush() override;
private:
std::vector<ContextualSuggestionsDebuggingEvent> events_;
ContextualSuggestionsDebuggingEvent current_event_;
DISALLOW_COPY_AND_ASSIGN(ContextualSuggestionsDebuggingReporter);
};
} // namespace contextual_suggestions
#endif // COMPONENTS_NTP_SNIPPETS_CONTEXTUAL_CONTEXTUAL_SUGGESTIONS_DEBUGGING_REPORTER_H_
...@@ -6,12 +6,14 @@ ...@@ -6,12 +6,14 @@
#include "base/debug/stack_trace.h" #include "base/debug/stack_trace.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_composite_reporter.h" #include "components/ntp_snippets/contextual/contextual_suggestions_composite_reporter.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_debugging_reporter.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_metrics_reporter.h" #include "components/ntp_snippets/contextual/contextual_suggestions_metrics_reporter.h"
namespace contextual_suggestions { namespace contextual_suggestions {
ContextualSuggestionsReporterProvider::ContextualSuggestionsReporterProvider() = ContextualSuggestionsReporterProvider::ContextualSuggestionsReporterProvider(
default; std::unique_ptr<ContextualSuggestionsDebuggingReporter> debugging_reporter)
: debugging_reporter_(std::move(debugging_reporter)) {}
ContextualSuggestionsReporterProvider:: ContextualSuggestionsReporterProvider::
~ContextualSuggestionsReporterProvider() = default; ~ContextualSuggestionsReporterProvider() = default;
...@@ -22,6 +24,7 @@ ContextualSuggestionsReporterProvider::CreateReporter() { ...@@ -22,6 +24,7 @@ ContextualSuggestionsReporterProvider::CreateReporter() {
std::make_unique<ContextualSuggestionsCompositeReporter>(); std::make_unique<ContextualSuggestionsCompositeReporter>();
reporter->AddOwnedReporter( reporter->AddOwnedReporter(
std::make_unique<ContextualSuggestionsMetricsReporter>()); std::make_unique<ContextualSuggestionsMetricsReporter>());
reporter->AddRawReporter(debugging_reporter_.get());
return reporter; return reporter;
} }
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
namespace contextual_suggestions { namespace contextual_suggestions {
class ContextualSuggestionsDebuggingReporter;
class ContextualSuggestionsReporter; class ContextualSuggestionsReporter;
// Class producing |ContextualSuggestionsReporter| instances. It enables // Class producing |ContextualSuggestionsReporter| instances. It enables
...@@ -21,11 +22,19 @@ class ContextualSuggestionsReporter; ...@@ -21,11 +22,19 @@ class ContextualSuggestionsReporter;
// initialize them. // initialize them.
class ContextualSuggestionsReporterProvider { class ContextualSuggestionsReporterProvider {
public: public:
ContextualSuggestionsReporterProvider(); explicit ContextualSuggestionsReporterProvider(
std::unique_ptr<ContextualSuggestionsDebuggingReporter>
debugging_reporter);
virtual ~ContextualSuggestionsReporterProvider(); virtual ~ContextualSuggestionsReporterProvider();
virtual std::unique_ptr<ContextualSuggestionsReporter> CreateReporter(); virtual std::unique_ptr<ContextualSuggestionsReporter> CreateReporter();
private:
// The debugging reporter is shared between instances of top-level reporter.
// Since multiple objects need a reference to this, it's kept as a unique
// pointer, and the raw pointer is given out to sub reporters.
std::unique_ptr<ContextualSuggestionsDebuggingReporter> debugging_reporter_;
DISALLOW_COPY_AND_ASSIGN(ContextualSuggestionsReporterProvider); DISALLOW_COPY_AND_ASSIGN(ContextualSuggestionsReporterProvider);
}; };
......
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