Wrong calculations - ruby code

Hi all,
logstash v.7.17.8
I have ~45 metrics to calculate, here is the example, code and results:

   ruby {
        code => "
                if !event.get('[Package2VersionCreatesWithoutValidation][Max]').nil? and !event.get('[Package2VersionCreatesWithoutValidation][Remaining]').nil?
                    Max = event.get('[Package2VersionCreatesWithoutValidation][Max]')
                    Remaining = event.get('[Package2VersionCreatesWithoutValidation][Remaining]')
                    Used = Max - Remaining
                    if Max == 0
                        UsedPercentage = 0
                    else
                        UsedPercentage = Used.to_f/Max * 100.00
                    end
                    event.set('[Package2VersionCreatesWithoutValidation][Used]', Used)
                    event.set('[Package2VersionCreatesWithoutValidation][UsedPercentage]', UsedPercentage.round(2))
                end
        "
   }

another example:
image

as you can see, usually it works fine but sometimes i have totally incorrect values.
Do you have any idea what's wrong in my code?

Best
Patryk

Also tried with BigDecimals, but problem exists:

ruby {
        code => "
                require 'bigdecimal'
                if !event.get('[AnalyticsExternalDataSizeMB][Max]').nil? and !event.get('[AnalyticsExternalDataSizeMB][Remaining]').nil?
                    Max = BigDecimal(event.get('[AnalyticsExternalDataSizeMB][Max]'))
                    event.set('[AnalyticsExternalDataSizeMB][Max_BD]', Max)
                    Remaining = BigDecimal(event.get('[AnalyticsExternalDataSizeMB][Remaining]'))
                    event.set('[AnalyticsExternalDataSizeMB][Remaining_BD]', Remaining)
                    Used = Max - Remaining
                    if Max.to_i == 0
                        UsedPercentage = 0
                    else
                        UsedPercentage = Used/Max * 100
                    end
                    event.set('[AnalyticsExternalDataSizeMB][Used]', Used.to_i)
                    event.set('[AnalyticsExternalDataSizeMB][UsedPercentage]', UsedPercentage.to_f.round(2))
                end
        "
   }

I'm not sure if BigDecimals works in logstash, eg. in AnalyticsExternalDataSizeMB.Max_BD i have normal int number, but it should be sth like 0.1e1

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