Merge branch 'zma_to_thread' of github.com:connortechnology/ZoneMinder into zma_to_thread

This commit is contained in:
Isaac Connor 2018-01-16 17:53:48 -05:00
commit d87e691363
11 changed files with 335 additions and 15 deletions

View File

@ -209,6 +209,7 @@ CREATE TABLE `Events` (
`StateId` int(10) unsigned NOT NULL,
`Orientation` enum('0','90','180','270','hori','vert') NOT NULL default '0',
`DiskSpace` bigint unsigned default NULL,
`Scheme` enum('Deep','Medium','Shallow') NOT NULL default 'Medium',
PRIMARY KEY (`Id`),
KEY `Events_MonitorId_idx` (`MonitorId`),
KEY `Events_StorageId_idx` (`StorageId`),
@ -267,6 +268,110 @@ CREATE TABLE `Events_Archived` (
KEY `Events_Month_MonitorId_idx` (`MonitorId`)
) ENGINE=@ZM_MYSQL_ENGINE@;
drop trigger if exists event_update_trigger;
delimiter //
CREATE TRIGGER event_update_trigger AFTER UPDATE ON Events
FOR EACH ROW
BEGIN
declare diff BIGINT default 0;
set diff = NEW.DiskSpace - OLD.DiskSpace;
IF ( NEW.StorageId = OLD.StorageID ) THEN
IF ( diff ) THEN
call update_storage_stats(OLD.StorageId, diff);
END IF;
ELSE
IF ( NEW.DiskSpace ) THEN
call update_storage_stats(NEW.StorageId, NEW.DiskSpace);
END IF;
IF ( OLD.DiskSpace ) THEN
call update_storage_stats(OLD.StorageId, -OLD.DiskSpace);
END IF;
END IF;
UPDATE Events_Hour SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
UPDATE Events_Day SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
UPDATE Events_Week SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
UPDATE Events_Month SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
IF ( NEW.Archived != OLD.Archived ) THEN
IF ( NEW.Archived ) THEN
INSERT INTO Events_Archived (EventId,MonitorId,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.DiskSpace);
UPDATE Monitors SET ArchivedEvents = ArchivedEvents+1, ArchivedEventDiskSpace = ArchivedEventDiskSpace + NEW.DiskSpace WHERE Id=NEW.MonitorId;
ELSEIF ( OLD.Archived ) THEN
DELETE FROM Events_Archived WHERE EventId=OLD.Id;
UPDATE Monitors SET ArchivedEvents =ArchivedEvents-1, ArchivedEventDiskSpace = ArchivedEventDiskSpace - OLD.DiskSpace WHERE Id=OLD.MonitorId;
ELSE
IF ( OLD.DiskSpace != NEW.DiskSpace ) THEN
UPDATE Events_Archived SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
UPDATE Monitors SET
ArchivedEventDiskSpace = ArchivedEventDiskSpace - OLD.DiskSpace + NEW.DiskSpace
WHERE Id=OLD.MonitorId;
END IF;
END IF;
END IF;
IF ( OLD.DiskSpace != NEW.DiskSpace ) THEN
UPDATE Monitors SET TotalEventDiskSpace = TotalEventDiskSpace - OLD.DiskSpace + NEW.DiskSpace WHERE Id=OLD.MonitorId;
END IF;
END;
//
delimiter ;
DROP TRIGGER IF EXISTS event_insert_trigger;
delimiter //
create trigger event_insert_trigger after insert on Events
for each row
begin
INSERT INTO Events_Hour (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0);
INSERT INTO Events_Day (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0);
INSERT INTO Events_Week (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0);
INSERT INTO Events_Month (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0);
end;
//
delimiter ;
drop trigger if exists event_delete_trigger;
delimiter //
CREATE TRIGGER event_delete_trigger BEFORE DELETe on Events
FOR EACH ROW
BEGIN
call update_storage_stats(OLD.StorageId, -OLD.DiskSpace);
DELETE FROM Events_Hour WHERE EventId=OLD.Id;
DELETE FROM Events_Day WHERE EventId=OLD.Id;
DELETE FROM Events_Week WHERE EventId=OLD.Id;
DELETE FROM Events_Month WHERE EventId=OLD.Id;
IF ( OLD.Archived ) THEN
DELETE FROM Events_Archived WHERE EventId=OLD.Id;
UPDATE Monitors SET
ArchivedEvents = ArchivedEvents - 1,
ArchivedEventDiskSpace = ArchivedEventDiskSpace - OLD.DiskSpace,
TotalEvents = TotalEvents - 1,
TotalEventDiskSpace = TotalEventDiskSpace - OLD.DiskSpace
WHERE Id=OLD.MonitorId;
ELSE
UPDATE Monitors SET
TotalEvents = TotalEvents-1,
TotalEventDiskSpace=TotalEventDiskSpace-OLD.DiskSpace
WHERE Id=OLD.MonitorId;
END IF;
END;
//
delimiter ;
--
-- Table structure for table `Filters`
--
@ -686,6 +791,24 @@ CREATE TABLE `Zones` (
KEY `MonitorId` (`MonitorId`)
) ENGINE=@ZM_MYSQL_ENGINE@;
DELIMITER //
DROP TRIGGER IF EXISTS Zone_Insert_Trigger//
CREATE TRIGGER Zone_Insert_Trigger AFTER INSERT ON Zones
FOR EACH ROW
BEGIN
UPDATE Monitors SET ZoneCount=(SELECT COUNT(*) FROM Zones WHERE MonitorId=NEW.MonitorId) WHERE Id=NEW.MonitorID;
END
//
DROP TRIGGER IF EXISTS Zone_Delete_Trigger//
CREATE TRIGGER Zone_Delete_Trigger AFTER DELETE ON Zones
FOR EACH ROW
BEGIN
UPDATE Monitors SET ZoneCount=(SELECT COUNT(*) FROM Zones WHERE MonitorId=OLD.MonitorId) WHERE Id=OLD.MonitorID;
END
//
DELIMITER ;
DROP TABLE IF EXISTS `Storage`;
CREATE TABLE `Storage` (
`Id` smallint(5) unsigned NOT NULL auto_increment,

150
db/zm_update-1.31.21.sql Normal file
View File

@ -0,0 +1,150 @@
delimiter //
DROP TRIGGER IF EXISTS Zone_Insert_Trigger//
CREATE TRIGGER Zone_Insert_Trigger AFTER INSERT ON Zones
FOR EACH ROW
BEGIN
UPDATE Monitors SET ZoneCount=(SELECT COUNT(*) FROM Zones WHERE MonitorId=NEW.MonitorId) WHERE Id=NEW.MonitorID;
END
//
DROP TRIGGER IF EXISTS Zone_Delete_Trigger//
CREATE TRIGGER Zone_Delete_Trigger AFTER DELETE ON Zones
FOR EACH ROW
BEGIN
UPDATE Monitors SET ZoneCount=(SELECT COUNT(*) FROM Zones WHERE MonitorId=OLD.MonitorId) WHERE Id=OLD.MonitorID;
END
//
UPDATE Monitors SET ZoneCount=(SELECT COUNT(Id) FROM Zones WHERE MonitorId=Monitors.Id)//
drop trigger if exists event_update_trigger//
CREATE TRIGGER event_update_trigger AFTER UPDATE ON Events
FOR EACH ROW
BEGIN
declare diff BIGINT default 0;
set diff = NEW.DiskSpace - OLD.DiskSpace;
IF ( NEW.StorageId = OLD.StorageID ) THEN
IF ( diff ) THEN
call update_storage_stats(OLD.StorageId, diff);
END IF;
ELSE
IF ( NEW.DiskSpace ) THEN
call update_storage_stats(NEW.StorageId, NEW.DiskSpace);
END IF;
IF ( OLD.DiskSpace ) THEN
call update_storage_stats(OLD.StorageId, -OLD.DiskSpace);
END IF;
END IF;
UPDATE Events_Hour SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
UPDATE Events_Day SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
UPDATE Events_Week SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
UPDATE Events_Month SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
IF ( NEW.Archived != OLD.Archived ) THEN
IF ( NEW.Archived ) THEN
INSERT INTO Events_Archived (EventId,MonitorId,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.DiskSpace);
UPDATE Monitors SET ArchivedEvents = ArchivedEvents+1, ArchivedEventDiskSpace = ArchivedEventDiskSpace + NEW.DiskSpace WHERE Id=NEW.MonitorId;
ELSEIF ( OLD.Archived ) THEN
DELETE FROM Events_Archived WHERE EventId=OLD.Id;
UPDATE Monitors SET ArchivedEvents =ArchivedEvents-1, ArchivedEventDiskSpace = ArchivedEventDiskSpace - OLD.DiskSpace WHERE Id=OLD.MonitorId;
ELSE
IF ( OLD.DiskSpace != NEW.DiskSpace ) THEN
UPDATE Events_Archived SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
UPDATE Monitors SET
ArchivedEventDiskSpace = ArchivedEventDiskSpace - OLD.DiskSpace + NEW.DiskSpace
WHERE Id=OLD.MonitorId;
END IF;
END IF;
END IF;
IF ( OLD.DiskSpace != NEW.DiskSpace ) THEN
UPDATE Monitors SET TotalEventDiskSpace = TotalEventDiskSpace - OLD.DiskSpace + NEW.DiskSpace WHERE Id=OLD.MonitorId;
END IF;
END;
//
delimiter ;
DROP TRIGGER IF EXISTS event_insert_trigger;
delimiter //
create trigger event_insert_trigger after insert on Events
for each row
begin
INSERT INTO Events_Hour (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0);
INSERT INTO Events_Day (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0);
INSERT INTO Events_Week (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0);
INSERT INTO Events_Month (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0);
end;
//
delimiter ;
drop trigger if exists event_delete_trigger;
delimiter //
CREATE TRIGGER event_delete_trigger BEFORE DELETe on Events
FOR EACH ROW
BEGIN
call update_storage_stats(OLD.StorageId, -OLD.DiskSpace);
DELETE FROM Events_Hour WHERE EventId=OLD.Id;
DELETE FROM Events_Day WHERE EventId=OLD.Id;
DELETE FROM Events_Week WHERE EventId=OLD.Id;
DELETE FROM Events_Month WHERE EventId=OLD.Id;
IF ( OLD.Archived ) THEN
DELETE FROM Events_Archived WHERE EventId=OLD.Id;
UPDATE Monitors SET
ArchivedEvents = ArchivedEvents - 1,
ArchivedEventDiskSpace = ArchivedEventDiskSpace - OLD.DiskSpace,
TotalEvents = TotalEvents - 1,
TotalEventDiskSpace = TotalEventDiskSpace - OLD.DiskSpace
WHERE Id=OLD.MonitorId;
ELSE
UPDATE Monitors SET
TotalEvents = TotalEvents-1,
TotalEventDiskSpace=TotalEventDiskSpace-OLD.DiskSpace
WHERE Id=OLD.MonitorId;
END IF;
END;
//
delimiter ;
UPDATE Monitors INNER JOIN (
SELECT MonitorId,
COUNT(Id) AS TotalEvents,
SUM(DiskSpace) AS TotalEventDiskSpace,
SUM(IF(Archived,1,0)) AS ArchivedEvents,
SUM(IF(Archived,DiskSpace,0)) AS ArchivedEventDiskSpace,
SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 hour),1,0)) AS HourEvents,
SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 hour),DiskSpace,0)) AS HourEventDiskSpace,
SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 day),1,0)) AS DayEvents,
SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 day),DiskSpace,0)) AS DayEventDiskSpace,
SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 week),1,0)) AS WeekEvents,
SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 week),DiskSpace,0)) AS WeekEventDiskSpace,
SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 month),1,0)) AS MonthEvents,
SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 month),DiskSpace,0)) AS MonthEventDiskSpace
FROM Events GROUP BY MonitorId
) AS E ON E.MonitorId=Monitors.Id SET
Monitors.TotalEvents = E.TotalEvents,
Monitors.TotalEventDiskSpace = E.TotalEventDiskSpace,
Monitors.ArchivedEvents = E.ArchivedEvents,
Monitors.ArchivedEventDiskSpace = E.ArchivedEventDiskSpace,
Monitors.HourEvents = E.HourEvents,
Monitors.HourEventDiskSpace = E.HourEventDiskSpace,
Monitors.DayEvents = E.DayEvents,
Monitors.DayEventDiskSpace = E.DayEventDiskSpace,
Monitors.WeekEvents = E.WeekEvents,
Monitors.WeekEventDiskSpace = E.WeekEventDiskSpace,
Monitors.MonthEvents = E.MonthEvents,
Monitors.MonthEventDiskSpace = E.MonthEventDiskSpace;

