Calculate Delta - Aggregate Filter

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

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"
        }
1 Like

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