# Find delta between successive bunch of mysql input fields

**URL:** <https://discuss.elastic.co/t/find-delta-between-successive-bunch-of-mysql-input-fields/79956>\
**Category:** Logstash\
**Created:** [March 24, 2017, 10:08pm UTC](https://discuss.elastic.co/t/find-delta-between-successive-bunch-of-mysql-input-fields/79956 "2017-03-24T22:08:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pks](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@pks](https://discuss.elastic.co/u/pks)\
**Post date:** [March 24, 2017, 10:08pm UTC](https://discuss.elastic.co/t/find-delta-between-successive-bunch-of-mysql-input-fields/79956/1 "2017-03-24T22:08:12Z")

</div>

I have mysql input via jdbc plugin which has a bunch of incremental values (per minute) for some fields like below

'timestamp' 'id' 'value1' 'value 2' ....  
00:00 1 10 14  
00:00 2 13 17  
00:01 1 12 18  
00:01 2 16 20  
.  
.  
.

So at minute 0, id1 had values 10 and 14 and at minute 1 they became 12 and 18. I want to calculate the difference between these values every minute. So for id1, value1diff = 2 and value2diff = 4 and so on for each id and each value per minute.

Right now I can do this by separately processing each value in a ruby filter like

ruby {  
init =\> "@@prev\_value1 = -1  
@@prev\_value2 = -1"  
code =\> "  
if (@@prev\_value1 == -1 and @@prev\_value2 == 1)  
delta1 = 0  
delta2 = 0  
else   
delta1 = event.get('value1') - @@prev\_value1  
delta2 = event.get('value2') - @@prev\_value2  
end  
event.set('value1diff', delta1)  
event.set('value2diff',delta2)

```
             @@prev_value1 = event.get('value1')
             @@prev_value2 = event.get('value2')				
    "
}

```

But this will balloon up as soon as I try to do it for more values and more ids. Is there a way to handle such delta calculation via array or something? I am trying to find the best way to handle this task because I need to be processing upto 8 ids and 14 values.

Thank you!

---

<div class="post-metadata">

**Author:** ![pks](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@pks](https://discuss.elastic.co/u/pks)\
**Post date:** [March 25, 2017, 8:37pm UTC](https://discuss.elastic.co/t/find-delta-between-successive-bunch-of-mysql-input-fields/79956/2 "2017-03-25T20:37:26Z")

</div>

Anyone? Any suggestions? I am not able to write compact code for this operation ☹

---

<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 22, 2017, 8:37pm UTC](https://discuss.elastic.co/t/find-delta-between-successive-bunch-of-mysql-input-fields/79956/3 "2017-04-22T20:37:57Z")

</div>

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