Skip to content

Commit e3f9693

Browse files
committed
schema: Fix sla report inconsistencies
1 parent 4ed4db3 commit e3f9693

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

schema/mysql/schema.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ BEGIN
136136
LEAVE read_loop;
137137
END IF;
138138

139-
IF row_previous_hard_state = 99 THEN
139+
IF last_hard_state = 99 OR row_previous_hard_state = 99 THEN
140140
SET total_time = total_time - (row_event_time - last_event_time);
141141
ELSEIF ((in_service_id IS NULL AND last_hard_state > 0) OR (in_service_id IS NOT NULL AND last_hard_state > 1))
142142
AND last_hard_state != 99
@@ -156,7 +156,11 @@ BEGIN
156156
END LOOP;
157157
CLOSE cur;
158158

159-
SET result = 100 * (total_time - problem_time) / total_time;
159+
-- prevents division by zero crashes
160+
IF total_time > 0 THEN
161+
SET result = 100 * (total_time - problem_time) / total_time;
162+
END IF; -- else no data available to be reported
163+
160164
RETURN result;
161165
END//
162166
DELIMITER ;

schema/pgsql/schema.sql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ DECLARE
3939
problem_time biguint := 0;
4040
total_time biguint;
4141
row record;
42+
result decimal(7, 4);
4243
BEGIN
4344
IF in_end_time <= in_start_time THEN
4445
RAISE 'end time must be greater than start time';
@@ -138,7 +139,7 @@ BEGIN
138139
)
139140
ORDER BY event_time, event_prio
140141
LOOP
141-
IF row.previous_hard_state = 99 THEN
142+
IF last_hard_state = 99 OR row.previous_hard_state = 99 THEN
142143
total_time := total_time - (row.event_time - last_event_time);
143144
ELSEIF ((in_service_id IS NULL AND last_hard_state > 0) OR (in_service_id IS NOT NULL AND last_hard_state > 1))
144145
AND last_hard_state != 99
@@ -157,7 +158,12 @@ BEGIN
157158
END IF;
158159
END LOOP;
159160

160-
RETURN 100 * (total_time - problem_time) / total_time;
161+
-- prevents division by zero crashes
162+
IF total_time > 0 THEN
163+
result := 100 * (total_time - problem_time) / total_time;
164+
END IF; -- else no data available to be reported
165+
166+
RETURN result;
161167
END;
162168
$$;
163169

0 commit comments

Comments
 (0)