Skip to content

Commit

Permalink
Merge pull request #626 from Icinga/history-event-type-order
Browse files Browse the repository at this point in the history
Schema: change sort order of history event type enum
  • Loading branch information
julianbrost authored Aug 7, 2023
2 parents cc0d1dd + 6068ab7 commit 1ecea76
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
10 changes: 9 additions & 1 deletion schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,15 @@ CREATE TABLE history (
flapping_history_id binary(20) DEFAULT NULL COMMENT 'flapping_history.id',
acknowledgement_history_id binary(20) DEFAULT NULL COMMENT 'acknowledgement_history.id',

event_type enum('notification','state_change','downtime_start', 'downtime_end','comment_add','comment_remove','flapping_start','flapping_end','ack_set','ack_clear') NOT NULL,
-- The enum values are ordered in a way that event_type provides a meaningful sort order for history entries with
-- the same event_time. state_change comes first as it can cause many of the other events like trigger downtimes,
-- remove acknowledgements and send notifications. Similarly, notification comes last as any other event can result
-- in a notification. End events sort before the corresponding start events as any ack/comment/downtime/flapping
-- period should last for more than a millisecond, therefore, the old period ends first and then the new one starts.
-- The remaining types are sorted by impact and cause: comments are informative, flapping is automatic and changes
-- mechanics, downtimes are semi-automatic, require user action (or configuration) and change mechanics, acks are pure
-- user actions and change mechanics.
event_type enum('state_change', 'ack_clear', 'downtime_end', 'flapping_end', 'comment_remove', 'comment_add', 'flapping_start', 'downtime_start', 'ack_set', 'notification') NOT NULL,
event_time bigint unsigned NOT NULL,

PRIMARY KEY (id),
Expand Down
14 changes: 14 additions & 0 deletions schema/mysql/upgrades/1.2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ ALTER TABLE servicegroup
ADD INDEX idx_servicegroup_name_ci (name_ci) COMMENT 'Servicegroup list filtered using quick search',
DROP INDEX idx_servicegroup_name,
ADD INDEX idx_servicegroup_name (name) COMMENT 'Host/service/service group list filtered by service group name; Servicegroup detail filter';

-- The following sequence of statements changes the type of history.event_type like the following statement would:
--
-- ALTER TABLE history MODIFY event_type enum('state_change', 'ack_clear', 'downtime_end', 'flapping_end', 'comment_remove', 'comment_add', 'flapping_start', 'downtime_start', 'ack_set', 'notification') NOT NULL;
--
-- It's just much faster to add a second column, copy the column using an UPDATE statement and then replace the
-- old column with the one just generated. Table locking ensures that no other connection inserts data in the meantime.
LOCK TABLES history WRITE;
ALTER TABLE history ADD COLUMN event_type_new enum('state_change', 'ack_clear', 'downtime_end', 'flapping_end', 'comment_remove', 'comment_add', 'flapping_start', 'downtime_start', 'ack_set', 'notification') NOT NULL AFTER event_type;
UPDATE history SET event_type_new = event_type;
ALTER TABLE history
DROP COLUMN event_type,
CHANGE COLUMN event_type_new event_type enum('state_change', 'ack_clear', 'downtime_end', 'flapping_end', 'comment_remove', 'comment_add', 'flapping_start', 'downtime_start', 'ack_set', 'notification') NOT NULL;
UNLOCK TABLES;
13 changes: 11 additions & 2 deletions schema/pgsql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ CREATE TYPE state_type AS ENUM ( 'hard', 'soft' );
CREATE TYPE checkable_type AS ENUM ( 'host', 'service' );
CREATE TYPE comment_type AS ENUM ( 'comment', 'ack' );
CREATE TYPE notification_type AS ENUM ( 'downtime_start', 'downtime_end', 'downtime_removed', 'custom', 'acknowledgement', 'problem', 'recovery', 'flapping_start', 'flapping_end' );
CREATE TYPE history_type AS ENUM ( 'notification', 'state_change', 'downtime_start', 'downtime_end', 'comment_add', 'comment_remove', 'flapping_start', 'flapping_end', 'ack_set', 'ack_clear' );

-- The enum values are ordered in a way that event_type provides a meaningful sort order for history entries with
-- the same event_time. state_change comes first as it can cause many of the other events like trigger downtimes,
-- remove acknowledgements and send notifications. Similarly, notification comes last as any other event can result
-- in a notification. End events sort before the corresponding start events as any ack/comment/downtime/flapping
-- period should last for more than a millisecond, therefore, the old period ends first and then the new one starts.
-- The remaining types are sorted by impact and cause: comments are informative, flapping is automatic and changes
-- mechanics, downtimes are semi-automatic, require user action (or configuration) and change mechanics, acks are pure
-- user actions and change mechanics.
CREATE TYPE history_type AS ENUM ( 'state_change', 'ack_clear', 'downtime_end', 'flapping_end', 'comment_remove', 'comment_add', 'flapping_start', 'downtime_start', 'ack_set', 'notification' );

CREATE OR REPLACE FUNCTION get_sla_ok_percent(
in_host_id bytea20,
Expand Down Expand Up @@ -2040,7 +2049,7 @@ CREATE TABLE history (
flapping_history_id bytea20 DEFAULT NULL,
acknowledgement_history_id bytea20 DEFAULT NULL,

event_type history_type NOT NULL DEFAULT 'notification',
event_type history_type NOT NULL DEFAULT 'state_change',
event_time biguint NOT NULL,

CONSTRAINT pk_history PRIMARY KEY (id),
Expand Down
8 changes: 8 additions & 0 deletions schema/pgsql/upgrades/1.2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ CREATE INDEX idx_servicegroup_name_ci ON servicegroup(name_ci);
COMMENT ON INDEX idx_servicegroup_display_name IS 'Servicegroup list filtered/ordered by display_name';
COMMENT ON INDEX idx_servicegroup_name_ci IS 'Servicegroup list filtered using quick search';
COMMENT ON INDEX idx_servicegroup_name IS 'Host/service/service group list filtered by service group name; Servicegroup detail filter';

ALTER TYPE history_type RENAME TO history_type_old;
CREATE TYPE history_type AS ENUM ( 'state_change', 'ack_clear', 'downtime_end', 'flapping_end', 'comment_remove', 'comment_add', 'flapping_start', 'downtime_start', 'ack_set', 'notification' );
ALTER TABLE history
ALTER COLUMN event_type DROP DEFAULT,
ALTER COLUMN event_type TYPE history_type USING event_type::text::history_type,
ALTER COLUMN event_type SET DEFAULT 'state_change'::history_type;
DROP TYPE history_type_old;

0 comments on commit 1ecea76

Please sign in to comment.