Skip to content

Instantly share code, notes, and snippets.

@palash25
Created May 19, 2019 17:36
Show Gist options
  • Select an option

  • Save palash25/550311c311eaf0b4232d745b00e17c35 to your computer and use it in GitHub Desktop.

Select an option

Save palash25/550311c311eaf0b4232d745b00e17c35 to your computer and use it in GitHub Desktop.
def create_filter_pipeline(body):
"Creates pipeline of filters based on the global filters present on the dashboard"
form_id = body.get("formId")
start_date = body.get("startDate")
end_date = body.get("endDate")
date_field = body.get("dateField", "_created_at")
user_id_array = body.get("userId")
filter_pipeline = []
if form_id:
form_match = {
'match': {
'formId': form_id
}
}
filter_pipeline.append(form_match)
is_active_match = {
'match': {
'isActive': True
}
}
filter_pipeline.append(is_active_match)
client_match = {
'match': {
'client': 'android'
}
}
filter_pipeline.append(client_match)
date_range_match = {
'range': {
date_field: {
'gte': start_date,
'lte': end_date
}
}
}
filter_pipeline.append(date_range_match)
if user_id_array:
user_id_array = ["_User${0}".format(user_id) for user_id in user_id_array]
user_id_match = {
'terms': {
'_p_createdBy': user_id_array
}
}
filter_pipeline.append(user_id_match)
return filter_pipeline
def run():
test = {
'formId': '324fds',
'startDate': 'fff',
'endDate': 'fff',
'dateField': 'fff',
'userId': 'dfsfd'
}
filter_pipeline = create_filter_pipeline(test)
query_body = {
'bool': {
'filter': filter_pipeline
}
}
# Add user agregation
aggregation = {
'responses_over_time': {
"date_histogram": {
"field": "fdsf",
"interval": "dsfsdf"
}
}
}
aggregation_body = {
'size': 0,
'query': query_body,
'aggs': aggregation
}
# print("==Aggregation body==", aggregation_body)
#index_name, doc_type = "dsfdsf", "fdsdf"
#result = es.search(index=index_name, body=aggregation_body)
#error = result.get('error')
#if error is None:
# result = result.get("aggregations").get("responses_over_time")
# Add slack notification on failure
print(aggregation_body)
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment