Fix core plugins page with list and UI logic when fetching metrics

This commit is contained in:
Théophile Diot 2024-02-20 16:22:20 +01:00
parent 30a4155127
commit d030b975ba
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
5 changed files with 31 additions and 31 deletions

View file

@ -1,13 +1,13 @@
from operator import itemgetter
def badbehavior(**kwargs):
try:
# Here we will have a list { 'counter_403': X, 'counter_401': Y ... }
data = kwargs["app"].config["INSTANCES"].get_metrics("badbehavior")
format_data = []
# Format to fit [{code: 403, count: X}, {code: 401, count: Y} ...]
for key, value in data.items():
format_data[key] = {"code": int(key.split("_")[1]), "count": value}
format_data = [{"code": int(key.split("_")[1]), "count": int(value)} for key, value in data.items()]
format_data.sort(key=itemgetter("count"), reverse=True)
return {"items": format_data}
except:
return {"items": []}

View file

@ -1,13 +1,13 @@
from operator import itemgetter
def errors(**kwargs):
try:
# Here we will have a list { 'counter_403': X, 'counter_401': Y ... }
data = kwargs["app"].config["INSTANCES"].get_metrics("errors")
format_data = []
# Format to fit [{code: 403, count: X}, {code: 401, count: Y} ...]
for key, value in data.items():
format_data[key] = {"code": int(key.split("_")[1]), "count": value}
format_data = [{"code": int(key.split("_")[1]), "count": int(value)} for key, value in data.items()]
format_data.sort(key=itemgetter("count"), reverse=True)
return {"items": format_data}
except:
return {"items": []}

View file

@ -1,3 +1,6 @@
from operator import itemgetter
def limit(**kwargs):
try:
# Here we will have a list { 'limit_uri_url1': X, 'limit_uri_url2': Y ... }
@ -5,9 +8,13 @@ def limit(**kwargs):
format_data = []
# Format to fit [{url: "url1", count: X}, {url: "url2", count: Y} ...]
for key, value in data.items():
format_data[key] = {"url": key.replace("limit_uri_", ""), "count": value}
key = key.split("/", 1)
if len(key) > 1:
key = key[1]
else:
key = ""
format_data.append({"url": f"/{key}", "count": int(value)})
format_data.sort(key=itemgetter("count"), reverse=True)
return {"items": format_data}
except:
return {"items": []}

View file

@ -1,13 +1,13 @@
from operator import itemgetter
def reversescan(**kwargs):
try:
# Here we will have a list { 'counter_403': X, 'counter_401': Y ... }
data = kwargs["app"].config["INSTANCES"].get_metrics("reversescan")
format_data = []
# Format to fit [{code: 403, count: X}, {code: 401, count: Y} ...]
for key, value in data.items():
format_data[key] = {"port": int(key.split("_")[1]), "count": value}
format_data = [{"port": int(key.split("_")[1]), "count": int(value)} for key, value in data.items()]
format_data.sort(key=itemgetter("count"), reverse=True)
return {"items": format_data}
except:
return {"items": []}

View file

@ -394,7 +394,7 @@ class Instances:
if not resp:
continue
if instance.name not in instance_metrics or instance_metrics[instance.name]["msg"] is None or instance_metrics[instance.name]["msg"] is not dict or instance_metrics[instance.name]["status"] != "success":
if not instance_metrics.get(instance.name, {"msg": None})["msg"] or not isinstance(instance_metrics[instance.name]["msg"], dict) or instance_metrics[instance.name]["status"] != "success":
continue
metric_data = instance_metrics[instance.name]["msg"]
@ -413,30 +413,23 @@ class Instances:
# Case value is number, add it to the existing value
if isinstance(value, (int, float)):
metrics[key] += value
continue
# Case value is string, replace the existing value
elif isinstance(value, str):
metrics[key] = value
continue
# Case value is list, extend it to the existing value
if isinstance(value, list):
elif isinstance(value, list):
metrics[key].extend(value)
continue
# Case value is a dict, loop on it and update the existing value
if isinstance(value, dict):
elif isinstance(value, dict):
for k, v in value.items():
if k not in metrics[key]:
metrics[key][k] = v
continue
if isinstance(v, (int, float)):
elif isinstance(v, (int, float)):
metrics[key][k] += v
continue
if isinstance(v, list):
elif isinstance(v, list):
metrics[key][k].extend(v)
continue
if isinstance(v, str):
elif isinstance(v, str):
metrics[key][k] = v
continue
return metrics
def get_ping(self, plugin_id: str):