Skip to content

Instantly share code, notes, and snippets.

@rokcarl
Created July 4, 2022 13:53
Show Gist options
  • Save rokcarl/bbb5577a74ccd733de5652c898aa4987 to your computer and use it in GitHub Desktop.
Save rokcarl/bbb5577a74ccd733de5652c898aa4987 to your computer and use it in GitHub Desktop.
{
"description": "Credits and keys per customer",
"dest": {"index": "stat_customers_with_keys"},
"source": {
"index": "stats-*",
"query" : {
"bool" : {
"must": [{
"range" : {"timestamp": {"gte" : "now-3h"}}
}]
}
}
},
"frequency": "1h",
"sync": {"time": {"field": "timestamp","delay": "90m"}},
"pivot": {
"aggs": {
"credits": { "sum": { "field": "credits" } },
"requests": { "value_count": { "field": "timestamp" }},
"error_codes": {
"scripted_metric": {
"init_script": "state.responses = [:]",
"map_script": """
def key_name = 'missing';
if (doc['error.code'].size() != 0) {
def error_code = doc['error.code'].value;
if (error_code >= 400 && error_code <= 499) {
key_name = '4xx';
}
else if (error_code >= 500 && error_code <= 599) {
key_name = '5xx';
}
else {
key_name = error_code;
}
}
if (!state.responses.containsKey(key_name)) {
state.responses[key_name] = 0;
}
state.responses[key_name] += 1;
""",
"combine_script": "state.responses",
"reduce_script": """
def counts = [:];
for (responses in states) {
for (key in responses.keySet()) {
if (key == 'missing') {
continue;
}
if (!counts.containsKey(key)) {
counts[key] = 0;
}
counts[key] += responses[key];
}
}
return counts;
"""
}
},
"keys": {
"scripted_metric": {
"init_script": "state.keys = [];",
"map_script": "state.keys.add(doc.key.value)",
"combine_script": "return state.keys",
"reduce_script": """
def key_counts = [:];
for (keys in states) {
for (key in keys) {
if (!key_counts.containsKey(key)) {
key_counts[key] = 0;
}
key_counts[key] += 1;
}
}
def results = [];
for (key in key_counts.keySet()) {
def count = key_counts[key];
// results.add({"name": key, "requests": count})
Map result = new HashMap();
result.put("name", key);
result.put("requests", count);
results.add(result);
}
return results;
"""
}
}
},
"group_by": {
"day": {"date_histogram": {"field": "timestamp", "fixed_interval": "1d"}},
"customer": {"terms": {"field":"customer"}}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment