| 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 import {luci_rpc} from "rpc/client"; | |
| 8 import {LogDog} from "logdog-stream/logdog"; | |
| 9 | |
| 10 export enum StreamType { | |
| 11 TEXT, | |
| 12 BINARY, | |
| 13 DATAGRAM, | |
| 14 } | |
| 15 | |
| 16 export interface QueryParams { | |
| 17 project: string; | |
| 18 getMeta?: boolean; | |
| 19 path?: string; | |
| 20 contentType?: string; | |
| 21 streamType?: StreamType; | |
| 22 purged?: boolean; | |
| 23 newer?: Date; | |
| 24 older?: Date; | |
| 25 protoVersion?: string; | |
| 26 tags?: { [key:string]:string }; | |
| 27 } | |
| 28 | |
| 29 export class LogDogQuery { | |
| 30 private client: luci_rpc.Client | |
| 31 private params: QueryParams; | |
| 32 | |
| 33 constructor(client: luci_rpc.Client) { | |
| 34 this.client = client; | |
| 35 } | |
| 36 | |
| 37 /** Returns true if "s" has glob characters in it. */ | |
| 38 static isQuery(s: string): boolean { | |
| 39 return (s.indexOf("*") >= 0); | |
| 40 }; | |
| 41 | |
| 42 get(params: QueryParams, cursor: string, limit: number): | |
| 43 Promise<[QueryResult[], string]> { | |
| 44 | |
| 45 let project = params.project; | |
| 46 let body: { [key:string]:any } = { | |
| 47 project: project, | |
| 48 path: params.path, | |
| 49 content_type: params.contentType, | |
| 50 proto_version: params.protoVersion, | |
| 51 tags: params.tags, | |
| 52 | |
| 53 next: cursor, | |
| 54 max_results: limit, | |
| 55 }; | |
| 56 | |
| 57 let trinary = (v: boolean): string => { | |
| 58 return ((v) ? "YES" : "NO"); | |
| 59 } | |
| 60 if ( params.purged !== null ) { | |
| 61 body["purged"] = trinary(params.purged); | |
| 62 } | |
| 63 | |
| 64 if ( params.streamType !== undefined ) { | |
| 65 let filter: { value?: string } = {}; | |
| 66 switch (params.streamType) { | |
| 67 case StreamType.TEXT: | |
| 68 filter.value = "TEXT"; | |
| 69 break; | |
| 70 case StreamType.BINARY: | |
| 71 filter.value = "BINARY"; | |
| 72 break; | |
| 73 case StreamType.DATAGRAM: | |
| 74 filter.value = "DATAGRAM"; | |
| 75 break; | |
| 76 } | |
| 77 body["stream_type"] = filter; | |
| 78 } | |
| 79 if (params.newer) { | |
| 80 body["newer"] = params.newer.toISOString(); | |
| 81 } | |
| 82 if (params.older) { | |
| 83 body["older"] = params.older.toISOString(); | |
| 84 } | |
| 85 | |
| 86 type responseType = { | |
| 87 streams: { | |
| 88 path: string, | |
| 89 state: any, | |
| 90 desc: any, | |
| 91 }[]; | |
| 92 next: string; | |
| 93 }; | |
| 94 | |
| 95 return this.client.call("logdog.Logs", "Query", body). | |
| 96 then( (resp: responseType): [QueryResult[], string] => { | |
| 97 | |
| 98 // Package the response in QueryResults. | |
| 99 let results = (resp.streams || []).map( (entry): QueryResult => { | |
| 100 return new QueryResult( | |
| 101 new LogDog.Stream(project, entry.path), | |
| 102 ((entry.state) ? | |
| 103 LogDog.makeLogStreamState(entry.state) : (null)), | |
| 104 ((entry.desc) ? | |
| 105 LogDog.makeLogStreamDescriptor(entry.desc) : (null)), | |
| 106 ); | |
| 107 }); | |
| 108 return [results, resp.next]; | |
| 109 }); | |
| 110 } | |
| 111 | |
| 112 /** | |
| 113 * Issues a query and iteratively pulls up to "this.limit" results. | |
| 114 */ | |
| 115 getAll(params: QueryParams, limit: number): Promise<QueryResult[]> { | |
| 116 let results: QueryResult[] = []; | |
| 117 let cursor: string = null; | |
| 118 limit = (limit || 100); | |
| 119 | |
| 120 let fetchRound = (first: boolean): Promise<QueryResult[]> => { | |
| 121 var remaining = (limit - results.length); | |
| 122 if (remaining <= 0 || (! (first || cursor)) ) { | |
| 123 return Promise.resolve(results); | |
| 124 } | |
| 125 | |
| 126 return this.get(params, cursor, remaining).then( (round) => { | |
| 127 if ( round[0] ) { | |
| 128 results.push.apply(results, round[0]); | |
| 129 } | |
| 130 cursor = round[1]; | |
| 131 return fetchRound(false); | |
| 132 }); | |
| 133 }; | |
| 134 | |
| 135 return fetchRound(true); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 export class QueryResult { | |
| 140 constructor(readonly stream: LogDog.Stream, | |
| 141 readonly state?: LogDog.LogStreamState, | |
| 142 readonly desc?: LogDog.LogStreamDescriptor) {} | |
| 143 } | |
| OLD | NEW |