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