|
| 1 | +"""Helpers to create Elasticsearch-specific Grafana queries.""" |
| 2 | + |
| 3 | +import attr |
| 4 | +import itertools |
| 5 | +from attr.validators import instance_of |
| 6 | + |
| 7 | +DATE_HISTOGRAM_DEFAULT_FIELD = "time_iso8601" |
| 8 | +ORDER_ASC = "asc" |
| 9 | +ORDER_DESC = "desc" |
| 10 | + |
| 11 | + |
| 12 | +@attr.s |
| 13 | +class CountMetricAgg(object): |
| 14 | + """An aggregator that counts the number of values. |
| 15 | +
|
| 16 | + https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html |
| 17 | +
|
| 18 | + It's the default aggregator for elasticsearch queries. |
| 19 | + """ |
| 20 | + def to_json_data(self): |
| 21 | + return { |
| 22 | + 'type': 'count', |
| 23 | + 'field': 'select field', |
| 24 | + 'settings': {}, |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | +@attr.s |
| 29 | +class MaxMetricAgg(object): |
| 30 | + """An aggregator that provides the max. value among the values. |
| 31 | +
|
| 32 | + https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html |
| 33 | +
|
| 34 | + :param field: name of elasticsearch field to provide the maximum for |
| 35 | + """ |
| 36 | + field = attr.ib(default="", validator=instance_of(str)) |
| 37 | + |
| 38 | + def to_json_data(self): |
| 39 | + return { |
| 40 | + 'type': 'max', |
| 41 | + 'field': self.field, |
| 42 | + 'settings': {}, |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +@attr.s |
| 47 | +class DateHistogramGroupBy(object): |
| 48 | + """A bucket aggregator that groups results by date. |
| 49 | +
|
| 50 | + https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html |
| 51 | +
|
| 52 | + :param id: ascending unique number per GroupBy clause |
| 53 | + :param field: name of the elasticsearch field to group by |
| 54 | + :param interval: interval to group by |
| 55 | + :param minDocCount: min. amount of records in the timespan to return a |
| 56 | + result |
| 57 | + """ |
| 58 | + id = attr.ib(default=0, validator=instance_of(int)) |
| 59 | + field = attr.ib(default=DATE_HISTOGRAM_DEFAULT_FIELD, |
| 60 | + validator=instance_of(str)) |
| 61 | + interval = attr.ib(default="auto", validator=instance_of(str)) |
| 62 | + minDocCount = attr.ib(default=0, validator=instance_of(int)) |
| 63 | + |
| 64 | + def to_json_data(self): |
| 65 | + return { |
| 66 | + 'field': self.field, |
| 67 | + 'id': str(self.id), |
| 68 | + 'settings': { |
| 69 | + 'interval': self.interval, |
| 70 | + 'min_doc_count': self.minDocCount, |
| 71 | + 'trimEdges': 0, |
| 72 | + }, |
| 73 | + 'type': 'date_histogram', |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | +@attr.s |
| 78 | +class Filter(object): |
| 79 | + """ A Filter for a FilterGroupBy aggregator. |
| 80 | +
|
| 81 | + https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html |
| 82 | +
|
| 83 | + :param label: label for the metric that is shown in the graph |
| 84 | + :param query: the query to filter by |
| 85 | + """ |
| 86 | + label = attr.ib(default="", validator=instance_of(str)) |
| 87 | + query = attr.ib(default="", validator=instance_of(str)) |
| 88 | + |
| 89 | + def to_json_data(self): |
| 90 | + return { |
| 91 | + 'label': self.label, |
| 92 | + 'query': self.query, |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +@attr.s |
| 97 | +class FiltersGroupBy(object): |
| 98 | + """ A bucket aggregator that groups records by a filter expression. |
| 99 | +
|
| 100 | + https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html |
| 101 | +
|
| 102 | + :param id: ascending unique number per GroupBy clause |
| 103 | + :param filters: list of Filter objects |
| 104 | + """ |
| 105 | + id = attr.ib(default=0, validator=instance_of(int)) |
| 106 | + filters = attr.ib(default=attr.Factory(list)) |
| 107 | + |
| 108 | + def to_json_data(self): |
| 109 | + return { |
| 110 | + 'id': str(self.id), |
| 111 | + 'settings': { |
| 112 | + 'filters': self.filters, |
| 113 | + }, |
| 114 | + 'type': 'filters', |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | +@attr.s |
| 119 | +class TermsGroupBy(object): |
| 120 | + """ A multi-bucket aggregator based on field values. |
| 121 | +
|
| 122 | + https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html |
| 123 | +
|
| 124 | + :param id: ascending unique number per GroupBy clause |
| 125 | + :param field: name of the field to group by |
| 126 | + :param minDocCount: min. amount of matching records to return a result |
| 127 | + :param order: ORDER_ASC or ORDER_DESC |
| 128 | + :param orderBy: term to order the bucker |
| 129 | + :param size: how many buckets are returned |
| 130 | + """ |
| 131 | + field = attr.ib(validator=instance_of(str)) |
| 132 | + id = attr.ib(default=0, validator=instance_of(int)) |
| 133 | + minDocCount = attr.ib(default=1, validator=instance_of(int)) |
| 134 | + order = attr.ib(default=ORDER_DESC, validator=instance_of(str)) |
| 135 | + orderBy = attr.ib(default="_term", validator=instance_of(str)) |
| 136 | + size = attr.ib(default=0, validator=instance_of(int)) |
| 137 | + |
| 138 | + def to_json_data(self): |
| 139 | + return { |
| 140 | + 'id': str(self.id), |
| 141 | + 'type': 'terms', |
| 142 | + 'field': self.field, |
| 143 | + 'settings': { |
| 144 | + 'min_doc_count': self.minDocCount, |
| 145 | + 'order': self.order, |
| 146 | + 'order_by': self.orderBy, |
| 147 | + 'size': self.size, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | +@attr.s |
| 153 | +class ElasticsearchTarget(object): |
| 154 | + """Generates Elasticsearch target JSON structure. |
| 155 | +
|
| 156 | + Grafana docs on using Elasticsearch: |
| 157 | + http://docs.grafana.org/features/datasources/elasticsearch/ |
| 158 | + Elasticsearch docs on querying or reading data: |
| 159 | + https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html |
| 160 | +
|
| 161 | + :param alias: legend alias |
| 162 | + :param bucketAggs: Bucket aggregators |
| 163 | + :param metricAggs: Metric Aggregators |
| 164 | + :param query: query |
| 165 | + :param refId: target reference id |
| 166 | + """ |
| 167 | + |
| 168 | + alias = attr.ib(default=None) |
| 169 | + bucketAggs = attr.ib(default=attr.Factory( |
| 170 | + lambda: [DateHistogramGroupBy()])) |
| 171 | + metricAggs = attr.ib(default=attr.Factory(lambda: [CountMetricAgg()])) |
| 172 | + query = attr.ib(default="", validator=instance_of(str)) |
| 173 | + refId = attr.ib(default="", validator=instance_of(str)) |
| 174 | + |
| 175 | + def _map_bucket_aggs(self, f): |
| 176 | + return attr.assoc(self, bucketAggs=list(map(f, self.bucketAggs))) |
| 177 | + |
| 178 | + def auto_bucket_agg_ids(self): |
| 179 | + """Give unique IDs all bucketAggs without ID. |
| 180 | +
|
| 181 | + Returns a new ``ElasticsearchTarget`` that is the same as this one, |
| 182 | + except all of the bucketAggs have their ``id`` property set. Any panels |
| 183 | + which had an ``id`` property set will keep that property, all others |
| 184 | + will have auto-generated IDs provided for them. |
| 185 | +
|
| 186 | + If the bucketAggs don't have unique ID associated with it, the |
| 187 | + generated graph will be broken. |
| 188 | + """ |
| 189 | + ids = set([agg.id for agg in self.bucketAggs if agg.id]) |
| 190 | + auto_ids = (i for i in itertools.count(1) if i not in ids) |
| 191 | + |
| 192 | + def set_id(agg): |
| 193 | + if agg.id: |
| 194 | + return agg |
| 195 | + |
| 196 | + return attr.evolve(agg, id=next(auto_ids)) |
| 197 | + |
| 198 | + return self._map_bucket_aggs(set_id) |
| 199 | + |
| 200 | + def to_json_data(self): |
| 201 | + return { |
| 202 | + 'alias': self.alias, |
| 203 | + 'bucketAggs': self.bucketAggs, |
| 204 | + 'metrics': self.metricAggs, |
| 205 | + 'query': self.query, |
| 206 | + 'refId': self.refId, |
| 207 | + } |
0 commit comments