Bug 828546 - Part 1: Support Health Report mode when gathering Places Telemetry; r=mak

This commit is contained in:
Gregory Szorc 2013-02-05 10:44:14 -08:00
parent 414925c4e3
commit f730c985a2
2 changed files with 52 additions and 6 deletions

View File

@ -817,13 +817,25 @@ this.PlacesDBUtils = {
/**
* Collects telemetry data.
*
* There are essentially two modes of collection and the mode is
* determined by the presence of aHealthReportCallback. If
* aHealthReportCallback is not defined (the default) then we are in
* "Telemetry" mode. Results will be reported to Telemetry. If we are
* in "Health Report" mode only the probes with a true healthreport
* flag will be collected and the results will be reported to the
* aHealthReportCallback.
*
* @param [optional] aTasks
* Tasks object to execute.
* @param [optional] aHealthReportCallback
* Function to receive data relevant for Firefox Health Report.
*/
telemetry: function PDBU_telemetry(aTasks)
telemetry: function PDBU_telemetry(aTasks, aHealthReportCallback=null)
{
let tasks = new Tasks(aTasks);
let isTelemetry = !aHealthReportCallback;
// This will be populated with one integer property for each probe result,
// using the histogram name as key.
let probeValues = {};
@ -840,15 +852,19 @@ this.PlacesDBUtils = {
// histogram. If a query is also present, its result is passed
// as the first argument of the function. If the function
// raises an exception, no data is added to the histogram.
// healthreport: Boolean indicating whether this probe is relevant
// to Firefox Health Report.
//
// Since all queries are executed in order by the database backend, the
// callbacks can also use the result of previous queries stored in the
// probeValues object.
let probes = [
{ histogram: "PLACES_PAGES_COUNT",
healthreport: true,
query: "SELECT count(*) FROM moz_places" },
{ histogram: "PLACES_BOOKMARKS_COUNT",
healthreport: true,
query: "SELECT count(*) FROM moz_bookmarks b "
+ "JOIN moz_bookmarks t ON t.id = b.parent "
+ "AND t.parent <> :tags_folder "
@ -944,7 +960,11 @@ this.PlacesDBUtils = {
places_root: PlacesUtils.placesRootId
};
function reportTelemetry(aProbe, aValue) {
let outstandingProbes = 0;
function reportResult(aProbe, aValue) {
outstandingProbes--;
try {
let value = aValue;
if ("callback" in aProbe) {
@ -956,13 +976,27 @@ this.PlacesDBUtils = {
} catch (ex) {
Components.utils.reportError(ex);
}
if (!outstandingProbes && aHealthReportCallback) {
try {
aHealthReportCallback(probeValues);
} catch (ex) {
Components.utils.reportError(ex);
}
}
}
for (let i = 0; i < probes.length; i++) {
let probe = probes[i];
if (!isTelemetry && !probe.healthreport) {
continue;
}
outstandingProbes++;
if (!("query" in probe)) {
reportTelemetry(probe);
reportResult(probe);
continue;
}
@ -978,7 +1012,7 @@ this.PlacesDBUtils = {
handleError: PlacesDBUtils._handleError,
handleResult: function (aResultSet) {
let row = aResultSet.getNextRow();
reportTelemetry(probe, row.getResultByIndex(0));
reportResult(probe, row.getResultByIndex(0));
},
handleCompletion: function () {}
});

View File

@ -128,5 +128,17 @@ add_task(function test_execute()
validate(snapshot.sum);
do_check_true(snapshot.counts.reduce(function(a, b) a + b) > 0);
}
do_test_finished();
});
add_test(function test_healthreport_callback() {
PlacesDBUtils.telemetry(null, function onResult(data) {
do_check_neq(data, null);
do_check_eq(Object.keys(data).length, 2);
do_check_eq(data.PLACES_PAGES_COUNT, 1);
do_check_eq(data.PLACES_BOOKMARKS_COUNT, 1);
run_next_test();
});
});