I have some event data, I need to print only single data with respect to a single event based on the Updated time of the event.
I have all the data saved in the hash of arrays and from there I need to extract a single record from each of the events which have the latest updated time.
Here is the script:
use strict;
use warnings;
my %data = (
'1342' => [
{
'Severity' => 'MEDIUM',
'Node' => 'Node002',
'State' => 'ACTIVE_UNACKNOWLEDGED',
'Updated' => '2020-10-01T12:00:00',
'eventId' => '1342'
},
{
'Severity' => 'HIGH',
'Node' => 'Node002',
'Updated' => '2020-10-01T12:05:00',
'eventId' => '1342',
'State' => 'ACTIVE_UNACKNOWLEDGED'
}
],
'1341' => [
{
'State' => 'ACTIVE_UNACKNOWLEDGED',
'eventId' => '1341',
'Updated' => '2020-10-01T12:10:00',
'Node' => 'Node001',
'Severity' => 'HIGH'
},
{
'State' => 'ACTIVE_UNACKNOWLEDGED',
'Updated' => '2020-10-01T12:15:00',
'eventId' => '1341',
'Severity' => 'HIGH',
'Node' => 'Node001'
},
{
'State' => 'CLEARED_ACKNOWLEDGED',
'Updated' => '2020-10-01T12:15:00',
'eventId' => '1341',
'Severity' => 'CLEARED',
'Node' => 'Node001'
}
]
);
foreach my $id ( sort keys %data ){
print "Event: $id\n";
foreach my $record( sort { $b->{Updated} cmp $a->{Updated} } @{ $data{$id} }) {
print "\t";
print "$record->{Node},$record->{Severity},$record->{State},$record->{Updated}";
print "\n";
last;
}
}
Current Output:
Event: 1341
Node001,HIGH,ACTIVE_UNACKNOWLEDGED,2020-10-01T12:15:00
Event: 1342
Node002,HIGH,ACTIVE_UNACKNOWLEDGED,2020-10-01T12:05:00
Expected Output:
Event: 1341
Node001,CLEARED,CLEARED_ACKNOWLEDGED,2020-10-01T12:15:00
Event: 1342
Node002,HIGH,ACTIVE_UNACKNOWLEDGED,2020-10-01T12:05:00
If the particular event has the same updated time for different severities, it should pick the record which has CLEARED severity.
In the above case, we can see for EventID 1341 there are CLEARED and HIGH severity exists at the same time i.e., 2020-10-01T12:15:00.
So, I should pick the CLEARED severity record instead of HIGH severity. How can I pick that record?