Skip to content

Commit b997831

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

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

schema/mysql/schema.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,9 @@ 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))
142-
AND last_hard_state != 99
143142
AND active_downtimes = 0
144143
THEN
145144
SET problem_time = problem_time + row_event_time - last_event_time;
@@ -156,7 +155,11 @@ BEGIN
156155
END LOOP;
157156
CLOSE cur;
158157

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

schema/pgsql/schema.sql

Lines changed: 8 additions & 3 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,10 +139,9 @@ 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))
144-
AND last_hard_state != 99
145145
AND active_downtimes = 0
146146
THEN
147147
problem_time := problem_time + row.event_time - last_event_time;
@@ -157,7 +157,12 @@ BEGIN
157157
END IF;
158158
END LOOP;
159159

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

0 commit comments

Comments
 (0)