Hi,
In an index we have multiple documents for sequential steps that are part of a single transaction.
We need to calculate the elapsed times for each step.
Instead of emitting 1 document with all timings for all steps,
{
....
...
"_source" : {
"transaction_id" : "0000017cf17ef9c5-116feb",
"end_time" : "2021-11-08T11:30:52.248Z",
"start_time" : "2021-11-08T11:30:52.174Z",
"total_elapsed_ms" : 51,
"processing_time" : {
"step-1" : {
"transaction_id" : "0000017cf17ef9c5-116feb",
"cum_elapsed_ms" : 15,
"elapsed_ms" : 15,
"handling_time" : "2021-11-08T11:30:52.189Z"
},
"step-0" : {
"transaction_id" : "0000017cf17ef9c5-116feb",
"cum_elapsed_ms" : 0,
"elapsed_ms" : 0,
"handling_time" : "2021-11-08T11:30:52.174Z"
},
"step-2" : {
"transaction_id" : "0000017cf17ef9c5-116feb",
"cum_elapsed_ms" : 51,
"elapsed_ms" : 36,
"handling_time" : "2021-11-08T11:30:52.225Z"
}
}
}
}
I need to have a document for each step
Is there a way to achieve this?
Regards Hans
Below are the pivot and aggregations parts
"pivot": {
"group_by": {
"transaction_id": {
"terms": {
"field": "transactionId"
}
}
},
"aggregations": {
"verwerkingstijden": {
"scripted_metric": {
"init_script": "state.docs = []",
"map_script":
"""
Map span = [
'handling_time':doc['handlingTime'].value,
'transaction_id':doc['transactionId'].value,
'messageId':doc['messageId'].value
];
state.docs.add(span)
""",
"combine_script": "return state.docs",
"reduce_script":
"""
def ret = new HashMap();
def all_docs = [];
for (s in states) {
for (span in s) {
all_docs.add(span);
}
}
all_docs.sort((HashMap o1, HashMap o2)-> o1['handlingTime'].millis.compareTo(o2['handlingTime'].millis));
def no_docs = all_docs.size();
def total_elapsed_ms = all_docs[no_docs-1]['handlingTime'].millis - all_docs[0]['handlingTime'].millis;
all_docs[0]['elapsed_ms'] = 0;
all_docs[0]['cum_elapsed_ms'] = 0;
ret['step-' + 0] = all_docs[0];
for (int i=1; i<no_docs; i++) {
all_docs[i]['elapsed_ms']= all_docs[i]['handlingTime'].millis - all_docs[i-1]['handlingTime'].millis;
all_docs[i]['cum_elapsed_ms']= all_docs[i]['handlingTime'].millis - all_docs[0]['handlingTime'].millis;
ret['step-' + i] = all_docs[i];
}
ret['total_elapsed_ms'] = total_elapsed_ms;
ret['start_time'] = all_docs[0]['handlingTime'];
ret['end_time'] = all_docs[no_docs-1]['handlingTime'];
return ret;
"""
}
}
}
}