| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright 2016 The LUCI Authors. All rights reserved. | 2 Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 Use of this source code is governed under the Apache License, Version 2.0 | 3 Use of this source code is governed under the Apache License, Version 2.0 |
| 4 that can be found in the LICENSE file. | 4 that can be found in the LICENSE file. |
| 5 | 5 |
| 6 This in an HTML Import-able file that contains the definition | 6 This in an HTML Import-able file that contains the definition |
| 7 of the following elements: | 7 of the following elements: |
| 8 | 8 |
| 9 <bot-page-data> | 9 <bot-page-data> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 Methods: | 26 Methods: |
| 27 request(): Force a fetch of the data. This happens automatically when | 27 request(): Force a fetch of the data. This happens automatically when |
| 28 auth_headers is set or bot_id is changed. | 28 auth_headers is set or bot_id is changed. |
| 29 | 29 |
| 30 Events: | 30 Events: |
| 31 None. | 31 None. |
| 32 --> | 32 --> |
| 33 | 33 |
| 34 | 34 |
| 35 <link rel="import" href="/res/imp/common/common-behavior.html"> | 35 <link rel="import" href="bot-page-shared-behavior.html"> |
| 36 | 36 |
| 37 <dom-module id="bot-page-data"> | 37 <dom-module id="bot-page-data"> |
| 38 <script> | 38 <script> |
| 39 (function(){ | 39 (function(){ |
| 40 // Time to wait before requesting a new bot. This is to allow a user to |
| 41 // type in a name and not have it make one set of requests for each |
| 42 // keystroke. |
| 43 var BOT_ID_DEBOUNCE_MS = 400; |
| 44 var lastRequest; |
| 45 |
| 46 var BOT_TIMES = ["first_seen_ts", "last_seen_ts"]; |
| 47 var TASK_TIMES = ["started_ts", "completed_ts", "abandoned_ts", "modified_ts
"]; |
| 48 |
| 49 var timezone; |
| 50 function formatDate(date) { |
| 51 if (!timezone) { |
| 52 // Date.toString() looks like "Mon Aug 29 2016 09:03:41 GMT-0400 (EDT)" |
| 53 // we want to extract the time zone part and append it to the |
| 54 // locale time. |
| 55 var str = date.toString(); |
| 56 timezone = str.substring(str.indexOf("(")); |
| 57 } |
| 58 return date.toLocaleString() + " " + timezone; |
| 59 } |
| 40 | 60 |
| 41 Polymer({ | 61 Polymer({ |
| 42 is: 'bot-page-data', | 62 is: 'bot-page-data', |
| 43 | 63 |
| 44 behaviors: [ | 64 behaviors: [ |
| 45 SwarmingBehaviors.CommonBehavior, | 65 SwarmingBehaviors.BotPageBehavior, |
| 46 ], | 66 ], |
| 47 | 67 |
| 48 properties: { | 68 properties: { |
| 49 // inputs | 69 // inputs |
| 50 auth_headers: { | 70 auth_headers: { |
| 51 type: Object, | 71 type: Object, |
| 52 }, | 72 }, |
| 53 bot_id: { | 73 bot_id: { |
| 54 type: String, | 74 type: String, |
| 55 }, | 75 }, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 type: Object, | 119 type: Object, |
| 100 }, | 120 }, |
| 101 }, | 121 }, |
| 102 | 122 |
| 103 observers: [ | 123 observers: [ |
| 104 "request(auth_headers,bot_id)", | 124 "request(auth_headers,bot_id)", |
| 105 ], | 125 ], |
| 106 | 126 |
| 107 request: function(){ | 127 request: function(){ |
| 108 if (!this.bot_id || !this.auth_headers) { | 128 if (!this.bot_id || !this.auth_headers) { |
| 109 console.log("bot_id and auth_headers can't be empty"); | |
| 110 return; | 129 return; |
| 111 } | 130 } |
| 112 var baseUrl = "/_ah/api/swarming/v1/bot/"+this.bot_id; | 131 if (lastRequest) { |
| 113 this._getJsonAsync("_bot", baseUrl + "/get", | 132 this.cancelAsync(lastRequest); |
| 114 "_busy1", this.auth_headers); | 133 } |
| 115 // We limit the fields on these two queries to make them faster. | 134 |
| 116 this._getJsonAsync("_events", | 135 lastRequest = this.async(function(){ |
| 117 baseUrl + "/events?fields=items(event_type%2Cmessage%2Cquarantined%2Ct
ask_id%2Cts%2Cversion)", | 136 lastRequest = undefined; |
| 118 "_busy2", this.auth_headers); | 137 var baseUrl = "/_ah/api/swarming/v1/bot/"+this.bot_id; |
| 119 this._getJsonAsync("_tasks", | 138 this._getJsonAsync("_bot", baseUrl + "/get", |
| 120 baseUrl + "/tasks?fields=items(abandoned_ts%2Cbot_version%2Ccompleted_
ts%2Cduration%2Cexit_code%2Cfailure%2Cinternal_failure%2Cmodified_ts%2Cname%2Cst
arted_ts%2Cstate%2Ctask_id%2Ctry_number)", | 139 "_busy1", this.auth_headers); |
| 121 "_busy3", this.auth_headers); | 140 // We limit the fields on these two queries to make them faster. |
| 141 this._getJsonAsync("_events", |
| 142 baseUrl + "/events?fields=items(event_type%2Cmessage%2Cquarantined%2
Ctask_id%2Cts%2Cversion)", |
| 143 "_busy2", this.auth_headers); |
| 144 this._getJsonAsync("_tasks", |
| 145 baseUrl + "/tasks?fields=items(abandoned_ts%2Cbot_version%2Ccomplete
d_ts%2Cduration%2Cexit_code%2Cfailure%2Cinternal_failure%2Cmodified_ts%2Cname%2C
started_ts%2Cstate%2Ctask_id%2Ctry_number)", |
| 146 "_busy3", this.auth_headers); |
| 147 }, BOT_ID_DEBOUNCE_MS); |
| 148 |
| 122 }, | 149 }, |
| 123 | 150 |
| 124 _parseBot: function(bot) { | 151 _parseBot: function(bot) { |
| 125 if (!bot) { | 152 if (!bot) { |
| 126 return {}; | 153 return {}; |
| 127 } | 154 } |
| 155 // Do any preprocessing here |
| 156 bot.state = bot.state || "{}"; |
| 157 bot.state = JSON.parse(bot.state); |
| 158 |
| 159 // get the disks in an easier to deal with format, sorted by size. |
| 160 var disks = bot.state.disks || {}; |
| 161 var keys = Object.keys(disks); |
| 162 if (!keys.length) { |
| 163 bot.disks = [{"id": "unknown", "mb": 0}]; |
| 164 } else { |
| 165 bot.disks = []; |
| 166 for (var i = 0; i < keys.length; i++) { |
| 167 bot.disks.push({"id":keys[i], "mb":disks[keys[i]].free_mb}); |
| 168 } |
| 169 // Sort these so the biggest disk comes first. |
| 170 bot.disks.sort(function(a, b) { |
| 171 return b.mb - a.mb; |
| 172 }); |
| 173 } |
| 174 |
| 175 BOT_TIMES.forEach(function(time) { |
| 176 if (bot[time]) { |
| 177 bot[time] = new Date(bot[time]); |
| 178 bot["human_"+time] = formatDate(bot[time]); |
| 179 } |
| 180 }); |
| 128 return bot; | 181 return bot; |
| 129 }, | 182 }, |
| 130 | 183 |
| 131 _parseEvents: function(events) { | 184 _parseEvents: function(events) { |
| 132 if (!events || !events.items) { | 185 if (!events || !events.items) { |
| 133 return []; | 186 return []; |
| 134 } | 187 } |
| 135 return events.items; | 188 var events = events.items; |
| 189 events.forEach(function(event){ |
| 190 // Do any preprocessing here |
| 191 if (event.ts) { |
| 192 event.ts = new Date(event.ts); |
| 193 event.human_ts = formatDate(event.ts); |
| 194 } |
| 195 }); |
| 196 |
| 197 // Sort the most recent events first. |
| 198 events.sort(function(a,b) { |
| 199 return b.ts - a.ts; |
| 200 }); |
| 201 |
| 202 return events; |
| 136 }, | 203 }, |
| 137 | 204 |
| 138 _parseTasks: function(tasks) { | 205 _parseTasks: function(tasks) { |
| 139 if (!tasks || !tasks.items) { | 206 if (!tasks || !tasks.items) { |
| 140 return []; | 207 return []; |
| 141 } | 208 } |
| 142 return tasks.items; | 209 var tasks = tasks.items; |
| 210 |
| 211 tasks.forEach(function(task){ |
| 212 // Do any preprocessing here |
| 213 TASK_TIMES.forEach(function(time) { |
| 214 if (task[time]) { |
| 215 task[time] = new Date(task[time]); |
| 216 task["human_"+time] = formatDate(task[time]); |
| 217 } |
| 218 }); |
| 219 |
| 220 if (task.duration) { |
| 221 task.human_duration = sk.human.strDuration(task.duration) || "0s"; |
| 222 } else { |
| 223 var end = task.completed_ts || task.abandoned_ts || task.modified_ts
|| new Date(); |
| 224 task.human_duration = this._timeDiffExact(task.started_ts, end); |
| 225 } |
| 226 |
| 227 task.state = task.state || "UNKNOWN"; |
| 228 if (task.state === "COMPLETED") { |
| 229 if (task.failure) { |
| 230 task.state = "FAILURE"; |
| 231 } else { |
| 232 task.state = "SUCCESS"; |
| 233 } |
| 234 } |
| 235 |
| 236 }.bind(this)); |
| 237 |
| 238 // Sort the most recent tasks first. |
| 239 tasks.sort(function(a,b) { |
| 240 return b.started_ts - a.started_ts; |
| 241 }); |
| 242 |
| 243 return tasks; |
| 143 } | 244 } |
| 144 | 245 |
| 145 }); | 246 }); |
| 146 })(); | 247 })(); |
| 147 </script> | 248 </script> |
| 148 </dom-module> | 249 </dom-module> |
| OLD | NEW |