From e3f9693860db920f6474b53bb2e0d7d4648e3aed Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 25 Jan 2023 15:14:49 +0100 Subject: [PATCH] schema: Fix sla report inconsistencies --- schema/mysql/schema.sql | 8 ++++++-- schema/pgsql/schema.sql | 10 ++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index f13fe4cef..6bb33c634 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -136,7 +136,7 @@ BEGIN LEAVE read_loop; END IF; - IF row_previous_hard_state = 99 THEN + IF last_hard_state = 99 OR row_previous_hard_state = 99 THEN SET total_time = total_time - (row_event_time - last_event_time); ELSEIF ((in_service_id IS NULL AND last_hard_state > 0) OR (in_service_id IS NOT NULL AND last_hard_state > 1)) AND last_hard_state != 99 @@ -156,7 +156,11 @@ BEGIN END LOOP; CLOSE cur; - SET result = 100 * (total_time - problem_time) / total_time; + -- prevents division by zero crashes + IF total_time > 0 THEN + SET result = 100 * (total_time - problem_time) / total_time; + END IF; -- else no data available to be reported + RETURN result; END// DELIMITER ; diff --git a/schema/pgsql/schema.sql b/schema/pgsql/schema.sql index b3b5be0ca..f6f4bec12 100644 --- a/schema/pgsql/schema.sql +++ b/schema/pgsql/schema.sql @@ -39,6 +39,7 @@ DECLARE problem_time biguint := 0; total_time biguint; row record; + result decimal(7, 4); BEGIN IF in_end_time <= in_start_time THEN RAISE 'end time must be greater than start time'; @@ -138,7 +139,7 @@ BEGIN ) ORDER BY event_time, event_prio LOOP - IF row.previous_hard_state = 99 THEN + IF last_hard_state = 99 OR row.previous_hard_state = 99 THEN total_time := total_time - (row.event_time - last_event_time); ELSEIF ((in_service_id IS NULL AND last_hard_state > 0) OR (in_service_id IS NOT NULL AND last_hard_state > 1)) AND last_hard_state != 99 @@ -157,7 +158,12 @@ BEGIN END IF; END LOOP; - RETURN 100 * (total_time - problem_time) / total_time; + -- prevents division by zero crashes + IF total_time > 0 THEN + result := 100 * (total_time - problem_time) / total_time; + END IF; -- else no data available to be reported + + RETURN result; END; $$;