##// END OF EJS Templates
elasticsearch: 5.x fixes
ergo -
Show More
@@ -1,63 +1,63 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
3 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
4 #
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
7 # You may obtain a copy of the License at
8 #
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
10 #
11 # Unless required by applicable law or agreed to in writing, software
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
15 # limitations under the License.
16
16
17 from appenlight.models import Datastores
17 from appenlight.models import Datastores
18 from appenlight.models.services.base import BaseService
18 from appenlight.models.services.base import BaseService
19 from appenlight.lib.enums import ReportType
19 from appenlight.lib.enums import ReportType
20 from appenlight.lib.utils import es_index_name_limiter
20 from appenlight.lib.utils import es_index_name_limiter
21
21
22
22
23 class ReportStatService(BaseService):
23 class ReportStatService(BaseService):
24 @classmethod
24 @classmethod
25 def count_by_type(cls, report_type, resource_id, since_when):
25 def count_by_type(cls, report_type, resource_id, since_when):
26 report_type = ReportType.key_from_value(report_type)
26 report_type = ReportType.key_from_value(report_type)
27
27
28 index_names = es_index_name_limiter(start_date=since_when, ixtypes=["reports"])
28 index_names = es_index_name_limiter(start_date=since_when, ixtypes=["reports"])
29
29
30 es_query = {
30 es_query = {
31 "aggs": {
31 "aggs": {
32 "reports": {
32 "reports": {
33 "aggs": {
33 "aggs": {
34 "sub_agg": {"value_count": {"field": "tags.group_id.values"}}
34 "sub_agg": {"value_count": {"field": "tags.group_id.values.keyword"}}
35 },
35 },
36 "filter": {
36 "filter": {
37 "bool": {
37 "bool": {
38 "filter": [
38 "filter": [
39 {"terms": {"resource_id": [resource_id]}},
39 {"terms": {"resource_id": [resource_id]}},
40 {"exists": {"field": "tags.group_id.values"}},
40 {"exists": {"field": "tags.group_id.values"}},
41 ]
41 ]
42 }
42 }
43 },
43 },
44 }
44 }
45 },
45 },
46 "query": {
46 "query": {
47 "bool": {
47 "bool": {
48 "filter": [
48 "filter": [
49 {"terms": {"resource_id": [resource_id]}},
49 {"terms": {"resource_id": [resource_id]}},
50 {"terms": {"tags.type.values": [report_type]}},
50 {"terms": {"tags.type.values": [report_type]}},
51 {"range": {"timestamp": {"gte": since_when}}},
51 {"range": {"timestamp": {"gte": since_when}}},
52 ]
52 ]
53 }
53 }
54 },
54 },
55 }
55 }
56
56
57 if index_names:
57 if index_names:
58 result = Datastores.es.search(
58 result = Datastores.es.search(
59 body=es_query, index=index_names, doc_type="log", size=0
59 body=es_query, index=index_names, doc_type="log", size=0
60 )
60 )
61 return result["aggregations"]["reports"]["sub_agg"]["value"]
61 return result["aggregations"]["reports"]["sub_agg"]["value"]
62 else:
62 else:
63 return 0
63 return 0
General Comments 4
Under Review
author

Auto status change to "Under Review"

Under Review
author

Auto status change to "Under Review"

You need to be logged in to leave comments. Login now