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
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336

ash / system / time / calendar_metrics.cc [blame]

// 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 "ash/system/time/calendar_metrics.h"

#include "ash/public/cpp/metrics_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "google_apis/common/api_error_codes.h"
#include "ui/compositor/animation_throughput_reporter.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/events/event.h"
#include "ui/views/view.h"

namespace ash {

namespace calendar_metrics {

namespace {

constexpr char kCalendarViewShowSourcePrefix[] = "Ash.Calendar.ShowSource.";
constexpr char kCalendarDateCellActivated[] = "Ash.Calendar.DateCell.Activated";
constexpr char kCalendarEventListItemActivated[] =
    "Ash.Calendar.EventListItem.Activated";
constexpr char kCalendarMonthDownArrowButtonActivated[] =
    "Ash.Calendar.MonthDownArrowButton.Activated";
constexpr char kCalendarMonthUpArrowButtonActivated[] =
    "Ash.Calendar.MonthUpArrowButton.Activated";
constexpr char kCalendarMonthDwellTime[] = "Ash.Calendar.MonthDwellTime";
constexpr char kCalendarScrollSource[] = "Ash.Calendar.ScrollSource";
constexpr char kCalendarKeyboardNavigation[] =
    "Ash.Calendar.KeyboardNavigation";
constexpr char kCalendarEventListItemInUpNextPressed[] =
    "Ash.Calendar.UpNextView.EventListItem.Pressed";
constexpr char kCalendarUpNextEventDisplayedCount[] =
    "Ash.Calendar.UpNextView.EventDisplayedCount";
constexpr char kCalendarEventListItemJoinButtonPressed[] =
    "Ash.Calendar.EventListView.JoinMeetingButton.Pressed";
constexpr char kCalendarUpNextJoinButtonPressed[] =
    "Ash.Calendar.UpNextView.JoinMeetingButton.Pressed";
constexpr char kCalendarEventListEventDisplayedCount[] =
    "Ash.Calendar.EventListViewJelly.EventDisplayedCount";
constexpr char kCalendarEventsDisplayedToUser[] =
    "Ash.Calendar.EventsDisplayedToUser";
constexpr char kCalendarUserAction[] = "Ash.Calendar.UserAction";
constexpr char kCalendarWebUiOpened[] =
    "Ash.Calendar.UserActionToOpenCalendarWebUi";
constexpr char kCalendarFetchCalendarsFetchDuration[] =
    "Ash.Calendar.FetchCalendars.FetchDuration";
constexpr char kCalendarFetchCalendarsResult[] =
    "Ash.Calendar.FetchCalendars.Result";
constexpr char kCalendarFetchCalendarsTimeout[] =
    "Ash.Calendar.FetchCalendars.Timeout";
constexpr char kCalendarFetchEventsFetchDuration[] =
    "Ash.Calendar.FetchEvents.FetchDuration";
constexpr char kCalendarFetchEventsResult[] = "Ash.Calendar.FetchEvents.Result";
constexpr char kCalendarFetchEventsSingleMonthSize[] =
    "Ash.Calendar.FetchEvents.SingleMonthSize";
constexpr char kCalendarFetchEventsTimeout[] =
    "Ash.Calendar.FetchEvents.Timeout";
constexpr char kCalendarFetchEventsTotalCacheSizeMonths[] =
    "Ash.Calendar.FetchEvents.TotalCacheSizeMonths";
constexpr char kCalendarFetchEventsTotalFetchDuration[] =
    "Ash.Calendar.FetchEvents.TotalFetchDuration";
constexpr char kCalendarFetchCalendarsTotalSelectedCalendars[] =
    "Ash.Calendar.FetchCalendars.TotalSelectedCalendars";
constexpr char kCalendarTimeToSeeTodaysEventDots[] =
    "Ash.Calendar.TimeToSeeTodaysEventDots";
constexpr char kCalendarTimeToSeeTodaysEventDotsForMultiCalendar[] =
    "Ash.Calendar.TimeToSeeTodaysEventDotsForMultiCalendar";

// This enum is used in histograms. These values are persisted to logs. Entries
// should not be renumbered and numeric values should never be reused, only add
// at the end and. Also remember to update the CalendarUserAction enum in
// tools/metrics/histograms/metadata/ash/enums.xml.
enum class CalendarUserActionType {
  kDateCellActivated = 0,
  kMonthDownArrowActivated = 1,
  kMonthUpArrowActivated = 2,
  kScrolled = 3,
  kKeyboardNavigation = 4,
  kEventListItemPressed = 5,
  kUpNextItemPressed = 6,
  kEventListItemJoinButtonPressed = 7,
  kUpNextJoinButtonPressed = 8,
  kResetToTodayPressed = 9,
  kTodaysEventsInUpNextPressed = 10,
  kScrollInUpNext = 11,
  kCalendarLaunchedFromEmptyEventList = 12,
  kEventListClosed = 13,
  kSettingsButtonPressed = 14,
  kMaxValue = kSettingsButtonPressed,
};

bool ActionOpensCalendarWebUi(CalendarUserActionType action) {
  switch (action) {
    case CalendarUserActionType::kDateCellActivated:
    case CalendarUserActionType::kMonthDownArrowActivated:
    case CalendarUserActionType::kMonthUpArrowActivated:
    case CalendarUserActionType::kScrolled:
    case CalendarUserActionType::kKeyboardNavigation:
    case CalendarUserActionType::kResetToTodayPressed:
    case CalendarUserActionType::kTodaysEventsInUpNextPressed:
    case CalendarUserActionType::kScrollInUpNext:
    case CalendarUserActionType::kEventListClosed:
    case CalendarUserActionType::kSettingsButtonPressed:
      return false;
    case CalendarUserActionType::kEventListItemPressed:
    case CalendarUserActionType::kUpNextItemPressed:
    case CalendarUserActionType::kEventListItemJoinButtonPressed:
    case CalendarUserActionType::kUpNextJoinButtonPressed:
    case CalendarUserActionType::kCalendarLaunchedFromEmptyEventList:
      return true;
  }
}

void RecordCalendarUserAction(CalendarUserActionType action) {
  if (ActionOpensCalendarWebUi(action)) {
    base::UmaHistogramEnumeration(kCalendarWebUiOpened, action);
  }
  base::UmaHistogramEnumeration(kCalendarUserAction, action);
}

}  // namespace

CalendarEventSource GetEventType(const ui::Event& event) {
  if (event.IsGestureEvent())
    return CalendarEventSource::kTap;

  if (event.IsMouseEvent())
    return CalendarEventSource::kClick;

  if (event.IsKeyEvent())
    return CalendarEventSource::kKeyboard;

  if (event.IsTouchEvent())
    return CalendarEventSource::kStylus;

  NOTREACHED();
}

void RecordCalendarShowMetrics(
    CalendarViewShowSource show_source,
    calendar_metrics::CalendarEventSource event_source) {
  std::string histogram_name = kCalendarViewShowSourcePrefix;
  switch (show_source) {
    case CalendarViewShowSource::kDateView:
      histogram_name += "DateView";
      break;
    case CalendarViewShowSource::kTimeView:
      histogram_name += "TimeView";
      break;
    case CalendarViewShowSource::kAccelerator:
      histogram_name += "Keyboard";
      break;
  }

  base::UmaHistogramEnumeration(histogram_name, event_source);
}

void RecordCalendarDateCellActivated(const ui::Event& event) {
  RecordCalendarUserAction(CalendarUserActionType::kDateCellActivated);

  base::UmaHistogramEnumeration(kCalendarDateCellActivated,
                                GetEventType(event));
}

void RecordMonthArrowButtonActivated(bool up, const ui::Event& event) {
  RecordCalendarUserAction(
      up ? CalendarUserActionType::kMonthUpArrowActivated
         : CalendarUserActionType::kMonthDownArrowActivated);

  base::UmaHistogramEnumeration(up ? kCalendarMonthUpArrowButtonActivated
                                   : kCalendarMonthDownArrowButtonActivated,
                                GetEventType(event));
}

void RecordEventListItemActivated(const ui::Event& event) {
  RecordCalendarUserAction(CalendarUserActionType::kEventListItemPressed);

  base::UmaHistogramEnumeration(kCalendarEventListItemActivated,
                                GetEventType(event));
}

void RecordEventListForTodayActivated() {
  RecordCalendarUserAction(
      CalendarUserActionType::kTodaysEventsInUpNextPressed);
}

void RecordMonthDwellTime(const base::TimeDelta& dwell_time) {
  base::UmaHistogramMediumTimes(kCalendarMonthDwellTime, dwell_time);
}

void RecordResetToTodayPressed() {
  RecordCalendarUserAction(CalendarUserActionType::kResetToTodayPressed);
}

void RecordScrollSource(CalendarViewScrollSource source) {
  RecordCalendarUserAction(CalendarUserActionType::kScrolled);

  base::UmaHistogramEnumeration(kCalendarScrollSource, source);
}

ui::AnimationThroughputReporter CreateAnimationReporter(
    views::View* view,
    const std::string& animation_histogram_name) {
  // TODO(crbug.com/1297376): Add unit tests for animation metrics recording.
  return ui::AnimationThroughputReporter(
      view->layer()->GetAnimator(),
      metrics_util::ForSmoothnessV3(base::BindRepeating(
          [](const std::string& animation_histogram_name, int smoothness) {
            base::UmaHistogramPercentage(animation_histogram_name, smoothness);
          },
          animation_histogram_name)));
}

void RecordCalendarKeyboardNavigation(
    const CalendarKeyboardNavigationSource key_source) {
  RecordCalendarUserAction(CalendarUserActionType::kKeyboardNavigation);

  base::UmaHistogramEnumeration(kCalendarKeyboardNavigation, key_source);
}

void RecordEventListItemInUpNextLaunched(const ui::Event& event) {
  RecordCalendarUserAction(CalendarUserActionType::kUpNextItemPressed);

  base::UmaHistogramEnumeration(kCalendarEventListItemInUpNextPressed,
                                GetEventType(event));
}

void RecordUpNextEventCount(const int event_count) {
  base::UmaHistogramCounts100(kCalendarUpNextEventDisplayedCount, event_count);
}

void RecordJoinButtonPressedFromEventListView(const ui::Event& event) {
  RecordCalendarUserAction(
      CalendarUserActionType::kEventListItemJoinButtonPressed);

  base::UmaHistogramEnumeration(kCalendarEventListItemJoinButtonPressed,
                                GetEventType(event));
}

void RecordJoinButtonPressedFromUpNextView(const ui::Event& event) {
  RecordCalendarUserAction(CalendarUserActionType::kUpNextJoinButtonPressed);

  base::UmaHistogramEnumeration(kCalendarUpNextJoinButtonPressed,
                                GetEventType(event));
}

void RecordEventListEventCount(const int event_count) {
  base::UmaHistogramCounts100(kCalendarEventListEventDisplayedCount,
                              event_count);
}

void RecordEventsDisplayedToUser() {
  base::UmaHistogramBoolean(kCalendarEventsDisplayedToUser, true);
}

void RecordScrollEventInUpNext() {
  RecordCalendarUserAction(CalendarUserActionType::kScrollInUpNext);
}

void RecordCalendarLaunchedFromEmptyEventList() {
  RecordCalendarUserAction(
      CalendarUserActionType::kCalendarLaunchedFromEmptyEventList);
}

void RecordEventListClosed() {
  RecordCalendarUserAction(CalendarUserActionType::kEventListClosed);
}

void RecordSettingsButtonPressed() {
  RecordCalendarUserAction(CalendarUserActionType::kSettingsButtonPressed);
}

void RecordCalendarListFetchDuration(const base::TimeDelta fetch_duration) {
  base::UmaHistogramTimes(kCalendarFetchCalendarsFetchDuration, fetch_duration);
}

void RecordCalendarListFetchErrorCode(google_apis::ApiErrorCode error) {
  base::UmaHistogramSparse(kCalendarFetchCalendarsResult, error);
}

void RecordCalendarListFetchTimeout(bool fetch_timed_out) {
  base::UmaHistogramBoolean(kCalendarFetchCalendarsTimeout, fetch_timed_out);
}

void RecordEventListFetchDuration(const base::TimeDelta fetch_duration) {
  base::UmaHistogramTimes(kCalendarFetchEventsFetchDuration, fetch_duration);
}

void RecordEventListFetchErrorCode(google_apis::ApiErrorCode error) {
  base::UmaHistogramSparse(kCalendarFetchEventsResult, error);
}

void RecordEventListFetchTimeout(bool fetch_timed_out) {
  base::UmaHistogramBoolean(kCalendarFetchEventsTimeout, fetch_timed_out);
}

void RecordEventListFetchesTotalDuration(const base::TimeDelta fetch_duration) {
  base::UmaHistogramTimes(kCalendarFetchEventsTotalFetchDuration,
                          fetch_duration);
}

void RecordSingleMonthSizeInBytes(size_t single_month_cache_size) {
  base::UmaHistogramCounts1M(kCalendarFetchEventsSingleMonthSize,
                             single_month_cache_size);
}

void RecordTotalEventsCacheSizeInMonths(unsigned int events_cache_size) {
  base::UmaHistogramCounts100000(kCalendarFetchEventsTotalCacheSizeMonths,
                                 events_cache_size);
}

void RecordTotalSelectedCalendars(unsigned int selected_calendars) {
  base::UmaHistogramCounts100000(kCalendarFetchCalendarsTotalSelectedCalendars,
                                 selected_calendars);
}

void RecordTimeToSeeTodaysEventDots(const base::TimeDelta time_elapsed,
                                    bool multi_calendar_enabled) {
  if (multi_calendar_enabled) {
    base::UmaHistogramMediumTimes(
        kCalendarTimeToSeeTodaysEventDotsForMultiCalendar, time_elapsed);
  } else {
    base::UmaHistogramMediumTimes(kCalendarTimeToSeeTodaysEventDots,
                                  time_elapsed);
  }
}

}  // namespace calendar_metrics

}  // namespace ash