Skip to content

Instantly share code, notes, and snippets.

@mreid-moz
Forked from georgf/TelemetryArchiveValidation.js
Last active August 29, 2015 14:27
Show Gist options
  • Save mreid-moz/98f34ed93e4bec640af4 to your computer and use it in GitHub Desktop.
Save mreid-moz/98f34ed93e4bec640af4 to your computer and use it in GitHub Desktop.
TelemetryArchive validation scratchpad
/*
* This is to be used from a Firefox scratchpad:
* - enable chrome devtools: in about:config set "devtools.chrome.enabled" to true
* - open scratchpad: Tools -> Web Developer -> Scratchpad
* - make it run as chrome: choose Environment -> Browser
* - click "Run"
*/
(function() {
Cu.import("resource://gre/modules/TelemetryArchive.jsm");
function getMainWindow() {
return window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
}
function showTextInNewTab(str) {
let win = getMainWindow();
let tab = win.gBrowser.addTab("data:text/plain," + encodeURIComponent(str));
win.gBrowser.selectedTab = tab;
}
function showHtmlInNewTab(str) {
let win = getMainWindow();
let tab = win.gBrowser.addTab("data:text/html," + encodeURIComponent(str));
win.gBrowser.selectedTab = tab;
}
Task.spawn(function*() {
// Retrieve a list of the archived main pings.
let pings = yield TelemetryArchive.promiseArchivedPingList();
pings = pings.filter(p => p.type == "main");
// Load and process the pings.
let data = [];
let previous = null;
let foundNewerBuild = false;
for (let archived of pings) {
let p;
try {
p = yield TelemetryArchive.promiseArchivedPingById(archived.id);
} catch (e) {
data.push({id: archived.id, timestampCreated: archived.timestampCreated, fileNotFound: true, isBroken: true})
}
// Skip all leading pings from build ids that are too old.
const isFromOldBuild = (parseInt(p.application.buildId, 10) < 20150722000000);
if (!foundNewerBuild && isFromOldBuild) {
continue;
}
foundNewerBuild = true;
// Build up reduced and flat ping data to work on.
const info = p.payload.info;
let current = {
pingId: p.id,
clientId: p.clientId,
reason: info.reason,
creationDate: p.creationDate,
channel: p.application.channel,
buildId: p.application.buildId,
sessionId: info.sessionId,
subsessionId: info.subsessionId,
previousSessionId: info.previousSessionId,
previousSubsessionId: info.previousSubsessionId,
profileSubsessionCounter: info.profileSubsessionCounter,
subsessionCounter: info.subsessionCounter,
sessionLength: info.sessionLength,
subsessionLength: info.subsessionLength,
isFromOldBuild: isFromOldBuild,
};
// Check for consistency issues etc.
if (previous) {
const c = current;
const p = previous;
const previousIsFinalFragment = (p.reason == "shutdown" || p.reason == "aborted-session");
c.channelSwitching = (c.channel != p.channel);
c.brokenSessionChain = previousIsFinalFragment && (c.previousSessionId != p.sessionId);
c.brokenSubsessionChain = (c.previousSubsessionId != p.subsessionId);
c.brokenProfileSubsessionCounter = (c.profileSubsessionCounter != (p.profileSubsessionCounter + 1));
c.brokenSubsessionCounter = (previousIsFinalFragment ?
(c.subsessionCounter != 1) :
(c.subsessionCounter != (p.subsessionCounter + 1)));
c.isBroken = !c.isFromOldBuild && !p.isFromOldBuild &&
!c.channelSwitching &&
(c.brokenSessionChain ||
c.brokenSubsessionChain ||
c.brokenProfileSubsessionCounter ||
c.brokenSubsessionCounter);
}
data.push(current);
previous = current;
}
// Fields to print in the order we want them listed.
const printFields = [
"pingId", "clientId", "reason", "channel", "buildId", "creationDate",
"sessionId", "previousSessionId", "subsessionId", "previousSubsessionId",
"profileSubsessionCounter", "subsessionCounter",
"sessionLength", "subsessionLength",
"fileNotFound", "channelSwitching", "brokenSessionChain", "brokenSubsessionChain", "brokenProfileSubsessionCounter", "brokenSubsessionCounter",
];
// Print an html table from the data.
let text = "<style type='text/css'>" +
"table { border-collapse: collapse; }" +
"th, td { border: solid 1px; }" +
"tr.broken { background-color: yellow; }" +
"</style>";
text += "<table>";
text += "<tr>" + [for (f of printFields) `<th>${f}</th>`].join("") + "</tr>";
for (let d of data) {
text += `<tr ${d.isBroken ? ' class="broken"' : ''}>`;
text += [for (f of printFields) `<td title="${f}">${d[f] != undefined ? d[f] : "-"}</td>`].join("");
text += "</tr>";
}
text += "</table>";
showHtmlInNewTab(text);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment