diff --git a/system_status/server.py b/system_status/server.py index 49a24c5..d5a8d06 100644 --- a/system_status/server.py +++ b/system_status/server.py @@ -31,6 +31,7 @@ def to_dict(self): class PrometheusData: instance: str job: str + hasData: bool is_up: bool values: List[TimestampAndValuePair] @@ -38,6 +39,7 @@ def to_dict(self): return { "instance": self.instance, "job": self.job, + "hasData": self.hasData, "is_up": self.is_up, "values": [v.to_dict() for v in self.values] } @@ -77,6 +79,11 @@ def get_args() -> argparse.Namespace: args = get_args() +def check_epoch_aggreement(epoch_time: int, assumed_epoch: int) -> bool: + #Ensures the epoch times from the data set are within the expected range, using the global scrape intervval for reference + aggreement = abs(epoch_time - assumed_epoch) < 5 + return aggreement + def get_prometheus_data() -> list[PrometheusData]: """Sends a PromQL query to Prometheus and returns the results.""" @@ -123,18 +130,50 @@ def get_prometheus_data() -> list[PrometheusData]: maybe_job = service_dict.get("metric", {}).get("job", "NO JOB AVAILABLE") maybe_values = service_dict.get("values", []) + timestamps_and_values = [] - for epoch_time, value in maybe_values: - timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(epoch_time)) - timestamps_and_values.append(TimestampAndValuePair(timestamp, value)) - + + if len(maybe_values) != 24: + # generate the expected epoch times based on start time and 1 hour intervals + current_epoch = int(params.get("start")) + expected_epochs = [] + for _ in range(24): + expected_epochs.append(current_epoch) + current_epoch += 3600 + for epoch_value in expected_epochs: + #catch for if the maybe_values queue is empty + if not maybe_values: + timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(epoch_value)) + timestamps_and_values.append(TimestampAndValuePair(timestamp, "-1")) #-1 for no data + continue + + actual_epoch = maybe_values[0][0] + if not check_epoch_aggreement(int(actual_epoch), epoch_value): + timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(epoch_value)) + timestamps_and_values.append(TimestampAndValuePair(timestamp, "-1")) #-1 for no data + else: + timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(actual_epoch)) + timestamps_and_values.append(TimestampAndValuePair(timestamp, maybe_values[0][1])) + maybe_values.pop(0) + else: + for value_pair in maybe_values: + timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(value_pair[0])) + timestamps_and_values.append(TimestampAndValuePair(timestamp, value_pair[1])) # the service is up if the maximum timestamp's value is "1" # prometheus returns data with the greatest timestamp last is_up = False + hasData = False if timestamps_and_values: - is_up = timestamps_and_values[-1].value == "1" + if timestamps_and_values[-1].value == "-1": + is_up = False + hasData = False + else: + hasData = True + is_up = timestamps_and_values[-1].value == "1" + + service = PrometheusData( - maybe_instance, maybe_job, is_up, timestamps_and_values + maybe_instance, maybe_job, hasData, is_up, timestamps_and_values ) result.append(service) diff --git a/system_status/templates/my_template.html b/system_status/templates/my_template.html index 44a2049..c3d6ea1 100644 --- a/system_status/templates/my_template.html +++ b/system_status/templates/my_template.html @@ -109,7 +109,9 @@