Skip to content

Commit

Permalink
schema: Fix sla report inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Jan 27, 2023
1 parent 4ed4db3 commit e62187c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 10 additions & 1 deletion schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ BEGIN
DECLARE active_downtimes int unsigned;
DECLARE problem_time bigint unsigned;
DECLARE total_time bigint unsigned;
DECLARE rowCounts int unsigned;
DECLARE done int;
DECLARE cur CURSOR FOR
(
Expand Down Expand Up @@ -69,6 +70,8 @@ BEGIN
AND ((in_service_id IS NULL AND s.service_id IS NULL) OR s.service_id = in_service_id)
AND s.event_time > in_start_time
AND s.event_time < in_end_time
AND s.hard_state IS NOT NULL
AND s.previous_hard_state IS NOT NULL
) UNION ALL (
-- end event to keep loop simple, values are not used
SELECT
Expand Down Expand Up @@ -130,6 +133,7 @@ BEGIN

SET done = 0;
OPEN cur;
SELECT FOUND_ROWS() INTO rowCounts;
read_loop: LOOP
FETCH cur INTO row_event_time, row_event_type, row_event_prio, row_hard_state, row_previous_hard_state;
IF done THEN
Expand All @@ -156,7 +160,12 @@ BEGIN
END LOOP;
CLOSE cur;

SET result = 100 * (total_time - problem_time) / total_time;
-- row count "1" because of the faked ending result used for the
-- cursor loop, whose result set is never used.
IF rowCounts > 1 THEN
SET result = 100 * (total_time - problem_time) / total_time;
END IF; -- else no data available to be reported

RETURN result;
END//
DELIMITER ;
Expand Down
13 changes: 12 additions & 1 deletion schema/pgsql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ DECLARE
active_downtimes uint := 0;
problem_time biguint := 0;
total_time biguint;
rowCounts uint := 0;
row record;
result decimal(7, 4);
BEGIN
IF in_end_time <= in_start_time THEN
RAISE 'end time must be greater than start time';
Expand Down Expand Up @@ -127,6 +129,8 @@ BEGIN
AND ((in_service_id IS NULL AND s.service_id IS NULL) OR s.service_id = in_service_id)
AND s.event_time > in_start_time
AND s.event_time < in_end_time
AND s.hard_state IS NOT NULL
AND s.previous_hard_state IS NOT NULL
) UNION ALL (
-- end event to keep loop simple, values are not used
SELECT
Expand All @@ -147,6 +151,7 @@ BEGIN
problem_time := problem_time + row.event_time - last_event_time;
END IF;

rowCounts := rowCounts + 1;
last_event_time := row.event_time;
IF row.event_type = 'state_change' THEN
last_hard_state := row.hard_state;
Expand All @@ -157,7 +162,13 @@ BEGIN
END IF;
END LOOP;

RETURN 100 * (total_time - problem_time) / total_time;
-- row count "1" because of the faked ending result used for the
-- cursor loop, whose result set is never used.
IF rowCounts > 1 THEN
result := 100 * (total_time - problem_time) / total_time;
END IF; -- else no data available to be reported

RETURN result;
END;
$$;

Expand Down

0 comments on commit e62187c

Please sign in to comment.