2012-11-05 12:50:11 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-01-27 11:26:48 -08:00
|
|
|
#ifndef MERGED_COMPARTMENT
|
|
|
|
|
2014-08-07 21:52:05 -07:00
|
|
|
"use strict";
|
|
|
|
|
2012-11-05 12:50:11 -08:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
2013-01-06 12:13:19 -08:00
|
|
|
"Measurement",
|
|
|
|
"Provider",
|
2012-11-05 12:50:11 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
const {utils: Cu} = Components;
|
|
|
|
|
2013-01-27 11:26:48 -08:00
|
|
|
const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-06-13 18:36:21 -07:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2013-04-15 12:45:37 -07:00
|
|
|
Cu.import("resource://gre/modules/Preferences.jsm");
|
2013-01-06 12:13:19 -08:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2013-08-26 11:55:58 -07:00
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
2013-01-06 12:13:19 -08:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
|
|
|
|
|
|
|
|
2012-11-05 12:50:11 -08:00
|
|
|
|
|
|
|
/**
|
2013-01-06 12:13:19 -08:00
|
|
|
* Represents a collection of related pieces/fields of data.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-02-21 14:11:54 -08:00
|
|
|
* This is an abstract base type.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* This type provides the primary interface for storing, retrieving, and
|
|
|
|
* serializing data.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* Each measurement consists of a set of named fields. Each field is primarily
|
|
|
|
* identified by a string name, which must be unique within the measurement.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-02-21 14:11:54 -08:00
|
|
|
* Each derived type must define the following properties:
|
|
|
|
*
|
|
|
|
* name -- String name of this measurement. This is the primary way
|
|
|
|
* measurements are distinguished within a provider.
|
|
|
|
*
|
|
|
|
* version -- Integer version of this measurement. This is a secondary
|
|
|
|
* identifier for a measurement within a provider. The version denotes
|
|
|
|
* the behavior of this measurement and the composition of its fields over
|
|
|
|
* time. When a new field is added or the behavior of an existing field
|
|
|
|
* changes, the version should be incremented. The initial version of a
|
|
|
|
* measurement is typically 1.
|
|
|
|
*
|
|
|
|
* fields -- Object defining the fields this measurement holds. Keys in the
|
|
|
|
* object are string field names. Values are objects describing how the
|
|
|
|
* field works. The following properties are recognized:
|
|
|
|
*
|
|
|
|
* type -- The string type of this field. This is typically one of the
|
|
|
|
* FIELD_* constants from the Metrics.Storage type.
|
|
|
|
*
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* FUTURE: provide hook points for measurements to supplement with custom
|
|
|
|
* storage needs.
|
2012-11-05 12:50:11 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
this.Measurement = function () {
|
|
|
|
if (!this.name) {
|
|
|
|
throw new Error("Measurement must have a name.");
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (!this.version) {
|
|
|
|
throw new Error("Measurement must have a version.");
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (!Number.isInteger(this.version)) {
|
|
|
|
throw new Error("Measurement's version must be an integer: " + this.version);
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-02-21 14:11:54 -08:00
|
|
|
if (!this.fields) {
|
|
|
|
throw new Error("Measurement must define fields.");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let [name, info] in Iterator(this.fields)) {
|
|
|
|
if (!info) {
|
|
|
|
throw new Error("Field does not contain metadata: " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info.type) {
|
|
|
|
throw new Error("Field is missing required type property: " + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-26 11:55:58 -07:00
|
|
|
this._log = Log.repository.getLogger("Services.Metrics.Measurement." + this.name);
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
this.id = null;
|
|
|
|
this.storage = null;
|
2013-02-21 14:11:54 -08:00
|
|
|
this._fields = {};
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
this._serializers = {};
|
|
|
|
this._serializers[this.SERIALIZE_JSON] = {
|
|
|
|
singular: this._serializeJSONSingular.bind(this),
|
|
|
|
daily: this._serializeJSONDay.bind(this),
|
|
|
|
};
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
Measurement.prototype = Object.freeze({
|
|
|
|
SERIALIZE_JSON: "json",
|
2012-11-05 12:50:11 -08:00
|
|
|
|
|
|
|
/**
|
2013-01-06 12:13:19 -08:00
|
|
|
* Obtain a serializer for this measurement.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* Implementations should return an object with the following keys:
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* singular -- Serializer for singular data.
|
|
|
|
* daily -- Serializer for daily data.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* Each item is a function that takes a single argument: the data to
|
|
|
|
* serialize. The passed data is a subset of that returned from
|
|
|
|
* this.getValues(). For "singular," data.singular is passed. For "daily",
|
|
|
|
* data.days.get(<day>) is passed.
|
|
|
|
*
|
|
|
|
* This function receives a single argument: the serialization format we
|
|
|
|
* are requesting. This is one of the SERIALIZE_* constants on this base type.
|
|
|
|
*
|
|
|
|
* For SERIALIZE_JSON, the function should return an object that
|
|
|
|
* JSON.stringify() knows how to handle. This could be an anonymous object or
|
|
|
|
* array or any object with a property named `toJSON` whose value is a
|
|
|
|
* function. The returned object will be added to a larger document
|
|
|
|
* containing the results of all `serialize` calls.
|
|
|
|
*
|
|
|
|
* The default implementation knows how to serialize built-in types using
|
|
|
|
* very simple logic. If small encoding size is a goal, the default
|
|
|
|
* implementation may not be suitable. If an unknown field type is
|
|
|
|
* encountered, the default implementation will error.
|
|
|
|
*
|
|
|
|
* @param format
|
|
|
|
* (string) A SERIALIZE_* constant defining what serialization format
|
|
|
|
* to use.
|
2012-11-05 12:50:11 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
serializer: function (format) {
|
|
|
|
if (!(format in this._serializers)) {
|
|
|
|
throw new Error("Don't know how to serialize format: " + format);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._serializers[format];
|
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* Whether this measurement contains the named field.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* (string) Name of field.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
hasField: function (name) {
|
2013-02-21 14:11:54 -08:00
|
|
|
return name in this.fields;
|
2013-01-06 12:13:19 -08:00
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* The unique identifier for a named field.
|
|
|
|
*
|
|
|
|
* This will throw if the field is not known.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* (string) Name of field.
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
fieldID: function (name) {
|
2013-02-21 14:11:54 -08:00
|
|
|
let entry = this._fields[name];
|
2013-01-06 12:13:19 -08:00
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
throw new Error("Unknown field: " + name);
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
return entry[0];
|
|
|
|
},
|
|
|
|
|
|
|
|
fieldType: function (name) {
|
2013-02-21 14:11:54 -08:00
|
|
|
let entry = this._fields[name];
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (!entry) {
|
|
|
|
throw new Error("Unknown field: " + name);
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
return entry[1];
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-02-21 14:11:54 -08:00
|
|
|
_configureStorage: function () {
|
2013-03-26 13:02:33 -07:00
|
|
|
let missing = [];
|
|
|
|
for (let [name, info] in Iterator(this.fields)) {
|
|
|
|
if (this.storage.hasFieldFromMeasurement(this.id, name)) {
|
|
|
|
this._fields[name] =
|
|
|
|
[this.storage.fieldIDFromMeasurement(this.id, name), info.type];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
missing.push([name, info.type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!missing.length) {
|
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
|
|
|
}
|
2013-01-06 12:13:19 -08:00
|
|
|
|
2013-03-26 13:02:33 -07:00
|
|
|
// We only perform a transaction if we have work to do (to avoid
|
|
|
|
// extra SQLite overhead).
|
|
|
|
return this.storage.enqueueTransaction(function registerFields() {
|
|
|
|
for (let [name, type] of missing) {
|
|
|
|
this._log.debug("Registering field: " + name + " " + type);
|
|
|
|
let id = yield this.storage.registerField(this.id, name, type);
|
|
|
|
this._fields[name] = [id, type];
|
2013-02-21 14:11:54 -08:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-01-06 12:13:19 -08:00
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Data Recording Functions
|
|
|
|
//
|
|
|
|
// Functions in this section are used to record new values against this
|
|
|
|
// measurement instance.
|
|
|
|
//
|
|
|
|
// Generally speaking, these functions will throw if the specified field does
|
|
|
|
// not exist or if the storage function requested is not appropriate for the
|
|
|
|
// type of that field. These functions will also return a promise that will
|
|
|
|
// be resolved when the underlying storage operation has completed.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment a daily counter field in this measurement by 1.
|
|
|
|
*
|
|
|
|
* By default, the counter for the current day will be incremented.
|
|
|
|
*
|
|
|
|
* If the field is not known or is not a daily counter, this will throw.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param field
|
|
|
|
* (string) The name of the field whose value to increment.
|
|
|
|
* @param date
|
|
|
|
* (Date) Day on which to increment the counter.
|
2013-05-08 17:02:06 -07:00
|
|
|
* @param by
|
|
|
|
* (integer) How much to increment by.
|
2013-02-18 16:11:50 -08:00
|
|
|
* @return Promise<>
|
|
|
|
*/
|
2013-05-08 17:02:06 -07:00
|
|
|
incrementDailyCounter: function (field, date=new Date(), by=1) {
|
2013-01-06 12:13:19 -08:00
|
|
|
return this.storage.incrementDailyCounterFromFieldID(this.fieldID(field),
|
2013-05-08 17:02:06 -07:00
|
|
|
date, by);
|
2013-01-06 12:13:19 -08:00
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* Record a new numeric value for a daily discrete numeric field.
|
|
|
|
*
|
|
|
|
* @param field
|
|
|
|
* (string) The name of the field to append a value to.
|
|
|
|
* @param value
|
|
|
|
* (Number) Number to append.
|
|
|
|
* @param date
|
|
|
|
* (Date) Day on which to append the value.
|
|
|
|
*
|
|
|
|
* @return Promise<>
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
addDailyDiscreteNumeric: function (field, value, date=new Date()) {
|
|
|
|
return this.storage.addDailyDiscreteNumericFromFieldID(
|
|
|
|
this.fieldID(field), value, date);
|
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* Record a new text value for a daily discrete text field.
|
|
|
|
*
|
|
|
|
* This is like `addDailyDiscreteNumeric` but for daily discrete text fields.
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
addDailyDiscreteText: function (field, value, date=new Date()) {
|
|
|
|
return this.storage.addDailyDiscreteTextFromFieldID(
|
|
|
|
this.fieldID(field), value, date);
|
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* Record the last seen value for a last numeric field.
|
|
|
|
*
|
|
|
|
* @param field
|
|
|
|
* (string) The name of the field to set the value of.
|
|
|
|
* @param value
|
|
|
|
* (Number) The value to set.
|
|
|
|
* @param date
|
|
|
|
* (Date) When this value was recorded.
|
|
|
|
*
|
|
|
|
* @return Promise<>
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
setLastNumeric: function (field, value, date=new Date()) {
|
|
|
|
return this.storage.setLastNumericFromFieldID(this.fieldID(field), value,
|
|
|
|
date);
|
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* Record the last seen value for a last text field.
|
|
|
|
*
|
|
|
|
* This is like `setLastNumeric` except for last text fields.
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
setLastText: function (field, value, date=new Date()) {
|
|
|
|
return this.storage.setLastTextFromFieldID(this.fieldID(field), value,
|
|
|
|
date);
|
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* Record the most recent value for a daily last numeric field.
|
|
|
|
*
|
|
|
|
* @param field
|
|
|
|
* (string) The name of a daily last numeric field.
|
|
|
|
* @param value
|
|
|
|
* (Number) The value to set.
|
|
|
|
* @param date
|
|
|
|
* (Date) Day on which to record the last value.
|
|
|
|
*
|
|
|
|
* @return Promise<>
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
setDailyLastNumeric: function (field, value, date=new Date()) {
|
|
|
|
return this.storage.setDailyLastNumericFromFieldID(this.fieldID(field),
|
|
|
|
value, date);
|
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* Record the most recent value for a daily last text field.
|
|
|
|
*
|
|
|
|
* This is like `setDailyLastNumeric` except for a daily last text field.
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
setDailyLastText: function (field, value, date=new Date()) {
|
|
|
|
return this.storage.setDailyLastTextFromFieldID(this.fieldID(field),
|
|
|
|
value, date);
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// End of data recording APIs.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2012-11-05 12:50:11 -08:00
|
|
|
/**
|
2013-01-06 12:13:19 -08:00
|
|
|
* Obtain all values stored for this measurement.
|
|
|
|
*
|
|
|
|
* The default implementation obtains all known types from storage. If the
|
|
|
|
* measurement provides custom types or stores values somewhere other than
|
|
|
|
* storage, it should define its own implementation.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* This returns a promise that resolves to a data structure which is
|
|
|
|
* understood by the measurement's serialize() function.
|
2012-11-05 12:50:11 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
getValues: function () {
|
|
|
|
return this.storage.getMeasurementValues(this.id);
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteLastNumeric: function (field) {
|
|
|
|
return this.storage.deleteLastNumericFromFieldID(this.fieldID(field));
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteLastText: function (field) {
|
|
|
|
return this.storage.deleteLastTextFromFieldID(this.fieldID(field));
|
|
|
|
},
|
|
|
|
|
2013-03-25 18:46:22 -07:00
|
|
|
/**
|
|
|
|
* This method is used by the default serializers to control whether a field
|
|
|
|
* is included in the output.
|
|
|
|
*
|
|
|
|
* There could be legacy fields in storage we no longer care about.
|
|
|
|
*
|
|
|
|
* This method is a hook to allow measurements to change this behavior, e.g.,
|
|
|
|
* to implement a dynamic fieldset.
|
|
|
|
*
|
|
|
|
* You will also need to override `fieldType`.
|
|
|
|
*
|
|
|
|
* @return (boolean) true if the specified field should be included in
|
|
|
|
* payload output.
|
|
|
|
*/
|
|
|
|
shouldIncludeField: function (field) {
|
|
|
|
return field in this._fields;
|
|
|
|
},
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
_serializeJSONSingular: function (data) {
|
2013-01-25 00:32:33 -08:00
|
|
|
let result = {"_v": this.version};
|
2013-01-06 12:13:19 -08:00
|
|
|
|
|
|
|
for (let [field, data] of data) {
|
|
|
|
// There could be legacy fields in storage we no longer care about.
|
2013-03-25 18:46:22 -07:00
|
|
|
if (!this.shouldIncludeField(field)) {
|
2013-01-06 12:13:19 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let type = this.fieldType(field);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case this.storage.FIELD_LAST_NUMERIC:
|
|
|
|
case this.storage.FIELD_LAST_TEXT:
|
|
|
|
result[field] = data[1];
|
|
|
|
break;
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
case this.storage.FIELD_DAILY_COUNTER:
|
|
|
|
case this.storage.FIELD_DAILY_DISCRETE_NUMERIC:
|
|
|
|
case this.storage.FIELD_DAILY_DISCRETE_TEXT:
|
|
|
|
case this.storage.FIELD_DAILY_LAST_NUMERIC:
|
|
|
|
case this.storage.FIELD_DAILY_LAST_TEXT:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error("Unknown field type: " + type);
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
}
|
2013-01-06 12:13:19 -08:00
|
|
|
|
|
|
|
return result;
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
_serializeJSONDay: function (data) {
|
2013-01-25 00:32:33 -08:00
|
|
|
let result = {"_v": this.version};
|
2013-01-06 12:13:19 -08:00
|
|
|
|
|
|
|
for (let [field, data] of data) {
|
2013-03-25 18:46:22 -07:00
|
|
|
if (!this.shouldIncludeField(field)) {
|
2013-01-06 12:13:19 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let type = this.fieldType(field);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case this.storage.FIELD_DAILY_COUNTER:
|
|
|
|
case this.storage.FIELD_DAILY_DISCRETE_NUMERIC:
|
|
|
|
case this.storage.FIELD_DAILY_DISCRETE_TEXT:
|
|
|
|
case this.storage.FIELD_DAILY_LAST_NUMERIC:
|
|
|
|
case this.storage.FIELD_DAILY_LAST_TEXT:
|
|
|
|
result[field] = data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case this.storage.FIELD_LAST_NUMERIC:
|
|
|
|
case this.storage.FIELD_LAST_TEXT:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error("Unknown field type: " + type);
|
|
|
|
}
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
return result;
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
2013-01-06 12:13:19 -08:00
|
|
|
});
|
2012-11-05 12:50:11 -08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-01-06 12:13:19 -08:00
|
|
|
* An entity that emits data.
|
|
|
|
*
|
|
|
|
* A `Provider` consists of a string name (must be globally unique among all
|
|
|
|
* known providers) and a set of `Measurement` instances.
|
|
|
|
*
|
|
|
|
* The main role of a `Provider` is to produce metrics data and to store said
|
|
|
|
* data in the storage backend.
|
|
|
|
*
|
2013-03-07 05:06:46 -08:00
|
|
|
* Metrics data collection is initiated either by a manager calling a
|
2013-01-06 12:13:19 -08:00
|
|
|
* `collect*` function on `Provider` instances or by the `Provider` registering
|
|
|
|
* to some external event and then reacting whenever they occur.
|
|
|
|
*
|
|
|
|
* `Provider` implementations interface directly with a storage backend. For
|
|
|
|
* common stored values (daily counters, daily discrete values, etc),
|
|
|
|
* implementations should interface with storage via the various helper
|
|
|
|
* functions on the `Measurement` instances. For custom stored value types,
|
|
|
|
* implementations will interact directly with the low-level storage APIs.
|
|
|
|
*
|
|
|
|
* Because multiple providers exist and could be responding to separate
|
|
|
|
* external events simultaneously and because not all operations performed by
|
|
|
|
* storage can safely be performed in parallel, writing directly to storage at
|
|
|
|
* event time is dangerous. Therefore, interactions with storage must be
|
|
|
|
* deferred until it is safe to perform them.
|
|
|
|
*
|
|
|
|
* This typically looks something like:
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* // This gets called when an external event worthy of recording metrics
|
|
|
|
* // occurs. The function receives a numeric value associated with the event.
|
|
|
|
* function onExternalEvent (value) {
|
|
|
|
* let now = new Date();
|
|
|
|
* let m = this.getMeasurement("foo", 1);
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* this.enqueueStorageOperation(function storeExternalEvent() {
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* // We interface with storage via the `Measurement` helper functions.
|
|
|
|
* // These each return a promise that will be resolved when the
|
|
|
|
* // operation finishes. We rely on behavior of storage where operations
|
|
|
|
* // are executed single threaded and sequentially. Therefore, we only
|
|
|
|
* // need to return the final promise.
|
|
|
|
* m.incrementDailyCounter("foo", now);
|
|
|
|
* return m.addDailyDiscreteNumericValue("my_value", value, now);
|
|
|
|
* }.bind(this));
|
2012-11-08 15:32:49 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* }
|
2012-11-08 15:32:49 -08:00
|
|
|
*
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* `Provider` is an abstract base class. Implementations must define a few
|
|
|
|
* properties:
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* name
|
|
|
|
* The `name` property should be a string defining the provider's name. The
|
|
|
|
* name must be globally unique for the application. The name is used as an
|
|
|
|
* identifier to distinguish providers from each other.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* measurementTypes
|
|
|
|
* This must be an array of `Measurement`-derived types. Note that elements
|
|
|
|
* in the array are the type functions, not instances. Instances of the
|
|
|
|
* `Measurement` are created at run-time by the `Provider` and are bound
|
|
|
|
* to the provider and to a specific storage backend.
|
2012-11-05 12:50:11 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
this.Provider = function () {
|
|
|
|
if (!this.name) {
|
|
|
|
throw new Error("Provider must define a name.");
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (!Array.isArray(this.measurementTypes)) {
|
|
|
|
throw new Error("Provider must define measurement types.");
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-08-26 11:55:58 -07:00
|
|
|
this._log = Log.repository.getLogger("Services.Metrics.Provider." + this.name);
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
this.measurements = null;
|
|
|
|
this.storage = null;
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
Provider.prototype = Object.freeze({
|
2013-01-31 08:58:19 -08:00
|
|
|
/**
|
2013-02-18 13:05:07 -08:00
|
|
|
* Whether the provider only pulls data from other sources.
|
2013-01-31 08:58:19 -08:00
|
|
|
*
|
2013-02-18 13:05:07 -08:00
|
|
|
* If this is true, the provider pulls data from other sources. By contrast,
|
|
|
|
* "push-based" providers subscribe to foreign sources and record/react to
|
|
|
|
* external events as they happen.
|
2013-01-31 08:58:19 -08:00
|
|
|
*
|
2013-02-18 13:05:07 -08:00
|
|
|
* Pull-only providers likely aren't instantiated until a data collection
|
|
|
|
* is performed. Thus, implementations cannot rely on a provider instance
|
|
|
|
* always being alive. This is an optimization so provider instances aren't
|
|
|
|
* dead weight while the application is running.
|
2013-01-31 08:58:19 -08:00
|
|
|
*
|
2013-02-18 13:05:07 -08:00
|
|
|
* This must be set on the prototype to have an effect.
|
2013-01-31 08:58:19 -08:00
|
|
|
*/
|
2013-02-18 13:05:07 -08:00
|
|
|
pullOnly: false,
|
2013-01-31 08:58:19 -08:00
|
|
|
|
2012-11-05 12:50:11 -08:00
|
|
|
/**
|
2013-01-06 12:13:19 -08:00
|
|
|
* Obtain a `Measurement` from its name and version.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* If the measurement is not found, an Error is thrown.
|
2012-11-05 12:50:11 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
getMeasurement: function (name, version) {
|
|
|
|
if (!Number.isInteger(version)) {
|
|
|
|
throw new Error("getMeasurement expects an integer version. Got: " + version);
|
|
|
|
}
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
let m = this.measurements.get([name, version].join(":"));
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (!m) {
|
|
|
|
throw new Error("Unknown measurement: " + name + " v" + version);
|
|
|
|
}
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
return m;
|
|
|
|
},
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
init: function (storage) {
|
|
|
|
if (this.storage !== null) {
|
|
|
|
throw new Error("Provider() not called. Did the sub-type forget to call it?");
|
|
|
|
}
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (this.storage) {
|
|
|
|
throw new Error("Provider has already been initialized.");
|
|
|
|
}
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
this.measurements = new Map();
|
|
|
|
this.storage = storage;
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
let self = this;
|
|
|
|
return Task.spawn(function init() {
|
2013-03-29 09:23:02 -07:00
|
|
|
let pre = self.preInit();
|
|
|
|
if (!pre || typeof(pre.then) != "function") {
|
|
|
|
throw new Error("preInit() does not return a promise.");
|
|
|
|
}
|
|
|
|
yield pre;
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
for (let measurementType of self.measurementTypes) {
|
|
|
|
let measurement = new measurementType();
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
measurement.provider = self;
|
|
|
|
measurement.storage = self.storage;
|
2012-11-08 15:32:49 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
let id = yield storage.registerMeasurement(self.name, measurement.name,
|
|
|
|
measurement.version);
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
measurement.id = id;
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-02-21 14:11:54 -08:00
|
|
|
yield measurement._configureStorage();
|
2013-01-06 12:13:19 -08:00
|
|
|
|
|
|
|
self.measurements.set([measurement.name, measurement.version].join(":"),
|
|
|
|
measurement);
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-03-29 09:23:02 -07:00
|
|
|
let post = self.postInit();
|
|
|
|
if (!post || typeof(post.then) != "function") {
|
|
|
|
throw new Error("postInit() does not return a promise.");
|
2013-01-06 12:13:19 -08:00
|
|
|
}
|
2013-03-29 09:23:02 -07:00
|
|
|
yield post;
|
2013-01-06 12:13:19 -08:00
|
|
|
});
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
shutdown: function () {
|
|
|
|
let promise = this.onShutdown();
|
2012-11-05 12:50:11 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (!promise || typeof(promise.then) != "function") {
|
|
|
|
throw new Error("onShutdown implementation does not return a promise.");
|
2012-11-05 12:50:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
return promise;
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-03-29 09:23:02 -07:00
|
|
|
* Hook point for implementations to perform pre-initialization activity.
|
|
|
|
*
|
|
|
|
* This method will be called before measurement registration.
|
|
|
|
*
|
|
|
|
* Implementations should return a promise which is resolved when
|
|
|
|
* initialization activities have completed.
|
|
|
|
*/
|
|
|
|
preInit: function () {
|
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook point for implementations to perform post-initialization activity.
|
|
|
|
*
|
|
|
|
* This method will be called after `preInit` and measurement registration,
|
|
|
|
* but before initialization is finished.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* If a `Provider` instance needs to register observers, etc, it should
|
|
|
|
* implement this function.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* Implementations should return a promise which is resolved when
|
|
|
|
* initialization activities have completed.
|
|
|
|
*/
|
2013-03-29 09:23:02 -07:00
|
|
|
postInit: function () {
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2013-01-06 12:13:19 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook point for shutdown of instances.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* This is the opposite of `onInit`. If a `Provider` needs to unregister
|
|
|
|
* observers, etc, this is where it should do it.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* Implementations should return a promise which is resolved when
|
|
|
|
* shutdown activities have completed.
|
2012-11-05 12:50:11 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
onShutdown: function () {
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-01-06 12:13:19 -08:00
|
|
|
* Collects data that doesn't change during the application's lifetime.
|
|
|
|
*
|
|
|
|
* Implementations should return a promise that resolves when all data has
|
|
|
|
* been collected and storage operations have been finished.
|
2013-02-05 09:59:13 -08:00
|
|
|
*
|
|
|
|
* @return Promise<>
|
2012-11-05 12:50:11 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
collectConstantData: function () {
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-02-05 09:59:13 -08:00
|
|
|
/**
|
|
|
|
* Collects data approximately every day.
|
|
|
|
*
|
|
|
|
* For long-running applications, this is called approximately every day.
|
|
|
|
* It may or may not be called every time the application is run. It also may
|
|
|
|
* be called more than once per day.
|
|
|
|
*
|
|
|
|
* Implementations should return a promise that resolves when all data has
|
|
|
|
* been collected and storage operations have completed.
|
|
|
|
*
|
|
|
|
* @return Promise<>
|
|
|
|
*/
|
|
|
|
collectDailyData: function () {
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2013-02-05 09:59:13 -08:00
|
|
|
},
|
|
|
|
|
2012-11-05 12:50:11 -08:00
|
|
|
/**
|
2013-01-06 12:13:19 -08:00
|
|
|
* Queue a deferred storage operation.
|
2012-11-05 12:50:11 -08:00
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* Deferred storage operations are the preferred method for providers to
|
|
|
|
* interact with storage. When collected data is to be added to storage,
|
|
|
|
* the provider creates a function that performs the necessary storage
|
|
|
|
* interactions and then passes that function to this function. Pending
|
|
|
|
* storage operations will be executed sequentially by a coordinator.
|
|
|
|
*
|
|
|
|
* The passed function should return a promise which will be resolved upon
|
|
|
|
* completion of storage interaction.
|
2012-11-05 12:50:11 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
enqueueStorageOperation: function (func) {
|
|
|
|
return this.storage.enqueueOperation(func);
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-02-18 16:11:50 -08:00
|
|
|
/**
|
|
|
|
* Obtain persisted provider state.
|
|
|
|
*
|
2013-02-22 16:02:27 -08:00
|
|
|
* Provider state consists of key-value pairs of string names and values.
|
|
|
|
* Providers can stuff whatever they want into state. They are encouraged to
|
|
|
|
* store as little as possible for performance reasons.
|
|
|
|
*
|
|
|
|
* State is backed by storage and is robust.
|
|
|
|
*
|
|
|
|
* These functions do not enqueue on storage automatically, so they should
|
|
|
|
* be guarded by `enqueueStorageOperation` or some other mutex.
|
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
* (string) The property to retrieve.
|
|
|
|
*
|
|
|
|
* @return Promise<string|null> String value on success. null if no state
|
|
|
|
* is available under this key.
|
2013-02-18 16:11:50 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
getState: function (key) {
|
2013-02-22 16:02:27 -08:00
|
|
|
return this.storage.getProviderState(this.name, key);
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-02-22 16:02:27 -08:00
|
|
|
/**
|
|
|
|
* Set state for this provider.
|
|
|
|
*
|
|
|
|
* This is the complementary API for `getState` and obeys the same
|
|
|
|
* storage restrictions.
|
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
setState: function (key, value) {
|
2013-02-22 16:02:27 -08:00
|
|
|
return this.storage.setProviderState(this.name, key, value);
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
_dateToDays: function (date) {
|
|
|
|
return Math.floor(date.getTime() / MILLISECONDS_PER_DAY);
|
2012-11-05 12:50:11 -08:00
|
|
|
},
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
_daysToDate: function (days) {
|
|
|
|
return new Date(days * MILLISECONDS_PER_DAY);
|
|
|
|
},
|
|
|
|
});
|
2012-11-05 12:50:11 -08:00
|
|
|
|