From 826eb46580734a4904bf276fe19055649c59a3e4 Mon Sep 17 00:00:00 2001 From: stan Date: Wed, 5 May 2004 09:10:22 +0000 Subject: [PATCH] Autocloses open events, plus corrected some bogus statement handles in errors. git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@1052 e3e1d417-86f3-4887-817a-d78f3d33393f --- scripts/zmaudit.pl.z | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/scripts/zmaudit.pl.z b/scripts/zmaudit.pl.z index bcc6efa8c..931890690 100755 --- a/scripts/zmaudit.pl.z +++ b/scripts/zmaudit.pl.z @@ -99,6 +99,9 @@ Parameters are :- sub confirm { + my $prompt = shift || "delete"; + my $action = shift || "deleting"; + my $yesno = $yes?1:0; if ( $report ) { @@ -106,11 +109,11 @@ sub confirm } elsif ( $yes ) { - print( ", deleting\n" ); + print( ", $action\n" ); } else { - print( ", delete y/n: " ); + print( ", $prompt y/n: " ); my $char = <>; chomp( $char ); if ( $char eq 'q' ) @@ -248,7 +251,7 @@ do { my $res = $sth4->execute( $db_event ) or die( "Can't execute: ".$sth4->errstr() ); $res = $sth5->execute( $db_event ) or die( "Can't execute: ".$sth5->errstr() ); - $res = $sth6->execute( $db_event ) or die( "Can't execute: ".$sth5->errstr() ); + $res = $sth6->execute( $db_event ) or die( "Can't execute: ".$sth6->errstr() ); } } } @@ -273,7 +276,7 @@ do print( "Found orphaned frame records for event '$frame->{EventId}'" ); if ( confirm() ) { - $res = $sth5->execute( $frame->{EventId} ) or die( "Can't execute: ".$sth7->errstr() ); + $res = $sth5->execute( $frame->{EventId} ) or die( "Can't execute: ".$sth6->errstr() ); } } @@ -285,7 +288,22 @@ do print( "Found orphaned statistic records for event '$stat->{EventId}'" ); if ( confirm() ) { - $res = $sth6->execute( $stat->{EventId} ) or die( "Can't execute: ".$sth8->errstr() ); + $res = $sth6->execute( $stat->{EventId} ) or die( "Can't execute: ".$sth6->errstr() ); + } + } + + # New audit to close any events that were left open for longer than 5 minutes + my $sql9 = "select E.Id, max(F.TimeStamp) as EndTime, unix_timestamp(max(F.TimeStamp)) - unix_timestamp(E.StartTime) as Length, count(F.Id) as Frames, count(if(F.Score>0,1,NULL)) as AlarmFrames, sum(F.Score) as TotScore, max(F.Score) as MaxScore from Events as E inner join Frames as F on E.Id = F.EventId where isnull(E.Frames) group by E.Id having EndTime < (now() - interval 5 minute)"; + my $sth9 = $dbh->prepare_cached( $sql9 ) or die( "Can't prepare '$sql9': ".$dbh->errstr() ); + my $sql10 = "update Events set Name = ?, EndTime = ?, Length = ?, Frames = ?, AlarmFrames = ?, TotScore = ?, AvgScore = ?, MaxScore = ? where Id = ?"; + my $sth10 = $dbh->prepare_cached( $sql10 ) or die( "Can't prepare '$sql10': ".$dbh->errstr() ); + $res = $sth9->execute() or die( "Can't execute: ".$sth9->errstr() ); + while( my $event = $sth9->fetchrow_hashref() ) + { + print( "Found open event '$event->{Id}'" ); + if ( confirm( 'close', 'closing' ) ) + { + $res = $sth10->execute( sprintf( "Event-%d(r)", $event->{Id}), $event->{EndTime}, $event->{Length}, $event->{Frames}, $event->{AlarmFrames}, $event->{TotScore}, $event->{AlarmFrames}?int($event->{TotScore}/$event->{AlarmFrames}):0, $event->{MaxScore}, $event->{Id} ) or die( "Can't execute: ".$sth10->errstr() ); } }