# Wrong calculations - ruby code

**URL:** <https://discuss.elastic.co/t/wrong-calculations-ruby-code/328093>\
**Category:** Logstash\
**Created:** [March 20, 2023, 3:00pm UTC](https://discuss.elastic.co/t/wrong-calculations-ruby-code/328093 "2023-03-20T15:00:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wedkarz014](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wedkarz014/32/48935_2.png) [@wedkarz014](https://discuss.elastic.co/u/wedkarz014)\
**Post date:** [March 20, 2023, 3:00pm UTC](https://discuss.elastic.co/t/wrong-calculations-ruby-code/328093/1 "2023-03-20T15:00:11Z")

</div>

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

```auto
   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
        "
   }

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/2/822f60d512150534ccec205a41eb97d4d33b5b71.png)

another example:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/6/e627e33dae225655cab958e3eae892486ffb744f.png)

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

---

<div class="post-metadata">

**Author:** ![wedkarz014](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wedkarz014/32/48935_2.png) [@wedkarz014](https://discuss.elastic.co/u/wedkarz014)\
**Post date:** [March 23, 2023, 3:13pm UTC](https://discuss.elastic.co/t/wrong-calculations-ruby-code/328093/2 "2023-03-23T15:13:40Z")

</div>

Also tried with BigDecimals, but problem exists:

```auto
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

---

<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:** [April 20, 2023, 3:14pm UTC](https://discuss.elastic.co/t/wrong-calculations-ruby-code/328093/3 "2023-04-20T15:14:11Z")

</div>

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