| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 this.swarming = this.swarming || function() { | 5 this.swarming = this.swarming || function() { |
| 6 | 6 |
| 7 var swarming = {}; | 7 var swarming = {}; |
| 8 | 8 |
| 9 swarming.stableSort = function(arr, comp) { | 9 swarming.stableSort = function(arr, comp) { |
| 10 if (!arr || !comp) { | 10 if (!arr || !comp) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 swarming.naturalCompare = function(a, b) { | 43 swarming.naturalCompare = function(a, b) { |
| 44 // Try numeric, aka "natural" sort and use it if ns is not NaN. | 44 // Try numeric, aka "natural" sort and use it if ns is not NaN. |
| 45 // Javascript will try to corece these to numbers or return NaN. | 45 // Javascript will try to corece these to numbers or return NaN. |
| 46 var ns = a - b; | 46 var ns = a - b; |
| 47 if (!isNaN(ns)) { | 47 if (!isNaN(ns)) { |
| 48 return ns; | 48 return ns; |
| 49 } | 49 } |
| 50 return a.localeCompare(b); | 50 return a.localeCompare(b); |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 | |
| 54 var TIME_DELTAS = [ | |
| 55 { units: "w", delta: 7*24*60*60 }, | |
| 56 { units: "d", delta: 24*60*60 }, | |
| 57 { units: "h", delta: 60*60 }, | |
| 58 { units: "m", delta: 60 }, | |
| 59 { units: "s", delta: 1 }, | |
| 60 ]; | |
| 61 | |
| 62 /** | |
| 63 * Returns the difference between the specified time and 's' as a string in a | |
| 64 * human friendly format. | |
| 65 * If left unspecified, "now" defaults to Date.now() | |
| 66 * If 's' is a number it is assumed to contain the time in milliseconds | |
| 67 * otherwise it is assumed to contain a time string. | |
| 68 * | |
| 69 * For example, a difference of 123 seconds between 's' and the current time | |
| 70 * would return "2m". | |
| 71 */ | |
| 72 swarming.diffDate = function(s, now) { | |
| 73 var ms = (typeof(s) == "number") ? s : Date.parse(s); | |
| 74 now = now || Date.now(); | |
| 75 var diff = (ms - now)/1000; | |
| 76 if (diff < 0) { | |
| 77 diff = -1.0 * diff; | |
| 78 } | |
| 79 return humanize(diff, TIME_DELTAS); | |
| 80 }; | |
| 81 | |
| 82 swarming.KB = 1024; | |
| 83 swarming.MB = swarming.KB * 1024; | |
| 84 swarming.GB = swarming.MB * 1024 | |
| 85 | |
| 86 var BYTES_DELTAS = [ | |
| 87 { units: " TB", delta: 1024*swarming.GB}, | |
| 88 { units: " GB", delta: swarming.GB}, | |
| 89 { units: " MB", delta: swarming.MB}, | |
| 90 { units: " KB", delta: swarming.KB}, | |
| 91 { units: " B", delta: 1}, | |
| 92 ]; | |
| 93 | |
| 94 swarming.humanBytes = function(b, unit) { | |
| 95 if (Number.isInteger(unit)) { | |
| 96 b = b * unit; | |
| 97 } | |
| 98 return humanize(b, BYTES_DELTAS); | |
| 99 } | |
| 100 | |
| 101 function humanize(n, deltas) { | |
| 102 for (var i=0; i<deltas.length-1; i++) { | |
| 103 // If n would round to '60s', return '1m' instead. | |
| 104 var nextDeltaRounded = | |
| 105 Math.round(n/deltas[i+1].delta)*deltas[i+1].delta; | |
| 106 if (nextDeltaRounded/deltas[i].delta >= 1) { | |
| 107 return Math.round(n/deltas[i].delta)+deltas[i].units; | |
| 108 } | |
| 109 } | |
| 110 var i = DELTAS.length-1; | |
| 111 return Math.round(n/DELTAS[i].delta)+DELTAS[i].units; | |
| 112 } | |
| 113 | |
| 114 return swarming; | 53 return swarming; |
| 115 }(); | 54 }(); |
| OLD | NEW |