From 81f3928cdc8d3aba4d128826b084429f56cb34fb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 11 Jun 2023 17:03:06 +0200 Subject: [PATCH] Rewrites: - rewrote api.api_top() to only 2 parameters, 'mode' and 'amount' - also remove duplicate `if amount > 500` lines and combined them into one --- api.py | 81 ++++++++++++++------------------- templates/views/index.html | 12 ++--- templates/views/scoreboard.html | 38 ++++++++-------- 3 files changed, 59 insertions(+), 72 deletions(-) diff --git a/api.py b/api.py index 15ed693..3ee3c5c 100644 --- a/api.py +++ b/api.py @@ -50,40 +50,31 @@ def api_info(): } @router.get(config.get("base_url") + "/api/top.json", response_class=JSONResponse) -def api_top(blocked: int = None, blockers: int = None, reference: int = None, software: int = None, command: int = None, error_code: int = None): - if blocked is not None: - if blocked > 500: - raise HTTPException(status_code=400, detail="Too many results") - fba.cursor.execute("SELECT blocked, COUNT(blocked) FROM blocks WHERE block_level = 'reject' GROUP BY blocked ORDER BY COUNT(blocked) DESC LIMIT ?", [blocked]) - elif blockers is not None: - if blockers > 500: - raise HTTPException(status_code=400, detail="Too many results") - fba.cursor.execute("SELECT blocker, COUNT(blocker) FROM blocks WHERE block_level = 'reject' GROUP BY blocker ORDER BY COUNT(blocker) DESC LIMIT ?", [blockers]) - elif reference is not None: - if reference > 500: - raise HTTPException(status_code=400, detail="Too many results") - fba.cursor.execute("SELECT origin, COUNT(domain) FROM instances WHERE software IS NOT NULL GROUP BY origin ORDER BY COUNT(domain) DESC LIMIT ?", [reference]) - elif software is not None: - if software > 500: - raise HTTPException(status_code=400, detail="Too many results") - fba.cursor.execute("SELECT software, COUNT(domain) FROM instances WHERE software IS NOT NULL GROUP BY software ORDER BY COUNT(domain) DESC, software ASC LIMIT ?", [software]) - elif command is not None: - if command > 500: - raise HTTPException(status_code=400, detail="Too many results") - fba.cursor.execute("SELECT command, COUNT(domain) FROM instances WHERE command IS NOT NULL GROUP BY command ORDER BY COUNT(domain) DESC, command ASC LIMIT ?", [command]) - elif error_code is not None: - if error_code > 500: - raise HTTPException(status_code=400, detail="Too many results") - fba.cursor.execute("SELECT last_status_code, COUNT(domain) AS cnt FROM instances WHERE last_status_code IS NOT NULL AND last_status_code != '200' GROUP BY last_status_code ORDER BY cnt DESC LIMIT ?", [error_code]) +def api_top(mode: str, amount: int): + if amount > 500: + raise HTTPException(status_code=400, detail="Too many results") + + if mode == "blocked": + fba.cursor.execute("SELECT blocked, COUNT(blocked) AS score FROM blocks WHERE block_level = 'reject' GROUP BY blocked ORDER BY score DESC LIMIT ?", [amount]) + elif mode == "blocker": + fba.cursor.execute("SELECT blocker, COUNT(blocker) AS score FROM blocks WHERE block_level = 'reject' GROUP BY blocker ORDER BY score DESC LIMIT ?", [amount]) + elif mode == "reference": + fba.cursor.execute("SELECT origin, COUNT(domain) AS score FROM instances WHERE software IS NOT NULL GROUP BY origin ORDER BY score DESC LIMIT ?", [amount]) + elif mode == "software": + fba.cursor.execute("SELECT software, COUNT(domain) AS score FROM instances WHERE software IS NOT NULL GROUP BY software ORDER BY score DESC, software ASC LIMIT ?", [amount]) + elif mode == "command": + fba.cursor.execute("SELECT command, COUNT(domain) AS score FROM instances WHERE command IS NOT NULL GROUP BY command ORDER BY score DESC, command ASC LIMIT ?", [amount]) + elif mode == "error_code": + fba.cursor.execute("SELECT last_status_code, COUNT(domain) AS score FROM instances WHERE last_status_code IS NOT NULL AND last_status_code != '200' GROUP BY last_status_code ORDER BY score DESC LIMIT ?", [amount]) else: raise HTTPException(status_code=400, detail="No filter specified") scores = list() - for domain, highscore in fba.cursor.fetchall(): + for domain, score in fba.cursor.fetchall(): scores.append({ - "domain" : domain, - "highscore": highscore + "domain": domain, + "score" : score }) return scores @@ -155,21 +146,21 @@ def api_mutual(domains: list[str] = Query()): return JSONResponse(status_code=200, content={}) @router.get(config.get("base_url") + "/scoreboard") -def scoreboard(request: Request, blockers: int = None, blocked: int = None, reference: int = None, software: int = None, command: int = None, error_code: int = None): +def scoreboard(request: Request, mode: str, amount: int): response = None - if blockers is not None and blockers > 0: - response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?blockers={blockers}") - elif blocked is not None and blocked > 0: - response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?blocked={blocked}") - elif reference is not None and reference > 0: - response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?reference={reference}") - elif software is not None and software > 0: - response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?software={software}") - elif command is not None and command > 0: - response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?command={command}") - elif error_code is not None and error_code > 0: - response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?error_code={error_code}") + if mode == "blocker" and amount > 0: + response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?mode=blocker&amount={amount}") + elif mode == "blocked" and amount > 0: + response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?mode=blocked&amount={amount}") + elif mode == "reference" and amount > 0: + response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?mode=reference&amount={amount}") + elif mode == "software" and amount > 0: + response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?mode=software&amount={amount}") + elif mode == "command" and amount > 0: + response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?mode=command&amount={amount}") + elif mode == "error_code" and amount > 0: + response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?mode=error_code&amount={amount}") else: raise HTTPException(status_code=400, detail="No filter specified") @@ -183,12 +174,8 @@ def scoreboard(request: Request, blockers: int = None, blocked: int = None, refe "slogan" : config.get("slogan"), "request" : request, "scoreboard": True, - "blockers" : blockers, - "blocked" : blocked, - "reference" : reference, - "software" : software, - "command" : command, - "error_code": error_code, + "mode" : mode, + "amount" : amount, "scores" : network.json_from_response(response) }) diff --git a/templates/views/index.html b/templates/views/index.html index 24f0f0b..7e8849a 100644 --- a/templates/views/index.html +++ b/templates/views/index.html @@ -25,12 +25,12 @@

