# Format Scripted metrics result

**URL:** <https://discuss.elastic.co/t/format-scripted-metrics-result/86442>\
**Category:** Elasticsearch\
**Created:** [May 19, 2017, 2:34pm UTC](https://discuss.elastic.co/t/format-scripted-metrics-result/86442 "2017-05-19T14:34:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayoubK](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ayoubK](https://discuss.elastic.co/u/ayoubK)\
**Post date:** [May 19, 2017, 2:34pm UTC](https://discuss.elastic.co/t/format-scripted-metrics-result/86442/1 "2017-05-19T14:34:14Z")

</div>

Hi everyone,

I'm tring to format the result of my scripted metric from seconds to hh:mm:ss  
I didn't find any easy way to perform that and i've tried using the reduce script to do so.  
Doing this i've got a surprising error telling me ""unexpected token [';'] was expecting one of [].""  
the semi-colon mentionned in this error is the one in the reduce script just after the for.

here is the request i used

POST elasticube-1101\_homolog-2017.05.15/\_search  
{  
"aggs": {  
"profit": {  
"scripted\_metric": {  
"init\_script" :  
"params.\_agg.numerator = []; params.\_agg.denominator = [];"  
,  
"map\_script" :  
"params.\_agg.numerator.add(doc["action.cp"].value); params.\_agg.denominator.add(doc["action.can"].value + doc["action.cap"].value);"  
,  
"combine\_script" : "int num\_sum=0; int den\_sum=0; for (t in params.\_agg.numerator) { num\_sum+= t} for (t in params.\_agg.denominator) { den\_sum += t } return ['num\_sum': num\_sum, 'den\_sum': den\_sum];",  
"reduce\_script" : "float num\_sum = 0;for (a in params.\_aggs) {num\_sum += a.num\_sum;};var h = Math.floor(num\_sum / 3600);var m = Math.floor(num\_sum % 3600 / 60);var s = Math.floor(num\_sum % 3600 % 60);return h + ":" + m + ":" + s;"  
}  
}  
}  
}

Can someone help me with formatting my result?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 26, 2017, 1:55pm UTC](https://discuss.elastic.co/t/format-scripted-metrics-result/86442/2 "2017-05-26T13:55:19Z")

</div>

I think the problem is the semicolon after the curly brace:

> [@ayoubK](#):
>
> for (a in params.aggs) {numsum += a.num\_sum;};

Formatted, that looks like:

```auto
for (a in params.aggs) {
  numsum += a.num_sum;
}; // <-- here

```

---

<div class="post-metadata">

**Author:** ![ayoubK](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ayoubK](https://discuss.elastic.co/u/ayoubK)\
**Post date:** [June 13, 2017, 8:25am UTC](https://discuss.elastic.co/t/format-scripted-metrics-result/86442/3 "2017-06-13T08:25:41Z")

</div>

Thanks Polyfractal for your answer but the semicolon wasn't the problem.  
I solved my issue by changing the type of my variables from int to double.  
I deal with a large ammount of milliseconds that just doesn't fit into int

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 11, 2017, 8:31am UTC](https://discuss.elastic.co/t/format-scripted-metrics-result/86442/4 "2017-07-11T08:31:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
