Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions system_status/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ def to_dict(self):
class PrometheusData:
instance: str
job: str
hasData: bool
is_up: bool
values: List[TimestampAndValuePair]

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]
}
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 6 additions & 2 deletions system_status/templates/my_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ <h2 id="fetch_time">Fetch Time: {{ fetch_time }}</h2>
{% for item in data %}
{% set job = item.job %}

{% if item.is_up %}
{% if not item.hasData %}
{% set status = "⚠️ NO DATA" %}
{% elif item.is_up %}
{% set status = "✅ UP" %}
{% else %}
{% set status = "❌ DOWN" %}
Expand All @@ -122,7 +124,9 @@ <h2 id="fetch_time">Fetch Time: {{ fetch_time }}</h2>
{# THIS IS THE CORRECTED SECTION for the EMOJI HISTORY #}
<td>
{% for value in item.values %}
{% if value.value == "1" %}
{% if value.value == "-1" %}
⚠️
{% elif value.value == "1" %}
{% else %}
Expand Down