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