19
db/zm_update-1.31.22.sql Normal file
View File

@ -0,0 +1,19 @@
delimiter //
DROP TRIGGER IF EXISTS Zone_Insert_Trigger//
CREATE TRIGGER Zone_Insert_Trigger AFTER INSERT ON Zones
FOR EACH ROW
BEGIN
UPDATE Monitors SET ZoneCount=(SELECT COUNT(*) FROM Zones WHERE MonitorId=NEW.MonitorId) WHERE Id=NEW.MonitorID;
END
//
DROP TRIGGER IF EXISTS Zone_Delete_Trigger//
CREATE TRIGGER Zone_Delete_Trigger AFTER DELETE ON Zones
FOR EACH ROW
BEGIN
UPDATE Monitors SET ZoneCount=(SELECT COUNT(*) FROM Zones WHERE MonitorId=OLD.MonitorId) WHERE Id=OLD.MonitorID;
END
//
UPDATE Monitors SET ZoneCount=(SELECT COUNT(Id) FROM Zones WHERE MonitorId=Monitors.Id)//
UPDATE Storage SET DiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE StorageId=Storage.Id);

View File

@ -297,7 +297,7 @@ sub run {
while( 1 ) {
if ( $Config{ZM_SERVER_ID} ) {
if ( $secs_count % 60 ) {
if ( ! ( $secs_count % 60 ) ) {
$dbh = zmDbConnect() if ! $dbh->ping();
my @cpuload = Sys::CpuLoad::load();
dPrint( ZoneMinder::Logger::INFO, 'Updating Server record' );

View File

@ -191,16 +191,23 @@ if ( $command =~ /^(?:start|restart)$/ ) {
zmMemTidy();
runCommand('zmdc.pl startup');
my $Server = undef;
my $sql;
my @values;
if ( $Config{ZM_SERVER_ID} ) {
require ZoneMinder::Server;
Info("Multi-server configuration detected. Starting up services for server $Config{ZM_SERVER_ID}\n");
$Server = new ZoneMinder::Server( $Config{ZM_SERVER_ID} );
$sql = 'SELECT * FROM Monitors WHERE ServerId=?';
@values = ( $Config{ZM_SERVER_ID} );
} else {
Info('Single server configuration detected. Starting up services.');
$sql = 'SELECT * FROM Monitors';
}
my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'SELECT * FROM Monitors';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
my $res = $sth->execute( @values )
or Fatal( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() ) {
if ( $monitor->{Function} ne 'None' ) {
@ -223,10 +230,18 @@ if ( $command =~ /^(?:start|restart)$/ ) {
# This is now started unconditionally
runCommand('zmdc.pl start zmfilter.pl');
if ( $Config{ZM_RUN_AUDIT} ) {
runCommand('zmdc.pl start zmaudit.pl -c');
if ( $Server and exists $$Server{'zmaudit.pl'} and ! $$Server{'zmaudit.pl'} ) {
Debug("Not running zmaudit.pl because it is turned off for this server.");
} else {
runCommand('zmdc.pl start zmaudit.pl -c');
}
}
if ( $Config{ZM_OPT_TRIGGERS} ) {
runCommand('zmdc.pl start zmtrigger.pl');
if ( $Server and exists $$Server{'zmtrigger.pl'} and ! $$Server{'zmtrigger.pl'} ) {
Debug("Not running zmtrigger.pl because it is turned off for this server.");
} else {
runCommand('zmdc.pl start zmtrigger.pl');
}
}
if ( $Config{ZM_OPT_X10} ) {
runCommand('zmdc.pl start zmx10.pl -c start');
@ -238,6 +253,11 @@ if ( $command =~ /^(?:start|restart)$/ ) {
if ( $Config{ZM_TELEMETRY_DATA} ) {
runCommand('zmdc.pl start zmtelemetry.pl');
}
if ( $Server and exists $$Server{'zmstats.pl'} and ! $$Server{'zmstats.pl'} ) {
Debug("Not running zmstats.pl because it is turned off for this server.");
} else {
runCommand('zmdc.pl start zmstats.pl');
}
} else {
$retval = 1;
}

View File

@ -164,7 +164,7 @@ while( 1 ) {
$eventcounts_week_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() );
$eventcounts_month_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() );
sleep( $Config{ZM_STATS_CHECK_INTERVAL} );
sleep( $Config{ZM_STATS_UPDATE_INTERVAL} );
} # end while (1)
Info( "Stats Daemon exiting\n" );

View File

@ -411,9 +411,6 @@ void Event::AddFramesInternal( int n_frames, int start_frame, Image **images, st
WriteFrameImage( images[i], *(timestamps[i]), snapshot_file.c_str() );
}
}
//if ( videowriter != NULL ) {
//WriteFrameVideo( images[i], *(timestamps[i]), videowriter );
//}
struct DeltaTimeval delta_time;
DELTA_TIMEVAL( delta_time, *(timestamps[i]), start_time, DT_PREC_2 );

View File

@ -1588,8 +1588,20 @@ void Monitor::Reload() {
function = (Function)atoi(dbrow[index++]);
enabled = atoi(dbrow[index++]);
const char *p_linked_monitors = dbrow[index++];
strncpy( event_prefix, dbrow[index++], sizeof(event_prefix)-1 );
strncpy( label_format, dbrow[index++], sizeof(label_format)-1 );
if ( dbrow[index] ) {
strncpy( event_prefix, dbrow[index++], sizeof(event_prefix)-1 );
} else {
event_prefix[0] = 0;
index++;
}
if ( dbrow[index] ) {
strncpy( label_format, dbrow[index++], sizeof(label_format)-1 );
} else {
label_format[0] = 0;
index++;
}
label_coord = Coord( atoi(dbrow[index]), atoi(dbrow[index+1]) ); index += 2;
label_size = atoi(dbrow[index++]);
warmup_count = atoi(dbrow[index++]);

View File

@ -1 +1 @@
1.31.20
1.31.22

View File

@ -120,6 +120,7 @@ public static function find_all( $parameters = null, $options = null ) {
}
$used = $this->disk_used_space();
$usage = round( ($used / $total) * 100);
Warning("Used $usage = round( ( $used / $total ) * 100 )");
return $usage;
}
public function disk_total_space() {
@ -130,7 +131,7 @@ public static function find_all( $parameters = null, $options = null ) {
}
public function disk_used_space() {
# This isn't a function like this in php, so we have to add up the space used in each event.
if ( ! array_key_exists( 'DiskSpace', $this ) ) {
if ( (! array_key_exists( 'DiskSpace', $this )) or (!$this->{'DiskSpace'}) ) {
$used = 0;
if ( $this->{'Type'} == 's3fs' ) {
$used = dbFetchOne('SELECT SUM(DiskSpace) AS DiskSpace FROM Events WHERE StorageId=? AND DiskSpace IS NOT NULL', 'DiskSpace', array($this->Id()) );

View File

@ -379,7 +379,6 @@ if ( !empty($_REQUEST['mid']) && canEdit( 'Monitors', $_REQUEST['mid'] ) ) {
dbQuery( 'UPDATE Zones SET '.implode( ', ', $changes ).' WHERE MonitorId=? AND Id=?', array( $mid, $zid) );
} else {
dbQuery( 'INSERT INTO Zones SET MonitorId=?, '.implode( ', ', $changes ), array( $mid ) );
dbQuery( 'UPDATE Monitors SET ZoneCount=(SELECT COUNT(Id) FROM Zones WHERE MonitorId=Monitors.Id) WHERE Id=?', array($mid));
}
//if ( $cookies ) session_write_close();
if ( daemonCheck() ) {
@ -432,7 +431,6 @@ if ( !empty($_REQUEST['mid']) && canEdit( 'Monitors', $_REQUEST['mid'] ) ) {
foreach( $_REQUEST['markZids'] as $markZid ) {
$zone = dbFetchOne( 'select * from Zones where Id=?', NULL, array($markZid) );
dbQuery( 'delete from Zones WHERE MonitorId=? AND Id=?', array( $mid, $markZid) );
dbQuery( 'UPDATE Monitors SET ZoneCount=(SELECT COUNT(Id) FROM Zones WHERE MonitorId=Monitors.Id) WHERE Id=?', array($mid));
$deletedZid = 1;
}
if ( $deletedZid ) {