Chromium Code Reviews| 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..5994cf24d6daf45a33c7a9bd3e5ff16cc5c7175d 100644 |
| --- a/appengine/swarming/elements/res/js/common.js |
| +++ b/appengine/swarming/elements/res/js/common.js |
| @@ -3,6 +3,7 @@ |
| // that can be found in the LICENSE file. |
| window.swarming = window.swarming || function() { |
|
jcgregorio
2016/08/08 16:14:42
None of this code depends on the DOM, so it should
kjlubick
2016/08/08 19:07:10
Changed window -> this, like we do for Skia in htt
|
| + |
| 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) { |
| + 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; |