2018-02-19 00:01:33 +08:00
|
|
|
|
|
|
|
delimiter //
|
|
|
|
DROP TRIGGER IF EXISTS Events_Hour_delete_trigger//
|
|
|
|
CREATE TRIGGER Events_Hour_delete_trigger BEFORE DELETE ON Events_Hour
|
|
|
|
FOR EACH ROW BEGIN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET
|
2019-05-01 05:29:18 +08:00
|
|
|
HourEvents = GREATEST(COALESCE(HourEvents,1)-1,0),
|
2019-04-24 22:38:08 +08:00
|
|
|
HourEventDiskSpace=GREATEST(COALESCE(HourEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS Events_Hour_update_trigger//
|
|
|
|
|
|
|
|
CREATE TRIGGER Events_Hour_update_trigger AFTER UPDATE ON Events_Hour
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
declare diff BIGINT default 0;
|
|
|
|
|
|
|
|
set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0);
|
|
|
|
IF ( diff ) THEN
|
|
|
|
IF ( NEW.MonitorID != OLD.MonitorID ) THEN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET HourEventDiskSpace=GREATEST(COALESCE(HourEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0),0) WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
|
|
|
UPDATE Event_Summaries SET HourEventDiskSpace=COALESCE(HourEventDiskSpace,0)+COALESCE(NEW.DiskSpace,0) WHERE Event_Summaries.MonitorId=NEW.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
ELSE
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET HourEventDiskSpace=COALESCE(HourEventDiskSpace,0)+diff WHERE Event_Summaries.MonitorId=NEW.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS Events_Day_delete_trigger//
|
|
|
|
CREATE TRIGGER Events_Day_delete_trigger BEFORE DELETE ON Events_Day
|
|
|
|
FOR EACH ROW BEGIN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET
|
2019-04-24 22:38:08 +08:00
|
|
|
DayEvents = GREATEST(COALESCE(DayEvents,1)-1,0),
|
|
|
|
DayEventDiskSpace=GREATEST(COALESCE(DayEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS Events_Day_update_trigger;
|
|
|
|
CREATE TRIGGER Events_Day_update_trigger AFTER UPDATE ON Events_Day
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
declare diff BIGINT default 0;
|
|
|
|
|
|
|
|
set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0);
|
|
|
|
IF ( diff ) THEN
|
|
|
|
IF ( NEW.MonitorID != OLD.MonitorID ) THEN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET DayEventDiskSpace=GREATEST(COALESCE(DayEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0),0) WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
|
|
|
UPDATE Event_Summaries SET DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)+COALESCE(NEW.DiskSpace,0) WHERE Event_Summaries.MonitorId=NEW.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
ELSE
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET DayEventDiskSpace=GREATEST(COALESCE(DayEventDiskSpace,0)+diff,0) WHERE Event_Summaries.MonitorId=NEW.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS Events_Week_delete_trigger//
|
|
|
|
CREATE TRIGGER Events_Week_delete_trigger BEFORE DELETE ON Events_Week
|
|
|
|
FOR EACH ROW BEGIN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET
|
2019-05-01 05:29:18 +08:00
|
|
|
WeekEvents = GREATEST(COALESCE(WeekEvents,1)-1,0),
|
2019-04-24 22:38:08 +08:00
|
|
|
WeekEventDiskSpace=GREATEST(COALESCE(WeekEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS Events_Week_update_trigger;
|
|
|
|
CREATE TRIGGER Events_Week_update_trigger AFTER UPDATE ON Events_Week
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
declare diff BIGINT default 0;
|
|
|
|
|
|
|
|
set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0);
|
|
|
|
IF ( diff ) THEN
|
|
|
|
IF ( NEW.MonitorID != OLD.MonitorID ) THEN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET WeekEventDiskSpace=GREATEST(COALESCE(WeekEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0),0) WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
|
|
|
UPDATE Event_Summaries SET WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)+COALESCE(NEW.DiskSpace,0) WHERE Event_Summaries.MonitorId=NEW.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
ELSE
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET WeekEventDiskSpace=GREATEST(COALESCE(WeekEventDiskSpace,0)+diff,0) WHERE Event_Summaries.MonitorId=NEW.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS Events_Month_delete_trigger//
|
|
|
|
CREATE TRIGGER Events_Month_delete_trigger BEFORE DELETE ON Events_Month
|
|
|
|
FOR EACH ROW BEGIN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET
|
2019-05-01 05:29:18 +08:00
|
|
|
MonthEvents = GREATEST(COALESCE(MonthEvents,1)-1,0),
|
2019-04-24 22:38:08 +08:00
|
|
|
MonthEventDiskSpace=GREATEST(COALESCE(MonthEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS Events_Month_update_trigger;
|
|
|
|
CREATE TRIGGER Events_Month_update_trigger AFTER UPDATE ON Events_Month
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
declare diff BIGINT default 0;
|
|
|
|
|
|
|
|
set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0);
|
|
|
|
IF ( diff ) THEN
|
|
|
|
IF ( NEW.MonitorID != OLD.MonitorID ) THEN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET MonthEventDiskSpace=GREATEST(COALESCE(MonthEventDiskSpace,0)-COALESCE(OLD.DiskSpace),0) WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
|
|
|
UPDATE Event_Summaries SET MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)+COALESCE(NEW.DiskSpace) WHERE Event_Summaries.MonitorId=NEW.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
ELSE
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET MonthEventDiskSpace=GREATEST(COALESCE(MonthEventDiskSpace,0)+diff,0) WHERE Event_Summaries.MonitorId=NEW.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
2018-04-20 20:54:25 +08:00
|
|
|
drop procedure if exists update_storage_stats//
|
2018-02-19 00:01:33 +08:00
|
|
|
|
|
|
|
drop trigger if exists event_update_trigger//
|
|
|
|
|
|
|
|
CREATE TRIGGER event_update_trigger AFTER UPDATE ON Events
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
2018-04-20 20:54:25 +08:00
|
|
|
declare diff BIGINT default 0;
|
2018-02-19 00:01:33 +08:00
|
|
|
|
2018-04-20 20:54:25 +08:00
|
|
|
set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0);
|
2018-02-19 00:01:33 +08:00
|
|
|
IF ( NEW.StorageId = OLD.StorageID ) THEN
|
|
|
|
IF ( diff ) THEN
|
2020-11-07 02:27:00 +08:00
|
|
|
UPDATE Storage SET DiskSpace = GREATEST(COALESCE(DiskSpace,0) + diff,0) WHERE Storage.Id = OLD.StorageId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
ELSE
|
|
|
|
IF ( NEW.DiskSpace ) THEN
|
2020-11-07 02:27:00 +08:00
|
|
|
UPDATE Storage SET DiskSpace = COALESCE(DiskSpace,0) + NEW.DiskSpace WHERE Storage.Id = NEW.StorageId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
IF ( OLD.DiskSpace ) THEN
|
2020-11-07 02:27:00 +08:00
|
|
|
UPDATE Storage SET DiskSpace = GREATEST(COALESCE(DiskSpace,0) - OLD.DiskSpace,0) WHERE Storage.Id = OLD.StorageId;
|
2018-02-19 00:01:33 +08:00
|
|
|
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);
|
2021-04-10 04:46:36 +08:00
|
|
|
INSERT INTO Event_Summaries (MonitorId,ArchivedEvents,ArchivedEventDiskSpace) VALUES (NEW.MonitorId,1,NEW.DiskSpace) ON DUPLICATE KEY
|
|
|
|
UPDATE ArchivedEvents = COALESCE(ArchivedEvents,0)+1, ArchivedEventDiskSpace = COALESCE(ArchivedEventDiskSpace,0) + COALESCE(NEW.DiskSpace,0);
|
2018-02-19 00:01:33 +08:00
|
|
|
ELSEIF ( OLD.Archived ) THEN
|
|
|
|
DELETE FROM Events_Archived WHERE EventId=OLD.Id;
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries
|
2019-04-24 22:38:08 +08:00
|
|
|
SET
|
|
|
|
ArchivedEvents = GREATEST(COALESCE(ArchivedEvents,0)-1,0),
|
|
|
|
ArchivedEventDiskSpace = GREATEST(COALESCE(ArchivedEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
ELSE
|
|
|
|
IF ( OLD.DiskSpace != NEW.DiskSpace ) THEN
|
|
|
|
UPDATE Events_Archived SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET
|
2019-04-24 22:38:08 +08:00
|
|
|
ArchivedEventDiskSpace = GREATEST(COALESCE(ArchivedEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0) + COALESCE(NEW.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
END IF;
|
2018-02-19 00:48:00 +08:00
|
|
|
ELSEIF ( NEW.Archived AND diff ) THEN
|
2018-02-19 00:01:33 +08:00
|
|
|
UPDATE Events_Archived SET DiskSpace=NEW.DiskSpace WHERE EventId=NEW.Id;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF ( diff ) THEN
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries
|
2019-04-24 22:38:08 +08:00
|
|
|
SET
|
|
|
|
TotalEventDiskSpace = GREATEST(COALESCE(TotalEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0) + COALESCE(NEW.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2019-04-24 22:38:08 +08:00
|
|
|
DROP TRIGGER IF EXISTS event_insert_trigger//
|
2018-02-19 00:01:33 +08:00
|
|
|
|
|
|
|
/* The assumption is that when an Event is inserted, it has no size yet, so don't bother updating the DiskSpace, just the count.
|
|
|
|
* The DiskSpace will get update in the Event Update Trigger
|
|
|
|
*/
|
|
|
|
CREATE TRIGGER event_insert_trigger AFTER INSERT ON Events
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
|
2020-11-05 02:49:39 +08:00
|
|
|
INSERT INTO Events_Hour (EventId,MonitorId,StartDateTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartDateTime,0);
|
|
|
|
INSERT INTO Events_Day (EventId,MonitorId,StartDateTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartDateTime,0);
|
|
|
|
INSERT INTO Events_Week (EventId,MonitorId,StartDateTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartDateTime,0);
|
|
|
|
INSERT INTO Events_Month (EventId,MonitorId,StartDateTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartDateTime,0);
|
2021-04-10 07:47:08 +08:00
|
|
|
INSERT INTO Event_Summaries (MonitorId,HourEvents,DayEvents,WeekEvents,MonthEvents,TotalEvents) VALUES (NEW.MonitorId,1,1,1,1,1) ON DUPLICATE KEY
|
2021-04-10 04:46:36 +08:00
|
|
|
UPDATE
|
2018-02-21 11:48:23 +08:00
|
|
|
HourEvents = COALESCE(HourEvents,0)+1,
|
2018-02-19 00:01:33 +08:00
|
|
|
DayEvents = COALESCE(DayEvents,0)+1,
|
2018-02-21 11:48:23 +08:00
|
|
|
WeekEvents = COALESCE(WeekEvents,0)+1,
|
|
|
|
MonthEvents = COALESCE(MonthEvents,0)+1,
|
2021-04-10 04:46:36 +08:00
|
|
|
TotalEvents = COALESCE(TotalEvents,0)+1;
|
2018-02-19 00:01:33 +08:00
|
|
|
END;
|
|
|
|
//
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS event_delete_trigger//
|
|
|
|
|
|
|
|
CREATE TRIGGER event_delete_trigger BEFORE DELETE ON Events
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
IF ( OLD.DiskSpace ) THEN
|
2020-11-07 02:27:00 +08:00
|
|
|
UPDATE Storage SET DiskSpace = GREATEST(COALESCE(DiskSpace,0) - COALESCE(OLD.DiskSpace,0),0) WHERE Storage.Id = OLD.StorageId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
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;
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET
|
2019-04-24 22:38:08 +08:00
|
|
|
ArchivedEvents = GREATEST(COALESCE(ArchivedEvents,1) - 1,0),
|
|
|
|
ArchivedEventDiskSpace = GREATEST(COALESCE(ArchivedEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0),0),
|
|
|
|
TotalEvents = GREATEST(COALESCE(TotalEvents,1) - 1,0),
|
|
|
|
TotalEventDiskSpace = GREATEST(COALESCE(TotalEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
ELSE
|
2021-04-09 22:16:16 +08:00
|
|
|
UPDATE Event_Summaries SET
|
2019-04-24 22:38:08 +08:00
|
|
|
TotalEvents = GREATEST(COALESCE(TotalEvents,1)-1,0),
|
|
|
|
TotalEventDiskSpace=GREATEST(COALESCE(TotalEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0),0)
|
2021-04-09 22:16:16 +08:00
|
|
|
WHERE Event_Summaries.MonitorId=OLD.MonitorId;
|
2018-02-19 00:01:33 +08:00
|
|
|
END IF;
|
|
|
|
END;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS Zone_Insert_Trigger//
|
|
|
|
CREATE TRIGGER Zone_Insert_Trigger AFTER INSERT ON Zones
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
2020-11-07 02:27:00 +08:00
|
|
|
UPDATE Monitors SET ZoneCount=(SELECT COUNT(*) FROM Zones WHERE MonitorId=NEW.MonitorId) WHERE Monitors.Id=NEW.MonitorID;
|
2018-02-19 00:01:33 +08:00
|
|
|
END
|
|
|
|
//
|
|
|
|
DROP TRIGGER IF EXISTS Zone_Delete_Trigger//
|
|
|
|
CREATE TRIGGER Zone_Delete_Trigger AFTER DELETE ON Zones
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
2020-11-07 02:27:00 +08:00
|
|
|
UPDATE Monitors SET ZoneCount=(SELECT COUNT(*) FROM Zones WHERE MonitorId=OLD.MonitorId) WHERE Monitors.Id=OLD.MonitorID;
|
2018-02-19 00:01:33 +08:00
|
|
|
END
|
|
|
|
//
|
|
|
|
|
|
|
|
DELIMITER ;
|