Scoreboards:

- top 50 defederating / - defederated instances / - referencing instances / - used software / - commands / - error codes + top 50 defederating / + defederated instances / + referencing instances / + used software / + commands / + error codes

{% endblock %} {% block footer %} diff --git a/templates/views/scoreboard.html b/templates/views/scoreboard.html index f5fd8a7..5b893c0 100644 --- a/templates/views/scoreboard.html +++ b/templates/views/scoreboard.html @@ -1,20 +1,20 @@ {% extends "base.html" %} -{% block title %}scoreboard - {% if software %}TOP {{software}} used software{% elif command %}TOP {{command}} scripts{% elif reference %}TOP {{reference}} referencing instances{% elif blocked %}TOP {{blocked}} deferated instances{% elif blockers %}TOP {{blockers}} deferating instances{% endif %}{% endblock %} +{% block title %}Scoreboard - {% if mode == 'software' %}TOP {{amount}} used software{% elif mode == 'command' %}TOP {{amount}} scripts{% elif mode == 'error_code' %}TOP {{amount}} error codes{% elif mode == 'reference' %}TOP {{amount}} referencing instances{% elif mode == 'blocked' %}TOP {{amount}} deferated instances{% elif mode == 'blocker' %}TOP {{amount}} deferating instances{% endif %}{% endblock %} {% block header %} - {% if blockers %} -

Top {{blockers}} defederating instances

- {% elif blocked %} -

Top {{blocked}} defederated instances

- {% elif reference %} -

Top {{reference}} referencing instances

- {% elif software %} -

Top {{software}} used software

- {% elif command %} -

TOP {{command}} scripts

- {% elif error_code %} -

TOP {{error_code}} error codes

+ {% if mode == 'blocker' %} +

Top {{amount}} defederating instances

+ {% elif mode == 'blocked' %} +

Top {{amount}} defederated instances

+ {% elif mode == 'reference' %} +

Top {{amount}} referencing instances

+ {% elif mode == 'software' %} +

Top {{amount}} used software

+ {% elif mode == 'command' %} +

TOP {{amount}} scripts

+ {% elif mode == 'error_code' %} +

TOP {{amount}} error codes

{% endif %} {% endblock %} @@ -23,8 +23,8 @@ - - + + @@ -32,16 +32,16 @@ - + {% endfor %} @@ -50,7 +50,7 @@ {% endblock %} {% block footer %} - {% if error_code %} + {% if mode == 'error_code' %}

Please note to error codes:

№{% if software %}Software{% elif error_code %}Error code{% else %}Instance{% endif %}{% if reference %}References{% elif software or error_code %}Total{% else %}Blocks{% endif %}{% if mode == 'software' %}Software{% elif mode == 'error_code' %}Error code{% else %}Instance{% endif %}{% if mode == 'reference' %}References{% elif mode in('software', 'error_code') %}Total{% else %}Blocks{% endif %}
{{loop.index}} - {% if software or command or error_code %} + {% if mode in ('software', 'command', 'error_code') %} {{entry['domain']}} {% elif entry['domain'] == None %} - {% else %} - {{entry['domain']}}  + {{entry['domain']}}  ↗ {% endif %} {{entry['highscore']}}{{entry['score']}}