| 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 | |
| 7 <link rel="import" href="../bower_components/polymer/polymer.html"> | |
| 8 <link rel="import" href="../rpc/rpc-client.html"> | |
| 9 <link rel="import" href="../logdog-stream/logdog-stream.html"> | |
| 10 <link rel="import" href="../logdog-stream/logdog-error.html"> | |
| 11 | |
| 12 <!-- | |
| 13 A class for issuing a LogDog query. | |
| 14 --> | |
| 15 <script> | |
| 16 "use strict"; | |
| 17 | |
| 18 function LogDogQueryParams(project) { | |
| 19 this.project = project; | |
| 20 | |
| 21 this._getMeta = false; | |
| 22 this._path = null; | |
| 23 this._contentType = null; | |
| 24 this._streamType = null; | |
| 25 this._purged = null; | |
| 26 this._newer = null; | |
| 27 this._older = null; | |
| 28 this._protoVersion = null; | |
| 29 this._tags = {}; | |
| 30 } | |
| 31 | |
| 32 /** Enables fetching of stream metadata. */ | |
| 33 LogDogQueryParams.prototype.getMeta = function() { | |
| 34 this._getMeta = true; | |
| 35 return this; | |
| 36 }; | |
| 37 | |
| 38 /** Set the log stream path glob to fetch. */ | |
| 39 LogDogQueryParams.prototype.path = function(v) { | |
| 40 this._path = v; | |
| 41 return this; | |
| 42 }; | |
| 43 | |
| 44 /** The content type to filter on. */ | |
| 45 LogDogQueryParams.prototype.contentType = function(v) { | |
| 46 this._contentType = v; | |
| 47 return this; | |
| 48 }; | |
| 49 | |
| 50 /** | |
| 51 * The stream type. Can be "text", "binary", "datagram", or null for any. | |
| 52 */ | |
| 53 LogDogQueryParams.prototype.streamType = function(v) { | |
| 54 this._streamType = v; | |
| 55 return this; | |
| 56 }; | |
| 57 | |
| 58 /** Filter purged streams? true, false, or null for no filter. */ | |
| 59 LogDogQueryParams.prototype.purged = function(v) { | |
| 60 this._purged = v; | |
| 61 return this; | |
| 62 }; | |
| 63 | |
| 64 /** Filter streams newer than this value? Null for no lower bound. */ | |
| 65 LogDogQueryParams.prototype.newer = function(v) { | |
| 66 this._newer = v.toISOString(); | |
| 67 return this; | |
| 68 }; | |
| 69 | |
| 70 /** Filter streams older than this value? Null for no lower bound. */ | |
| 71 LogDogQueryParams.prototype.older = function(v) { | |
| 72 this._older = v.toISOString(); | |
| 73 return this; | |
| 74 }; | |
| 75 | |
| 76 /** Filter on protocol version (null for no filter). */ | |
| 77 LogDogQueryParams.prototype.protoVersion = function(v) { | |
| 78 this._protoVersion = v; | |
| 79 return this; | |
| 80 }; | |
| 81 | |
| 82 /** Filter on protocol version (null for no filter). */ | |
| 83 LogDogQueryParams.prototype.addTag = function(key, value) { | |
| 84 this._tags[key] = value; | |
| 85 return this; | |
| 86 }; | |
| 87 | |
| 88 function LogDogQuery(client, params) { | |
| 89 this.client = client; | |
| 90 this.params = params; | |
| 91 } | |
| 92 | |
| 93 /** Returns true if "s" has glob characters in it. */ | |
| 94 LogDogQuery.isQuery = function(s) { | |
| 95 return (s.indexOf("*") >= 0); | |
| 96 }; | |
| 97 | |
| 98 LogDogQuery.prototype.get = function(cursor, limit) { | |
| 99 var project = this.params.project; | |
| 100 var body = { | |
| 101 project: project, | |
| 102 path: this.params._path, | |
| 103 content_type: this.params._contentType, | |
| 104 proto_version: this.params._protoVersion, | |
| 105 tags: this.params._tags, | |
| 106 next: cursor, | |
| 107 max_results: limit, | |
| 108 }; | |
| 109 | |
| 110 var trinary = function(field, v) { | |
| 111 if (v != null) { | |
| 112 body[field] = ((v) ? "YES" : "NO"); | |
| 113 } | |
| 114 } | |
| 115 trinary("purged", this.params._purged); | |
| 116 | |
| 117 if (this.params._streamType) { | |
| 118 var filter = {}; | |
| 119 switch (this.params._streamType) { | |
| 120 case "text": | |
| 121 filter.value = "TEXT"; | |
| 122 break; | |
| 123 case "binary": | |
| 124 filter.value = "BINARY"; | |
| 125 break; | |
| 126 case "datagram": | |
| 127 filter.value = "DATAGRAM"; | |
| 128 break; | |
| 129 default: | |
| 130 throw ("Invalid stream type: " + this.params._streamType); | |
| 131 } | |
| 132 body.stream_type = filter; | |
| 133 } | |
| 134 if (this.params._newer) { | |
| 135 body.newer = this.params._newer; | |
| 136 } | |
| 137 if (this.params._older) { | |
| 138 body.older = this.params._older; | |
| 139 } | |
| 140 | |
| 141 this.client.service = "logdog.Logs"; | |
| 142 this.client.method = "Query"; | |
| 143 this.client.request = body; | |
| 144 | |
| 145 return this.client.call().completes.then(function(resp) { | |
| 146 resp = resp.response; | |
| 147 | |
| 148 // Normalize the JSON values in "desc". | |
| 149 // | |
| 150 // JSONPB timestamps are in the form of RFC3339 strings. | |
| 151 resp.streams = (resp.streams || []); | |
| 152 resp.streams.forEach(function(s) { | |
| 153 s.stream = new LogDogStream(project, s.path); | |
| 154 if (s.state) { | |
| 155 patchState(s.state); | |
| 156 } | |
| 157 if (s.desc) { | |
| 158 patchDescriptor(s.desc); | |
| 159 } | |
| 160 }); | |
| 161 return resp; | |
| 162 }).catch(function(error) { | |
| 163 throw LogDogError.wrapGrpc(error); | |
| 164 }); | |
| 165 }; | |
| 166 | |
| 167 /** | |
| 168 * Issues a query and iteratively pulls up to "this.limit" results. | |
| 169 */ | |
| 170 LogDogQuery.prototype.getAll = function(limit) { | |
| 171 var streams = []; | |
| 172 var cursor = null; | |
| 173 limit = (limit || 100); | |
| 174 | |
| 175 var fetchRound = function(inStreams) { | |
| 176 if (inStreams) { | |
| 177 streams.push.apply(streams, inStreams); | |
| 178 } | |
| 179 | |
| 180 var remaining = (limit - streams.length); | |
| 181 if (remaining <= 0 || (inStreams && !cursor)) { | |
| 182 return streams; | |
| 183 } | |
| 184 | |
| 185 return this.get(cursor, remaining).then(function(resp) { | |
| 186 cursor = resp.next; | |
| 187 return fetchRound(resp.streams); | |
| 188 }); | |
| 189 }.bind(this); | |
| 190 | |
| 191 return fetchRound(null); | |
| 192 }; | |
| 193 </script> | |
| OLD | NEW |