Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2476)

Unified Diff: appengine/swarming/elements/res/js/common.js

Issue 2211163003: Update new botlist to use dimensions endpoint (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@limiting
Patch Set: put demo data in luci-py wiki Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: appengine/swarming/elements/res/js/common.js
diff --git a/appengine/swarming/elements/res/js/common.js b/appengine/swarming/elements/res/js/common.js
index 0222686f2ceec2ad6e2b6d2fdc7c04ae1b7ff7a3..89038897419b998b30aa211d6cc495cf3f8d7000 100644
--- a/appengine/swarming/elements/res/js/common.js
+++ b/appengine/swarming/elements/res/js/common.js
@@ -2,7 +2,8 @@
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
-window.swarming = window.swarming || function() {
+this.swarming = this.swarming || function() {
+
var swarming = {};
swarming.stableSort = function(arr, comp) {
@@ -43,10 +44,71 @@ window.swarming = window.swarming || function() {
// Try numeric, aka "natural" sort and use it if ns is not NaN.
// Javascript will try to corece these to numbers or return NaN.
var ns = a - b;
- if (ns) {
+ if (!isNaN(ns)) {
return ns;
}
return a.localeCompare(b);
+ };
+
+
+ var TIME_DELTAS = [
+ { units: "w", delta: 7*24*60*60 },
+ { units: "d", delta: 24*60*60 },
+ { units: "h", delta: 60*60 },
+ { units: "m", delta: 60 },
+ { units: "s", delta: 1 },
+ ];
+
+ /**
+ * Returns the difference between the specified time and 's' as a string in a
+ * human friendly format.
+ * If left unspecified, "now" defaults to Date.now()
+ * If 's' is a number it is assumed to contain the time in milliseconds
+ * otherwise it is assumed to contain a time string.
+ *
+ * For example, a difference of 123 seconds between 's' and the current time
+ * would return "2m".
+ */
+ swarming.diffDate = function(s, now) {
jcgregorio 2016/08/08 20:54:32 This is all copied from common.js. We should discu
kjlubick 2016/08/09 12:07:21 Agreed.
+ var ms = (typeof(s) == "number") ? s : Date.parse(s);
+ now = now || Date.now();
+ var diff = (ms - now)/1000;
+ if (diff < 0) {
+ diff = -1.0 * diff;
+ }
+ return humanize(diff, TIME_DELTAS);
+ };
+
+ swarming.KB = 1024;
+ swarming.MB = swarming.KB * 1024;
+ swarming.GB = swarming.MB * 1024
+
+ var BYTES_DELTAS = [
+ { units: " TB", delta: 1024*swarming.GB},
+ { units: " GB", delta: swarming.GB},
+ { units: " MB", delta: swarming.MB},
+ { units: " KB", delta: swarming.KB},
+ { units: " B", delta: 1},
+ ];
+
+ swarming.humanBytes = function(b, unit) {
+ if (Number.isInteger(unit)) {
+ b = b * unit;
+ }
+ return humanize(b, BYTES_DELTAS);
+ }
+
+ function humanize(n, deltas) {
+ for (var i=0; i<deltas.length-1; i++) {
+ // If n would round to '60s', return '1m' instead.
+ var nextDeltaRounded =
+ Math.round(n/deltas[i+1].delta)*deltas[i+1].delta;
+ if (nextDeltaRounded/deltas[i].delta >= 1) {
+ return Math.round(n/deltas[i].delta)+deltas[i].units;
+ }
+ }
+ var i = DELTAS.length-1;
+ return Math.round(n/DELTAS[i].delta)+DELTAS[i].units;
}
return swarming;

Powered by Google App Engine
This is Rietveld 408576698