Commit 86fed075 authored by Kuo Jen Wei's avatar Kuo Jen Wei Committed by Commit Bot

[CCA] Convert metrics.js into ES6 module.

Bug: 141518780
Test: Pass closure compiler check, tast run <DUT> 'camera.CCAUI*' on
both HALv1 and v3 devices.
Test: Manually verify validity of metrics data.

Change-Id: I2c9af053a29ac42d903bc75e4de3222f9b65fc97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1986569
Auto-Submit: Kuo Jen Wei <inker@chromium.org>
Commit-Queue: Shik Chen <shik@chromium.org>
Reviewed-by: default avatarShik Chen <shik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#728476}
parent 8b4578d6
...@@ -2,26 +2,18 @@ ...@@ -2,26 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
'use strict'; import * as state from './state.js';
import {Mode,
/** Resolution, // eslint-disable-line no-unused-vars
* Namespace for the Camera app. } from './type.js';
*/
var cca = cca || {};
/**
* Namespace for metrics.
*/
cca.metrics = cca.metrics || {};
/** /**
* Event builder for basic metrics. * Event builder for basic metrics.
* @type {?analytics.EventBuilder} * @type {?analytics.EventBuilder}
* @private
*/ */
cca.metrics.base_ = null; let base = null;
var analytics = window['analytics'] || {}; /* global analytics */
/** /**
* Fixes analytics.EventBuilder's dimension() method. * Fixes analytics.EventBuilder's dimension() method.
...@@ -37,15 +29,14 @@ analytics.EventBuilder.prototype.dimen = function(i, v) { ...@@ -37,15 +29,14 @@ analytics.EventBuilder.prototype.dimen = function(i, v) {
* Promise for Google Analytics tracker. * Promise for Google Analytics tracker.
* @type {Promise<analytics.Tracker>} * @type {Promise<analytics.Tracker>}
* @suppress {checkTypes} * @suppress {checkTypes}
* @private
*/ */
cca.metrics.ga_ = (function() { const ga = (function() {
const id = 'UA-134822711-1'; const id = 'UA-134822711-1';
const service = analytics.getService('chrome-camera-app'); const service = analytics.getService('chrome-camera-app');
var getConfig = () => const getConfig = () =>
new Promise((resolve) => service.getConfig().addCallback(resolve)); new Promise((resolve) => service.getConfig().addCallback(resolve));
var checkEnabled = () => { const checkEnabled = () => {
return new Promise((resolve) => { return new Promise((resolve) => {
try { try {
chrome.metricsPrivate.getIsCrashReportingEnabled(resolve); chrome.metricsPrivate.getIsCrashReportingEnabled(resolve);
...@@ -54,7 +45,7 @@ cca.metrics.ga_ = (function() { ...@@ -54,7 +45,7 @@ cca.metrics.ga_ = (function() {
} }
}); });
}; };
var initBuilder = () => { const initBuilder = () => {
return new Promise((resolve) => { return new Promise((resolve) => {
try { try {
chrome.chromeosInfoPrivate.get( chrome.chromeosInfoPrivate.get(
...@@ -64,10 +55,10 @@ cca.metrics.ga_ = (function() { ...@@ -64,10 +55,10 @@ cca.metrics.ga_ = (function() {
} }
}) })
.then((board) => { .then((board) => {
var boardName = /^(x86-)?(\w*)/.exec(board)[0]; const boardName = /^(x86-)?(\w*)/.exec(board)[0];
var match = navigator.appVersion.match(/CrOS\s+\S+\s+([\d.]+)/); const match = navigator.appVersion.match(/CrOS\s+\S+\s+([\d.]+)/);
var osVer = match ? match[1] : ''; const osVer = match ? match[1] : '';
cca.metrics.base_ = analytics.EventBuilder.builder() base = analytics.EventBuilder.builder()
.dimen(1, boardName) .dimen(1, boardName)
.dimen(2, osVer); .dimen(2, osVer);
}); });
...@@ -84,18 +75,17 @@ cca.metrics.ga_ = (function() { ...@@ -84,18 +75,17 @@ cca.metrics.ga_ = (function() {
* Returns event builder for the metrics type: launch. * Returns event builder for the metrics type: launch.
* @param {boolean} ackMigrate Whether acknowledged to migrate during launch. * @param {boolean} ackMigrate Whether acknowledged to migrate during launch.
* @return {!analytics.EventBuilder} * @return {!analytics.EventBuilder}
* @private
*/ */
cca.metrics.launchType_ = function(ackMigrate) { function launchType(ackMigrate) {
return cca.metrics.base_.category('launch').action('start').label( return base.category('launch').action('start').label(
ackMigrate ? 'ack-migrate' : ''); ackMigrate ? 'ack-migrate' : '');
}; }
/** /**
* Types of intent result dimension. * Types of intent result dimension.
* @enum {string} * @enum {string}
*/ */
cca.metrics.IntentResultType = { export const IntentResultType = {
NOT_INTENT: '', NOT_INTENT: '',
CANCELED: 'canceled', CANCELED: 'canceled',
CONFIRMED: 'confirmed', CONFIRMED: 'confirmed',
...@@ -105,33 +95,30 @@ cca.metrics.IntentResultType = { ...@@ -105,33 +95,30 @@ cca.metrics.IntentResultType = {
* Returns event builder for the metrics type: capture. * Returns event builder for the metrics type: capture.
* @param {?string} facingMode Camera facing-mode of the capture. * @param {?string} facingMode Camera facing-mode of the capture.
* @param {number} length Length of 1 minute buckets for captured video. * @param {number} length Length of 1 minute buckets for captured video.
* @param {!cca.Resolution} resolution Capture resolution. * @param {!Resolution} resolution Capture resolution.
* @param {!cca.metrics.IntentResultType} intentResult * @param {!IntentResultType} intentResult
* @return {!analytics.EventBuilder} * @return {!analytics.EventBuilder}
* @private
*/ */
cca.metrics.captureType_ = function( function captureType(facingMode, length, resolution, intentResult) {
facingMode, length, resolution, intentResult) {
/** /**
* @param {!Array<cca.state.StateUnion>} states * @param {!Array<state.StateUnion>} states
* @param {cca.state.StateUnion=} cond * @param {state.StateUnion=} cond
* @param {boolean=} strict * @param {boolean=} strict
* @return {string} * @return {string}
*/ */
const condState = (states, cond = undefined, strict = undefined) => { const condState = (states, cond = undefined, strict = undefined) => {
// Return the first existing state among the given states only if there is // Return the first existing state among the given states only if there is
// no gate condition or the condition is met. // no gate condition or the condition is met.
const prerequisite = !cond || cca.state.get(cond); const prerequisite = !cond || state.get(cond);
if (strict && !prerequisite) { if (strict && !prerequisite) {
return ''; return '';
} }
return prerequisite && states.find((state) => cca.state.get(state)) || return prerequisite && states.find((s) => state.get(s)) || 'n/a';
'n/a';
}; };
const State = cca.state.State; const State = state.State;
return cca.metrics.base_.category('capture') return base.category('capture')
.action(condState(Object.values(cca.Mode))) .action(condState(Object.values(Mode)))
.label(facingMode || '(not set)') .label(facingMode || '(not set)')
// Skips 3rd dimension for obsolete 'sound' state. // Skips 3rd dimension for obsolete 'sound' state.
.dimen(4, condState([State.MIRROR])) .dimen(4, condState([State.MIRROR]))
...@@ -140,14 +127,14 @@ cca.metrics.captureType_ = function( ...@@ -140,14 +127,14 @@ cca.metrics.captureType_ = function(
condState( condState(
[State.GRID_3x3, State.GRID_4x4, State.GRID_GOLDEN], State.GRID)) [State.GRID_3x3, State.GRID_4x4, State.GRID_GOLDEN], State.GRID))
.dimen(6, condState([State.TIMER_3SEC, State.TIMER_10SEC], State.TIMER)) .dimen(6, condState([State.TIMER_3SEC, State.TIMER_10SEC], State.TIMER))
.dimen(7, condState([State.MIC], cca.Mode.VIDEO, true)) .dimen(7, condState([State.MIC], Mode.VIDEO, true))
.dimen(8, condState([State.MAX_WND])) .dimen(8, condState([State.MAX_WND]))
.dimen(9, condState([State.TALL])) .dimen(9, condState([State.TALL]))
.dimen(10, resolution.toString()) .dimen(10, resolution.toString())
.dimen(11, condState([State.FPS_30, State.FPS_60], cca.Mode.VIDEO, true)) .dimen(11, condState([State.FPS_30, State.FPS_60], Mode.VIDEO, true))
.dimen(12, intentResult) .dimen(12, intentResult)
.value(length || 0); .value(length || 0);
}; }
/** /**
* Returns event builder for the metrics type: perf. * Returns event builder for the metrics type: perf.
...@@ -155,33 +142,39 @@ cca.metrics.captureType_ = function( ...@@ -155,33 +142,39 @@ cca.metrics.captureType_ = function(
* @param {number} duration The duration of the event in ms. * @param {number} duration The duration of the event in ms.
* @param {Object=} extras Optional information for the event. * @param {Object=} extras Optional information for the event.
* @return {!analytics.EventBuilder} * @return {!analytics.EventBuilder}
* @private
*/ */
cca.metrics.perfType_ = function(event, duration, extras = {}) { function perfType(event, duration, extras = {}) {
const {resolution = ''} = extras; const {resolution = ''} = extras;
return cca.metrics.base_.category('perf') return base.category('perf')
.action(event) .action(event)
// Round the duration here since GA expects that the value is an integer. // Round the duration here since GA expects that the value is an integer.
// Reference: https://support.google.com/analytics/answer/1033068 // Reference: https://support.google.com/analytics/answer/1033068
.value(Math.round(duration)) .value(Math.round(duration))
.dimen(3, `${resolution}`); .dimen(3, `${resolution}`);
}; }
/** /**
* Metrics types. * Metrics types.
* @enum {function(...): !analytics.EventBuilder} * @enum {function(...): !analytics.EventBuilder}
*/ */
cca.metrics.Type = { export const Type = {
LAUNCH: cca.metrics.launchType_, LAUNCH: launchType,
CAPTURE: cca.metrics.captureType_, CAPTURE: captureType,
PERF: cca.metrics.perfType_, PERF: perfType,
}; };
/** /**
* Logs the given metrics. * Logs the given metrics.
* @param {!cca.metrics.Type} type Metrics type. * @param {!Type} type Metrics type.
* @param {...*} args Optional rest parameters for logging metrics. * @param {...*} args Optional rest parameters for logging metrics.
*/ */
cca.metrics.log = function(type, ...args) { export function log(type, ...args) {
cca.metrics.ga_.then((tracker) => tracker.send(type(...args))); ga.then((tracker) => tracker.send(type(...args)));
}; }
/** const */
cca.metrics.IntentResultType = IntentResultType;
/** const */
cca.metrics.Type = Type;
/** const */
cca.metrics.log = log;
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
var cca = { var cca = {
device: {}, device: {},
intent: {}, intent: {},
metrics: {},
models: { models: {
FileSystem: {}, FileSystem: {},
}, },
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<script src="../js/google-analytics-bundle.js"></script> <script src="../js/google-analytics-bundle.js"></script>
<script type="module" src="../js/type.js"></script> <script type="module" src="../js/type.js"></script>
<script type="module" src="../js/perf.js"></script> <script type="module" src="../js/perf.js"></script>
<script defer src="../js/metrics.js"></script> <script type="module" src="../js/metrics.js"></script>
<script type="module" src="../js/intent.js"></script> <script type="module" src="../js/intent.js"></script>
<script type="module" src="../js/util.js"></script> <script type="module" src="../js/util.js"></script>
<script type="module" src="../js/toast.js"></script> <script type="module" src="../js/toast.js"></script>
......
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