| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 Copyright 2017 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 ///<reference path="../logdog-stream/logdog.ts" /> |
| 8 ///<reference path="../logdog-stream/client.ts" /> |
| 9 ///<reference path="../luci-operation/operation.ts" /> |
| 10 ///<reference path="../luci-sleep-promise/promise.ts" /> |
| 11 ///<reference path="../rpc/client.ts" /> |
| 12 |
| 13 namespace LogDog { |
| 14 |
| 15 /** UI element used for rendering an individual query result. */ |
| 16 type QueryEntry = {fullPath: string; title: string;}; |
| 17 |
| 18 /** |
| 19 * Represents the fields of a "logdog-query-view" Polymer element used by |
| 20 * QueryView. |
| 21 */ |
| 22 type QueryComponent = { |
| 23 $: { |
| 24 client: luci.PolymerClient; |
| 25 queryPanel: {project: string; path: string; streamType: string;}, |
| 26 } |
| 27 |
| 28 _setSharedPrefix(prefix: string|null): void; |
| 29 _setQueryResults(results: QueryEntry[]): void; |
| 30 }; |
| 31 |
| 32 const QUERY_LIMIT = 50; |
| 33 |
| 34 /** QueryView manages a "logdog-query-view" Polymer element. */ |
| 35 export class QueryView { |
| 36 private currentQuery: luci.Operation|null; |
| 37 private client: LogDog.Client; |
| 38 |
| 39 constructor(readonly comp: QueryComponent) {} |
| 40 |
| 41 /** |
| 42 * Cancels any ongoing queries and clears all elements to return this to an |
| 43 * initializsed state. |
| 44 */ |
| 45 reset() { |
| 46 this._cancelCurrentQuery(); |
| 47 this.client = new LogDog.Client(new luci.Client(this.comp.$.client)); |
| 48 |
| 49 this.comp._setSharedPrefix(null); |
| 50 this.comp._setQueryResults([]); |
| 51 } |
| 52 |
| 53 /** |
| 54 * Called to execute a new query. |
| 55 * |
| 56 * If a query is currently executing, it will be cancelled. |
| 57 * |
| 58 * When the query is complete, the UI will be updated with the results. |
| 59 */ |
| 60 doQuery() { |
| 61 this._cancelCurrentQuery(); |
| 62 |
| 63 let qp = this.comp.$.queryPanel; |
| 64 let project = qp.project; |
| 65 let params: LogDog.QueryRequest = { |
| 66 project: project, |
| 67 path: qp.path, |
| 68 }; |
| 69 |
| 70 /** |
| 71 * Map the "Stream Type" text values in "logdog-query-panel" to their |
| 72 * streamType constants. |
| 73 */ |
| 74 switch (qp.streamType) { |
| 75 case 'Text': |
| 76 params.streamType = LogDog.StreamType.TEXT; |
| 77 break; |
| 78 |
| 79 case 'Binary': |
| 80 params.streamType = LogDog.StreamType.BINARY; |
| 81 break; |
| 82 |
| 83 case 'Datagram': |
| 84 params.streamType = LogDog.StreamType.DATAGRAM; |
| 85 break; |
| 86 |
| 87 case 'Any': |
| 88 default: |
| 89 break; |
| 90 } |
| 91 |
| 92 let op = new luci.Operation(); |
| 93 this.client.query(op, params, '', QUERY_LIMIT).then((result) => { |
| 94 // Do all of the results have the same prefix? |
| 95 let streams = result[0] || []; |
| 96 let sharedPrefix: string|null = null; |
| 97 if (streams.length > 0) { |
| 98 sharedPrefix = streams[0].stream.prefix; |
| 99 for (let i = 1; i < streams.length; i++) { |
| 100 if (streams[i].stream.prefix !== sharedPrefix) { |
| 101 sharedPrefix = null; |
| 102 break; |
| 103 } |
| 104 } |
| 105 } |
| 106 this.comp._setSharedPrefix(sharedPrefix); |
| 107 |
| 108 let queryResults = result[0].map((v): QueryEntry => { |
| 109 return { |
| 110 fullPath: v.stream.fullName(), |
| 111 title: |
| 112 ((sharedPrefix) ? ('.../+/' + v.stream.name) : v.stream.path), |
| 113 }; |
| 114 }); |
| 115 this.comp._setQueryResults(queryResults); |
| 116 }); |
| 117 this.currentQuery = op; |
| 118 } |
| 119 |
| 120 private _cancelCurrentQuery() { |
| 121 if (this.currentQuery) { |
| 122 this.currentQuery.cancel(); |
| 123 this.currentQuery = null; |
| 124 } |
| 125 } |
| 126 } |
| 127 } |
| OLD | NEW |