I'm using the output of a scripted metric aggregation like the one I describe here as the payload for a Watcher condition. I'd expect the ratio of two numbers to also be a number, but it's coming back as a string unless I explicitly cast it as a float like this:
The dynamic nature of things can often burn you as often as it can help you in Groovy, as it seems to be doing here. The core problem here is that Groovy does division with integers using BigDecimal, which ends up being a string when it's time to serialize it. You can prove this by using script fields to see what is happening (note: with script.inline: on):
Your best bet is to cast the values to tip off Groovy what it needs to become before returning it. It's effectively what you're doing with toFloat();.
Another solution exists without casting, as you are using a constant of 100 in your calculation: change it to 100.0, which will make it a double rather than an integer, and the result will automatically be a double as a result.
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.