Bug 1029031 - Record default search provider in FHR; r=bsmedberg, MattN

--HG--
extra : rebase_source : 7e3c2688db567bac8760c405d9e9d7d41e8564d9
extra : amend_source : a5f4264ec947ae27e3036018034e0b17bf1e8590
This commit is contained in:
Gregory Szorc 2014-06-25 12:44:00 -07:00
Родитель 39d917bcf8
Коммит 53e5c741a2
4 изменённых файлов: 149 добавлений и 3 удалений

Просмотреть файл

@ -60,5 +60,20 @@ add_task(function* test_healthreport_search_recording() {
let newCount = day.get(field);
is(newCount, oldCount + 1, "We recorded one new search.");
// We should record the default search engine if Telemetry is enabled.
// Test with disabled first.
let oldTelemetry = Services.prefs.getBoolPref("toolkit.telemetry.enabled");
Services.prefs.setBoolPref("toolkit.telemetry.enabled", true);
yield provider.collectDailyData();
data = yield m.getValues();
ok(data.days.hasDay(now), "Have engines data when Telemetry is enabled.");
day = data.days.getDay(now);
ok(day.has("default"), "We have default engine data.");
is(day.get("default"), "google", "The default engine is reported properly.");
// Restore.
Services.prefs.setBoolPref("toolkit.telemetry.enabled", oldTelemetry);
gBrowser.removeTab(tab);
});

Просмотреть файл

@ -1402,6 +1402,36 @@ Example
"google.urlbar": 7
},
org.mozilla.searches.engines
----------------------------
This measurement contains information about search engines.
Version 1
^^^^^^^^^
This version debuted with Firefox 31 on desktop. It contains the
following properties:
default
Daily string identifier or name of the default search engine provider.
This field will only be collected if Telemetry is enabled. If
Telemetry is enabled and then later disabled, this field may
disappear from future days in the payload.
The special value ``NONE`` could occur if there is no default search
engine.
The special value ``UNDEFINED`` could occur if a default search
engine exists but its identifier could not be determined.
This field's contents are
``Services.search.defaultEngine.identifier`` (if defined) or
``"other-"`` + ``Services.search.defaultEngine.name`` if not.
In other words, search engines without an ``.identifier``
are prefixed with ``other-``.
org.mozilla.sync.sync
---------------------

Просмотреть файл

