Skip to content

Instantly share code, notes, and snippets.

@darkowlzz
Last active December 16, 2015 12:09
Show Gist options
  • Save darkowlzz/5432896 to your computer and use it in GitHub Desktop.
Save darkowlzz/5432896 to your computer and use it in GitHub Desktop.
let Histogram = {
hgramSamplesCaption: bundle.GetStringFromName("histogramSamples"),
hgramAverageCaption: bundle.GetStringFromName("histogramAverage"),
hgramSumCaption: bundle.GetStringFromName("histogramSum"),
hgramCopyCaption: bundle.GetStringFromName("histogramCopy"),
/**
* Renders a single Telemetry histogram
*
* @param aParent Parent element
* @param aName Histogram name
* @param aHgram Histogram information
*/
render: function Histogram_render(aParent, aName, aHgram) {
let hgram = this.unpack(aHgram);
let outerDiv = document.createElement("div");
outerDiv.className = "histogram";
outerDiv.id = aName;
let divTitle = document.createElement("div");
divTitle.className = "histogram-title";
divTitle.appendChild(document.createTextNode(aName));
let copyLink = document.createElement("a");
copyLink.className = "copy-text";
copyLink.appendChild(document.createTextNode(this.hgramCopyCaption));
copyLink.id = "COPY_" + aName;
copyLink.classList.add("hidden");
copyLink.addEventListener("click", this.copy, false);
divTitle.appendChild(copyLink);
outerDiv.appendChild(divTitle);
outerDiv.addEventListener("mouseenter", this.showCopyLink, false);
outerDiv.addEventListener("mouseleave", this.hideCopyLink, false);
let stats = hgram.sample_count + " " + this.hgramSamplesCaption + ", " +
this.hgramAverageCaption + " = " + hgram.pretty_average + ", " +
this.hgramSumCaption + " = " + hgram.sum;
let divStats = document.createElement("div");
divStats.appendChild(document.createTextNode(stats));
outerDiv.appendChild(divStats);
if (isRTL())
hgram.values.reverse();
this.renderValues(outerDiv, hgram.values, hgram.max);
aParent.appendChild(outerDiv);
},
/**
* Show Copy-Histogram link
*
* @param aEvent node which calls this method
*/
showCopyLink: function Histogram_showCopyLink(aEvent) {
let id = "COPY_" + aEvent.target.id;
let elem = document.getElementById(id);
elem.classList.remove("hidden");
},
/**
* Hide Copy-Histogram link
*
* @param aEvent node which calls this method
*/
hideCopyLink: function Histogram_hideCopyLink(aEvent) {
let id = "COPY_" + aEvent.target.id;
let elem = document.getElementById(id);
elem.classList.add("hidden");
},
/**
* Copy text histogram to clipboard.
*
* @param aEvent node which calls this method
*/
copy: function Histogram_copy(aEvent) {
let histId = aEvent.target.id.slice(5);
let hgram = Telemetry.histogramSnapshots[histId];
// Get the required data.
let sample_count = hgram.counts.reduce(function (a, b) a + b);
let average = hgram.sum.toFixed(1);
let buckets = [0, 1];
if (hgram.histogram_type != Telemetry.HISTOGRAM_BOOLEAN) {
buckets = hgram.ranges;
}
// Form the text histogram string.
let stats = sample_count + " " + Histogram.hgramSamplesCaption + ", " +
Histogram.hgramAverageCaption + " = " + average + ", " +
Histogram.hgramSumCaption + " = " + hgram.sum;
var clipboardData = histId + "\n" + stats + "\n" + Array(42).join("-");
function copyToClipboardData(label, value, labelLength, barLength) {
console.log("using the new func");
clipboardData += "\n" + label + Array(labelLength).join(" ") + ": ";
clipboardData += Array(barLength).join("#");
clipboardData += " " + value;
}
let first = true;
let last = 0;
let label = null;
let value = null;
let maxBucket = Math.max.apply(Math, hgram.counts);
let barLength = null;
let labelLength = null;
console.log("buckets length: " + buckets.length);
for (let i = 0; i < buckets.length - 1; i++) {
value = hgram.counts[i];
label = String(buckets[i]);
if (!value)
continue;
last = i + 1;
if (first) {
first = false;
if (i) {
let newlabel = String(buckets[i-1]);
let newvalue = 0;
barLength = Math.round(MAX_COPY_BAR_HEIGHT * (newvalue / maxBucket));
labelLength = MAX_DIGITS - (newlabel.length - 1);
/*
clipboardData += "\n" + newlabel + Array(labelLength).join(" ") + ": ";
clipboardData += Array(barLength).join("#");
clipboardData += " " + newvalue;
*/
copyToClipboardData(newlabel, newvalue, labelLength, barLength);
}
}
barLength = Math.round(MAX_COPY_BAR_HEIGHT * (value / maxBucket));
console.log("label: " + label);
console.log("bar length: " + barLength + "\n");
labelLength = MAX_DIGITS - (label.length - 1);
copyToClipboardData(label, value, labelLength, barLength);
}
if (last && last < hgram.ranges.length) {
console.log("inside last");
label = String(buckets[last]);
value = 0;
console.log("labal: " + label);
console.log("value: " + value);
barLength = Math.round(MAX_COPY_BAR_HEIGHT * (value / maxBucket));
console.log("bar length: " + barLength + "\n");
labelLength = MAX_DIGITS - (label.length - 1);
/*
clipboardData += "\n" + label + Array(labelLength).join(" ") + ": ";
clipboardData += array(barLength).join("#");
clipboardData += " " + value;
*/
copyToClipboardData(label, value, labelLength, barLength);
}
console.log("MaxBucket: " + maxBucket);
Histogram.copyToClipboard(clipboardData);
},
/**
* Copy data to the clipboard
*
* @param clipboardData Data to be copied to the clipboard
*/
copyToClipboard: function Histogram_copyToClipboard(clipboardData) {
let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
str.data = clipboardData;
let transferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
transferable.init(window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsILoadContext));
transferable.addDataFlavor("text/unicode");
transferable.setTransferData("text/unicode", str, str.data.length * 2);
Cc["@mozilla.org/widget/clipboard;1"].
getService(Ci.nsIClipboard).
setData(transferable, null, Ci.nsIClipboard.kGlobalClipboard);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment