Calculate Delta - Aggregate Filter


(karnamonkster) #1

Hi,

I need to calculate delta consumption from a stream of events generated by multiple devices uniquely identified by an ID.
Based on some other forums i could see this is possible using Logstash Aggregate filter

Here is my attempt but unsuccessful to calculate delta between two readings for every device.

filter {
        aggregate {
        task_id => "%{uniquedeviceid}"
        code => "
                map['next'] ||= 0 ;
                map['next'] -= event.get('reading');
                event.set('delta', (map['next'] - event.get('reading')))"
                timeout_task_id_field => "uniquedeviceid"
        }

Need some help in getting towards the solution
I need to calculate
delta = Current Reading - Previous Reading


(karnamonkster) #2

Figured out..
Posting the solution, may help someone.

filter {
        aggregate {
        task_id => "%{uniquedeviceid}"
        code => "
                map['next'] ||= event.get('reading');
                event.set('delta', (event.get('reading') - map['next']))"
                timeout_task_id_field => "uniquedeviceid"
        }