@ -55,10 +55,15 @@ const LAST_NUMERIC_FIELD = {type: Metrics.Storage.FIELD_LAST_NUMERIC};
const LAST_TEXT_FIELD = {type: Metrics.Storage.FIELD_LAST_TEXT};
const DAILY_DISCRETE_NUMERIC_FIELD = {type: Metrics.Storage.FIELD_DAILY_DISCRETE_NUMERIC};
const DAILY_LAST_NUMERIC_FIELD = {type: Metrics.Storage.FIELD_DAILY_LAST_NUMERIC};
const DAILY_LAST_TEXT_FIELD = {type: Metrics.Storage.FIELD_DAILY_LAST_TEXT};
const DAILY_COUNTER_FIELD = {type: Metrics.Storage.FIELD_DAILY_COUNTER};
const TELEMETRY_PREF = "toolkit.telemetry.enabled";
function isTelemetryEnabled(prefs) {
return prefs.get(TELEMETRY_PREF, false);
}
/**
* Represents basic application state.
*
@ -353,7 +358,7 @@ AppInfoProvider.prototype = Object.freeze({
},
_recordIsTelemetryEnabled: function (m) {
let enabled = TELEMETRY_PREF && this._prefs.get(TELEMETRY_PREF, false);
let enabled = isTelemetryEnabled(this._prefs);
this._log.debug("Recording telemetry enabled (" + TELEMETRY_PREF + "): " + enabled);
yield m.setDailyLastNumeric("isTelemetryEnabled", enabled ? 1 : 0);
},
@ -1282,8 +1287,25 @@ SearchCountMeasurement3.prototype = Object.freeze({
},
});
function SearchEnginesMeasurement1() {
Metrics.Measurement.call(this);
}
SearchEnginesMeasurement1.prototype = Object.freeze({
__proto__: Metrics.Measurement.prototype,
name: "engines",
version: 1,
fields: {
default: DAILY_LAST_TEXT_FIELD,
},
});
this.SearchesProvider = function () {
Metrics.Provider.call(this);
this._prefs = new Preferences({defaultBranch: null});
};
this.SearchesProvider.prototype = Object.freeze({
@ -1294,6 +1316,7 @@ this.SearchesProvider.prototype = Object.freeze({
SearchCountMeasurement1,
SearchCountMeasurement2,
SearchCountMeasurement3,
SearchEnginesMeasurement1,
],
/**
@ -1308,6 +1331,36 @@ this.SearchesProvider.prototype = Object.freeze({
return deferred.promise;
},
collectDailyData: function () {
return this.storage.enqueueTransaction(function getDaily() {
// We currently only record this if Telemetry is enabled.
if (!isTelemetryEnabled(this._prefs)) {
return;
}
let m = this.getMeasurement(SearchEnginesMeasurement1.prototype.name,
SearchEnginesMeasurement1.prototype.version);
let engine;
try {
engine = Services.search.defaultEngine;
} catch (e) {}
let name;
if (!engine) {
name = "NONE";
} else if (engine.identifier) {
name = engine.identifier;
} else if (engine.name) {
name = "other-" + engine.name;
} else {
name = "UNDEFINED";
}
yield m.setDailyLastText("default", name);
}.bind(this));
},
/**
* Record that a search occurred.
*

Просмотреть файл

@ -6,6 +6,7 @@
const {utils: Cu} = Components;
Cu.import("resource://gre/modules/Metrics.jsm");
Cu.import("resource://gre/modules/Services.jsm");
let bsp = Cu.import("resource://gre/modules/services/healthreport/providers.jsm");
const DEFAULT_ENGINES = [
@ -41,7 +42,7 @@ add_test(function test_constructor() {
run_next_test();
});
add_task(function test_record() {
add_task(function* test_record() {
let storage = yield Metrics.Storage("record");
let provider = new MockSearchesProvider();
@ -104,7 +105,7 @@ add_task(function test_record() {
yield storage.close();
});
add_task(function test_includes_other_fields() {
add_task(function* test_includes_other_fields() {
let storage = yield Metrics.Storage("includes_other_fields");
let provider = new MockSearchesProvider();
@ -134,3 +135,50 @@ add_task(function test_includes_other_fields() {
yield storage.close();
});
add_task(function* test_default_search_engine() {
let storage = yield Metrics.Storage("default_search_engine");
let provider = new SearchesProvider();
yield provider.init(storage);
let m = provider.getMeasurement("engines", 1);
// Ensure no collection if Telemetry not enabled.
Services.prefs.setBoolPref("toolkit.telemetry.enabled", false);
let now = new Date();
yield provider.collectDailyData();
let data = yield m.getValues();
Assert.equal(data.days.hasDay(now), false);
// Now enable telemetry and ensure we populate.
Services.prefs.setBoolPref("toolkit.telemetry.enabled", true);
yield provider.collectDailyData();
data = yield m.getValues();
Assert.ok(data.days.hasDay(now));
let day = data.days.getDay(now);
Assert.equal(day.size, 1);
Assert.ok(day.has("default"));
// test environment doesn't have a default engine.
Assert.equal(day.get("default"), "NONE");
Services.search.addEngineWithDetails("testdefault",
"http://localhost/icon.png",
null,
"test description",
"GET",
"http://localhost/search/%s");
let engine1 = Services.search.getEngineByName("testdefault");
Assert.ok(engine1);
Services.search.defaultEngine = engine1;
yield provider.collectDailyData();
data = yield m.getValues();
Assert.equal(data.days.getDay(now).get("default"), "other-testdefault");
yield storage.close();
});