Michal Kobus | 14e835b | 2019-04-11 14:25:27 +0200 | [diff] [blame] | 1 | from collections import OrderedDict |
| 2 | |
| 3 | |
| 4 | def parse_buckets(agg_key, buckets, metric=None, labels=None): |
| 5 | if metric is None: |
| 6 | metric = [] |
| 7 | if labels is None: |
| 8 | labels = OrderedDict() |
| 9 | |
| 10 | result = [] |
| 11 | |
| 12 | for index, bucket in enumerate(buckets): |
| 13 | labels_next = labels.copy() |
| 14 | |
| 15 | if 'key' in bucket.keys(): |
| 16 | bucket_key = str(bucket['key']) |
| 17 | if agg_key in labels_next.keys(): |
| 18 | labels_next[agg_key] = labels_next[agg_key] + [bucket_key] |
| 19 | else: |
| 20 | labels_next[agg_key] = [bucket_key] |
| 21 | del bucket['key'] |
| 22 | else: |
| 23 | bucket_key = 'filter_' + str(index) |
| 24 | if agg_key in labels_next.keys(): |
| 25 | labels_next[agg_key] = labels_next[agg_key] + [bucket_key] |
| 26 | else: |
| 27 | labels_next[agg_key] = [bucket_key] |
| 28 | |
| 29 | result.extend(parse_agg(bucket_key, bucket, metric=metric, labels=labels_next)) |
| 30 | |
| 31 | return result |
| 32 | |
| 33 | |
| 34 | def parse_buckets_fixed(agg_key, buckets, metric=None, labels=None): |
| 35 | if metric is None: |
| 36 | metric = [] |
| 37 | if labels is None: |
| 38 | labels = OrderedDict() |
| 39 | |
| 40 | result = [] |
| 41 | |
| 42 | for bucket_key, bucket in buckets.items(): |
| 43 | labels_next = labels.copy() |
| 44 | |
| 45 | if agg_key in labels_next.keys(): |
| 46 | labels_next[agg_key] = labels_next[agg_key] + [bucket_key] |
| 47 | else: |
| 48 | labels_next[agg_key] = [bucket_key] |
| 49 | |
| 50 | result.extend(parse_agg(bucket_key, bucket, metric=metric, labels=labels_next)) |
| 51 | |
| 52 | return result |
| 53 | |
| 54 | |
| 55 | def parse_agg(agg_key, agg, metric=None, labels=None): |
| 56 | if metric is None: |
| 57 | metric = [] |
| 58 | if labels is None: |
| 59 | labels = OrderedDict() |
| 60 | |
| 61 | result = [] |
| 62 | |
| 63 | for key, value in agg.items(): |
| 64 | if key == 'buckets' and isinstance(value, list): |
| 65 | result.extend(parse_buckets(agg_key, value, metric=metric, labels=labels)) |
| 66 | elif key == 'buckets' and isinstance(value, dict): |
| 67 | result.extend(parse_buckets_fixed(agg_key, value, metric=metric, labels=labels)) |
| 68 | elif isinstance(value, dict): |
| 69 | result.extend(parse_agg(key, value, metric=metric + [key], labels=labels)) |
| 70 | else: |
| 71 | result.append((metric + [key], labels, value)) |
| 72 | |
| 73 | return result |
| 74 | |
| 75 | |
| 76 | def parse_response(response, metric=None): |
| 77 | if metric is None: |
| 78 | metric = [] |
| 79 | |
| 80 | result = [] |
| 81 | |
| 82 | if not response['timed_out']: |
Michal Kobus | 15b5f76 | 2019-08-28 14:42:09 +0200 | [diff] [blame^] | 83 | hits_total = response['hits']['total'] |
| 84 | if isinstance(hits_total, dict): |
| 85 | hits_total = hits_total.get('value', 0) |
| 86 | result.append((metric + ['hits'], {}, hits_total)) |
Michal Kobus | 14e835b | 2019-04-11 14:25:27 +0200 | [diff] [blame] | 87 | result.append((metric + ['took', 'milliseconds'], {}, response['took'])) |
| 88 | |
| 89 | if 'aggregations' in response.keys(): |
| 90 | for key, value in response['aggregations'].items(): |
| 91 | result.extend(parse_agg(key, value, metric=metric + [key])) |
| 92 | |
| 93 | return result |