Let me expose what i'm trying to achieve here. I calculate an elapsed_time (with the elapsed plugin) between two documents. elapsed_time is in second and i want it like this : HH:MM:SS so as an example : 80 sec = 00:01:20 but i'm struggling to do this. At first, I tried in kibana with Scripted fields and follow the response of LeeDr in this link : JSON Input: Converting Minutes to Minutes and Seconds
In terms of Kibana and the error that you have in your screenshot, it's pointing out that you have some documents that have a missing value for elapsed_time and that you should do a check first. So with that and assuming that you want to allow hours to be larger than 24 (or else in a date/time format, that would increment the day field), here is an example:
if (doc['elapsed_time'].size()==0) {
return ""
} else {
long elapsed_time=(long)doc["elapsed_time"].value*1000;
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
// long elapsedDays = elapsed_time / daysInMilli;
// elapsed_time = elapsed_time % daysInMilli;/
long elapsedHours = elapsed_time / hoursInMilli;
elapsed_time = elapsed_time % hoursInMilli;
long elapsedMinutes = elapsed_time / minutesInMilli;
elapsed_time = elapsed_time % minutesInMilli;
long elapsedSeconds = elapsed_time / secondsInMilli;
String my_answer = String.format('%02d:%02d:%02d', new def[] {(int)elapsedHours, (int)elapsedMinutes, (int)elapsedSeconds});
return my_answer;
}
I left elapsedDays as a comment just in case you did want to use it.
It displayed the time how i want but the field is in a date format, so i cannot use this to do an average or max vizualisation since it's not a number.
Angelo, in your script if i only let
if (doc['elapsed_time'].size()==0) {
return ""
} else {
doc["elapsed_time"].value*1000;
}
and keep this as a Number and not a date
I have my value multiply by 1000 (seems logic) but when i try to add the next piece of script that you gave, I have the 0 value that I show presviously
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.