Merge pull request #1504 from ZoneMinder/improve_filter

Improve filter
This commit is contained in:
Andrew Bauer 2017-03-18 21:12:58 -05:00 committed by GitHub
commit 2dcd95bc7f
4 changed files with 83 additions and 65 deletions

View File

@ -43,8 +43,8 @@ our @ISA = qw(Exporter ZoneMinder::Base);
# will save memory. # will save memory.
our %EXPORT_TAGS = ( our %EXPORT_TAGS = (
'functions' => [ qw( 'functions' => [ qw(
) ] ) ]
); );
push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS; push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS;
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
@ -130,6 +130,8 @@ sub find {
my $filter = new ZoneMinder::Filter( $$db_filter{Id}, $db_filter ); my $filter = new ZoneMinder::Filter( $$db_filter{Id}, $db_filter );
push @results, $filter; push @results, $filter;
} # end while } # end while
$sth->finish();
return @results; return @results;
} }
@ -176,78 +178,74 @@ sub Sql {
my $self = $_[0]; my $self = $_[0];
if ( ! $$self{Sql} ) { if ( ! $$self{Sql} ) {
my $filter_expr = ZoneMinder::General::jsonDecode( $self->{Query} ); my $filter_expr = ZoneMinder::General::jsonDecode( $self->{Query} );
my $sql = "SELECT E.Id, my $sql = "SELECT E.*,
E.MonitorId, unix_timestamp(E.StartTime) as Time,
M.Name as MonitorName, M.Name as MonitorName,
M.DefaultRate, M.DefaultRate,
M.DefaultScale, M.DefaultScale
E.Name,
E.Cause,
E.Notes,
E.StartTime,
unix_timestamp(E.StartTime) as Time,
E.Length,
E.Frames,
E.AlarmFrames,
E.TotScore,
E.AvgScore,
E.MaxScore,
E.Archived,
E.Videoed,
E.Uploaded,
E.Emailed,
E.Messaged,
E.Executed
FROM Events as E FROM Events as E
INNER JOIN Monitors as M on M.Id = E.MonitorId INNER JOIN Monitors as M on M.Id = E.MonitorId
"; ";
$self->{Sql} = ''; $self->{Sql} = '';
if ( $filter_expr->{terms} ) { if ( $filter_expr->{terms} ) {
for ( my $i = 0; $i < @{$filter_expr->{terms}}; $i++ ) { foreach my $term ( @{$filter_expr->{terms}} ) {
if ( exists($filter_expr->{terms}[$i]->{cnj}) ) {
$self->{Sql} .= " ".$filter_expr->{terms}[$i]->{cnj}." "; if ( exists($term->{cnj}) ) {
$self->{Sql} .= " ".$term->{cnj}." ";
} }
if ( exists($filter_expr->{terms}[$i]->{obr}) ) { if ( exists($term->{obr}) ) {
$self->{Sql} .= " ".str_repeat( "(", $filter_expr->{terms}[$i]->{obr} )." "; $self->{Sql} .= " ".str_repeat( "(", $term->{obr} )." ";
} }
my $value = $filter_expr->{terms}[$i]->{val}; my $value = $term->{val};
my @value_list; my @value_list;
if ( $filter_expr->{terms}[$i]->{attr} ) { if ( $term->{attr} ) {
if ( $filter_expr->{terms}[$i]->{attr} =~ /^Monitor/ ) { if ( $term->{attr} =~ /^Monitor/ ) {
my ( $temp_attr_name ) = $filter_expr->{terms}[$i]->{attr} =~ /^Monitor(.+)$/; my ( $temp_attr_name ) = $term->{attr} =~ /^Monitor(.+)$/;
$self->{Sql} .= "M.".$temp_attr_name; $self->{Sql} .= "M.".$temp_attr_name;
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'DateTime' ) { } elsif ( $term->{attr} =~ /^Server/ ) {
$self->{Sql} .= "M.".$term->{attr};
} elsif ( $term->{attr} eq 'DateTime' ) {
$self->{Sql} .= "E.StartTime"; $self->{Sql} .= "E.StartTime";
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Date' ) { } elsif ( $term->{attr} eq 'Date' ) {
$self->{Sql} .= "to_days( E.StartTime )"; $self->{Sql} .= "to_days( E.StartTime )";
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Time' ) { } elsif ( $term->{attr} eq 'Time' ) {
$self->{Sql} .= "extract( hour_second from E.StartTime )"; $self->{Sql} .= "extract( hour_second from E.StartTime )";
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Weekday' ) { } elsif ( $term->{attr} eq 'Weekday' ) {
$self->{Sql} .= "weekday( E.StartTime )"; $self->{Sql} .= "weekday( E.StartTime )";
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'DiskPercent' ) { } elsif ( $term->{attr} eq 'DiskPercent' ) {
$self->{Sql} .= "zmDiskPercent"; $self->{Sql} .= "zmDiskPercent";
$self->{HasDiskPercent} = !undef; $self->{HasDiskPercent} = !undef;
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'DiskBlocks' ) { } elsif ( $term->{attr} eq 'DiskBlocks' ) {
$self->{Sql} .= "zmDiskBlocks"; $self->{Sql} .= "zmDiskBlocks";
$self->{HasDiskBlocks} = !undef; $self->{HasDiskBlocks} = !undef;
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'SystemLoad' ) { } elsif ( $term->{attr} eq 'SystemLoad' ) {
$self->{Sql} .= "zmSystemLoad"; $self->{Sql} .= "zmSystemLoad";
$self->{HasSystemLoad} = !undef; $self->{HasSystemLoad} = !undef;
} else { } else {
$self->{Sql} .= "E.".$filter_expr->{terms}[$i]->{attr}; $self->{Sql} .= "E.".$term->{attr};
} }
( my $stripped_value = $value ) =~ s/^["\']+?(.+)["\']+?$/$1/; ( my $stripped_value = $value ) =~ s/^["\']+?(.+)["\']+?$/$1/;
foreach my $temp_value ( split( /["'\s]*?,["'\s]*?/, $stripped_value ) ) { foreach my $temp_value ( split( /["'\s]*?,["'\s]*?/, $stripped_value ) ) {
if ( $filter_expr->{terms}[$i]->{attr} =~ /^Monitor/ ) { if ( $term->{attr} =~ /^Monitor/ ) {
$value = "'$temp_value'"; $value = "'$temp_value'";
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Name' } elsif ( $term->{attr} eq 'ServerId' ) {
|| $filter_expr->{terms}[$i]->{attr} eq 'Cause' if ( $temp_value eq 'ZM_SERVER_ID' ) {
|| $filter_expr->{terms}[$i]->{attr} eq 'Notes' $value = "'$Config{ZM_SERVER_ID}'";
# This gets used later, I forget for what
$$self{Server} = new ZoneMinder::Server( $Config{ZM_SERVER_ID} );
} else {
$value = "'$temp_value'";
# This gets used later, I forget for what
$$self{Server} = new ZoneMinder::Server( $temp_value );
}
} elsif ( $term->{attr} eq 'Name'
|| $term->{attr} eq 'Cause'
|| $term->{attr} eq 'Notes'
) { ) {
$value = "'$temp_value'"; $value = "'$temp_value'";
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'DateTime' ) { } elsif ( $term->{attr} eq 'DateTime' ) {
$value = DateTimeToSQL( $temp_value ); $value = DateTimeToSQL( $temp_value );
if ( !$value ) { if ( !$value ) {
Error( "Error parsing date/time '$temp_value', " Error( "Error parsing date/time '$temp_value', "
@ -255,7 +253,7 @@ sub Sql {
return; return;
} }
$value = "'$value'"; $value = "'$value'";
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Date' ) { } elsif ( $term->{attr} eq 'Date' ) {
$value = DateTimeToSQL( $temp_value ); $value = DateTimeToSQL( $temp_value );
if ( !$value ) { if ( !$value ) {
Error( "Error parsing date/time '$temp_value', " Error( "Error parsing date/time '$temp_value', "
@ -263,7 +261,7 @@ sub Sql {
return; return;
} }
$value = "to_days( '$value' )"; $value = "to_days( '$value' )";
} elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Time' ) { } elsif ( $term->{attr} eq 'Time' ) {
$value = DateTimeToSQL( $temp_value ); $value = DateTimeToSQL( $temp_value );
if ( !$value ) { if ( !$value ) {
Error( "Error parsing date/time '$temp_value', " Error( "Error parsing date/time '$temp_value', "
@ -277,21 +275,21 @@ sub Sql {
push( @value_list, $value ); push( @value_list, $value );
} # end foreach temp_value } # end foreach temp_value
} # end if has an attr } # end if has an attr
if ( $filter_expr->{terms}[$i]->{op} ) { if ( $term->{op} ) {
if ( $filter_expr->{terms}[$i]->{op} eq '=~' ) { if ( $term->{op} eq '=~' ) {
$self->{Sql} .= " regexp $value"; $self->{Sql} .= " regexp $value";
} elsif ( $filter_expr->{terms}[$i]->{op} eq '!~' ) { } elsif ( $term->{op} eq '!~' ) {
$self->{Sql} .= " not regexp $value"; $self->{Sql} .= " not regexp $value";
} elsif ( $filter_expr->{terms}[$i]->{op} eq '=[]' ) { } elsif ( $term->{op} eq '=[]' ) {
$self->{Sql} .= " in (".join( ",", @value_list ).")"; $self->{Sql} .= " in (".join( ",", @value_list ).")";
} elsif ( $filter_expr->{terms}[$i]->{op} eq '!~' ) { } elsif ( $term->{op} eq '!~' ) {
$self->{Sql} .= " not in (".join( ",", @value_list ).")"; $self->{Sql} .= " not in (".join( ",", @value_list ).")";
} else { } else {
$self->{Sql} .= " ".$filter_expr->{terms}[$i]->{op}." $value"; $self->{Sql} .= " ".$term->{op}." $value";
} }
} # end if has an operator } # end if has an operator
if ( exists($filter_expr->{terms}[$i]->{cbr}) ) { if ( exists($term->{cbr}) ) {
$self->{Sql} .= " ".str_repeat( ")", $filter_expr->{terms}[$i]->{cbr} )." "; $self->{Sql} .= " ".str_repeat( ")", $term->{cbr} )." ";
} }
} # end foreach term } # end foreach term
} # end if terms } # end if terms
@ -308,22 +306,22 @@ sub Sql {
} }
my @auto_terms; my @auto_terms;
if ( $self->{AutoArchive} ) { if ( $self->{AutoArchive} ) {
push( @auto_terms, "E.Archived = 0" ) push @auto_terms, "E.Archived = 0";
} }
if ( $self->{AutoVideo} ) { if ( $self->{AutoVideo} ) {
push( @auto_terms, "E.Videoed = 0" ) push @auto_terms, "E.Videoed = 0";
} }
if ( $self->{AutoUpload} ) { if ( $self->{AutoUpload} ) {
push( @auto_terms, "E.Uploaded = 0" ) push @auto_terms, "E.Uploaded = 0";
} }
if ( $self->{AutoEmail} ) { if ( $self->{AutoEmail} ) {
push( @auto_terms, "E.Emailed = 0" ) push @auto_terms, "E.Emailed = 0";
} }
if ( $self->{AutoMessage} ) { if ( $self->{AutoMessage} ) {
push( @auto_terms, "E.Messaged = 0" ) push @auto_terms, "E.Messaged = 0";
} }
if ( $self->{AutoExecute} ) { if ( $self->{AutoExecute} ) {
push( @auto_terms, "E.Executed = 0" ) push @auto_terms, "E.Executed = 0";
} }
if ( @auto_terms ) { if ( @auto_terms ) {
$sql .= " and ( ".join( " or ", @auto_terms )." )"; $sql .= " and ( ".join( " or ", @auto_terms )." )";

View File

@ -1221,6 +1221,9 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&amp;' ) {
case 'MonitorName': case 'MonitorName':
$filter['sql'] .= 'M.'.preg_replace( '/^Monitor/', '', $filter['terms'][$i]['attr'] ); $filter['sql'] .= 'M.'.preg_replace( '/^Monitor/', '', $filter['terms'][$i]['attr'] );
break; break;
case 'ServerId':
$filter['sql'] .= 'M.ServerId';
break;
case 'DateTime': case 'DateTime':
$filter['sql'] .= "E.StartTime"; $filter['sql'] .= "E.StartTime";
break; break;
@ -1266,6 +1269,13 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&amp;' ) {
case 'Notes': case 'Notes':
$value = dbEscape($value); $value = dbEscape($value);
break; break;
case 'ServerId':
if ( $value == 'ZM_SERVER_ID' ) {
$value = ZM_SERVER_ID;
} else {
$value = dbEscape($value);
}
break;
case 'DateTime': case 'DateTime':
$value = "'".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."'"; $value = "'".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."'";
break; break;

View File

@ -125,8 +125,7 @@ $SLANG = array(
'AttrMaxScore' => 'Max. Score', 'AttrMaxScore' => 'Max. Score',
'AttrMonitorId' => 'Monitor Id', 'AttrMonitorId' => 'Monitor Id',
'AttrMonitorName' => 'Monitor Name', 'AttrMonitorName' => 'Monitor Name',
'AttrServerId' => 'Server Id', 'AttrServer' => 'Server',
'AttrServerName' => 'Server Name',
'AttrName' => 'Name', 'AttrName' => 'Name',
'AttrNotes' => 'Notes', 'AttrNotes' => 'Notes',
'AttrSystemLoad' => 'System Load', 'AttrSystemLoad' => 'System Load',

View File

@ -97,8 +97,7 @@ $attrTypes = array(
'DiskPercent' => translate('AttrDiskPercent'), 'DiskPercent' => translate('AttrDiskPercent'),
'DiskBlocks' => translate('AttrDiskBlocks'), 'DiskBlocks' => translate('AttrDiskBlocks'),
'SystemLoad' => translate('AttrSystemLoad'), 'SystemLoad' => translate('AttrSystemLoad'),
'ServerId' => translate('AttrServerId'), 'ServerId' => translate('AttrServer'),
'ServerName' => translate('AttrServerName'),
); );
$opTypes = array( $opTypes = array(
'=' => translate('OpEq'), '=' => translate('OpEq'),
@ -239,6 +238,18 @@ for ( $i = 0; isset($_REQUEST['filter']) && $i < count($_REQUEST['filter']['term
?> ?>
<td><?php echo buildSelect( "filter[terms][$i][op]", $opTypes ); ?></td> <td><?php echo buildSelect( "filter[terms][$i][op]", $opTypes ); ?></td>
<td><?php echo buildSelect( "filter[terms][$i][val]", $monitors ); ?></td> <td><?php echo buildSelect( "filter[terms][$i][val]", $monitors ); ?></td>
<?php
}
elseif ( $_REQUEST['filter']['terms'][$i]['attr'] == "ServerId" )
{
$servers = array();
$servers['ZM_SERVER_ID'] = 'Current Server';
foreach ( dbFetchAll( "SELECT Id,Name FROM Servers ORDER BY lower(Name) ASC" ) as $server ) {
$servers[$server['Id']] = $server['Name'];
}
?>
<td><?php echo buildSelect( "filter[terms][$i][op]", $opTypes ); ?></td>
<td><?php echo buildSelect( "filter[terms][$i][val]", $servers ); ?></td>
<?php <?php
} }
else else