Bug 1615985 - Collect number of calendars with types and readonly status. r=mkmelin,pmorris,darktrojan a=darktrojan
authorPing Chen <remotenonsense@gmail.com>
Mon, 08 Jun 2020 13:31:08 +0300
changeset 39362 3d89c61c63024587e9832734b488cb8e97709d22
parent 39361 0525eca6f121e05cb4df0a5f7e0786188c0c1334
child 39363 c19b6d0fdf3a393922362297f305aee5f5e0109a
push id402
push userclokep@gmail.com
push dateMon, 29 Jun 2020 20:48:04 +0000
reviewersmkmelin, pmorris, darktrojan, darktrojan
bugs1615985
Bug 1615985 - Collect number of calendars with types and readonly status. r=mkmelin,pmorris,darktrojan a=darktrojan
calendar/base/content/calendar-management.js
calendar/test/browser/browser.ini
calendar/test/browser/browser_calendarTelemetry.js
mail/components/telemetry/Scalars.yaml
--- a/calendar/base/content/calendar-management.js
+++ b/calendar/base/content/calendar-management.js
@@ -119,20 +119,22 @@ function loadCalendarManager() {
       }
     }
   } else {
     initHomeCalendar();
   }
 
   let calendarManager = cal.getCalendarManager();
 
-  for (let calendar of sortCalendarArray(cal.getCalendarManager().getCalendars())) {
+  for (let calendar of sortCalendarArray(calendarManager.getCalendars())) {
     addCalendarItem(calendar);
   }
 
+  reportCalendars();
+
   function addCalendarItem(calendar) {
     let item = document.createXULElement("richlistitem");
     item.searchLabel = calendar.name;
     item.setAttribute("calendar-id", calendar.id);
     if (calendar.getProperty("disabled")) {
       item.setAttribute("calendar-disabled", "true");
     }
     if (!Components.isSuccessCode(calendar.getProperty("currentStatus"))) {
@@ -397,16 +399,42 @@ function loadCalendarManager() {
       saveSortOrder();
     },
     onCalendarDeleting(calendar) {},
   };
   calendarManager.addObserver(calendarList._calendarManagerObserver);
 }
 
 /**
+ * A telemetry probe to report calendar count and read only calendar count.
+ */
+function reportCalendars() {
+  let telemetryReport = {};
+
+  for (let calendar of cal.getCalendarManager().getCalendars()) {
+    if (!telemetryReport[calendar.type]) {
+      telemetryReport[calendar.type] = { count: 0, readOnlyCount: 0 };
+    }
+    telemetryReport[calendar.type].count++;
+    if (calendar.readOnly) {
+      telemetryReport[calendar.type].readOnlyCount++;
+    }
+  }
+
+  for (let [type, { count, readOnlyCount }] of Object.entries(telemetryReport)) {
+    Services.telemetry.keyedScalarSet("tb.calendar.calendar_count", type.toLowerCase(), count);
+    Services.telemetry.keyedScalarSet(
+      "tb.calendar.read_only_calendar_count",
+      type.toLowerCase(),
+      readOnlyCount
+    );
+  }
+}
+
+/**
  * Creates the initial "Home" calendar if no calendar exists.
  */
 function initHomeCalendar() {
   let calMgr = cal.getCalendarManager();
   let composite = cal.view.getCompositeCalendar(window);
   let url = Services.io.newURI("moz-storage-calendar://");
   let homeCalendar = calMgr.createCalendar("storage", url);
   homeCalendar.name = cal.l10n.getCalString("homeCalendarName");
--- a/calendar/test/browser/browser.ini
+++ b/calendar/test/browser/browser.ini
@@ -7,16 +7,17 @@ prefs =
   mail.winsearch.firstRunDone=true
   mailnews.start_page.override_url=about:blank
   mailnews.start_page.url=about:blank
 subsuite = thunderbird
 support-files = data/**
 
 [browser_basicFunctionality.js]
 [browser_calendarList.js]
+[browser_calendarTelemetry.js]
 [browser_eventDisplay.js]
 [browser_import.js]
 [browser_localICS.js]
 [browser_tabs.js]
 [browser_taskDisplay.js]
 [browser_todayPane.js]
 skip-if = debug
 [browser_todayPane_visibility.js]
new file mode 100644
--- /dev/null
+++ b/calendar/test/browser/browser_calendarTelemetry.js
@@ -0,0 +1,48 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/* global reportCalendars */
+
+/**
+ * Test telemetry related to calendar.
+ */
+
+let { TelemetryTestUtils } = ChromeUtils.import("resource://testing-common/TelemetryTestUtils.jsm");
+
+/**
+ * Check that we're counting calendars and read only calendars.
+ */
+add_task(async function test_calendar_count() {
+  Services.telemetry.clearScalars();
+
+  let manager = cal.getCalendarManager();
+  let uri = Services.io.newURI("moz-memory-calendar://");
+  let calendars = manager.getCalendars();
+  for (let i = 1; i <= 3; i++) {
+    calendars[i] = manager.createCalendar("memory", uri);
+    calendars[i].name = `Mochitest ${i}`;
+    if (i === 1 || i === 3) {
+      calendars[i].readOnly = true;
+    }
+    manager.registerCalendar(calendars[i]);
+  }
+
+  reportCalendars();
+
+  let scalars = TelemetryTestUtils.getProcessScalars("parent", true);
+  Assert.equal(
+    scalars["tb.calendar.calendar_count"].memory,
+    3,
+    "Count of calendars must be correct."
+  );
+  Assert.equal(
+    scalars["tb.calendar.read_only_calendar_count"].memory,
+    2,
+    "Count of readonly calendars must be correct."
+  );
+
+  // Clean up.
+  for (let i = 1; i <= 3; i++) {
+    manager.removeCalendar(calendars[i]);
+  }
+});
--- a/mail/components/telemetry/Scalars.yaml
+++ b/mail/components/telemetry/Scalars.yaml
@@ -103,16 +103,45 @@ tb.filelink:
       - 'thunderbird'
     keyed: true
     kind: uint
     notification_emails:
       - "telemetry-client-dev@thunderbird.net"
     record_in_processes:
       - 'main'
 
+tb.calendar:
+  calendar_count:
+    bug_numbers:
+      - 1615985
+    description: How many calendars were set up, keyed by caldendar type.
+    expires: never
+    products:
+      - 'thunderbird'
+    keyed: true
+    kind: uint
+    notification_emails:
+      - "telemetry-client-dev@thunderbird.net"
+    record_in_processes:
+      - 'main'
+
+  read_only_calendar_count:
+    bug_numbers:
+      - 1615985
+    description: How many read only calendars were set up, keyed by caldendar type.
+    expires: never
+    products:
+      - 'thunderbird'
+    keyed: true
+    kind: uint
+    notification_emails:
+      - "telemetry-client-dev@thunderbird.net"
+    record_in_processes:
+      - 'main'
+
   ignored:
     bug_numbers:
       - 1615984
     description: How many times filelink suggestion are ignored.
     expires: never
     products:
       - 'thunderbird'
     kind: uint