feat: add automation last_run_start, next_run, enabled in the automation collector

This commit is contained in:
Ana Custura 2024-12-04 14:45:55 +00:00
parent 3922082a56
commit 348a4b5cf0

View file

@ -121,19 +121,40 @@ class AutomationCollector(Collector):
"Status of a database collector (0: bad, 1: good)",
labels=["collector"])
try:
c = GaugeMetricFamily("automation_state", "The automation state (0: idle, 1: running, 2: error)",
labels=['automation_name'])
state = GaugeMetricFamily("automation_state", "The automation state (0: idle, 1: running, 2: error)",
labels=['automation_name'])
enabled = GaugeMetricFamily("automation_enabled",
"Whether an automation is enabled (0: disabled, 1: enabled)",
labels=['automation_name'])
next_run = GaugeMetricFamily("automation_next_run", "The timestamp of the next run of the automation",
labels=['automation_name'])
last_run_start = GaugeMetricFamily("automation_last_run_start",
"The timestamp of the last run of the automation ",
labels=['automation_name'])
automations = Automation.query.all()
for automation in automations:
if automation.short_name in app.config['HIDDEN_AUTOMATIONS']:
continue
if automation.state == AutomationState.IDLE:
c.add_metric([automation.short_name], 0)
state.add_metric([automation.short_name], 0)
elif automation.state == AutomationState.RUNNING:
c.add_metric([automation.short_name], 1)
state.add_metric([automation.short_name], 1)
else:
c.add_metric([automation.short_name], 2)
yield c
state.add_metric([automation.short_name], 2)
enabled.add_metric([automation.short_name], 1 if automation.enabled else 0)
if automation.next_run:
next_run.add_metric([automation.short_name], automation.next_run.timestamp())
else:
next_run.add_metric([automation.short_name], 0)
if automation.last_run:
last_run_start.add_metric([automation.short_name], automation.last_run.timestamp())
else:
last_run_start.add_metric([automation.short_name], 0)
yield state
yield enabled
yield next_run
yield last_run_start
ok.add_metric(["automation_state"], 1)
except SQLAlchemyError:
ok.add_metric(["automation_state"], 0)