##// END OF EJS Templates
elasticsearch: replace "filtered" with "bool" clause
elasticsearch: replace "filtered" with "bool" clause

File last commit:

r156:ba532c17
r156:ba532c17
Show More
report_stat.py
63 lines | 2.3 KiB | text/x-python | PythonLexer
project: initial commit
r0 # -*- coding: utf-8 -*-
license: change the license to Apache 2.0
r112 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
project: initial commit
r0 #
license: change the license to Apache 2.0
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
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # http://www.apache.org/licenses/LICENSE-2.0
project: initial commit
r0 #
license: change the license to Apache 2.0
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.
project: initial commit
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)
black: reformat source
r153 index_names = es_index_name_limiter(start_date=since_when, ixtypes=["reports"])
project: initial commit
r0
es_query = {
black: reformat source
r153 "aggs": {
"reports": {
"aggs": {
"sub_agg": {"value_count": {"field": "tags.group_id.values"}}
},
"filter": {
"and": [
{"terms": {"resource_id": [resource_id]}},
{"exists": {"field": "tags.group_id.values"}},
]
},
}
},
"query": {
elasticsearch: replace "filtered" with "bool" clause
r156 "bool": {
black: reformat source
r153 "filter": {
"and": [
{"terms": {"resource_id": [resource_id]}},
{"terms": {"tags.type.values": [report_type]}},
{"range": {"timestamp": {"gte": since_when}}},
]
}
}
},
}
project: initial commit
r0
if index_names:
black: reformat source
r153 result = Datastores.es.search(
body=es_query, index=index_names, doc_type="log", size=0
)
return result["aggregations"]["reports"]["sub_agg"]["value"]
project: initial commit
r0 else:
return 0