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

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

View File

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

View File

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

View File

@ -97,8 +97,7 @@ $attrTypes = array(
'DiskPercent' => translate('AttrDiskPercent'),
'DiskBlocks' => translate('AttrDiskBlocks'),
'SystemLoad' => translate('AttrSystemLoad'),
'ServerId' => translate('AttrServerId'),
'ServerName' => translate('AttrServerName'),
'ServerId' => translate('AttrServer'),
);
$opTypes = array(
'=' => 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][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
}
else