| 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 | 6 |
| 7 <link rel="import" href="../bower_components/polymer/polymer.html"> | 7 <link rel="import" href="../bower_components/polymer/polymer.html"> |
| 8 <link rel="import" href="../bower_components/promise-polyfill/promise-polyfill-l
ite.html"> | 8 <link rel="import" href="../bower_components/promise-polyfill/promise-polyfill-l
ite.html"> |
| 9 | 9 |
| 10 <link rel="import" href="../luci-sleep-promise/luci-sleep-promise.html"> | 10 <link rel="import" href="../luci-sleep-promise/luci-sleep-promise.html"> |
| 11 | 11 |
| 12 <script> | 12 <script> |
| 13 "use strict"; | 13 "use strict"; |
| 14 | 14 |
| 15 function LogDogFetcher(client, project, path) { | 15 function LogDogFetcher(client, stream) { |
| 16 this.client = client; | 16 this.client = client; |
| 17 this.project = project; | 17 this.stream = stream; |
| 18 this.path = path; | |
| 19 | 18 |
| 20 // Fetching parameters, will be updated as logs are fetched. | 19 // Fetching parameters, will be updated as logs are fetched. |
| 21 this.sleepTimeSecs = 5; | 20 this.sleepTimeSecs = 5; |
| 21 this.byteCount = null; |
| 22 this.logCount = null; |
| 22 this.reset(); | 23 this.reset(); |
| 23 } | 24 } |
| 24 | 25 |
| 25 LogDogFetcher.prototype.reset = function() { | 26 LogDogFetcher.prototype.reset = function() { |
| 26 this.nextIndex = 0; | 27 this.nextIndex = 0; |
| 27 this.finished = false; | 28 this.finished = false; |
| 28 this.desc = null; | 29 this.desc = null; |
| 29 this.state = null; | 30 this.state = null; |
| 30 | 31 |
| 31 this._current = null; | 32 this._current = null; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 64 |
| 64 var tidx = this.terminalIndex(); | 65 var tidx = this.terminalIndex(); |
| 65 if (tidx >= 0 && tidx < this.nextIndex) { | 66 if (tidx >= 0 && tidx < this.nextIndex) { |
| 66 // We have punted the full log stream. Mark finished. | 67 // We have punted the full log stream. Mark finished. |
| 67 this.finished = true; | 68 this.finished = true; |
| 68 } | 69 } |
| 69 } | 70 } |
| 70 | 71 |
| 71 this._nextLogsPromise = null; | 72 this._nextLogsPromise = null; |
| 72 return result; | 73 return result; |
| 74 }.bind(this)).catch(function(error) { |
| 75 this._nextLogsPromise = null; |
| 76 throw error; |
| 73 }.bind(this)); | 77 }.bind(this)); |
| 74 } | 78 } |
| 75 return this._nextLogsPromise; | 79 return this._nextLogsPromise; |
| 76 }, | 80 }, |
| 77 | 81 |
| 78 /** Creates and returns a Promise for the next batch of logs. */ | 82 /** Creates and returns a Promise for the next batch of logs. */ |
| 79 LogDogFetcher.prototype._fetchNextBatch = function() { | 83 LogDogFetcher.prototype._fetchNextBatch = function() { |
| 80 // If we're already finished, return the terminal result. | 84 // If we're already finished, return the terminal result. |
| 81 if (this.finished) { | 85 if (this.finished) { |
| 82 return this._resolvedLogs(null); | 86 return this._resolvedLogs(null); |
| 83 } | 87 } |
| 84 | 88 |
| 85 // Fetch and return the next batch of logs. | 89 // Fetch and return the next batch of logs. |
| 86 return this._scheduleAsyncGet().then(function(resp) { | 90 return this._scheduleAsyncGet().then(function(resp) { |
| 87 // Update our state/desc. | 91 // Update our state/desc. |
| 88 if (resp.state) { | 92 if (resp.state) { |
| 89 this.state = resp.state; | 93 this.state = resp.state; |
| 90 } | 94 } |
| 91 if (resp.desc) { | 95 if (resp.desc) { |
| 92 this.desc = resp.desc; | 96 this.desc = resp.desc; |
| 93 } | 97 } |
| 94 | 98 |
| 95 var logs = resp.logs; | 99 var logs = resp.logs; |
| 96 if (!logs.length) { | 100 if (! (logs && logs.length)) { |
| 97 // No logs were loaded this round. Sleep for a bit then try again. | 101 // No logs were loaded this round. Sleep for a bit then try again. |
| 98 // (Streaming case). | 102 // (Streaming case). |
| 99 console.log("No logs for", this.path, "; sleeping..."); | 103 console.log("No logs for", this.stream, "; sleeping..."); |
| 100 return new LuciSleepPromise(this.sleepTimeSecs * 1000). | 104 return new LuciSleepPromise(this.sleepTimeSecs * 1000). |
| 101 then(function() { | 105 then(function() { |
| 102 return this._fetchNextBatch(); | 106 return this._fetchNextBatch(); |
| 103 }.bind(this)); | 107 }.bind(this)); |
| 104 } | 108 } |
| 105 | 109 |
| 106 return this._resolvedLogs(logs); | 110 return this._resolvedLogs(logs); |
| 107 }.bind(this)); | 111 }.bind(this)); |
| 108 }; | 112 }; |
| 109 | 113 |
| 110 /** Generates a structured Promise for a given block of log entries. */ | 114 /** Generates a structured Promise for a given block of log entries. */ |
| 111 LogDogFetcher.prototype._resolvedLogs = function(punt) { | 115 LogDogFetcher.prototype._resolvedLogs = function(punt) { |
| 112 return Promise.resolve({ | 116 return Promise.resolve({ |
| 113 desc: this.desc, | 117 desc: this.desc, |
| 114 state: this.state, | 118 state: this.state, |
| 115 entries: punt, | 119 entries: punt, |
| 116 }); | 120 }); |
| 117 }; | 121 }; |
| 118 | 122 |
| 119 /** Schedules the next asynchronous fetch. */ | 123 /** Schedules the next asynchronous fetch. */ |
| 120 LogDogFetcher.prototype._scheduleAsyncGet = function() { | 124 LogDogFetcher.prototype._scheduleAsyncGet = function() { |
| 121 this.client.service = "logdog.Logs"; | 125 this.client.service = "logdog.Logs"; |
| 122 this.client.method = "Get"; | 126 this.client.method = "Get"; |
| 123 this.client.request = { | 127 this.client.request = { |
| 124 project: this.project, | 128 project: this.stream.project, |
| 125 path: this.path, | 129 path: this.stream.path, |
| 126 state: (!this.state || this.terminalIndex() < 0), | 130 state: (!this.state || this.terminalIndex() < 0), |
| 127 index: this.nextIndex, | 131 index: this.nextIndex, |
| 128 }; | 132 }; |
| 129 | 133 |
| 134 if (this.byteCount !== null) { |
| 135 this.client.request.byteCount = this.byteCount; |
| 136 } |
| 137 if (this.logCount !== null) { |
| 138 this.client.request.logCount = this.logCount; |
| 139 } |
| 140 |
| 130 return this.client.call().completes.then(function(resp) { | 141 return this.client.call().completes.then(function(resp) { |
| 131 resp = resp.response; | 142 resp = resp.response; |
| 132 | 143 |
| 133 // Normalize the resulting logs. | 144 // Normalize the resulting logs. |
| 134 // | 145 // |
| 135 // JSONPB timestamps are in the form of RFC3339 strings. | 146 // JSONPB timestamps are in the form of RFC3339 strings. |
| 136 if (resp.desc) { | 147 if (resp.desc) { |
| 137 patchDescriptor(resp.desc); | 148 patchDescriptor(resp.desc); |
| 138 } | 149 } |
| 139 if (resp.state) { | 150 if (resp.state) { |
| 140 patchState(resp.state); | 151 patchState(resp.state); |
| 141 } | 152 } |
| 142 if (resp.logs) { | 153 if (resp.logs) { |
| 143 resp.logs.forEach(function(le) { | 154 resp.logs.forEach(function(le) { |
| 144 patchLogEntry(le, resp.desc); | 155 patchLogEntry(le, resp.desc); |
| 145 }); | 156 }); |
| 146 } | 157 } |
| 147 | 158 |
| 148 return resp; | 159 return resp; |
| 160 }, function(error) { |
| 161 throw LogDogError.wrapGrpc(error); |
| 149 }); | 162 }); |
| 150 }; | 163 }; |
| 151 </script> | 164 </script> |
| OLD | NEW |