|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import argparse |
| 4 | +import client |
| 5 | +# non-standard! requires setup.py!! |
| 6 | +import tabulate |
| 7 | + |
| 8 | + |
| 9 | +def list_races(es, args): |
| 10 | + limit = args.limit |
| 11 | + environment = args.environment |
| 12 | + track = args.track |
| 13 | + |
| 14 | + if args.track: |
| 15 | + print("Listing %d most recent races for track %s in environment %s.\n" % (limit, track, environment)) |
| 16 | + query = { |
| 17 | + "query": { |
| 18 | + "bool": { |
| 19 | + "filter": [ |
| 20 | + { |
| 21 | + "term": { |
| 22 | + "environment": environment |
| 23 | + } |
| 24 | + }, |
| 25 | + { |
| 26 | + "term": { |
| 27 | + "track": track |
| 28 | + } |
| 29 | + } |
| 30 | + ] |
| 31 | + } |
| 32 | + |
| 33 | + } |
| 34 | + } |
| 35 | + else: |
| 36 | + print("Listing %d most recent races in environment %s.\n" % (limit, environment)) |
| 37 | + query = { |
| 38 | + "query": { |
| 39 | + "term": { |
| 40 | + "environment": environment |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + query["sort"] = [ |
| 46 | + { |
| 47 | + "trial-timestamp": "desc" |
| 48 | + }, |
| 49 | + { |
| 50 | + "track": "asc" |
| 51 | + }, |
| 52 | + { |
| 53 | + "challenge": "asc" |
| 54 | + } |
| 55 | + ] |
| 56 | + |
| 57 | + result = es.search(index="rally-races-*", body=query, size=limit) |
| 58 | + races = [] |
| 59 | + for hit in result["hits"]["hits"]: |
| 60 | + src = hit["_source"] |
| 61 | + races.append([src["trial-timestamp"], src["track"], src["challenge"], src["car"], |
| 62 | + src["cluster"]["distribution-version"], src["user-tag"]]) |
| 63 | + if races: |
| 64 | + print(tabulate.tabulate(races, headers=["Race Timestamp", "Track", "Challenge", "Car", "Version", "User Tag"])) |
| 65 | + else: |
| 66 | + print("No results") |
| 67 | + |
| 68 | + |
| 69 | +def list_annotations(es, args): |
| 70 | + limit = args.limit |
| 71 | + environment = args.environment |
| 72 | + track = args.track |
| 73 | + if track: |
| 74 | + print("Listing %d most recent annotations in environment %s for track %s.\n" % (limit, environment, track)) |
| 75 | + query = { |
| 76 | + "query": { |
| 77 | + "bool": { |
| 78 | + "filter": [ |
| 79 | + { |
| 80 | + "term": { |
| 81 | + "environment": environment |
| 82 | + } |
| 83 | + }, |
| 84 | + { |
| 85 | + "term": { |
| 86 | + "track": track |
| 87 | + } |
| 88 | + } |
| 89 | + ] |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + } |
| 94 | + else: |
| 95 | + print("Listing %d most recent annotations in environment %s.\n" % (limit, environment)) |
| 96 | + query = { |
| 97 | + "query": { |
| 98 | + "term": { |
| 99 | + "environment": environment |
| 100 | + } |
| 101 | + }, |
| 102 | + } |
| 103 | + query["sort"] = [ |
| 104 | + { |
| 105 | + "trial-timestamp": "desc" |
| 106 | + }, |
| 107 | + { |
| 108 | + "track": "asc" |
| 109 | + }, |
| 110 | + { |
| 111 | + "chart": "asc" |
| 112 | + } |
| 113 | + ] |
| 114 | + |
| 115 | + result = es.search(index="rally-annotations", body=query, size=limit) |
| 116 | + annotations = [] |
| 117 | + for hit in result["hits"]["hits"]: |
| 118 | + src = hit["_source"] |
| 119 | + annotations.append([hit["_id"], src["trial-timestamp"], src.get("track", ""), src.get("chart", ""), src["message"]]) |
| 120 | + if annotations: |
| 121 | + print(tabulate.tabulate(annotations, headers=["Annotation Id", "Timestamp", "Track", "Chart", "Message"])) |
| 122 | + else: |
| 123 | + print("No results") |
| 124 | + |
| 125 | + |
| 126 | +def add_annotation(es, args): |
| 127 | + environment = args.environment |
| 128 | + trial_timestamp = args.trial_timestamp |
| 129 | + track = args.track |
| 130 | + chart = args.chart |
| 131 | + message = args.message |
| 132 | + dry_run = args.dry_run |
| 133 | + |
| 134 | + if dry_run: |
| 135 | + print("Would add annotation with message [%s] for environment=[%s], trial timestamp=[%s], track=[%s], chart=[%s]" % |
| 136 | + (message, environment, trial_timestamp, track, chart)) |
| 137 | + else: |
| 138 | + if not es.indices.exists(index="rally-annotations"): |
| 139 | + body = open("%s/resources/annotation-mapping.json" % os.path.dirname(os.path.realpath(__file__)), "rt").read() |
| 140 | + es.indices.create(index="rally-annotations", body=body) |
| 141 | + es.index(index="rally-annotations", doc_type="type", body={ |
| 142 | + "environment": environment, |
| 143 | + "trial-timestamp": trial_timestamp, |
| 144 | + "track": track, |
| 145 | + "chart": chart, |
| 146 | + "message": message |
| 147 | + }) |
| 148 | + |
| 149 | + |
| 150 | +def delete_annotation(es, args): |
| 151 | + import elasticsearch |
| 152 | + annotations = args.id.split(",") |
| 153 | + if args.dry_run: |
| 154 | + if len(annotations) == 1: |
| 155 | + print("Would delete annotation with id [%s]." % annotations[0]) |
| 156 | + else: |
| 157 | + print("Would delete %s annotations: %s." % (len(annotations, annotations))) |
| 158 | + else: |
| 159 | + for annotation_id in annotations: |
| 160 | + try: |
| 161 | + es.delete(index="rally-annotations", doc_type="type", id=annotation_id) |
| 162 | + print("Successfully deleted [%s]." % annotation_id) |
| 163 | + except elasticsearch.TransportError as e: |
| 164 | + if e.status_code == 404: |
| 165 | + print("Did not find [%s]." % annotation_id) |
| 166 | + else: |
| 167 | + raise |
| 168 | + |
| 169 | + |
| 170 | +def arg_parser(): |
| 171 | + parser = argparse.ArgumentParser(description="Admin tool for Elasticsearch benchmarks", |
| 172 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 173 | + |
| 174 | + subparsers = parser.add_subparsers( |
| 175 | + title="subcommands", |
| 176 | + dest="subcommand", |
| 177 | + help="") |
| 178 | + |
| 179 | + # list races --max-results=20 |
| 180 | + # list annotations --max-results=20 |
| 181 | + list_parser = subparsers.add_parser("list", help="List configuration options") |
| 182 | + list_parser.add_argument( |
| 183 | + "configuration", |
| 184 | + metavar="configuration", |
| 185 | + help="What the admin tool should list. Possible values are: races, annotations", |
| 186 | + choices=["races", "annotations"]) |
| 187 | + |
| 188 | + list_parser.add_argument( |
| 189 | + "--limit", |
| 190 | + help="Limit the number of search results (default: 20).", |
| 191 | + default=20, |
| 192 | + ) |
| 193 | + list_parser.add_argument( |
| 194 | + "--environment", |
| 195 | + help="Show only records from this environment", |
| 196 | + required=True |
| 197 | + ) |
| 198 | + list_parser.add_argument( |
| 199 | + "--track", |
| 200 | + help="Show only records from this track", |
| 201 | + default=None |
| 202 | + ) |
| 203 | + |
| 204 | + # if no "track" is given -> annotate all tracks |
| 205 | + # "chart" indicates the graph. If no chart, is given, it is empty -> we need to write the queries to that we update all chart |
| 206 | + # |
| 207 | + # add [annotation] --environment=nightly --trial-timestamp --track --chart --text |
| 208 | + add_parser = subparsers.add_parser("add", help="Add records") |
| 209 | + add_parser.add_argument( |
| 210 | + "configuration", |
| 211 | + metavar="configuration", |
| 212 | + help="", |
| 213 | + choices=["annotation"]) |
| 214 | + add_parser.add_argument( |
| 215 | + "--dry-run", |
| 216 | + help="Just show what would be done but do not apply the operation.", |
| 217 | + default=False, |
| 218 | + action="store_true" |
| 219 | + ) |
| 220 | + add_parser.add_argument( |
| 221 | + "--environment", |
| 222 | + help="Environment (default: nightly)", |
| 223 | + default="nightly" |
| 224 | + ) |
| 225 | + add_parser.add_argument( |
| 226 | + "--trial-timestamp", |
| 227 | + help="Trial timestamp" |
| 228 | + ) |
| 229 | + add_parser.add_argument( |
| 230 | + "--track", |
| 231 | + help="Track. If none given, applies to all tracks", |
| 232 | + default=None |
| 233 | + ) |
| 234 | + add_parser.add_argument( |
| 235 | + "--chart", |
| 236 | + help="Chart to target. If none given, applies to all charts.", |
| 237 | + choices=['query', 'script', 'stats', 'indexing', 'gc', 'index_times', 'merge_times', 'segment_count', 'segment_memory', 'io'], |
| 238 | + default=None |
| 239 | + ) |
| 240 | + add_parser.add_argument( |
| 241 | + "--message", |
| 242 | + help="Annotation message", |
| 243 | + required=True |
| 244 | + ) |
| 245 | + |
| 246 | + delete_parser = subparsers.add_parser("delete", help="Delete records") |
| 247 | + delete_parser.add_argument( |
| 248 | + "configuration", |
| 249 | + metavar="configuration", |
| 250 | + help="", |
| 251 | + choices=["annotation"]) |
| 252 | + delete_parser.add_argument( |
| 253 | + "--dry-run", |
| 254 | + help="Just show what would be done but do not apply the operation.", |
| 255 | + default=False, |
| 256 | + action="store_true" |
| 257 | + ) |
| 258 | + delete_parser.add_argument( |
| 259 | + "--id", |
| 260 | + help="Id of the annotation to delete. Separate multiple ids with a comma.", |
| 261 | + required=True |
| 262 | + ) |
| 263 | + return parser |
| 264 | + |
| 265 | + |
| 266 | +def main(): |
| 267 | + parser = arg_parser() |
| 268 | + es = client.create_client() |
| 269 | + |
| 270 | + args = parser.parse_args() |
| 271 | + if args.subcommand == "list": |
| 272 | + if args.configuration == "races": |
| 273 | + list_races(es, args) |
| 274 | + elif args.configuration == "annotations": |
| 275 | + list_annotations(es, args) |
| 276 | + else: |
| 277 | + print("Do not know how to list [%s]" % args.configuration, file=sys.stderr) |
| 278 | + exit(1) |
| 279 | + elif args.subcommand == "add" and args.configuration == "annotation": |
| 280 | + add_annotation(es, args) |
| 281 | + elif args.subcommand == "delete" and args.configuration == "annotation": |
| 282 | + delete_annotation(es, args) |
| 283 | + else: |
| 284 | + parser.print_help(file=sys.stderr) |
| 285 | + exit(1) |
| 286 | + |
| 287 | + |
| 288 | +if __name__ == '__main__': |
| 289 | + main() |
0 commit comments