| OLD | NEW |
| (Empty) |
| 1 <!-- | |
| 2 Copyright 2016 The LUCI Authors. All rights reserved. | |
| 3 Use of this source code is governed under the Apache License, Version 2.0 | |
| 4 that can be found in the LICENSE file. | |
| 5 | |
| 6 It contains the definition of the following Behaviors: | |
| 7 | |
| 8 SwarmingBehaviors.CommonBehavior | |
| 9 | |
| 10 To use it, include | |
| 11 behaviors: [SwarmingBehaviors.CommonBehavior] | |
| 12 in the creation of any Polymer element. | |
| 13 | |
| 14 SwarmingBehaviors.CommonBehavior contains shared methods to ease | |
| 15 templating, such as _or() and _not() as well as general utility methods | |
| 16 such as _getJsonAsync. | |
| 17 --> | |
| 18 | |
| 19 <script> | |
| 20 window.SwarmingBehaviors = window.SwarmingBehaviors || {}; | |
| 21 (function(){ | |
| 22 // This behavior wraps up all the shared swarming functionality. | |
| 23 SwarmingBehaviors.CommonBehavior = { | |
| 24 | |
| 25 _botLink: function(bot_id) { | |
| 26 if (!bot_id) { | |
| 27 return undefined; | |
| 28 } | |
| 29 return "/newui/bot?id=" + bot_id; | |
| 30 }, | |
| 31 | |
| 32 // Create a link to a bot list with the preloaded filters and columns. | |
| 33 // filters should be an array of {key:String, value:String} and | |
| 34 // columns should be an array of Strings. | |
| 35 _botListLink: function(filters, columns) { | |
| 36 filters = filters || []; | |
| 37 columns = columns || []; | |
| 38 var fArr = []; | |
| 39 filters.forEach(function(f){ | |
| 40 fArr.push(f.key + ":" + f.value); | |
| 41 }); | |
| 42 var obj = { | |
| 43 f: fArr, | |
| 44 c: columns, | |
| 45 } | |
| 46 return "/newui/botlist?" + sk.query.fromParamSet(obj); | |
| 47 }, | |
| 48 | |
| 49 // Create a link to a bot in Google Cloud Console. Cloud console will try | |
| 50 // to find the bot in the last project the user was logged in as, which | |
| 51 // is the best we can do atm. | |
| 52 _cloudConsoleLink: function(zone, bot_id) { | |
| 53 return `http://console.cloud.google.com/compute/instancesDetail/zones/${
zone}/instances/${bot_id}` | |
| 54 }, | |
| 55 | |
| 56 // _getJsonAsync makes an XHR to a url, parses the response as JSON | |
| 57 // and sticks the resulting object into the property with the name given | |
| 58 // by "bindTo". If busy is defined, the property with that name will be | |
| 59 // set to true while the request is in flight and false afterwards. | |
| 60 // request headers (e.g. authentication) and query params will be used if | |
| 61 // provided. Query params is an object like {String:Array<String>}. On | |
| 62 // error, bindTo will be set to false. It is not set to undefined | |
| 63 // because computed values in Polymer don't fire if a property is | |
| 64 // undefined. Clients should check that bindTo is not falsey. | |
| 65 // To avoid multiple requests clobering one another, an object _jsonAsync | |
| 66 // is created on "this" to debounce requests - the most recent request | |
| 67 // will win out. | |
| 68 _getJsonAsync: function(bindTo, url, busy, headers, params) { | |
| 69 if (!bindTo || !url || !busy) { | |
| 70 console.log("Need at least a polymer element to bind to, a busy elemen
t, and a url"); | |
| 71 return; | |
| 72 } | |
| 73 this.set(busy, true); | |
| 74 var now = new Date(); | |
| 75 this._jsonAsync = this._jsonAsync || {}; | |
| 76 this._jsonAsync[bindTo] = now; | |
| 77 if (params) { | |
| 78 url = url + "?" + sk.query.fromParamSet(params); | |
| 79 } | |
| 80 sk.request("GET", url, "", headers).then(JSON.parse).then(function(json)
{ | |
| 81 if (this._jsonAsync[bindTo] !== now) { | |
| 82 console.log("ignoring result because a second request happened."); | |
| 83 this.set(busy, false); | |
| 84 return; | |
| 85 } | |
| 86 this.set(bindTo, json); | |
| 87 this.set(busy, false); | |
| 88 }.bind(this)).catch(function(reason){ | |
| 89 console.log("Reason for failure of request to " + url, reason); | |
| 90 | |
| 91 if (this._jsonAsync[bindTo] !== now) { | |
| 92 console.log("ignoring failure because a second request happened."); | |
| 93 this.set(busy, false); | |
| 94 return; | |
| 95 } | |
| 96 this.set(bindTo, false); | |
| 97 this.set(busy, false); | |
| 98 }.bind(this)); | |
| 99 }, | |
| 100 | |
| 101 _humanDuration: function(timeInSecs) { | |
| 102 return sk.human.strDuration(timeInSecs) || "0s"; | |
| 103 }, | |
| 104 | |
| 105 _not: function(a) { | |
| 106 return !a; | |
| 107 }, | |
| 108 | |
| 109 _or: function() { | |
| 110 var result = false; | |
| 111 // can't use .foreach, as arguments isn't really an Array. | |
| 112 for (var i = 0; i < arguments.length; i++) { | |
| 113 result = result || arguments[i]; | |
| 114 } | |
| 115 return result; | |
| 116 }, | |
| 117 | |
| 118 _taskLink: function(taskId, disableCanonicalID) { | |
| 119 if (!taskId) { | |
| 120 return undefined; | |
| 121 } | |
| 122 if (!disableCanonicalID) { | |
| 123 // task abcefgh0 is the "canonical" task id. The first try has the id | |
| 124 // abcefgh1. If there is a second (transparent retry), it will be | |
| 125 // abcefgh2. We almost always want to link to the canonical one, | |
| 126 // because the milo output (if any) will only be generated for | |
| 127 // abcefgh0, not abcefgh1 or abcefgh2. | |
| 128 taskId = taskId.substring(0, taskId.length - 1) + "0"; | |
| 129 } | |
| 130 return "/newui/task?id=" + taskId; | |
| 131 }, | |
| 132 | |
| 133 // _timeDiffApprox returns the approximate difference between now and | |
| 134 // the specified date. | |
| 135 _timeDiffApprox: function(date){ | |
| 136 if (!date) { | |
| 137 return "eons"; | |
| 138 } | |
| 139 return sk.human.diffDate(date.getTime()); | |
| 140 }, | |
| 141 | |
| 142 // timeDiffExact returns the exact difference between the two specified | |
| 143 // dates. E.g. 2d 22h 22m 28s ago If a second date is not provided, | |
| 144 // now is used. | |
| 145 _timeDiffExact: function(first, second){ | |
| 146 if (!first) { | |
| 147 return "eons"; | |
| 148 } | |
| 149 if (!second) { | |
| 150 second = new Date(); | |
| 151 } | |
| 152 return this._humanDuration((second.getTime() - first.getTime())/1000); | |
| 153 }, | |
| 154 | |
| 155 _truthy: function(a){ | |
| 156 return !!a; | |
| 157 } | |
| 158 }; | |
| 159 })(); | |
| 160 </script> | |
| OLD | NEW |