Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit b51e9e6

Browse files
author
Hannu Varjoranta
committed
fix division by zero errors
1 parent bcb5c55 commit b51e9e6

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

postgresql_metrics/postgres_queries.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def get_metric_diff(db_name, metric_name, current_time, current_value):
5757
if derive_dict_key in DERIVE_DICT:
5858
last_time, last_value = DERIVE_DICT[derive_dict_key]
5959
seconds_since_last_check = (current_time - last_time).seconds
60-
diff = float(current_value - last_value) / seconds_since_last_check
60+
if seconds_since_last_check == 0:
61+
diff = 0
62+
else:
63+
diff = float(current_value - last_value) / seconds_since_last_check
6164
DERIVE_DICT[derive_dict_key] = (current_time, current_value)
6265
return diff
6366

@@ -156,7 +159,10 @@ def get_heap_hit_statistics(conn):
156159
recent_heap_hit = get_metric_diff(db_name, 'heap_hit', time_now, heap_hit_now)
157160
recent_heap_hit_ratio = None
158161
if recent_heap_read is not None:
159-
recent_heap_hit_ratio = recent_heap_hit / float(recent_heap_hit + recent_heap_read)
162+
if recent_heap_hit == 0:
163+
recent_heap_hit_ratio = 0
164+
else:
165+
recent_heap_hit_ratio = recent_heap_hit / float(recent_heap_hit + recent_heap_read)
160166
return db_name, recent_heap_read, recent_heap_hit, recent_heap_hit_ratio
161167

162168

@@ -217,8 +223,11 @@ def get_index_hit_rates(conn):
217223
index_hit_rates = []
218224
LOG.debug(results)
219225
for db_name, table_name, index_hit, index_miss in results:
220-
if index_hit and index_miss:
221-
recent_ratio = index_hit / float(index_miss + index_hit)
226+
if index_hit is not None and index_miss is not None:
227+
if index_hit == 0:
228+
recent_ratio = 0
229+
else:
230+
recent_ratio = index_hit / float(index_miss + index_hit)
222231
index_hit_rates.append((db_name, table_name, recent_ratio))
223232
else:
224233
index_hit_rates.append((db_name, table_name, None))

0 commit comments

Comments
 (0)