lint: reformat python code with black

This commit is contained in:
Iain Learmonth 2024-12-06 18:15:47 +00:00
parent 331beb01b4
commit a406a7974b
88 changed files with 2579 additions and 1608 deletions

View file

@ -6,8 +6,7 @@ import yaml
from flask import Flask, redirect, send_from_directory, url_for
from flask.typing import ResponseReturnValue
from prometheus_client import CollectorRegistry, Metric, make_wsgi_app
from prometheus_client.metrics_core import (CounterMetricFamily,
GaugeMetricFamily)
from prometheus_client.metrics_core import CounterMetricFamily, GaugeMetricFamily
from prometheus_client.registry import REGISTRY, Collector
from prometheus_flask_exporter import PrometheusMetrics
from sqlalchemy import text
@ -28,9 +27,9 @@ app.config.from_file("../config.yaml", load=yaml.safe_load)
registry = CollectorRegistry()
metrics = PrometheusMetrics(app, registry=registry)
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { # type: ignore[method-assign]
'/metrics': make_wsgi_app(registry)
})
app.wsgi_app = DispatcherMiddleware( # type: ignore[method-assign]
app.wsgi_app, {"/metrics": make_wsgi_app(registry)}
)
# register default collectors to our new registry
collectors = list(REGISTRY._collector_to_names.keys())
@ -54,12 +53,16 @@ def not_migrating() -> bool:
class DefinedProxiesCollector(Collector):
def collect(self) -> Iterator[Metric]:
with app.app_context():
ok = GaugeMetricFamily("database_collector",
"Status of a database collector (0: bad, 1: good)",
labels=["collector"])
ok = GaugeMetricFamily(
"database_collector",
"Status of a database collector (0: bad, 1: good)",
labels=["collector"],
)
try:
with db.engine.connect() as conn:
result = conn.execute(text("""
result = conn.execute(
text(
"""
SELECT origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name,
COUNT(proxy.id) FROM proxy, origin, pool, "group"
WHERE proxy.origin_id = origin.id
@ -67,13 +70,24 @@ class DefinedProxiesCollector(Collector):
AND proxy.pool_id = pool.id
AND proxy.destroyed IS NULL
GROUP BY origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name;
"""))
c = GaugeMetricFamily("defined_proxies", "Number of proxies currently defined for deployment",
labels=['group_id', 'group_name', 'provider', 'pool_id',
'pool_name'])
"""
)
)
c = GaugeMetricFamily(
"defined_proxies",
"Number of proxies currently defined for deployment",
labels=[
"group_id",
"group_name",
"provider",
"pool_id",
"pool_name",
],
)
for row in result:
c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4]],
row[5])
c.add_metric(
[str(row[0]), row[1], row[2], str(row[3]), row[4]], row[5]
)
yield c
ok.add_metric(["defined_proxies"], 1)
except SQLAlchemyError:
@ -84,12 +98,16 @@ class DefinedProxiesCollector(Collector):
class BlockedProxiesCollector(Collector):
def collect(self) -> Iterator[Metric]:
with app.app_context():
ok = GaugeMetricFamily("database_collector",
"Status of a database collector (0: bad, 1: good)",
labels=["collector"])
ok = GaugeMetricFamily(
"database_collector",
"Status of a database collector (0: bad, 1: good)",
labels=["collector"],
)
try:
with db.engine.connect() as conn:
result = conn.execute(text("""
result = conn.execute(
text(
"""
SELECT origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name,
proxy.deprecation_reason, COUNT(proxy.id) FROM proxy, origin, pool, "group"
WHERE proxy.origin_id = origin.id
@ -98,14 +116,26 @@ class BlockedProxiesCollector(Collector):
AND proxy.deprecated IS NOT NULL
GROUP BY origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name,
proxy.deprecation_reason;
"""))
c = CounterMetricFamily("deprecated_proxies",
"Number of proxies deprecated",
labels=['group_id', 'group_name', 'provider', 'pool_id', 'pool_name',
'deprecation_reason'])
"""
)
)
c = CounterMetricFamily(
"deprecated_proxies",
"Number of proxies deprecated",
labels=[
"group_id",
"group_name",
"provider",
"pool_id",
"pool_name",
"deprecation_reason",
],
)
for row in result:
c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4], row[5]],
row[6])
c.add_metric(
[str(row[0]), row[1], row[2], str(row[3]), row[4], row[5]],
row[6],
)
yield c
ok.add_metric(["deprecated_proxies"], 0)
except SQLAlchemyError:
@ -116,24 +146,36 @@ class BlockedProxiesCollector(Collector):
class AutomationCollector(Collector):
def collect(self) -> Iterator[Metric]:
with app.app_context():
ok = GaugeMetricFamily("database_collector",
"Status of a database collector (0: bad, 1: good)",
labels=["collector"])
ok = GaugeMetricFamily(
"database_collector",
"Status of a database collector (0: bad, 1: good)",
labels=["collector"],
)
try:
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'])
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']:
if automation.short_name in app.config["HIDDEN_AUTOMATIONS"]:
continue
if automation.state == AutomationState.IDLE:
state.add_metric([automation.short_name], 0)
@ -141,13 +183,19 @@ class AutomationCollector(Collector):
state.add_metric([automation.short_name], 1)
else:
state.add_metric([automation.short_name], 2)
enabled.add_metric([automation.short_name], 1 if automation.enabled else 0)
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())
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())
last_run_start.add_metric(
[automation.short_name], automation.last_run.timestamp()
)
else:
last_run_start.add_metric([automation.short_name], 0)
yield state
@ -161,31 +209,31 @@ class AutomationCollector(Collector):
# register all custom collectors to registry
if not_migrating() and 'DISABLE_METRICS' not in os.environ:
if not_migrating() and "DISABLE_METRICS" not in os.environ:
registry.register(DefinedProxiesCollector())
registry.register(BlockedProxiesCollector())
registry.register(AutomationCollector())
@app.route('/ui')
@app.route("/ui")
def redirect_ui() -> ResponseReturnValue:
return redirect("/ui/")
@app.route('/ui/', defaults={'path': ''})
@app.route('/ui/<path:path>')
@app.route("/ui/", defaults={"path": ""})
@app.route("/ui/<path:path>")
def serve_ui(path: str) -> ResponseReturnValue:
if path != "" and os.path.exists("app/static/ui/" + path):
return send_from_directory('static/ui', path)
return send_from_directory("static/ui", path)
else:
return send_from_directory('static/ui', 'index.html')
return send_from_directory("static/ui", "index.html")
@app.route('/')
@app.route("/")
def index() -> ResponseReturnValue:
# TODO: update to point at new UI when ready
return redirect(url_for("portal.portal_home"))
if __name__ == '__main__':
if __name__ == "__main__":
app.run()