feat: add _send_request

This commit is contained in:
def 2024-06-10 05:27:37 +03:00
parent cbdcb4a63f
commit 2e92557e84

View file

@ -25,13 +25,15 @@ class MonitoringInfo:
class Monitoring: class Monitoring:
"""GraphQL queries to get prometheus monitoring information.""" """GraphQL queries to get prometheus monitoring information."""
@strawberry.field def _send_request(self, endpoint="/api/v1/query", query=None) -> MonitoringInfo:
def status(self) -> MonitoringInfo:
"""Get status"""
endpoint = "/api/v1/status/runtimeinfo"
try: try:
if query:
response = requests.get(
f"{PROMETHEUS_URL}{endpoint}", params={"query": query}
)
else:
response = requests.get(f"{PROMETHEUS_URL}{endpoint}") response = requests.get(f"{PROMETHEUS_URL}{endpoint}")
return MonitoringInfo( return MonitoringInfo(
http_response=response.status_code, http_response=response.status_code,
output=( output=(
@ -44,47 +46,25 @@ class Monitoring:
error=str(error), error=str(error),
) )
@strawberry.field
def status(self) -> MonitoringInfo:
"""Get status"""
endpoint = "/api/v1/status/runtimeinfo"
return _send_request(endpoint=endpoint)
@strawberry.field @strawberry.field
def build(self) -> MonitoringInfo: def build(self) -> MonitoringInfo:
"""Get build information""" """Get build information"""
endpoint = "/api/v1/status/buildinfo" endpoint = "/api/v1/status/buildinfo"
return _send_request(endpoint=endpoint)
try: @strawberry.field
response = requests.get(f"{PROMETHEUS_URL}{endpoint}") def cpu_usage(self) -> MonitoringInfo:
return MonitoringInfo(
http_response=response.status_code,
output=(
json.dumps(response.json()) if response.status_code == 200 else None
),
)
except requests.RequestException as error:
return MonitoringInfo(
http_response=500,
error=str(error),
)
def cpu_usage(self):
"""Get CPU information""" """Get CPU information"""
endpoint = "/api/v1/query"
query = """100 - (avg by (instance) (irate(node_cpu_seconds_total{job="node",mode="idle"}[5m])) * 100)""" query = """100 - (avg by (instance) (irate(node_cpu_seconds_total{job="node",mode="idle"}[5m])) * 100)"""
# TODO: не представляю рабочий ли этот запрос вообще # TODO: не представляю рабочий ли этот запрос вообще
# https://overflow.hostux.net/questions/34923788/prometheus-convert-cpu-user-seconds-to-cpu-usage # https://overflow.hostux.net/questions/34923788/prometheus-convert-cpu-user-seconds-to-cpu-usage
# ему бы еще сделать кастомные минуты, чтоб разное время указывать, но так чтоб нельзя было инжектить # ему бы еще сделать кастомные минуты, чтоб разное время указывать, но так чтоб нельзя было инжектить
return _send_request(query=query)
try:
response = requests.get(
f"{PROMETHEUS_URL}{endpoint}", params={"query": query}
)
return MonitoringInfo(
http_response=response.status_code,
output=(
json.dumps(response.json()) if response.status_code == 200 else None
),
)
except requests.RequestException as error:
return MonitoringInfo(
http_response=500,
error=str(error),
)
def disks_usage(): ... def disks_usage(): ...