report_stat.py
63 lines
| 2.3 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r0 | ||||
from appenlight.models import Datastores | ||||
from appenlight.models.services.base import BaseService | ||||
from appenlight.lib.enums import ReportType | ||||
from appenlight.lib.utils import es_index_name_limiter | ||||
class ReportStatService(BaseService): | ||||
@classmethod | ||||
def count_by_type(cls, report_type, resource_id, since_when): | ||||
report_type = ReportType.key_from_value(report_type) | ||||
r153 | index_names = es_index_name_limiter(start_date=since_when, ixtypes=["reports"]) | |||
r0 | ||||
es_query = { | ||||
r153 | "aggs": { | |||
"reports": { | ||||
"aggs": { | ||||
"sub_agg": {"value_count": {"field": "tags.group_id.values"}} | ||||
}, | ||||
r158 | "filter": { | |||
r165 | "bool": { | |||
"filter": [ | ||||
{"terms": {"resource_id": [resource_id]}}, | ||||
{"exists": {"field": "tags.group_id.values"}}, | ||||
] | ||||
} | ||||
r158 | }, | |||
r153 | } | |||
}, | ||||
"query": { | ||||
r156 | "bool": { | |||
r157 | "filter": [ | |||
{"terms": {"resource_id": [resource_id]}}, | ||||
{"terms": {"tags.type.values": [report_type]}}, | ||||
{"range": {"timestamp": {"gte": since_when}}}, | ||||
] | ||||
r153 | } | |||
}, | ||||
} | ||||
r0 | ||||
if index_names: | ||||
r153 | result = Datastores.es.search( | |||
body=es_query, index=index_names, doc_type="log", size=0 | ||||
) | ||||
return result["aggregations"]["reports"]["sub_agg"]["value"] | ||||
r0 | else: | |||
return 0 | ||||