]> git.mxchange.org Git - fba.git/commitdiff
Rewrites:
authorRoland Häder <roland@mxchange.org>
Sun, 11 Jun 2023 15:03:06 +0000 (17:03 +0200)
committerRoland Häder <roland@mxchange.org>
Sun, 11 Jun 2023 15:07:25 +0000 (17:07 +0200)
- 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
templates/views/index.html
templates/views/scoreboard.html

diff --git a/api.py b/api.py
index 15ed693beb9beaf0db34ee6af4666a6b22e0c37e..3ee3c5c49aaf5926bdb0d352e3b11176619f522b 100644 (file)
--- 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)
     })
 
index 24f0f0b9c581490338c190600fee4d426f156218..7e8849aa476c39302fd3d0d7a852940f116cee93 100644 (file)
 
     <h2>Scoreboards:</h2>
     <p>
-        <a href="{{base_url}}/scoreboard?blockers=50">top 50 defederating</a> /
-        <a href="{{base_url}}/scoreboard?blocked=50">defederated instances</a> /
-        <a href="{{base_url}}/scoreboard?reference=50">referencing instances</a> /
-        <a href="{{base_url}}/scoreboard?software=50">used software</a> /
-        <a href="{{base_url}}/scoreboard?command=10">commands</a> /
-        <a href="{{base_url}}/scoreboard?error_code=30">error codes</a>
+        <a href="{{base_url}}/scoreboard?mode=blocker&amp;amount=50">top 50 defederating</a> /
+        <a href="{{base_url}}/scoreboard?mode=blocked&amp;amount=50">defederated instances</a> /
+        <a href="{{base_url}}/scoreboard?mode=reference&amp;amount=50">referencing instances</a> /
+        <a href="{{base_url}}/scoreboard?mode=software&amp;amount=50">used software</a> /
+        <a href="{{base_url}}/scoreboard?mode=command&amp;amount=10">commands</a> /
+        <a href="{{base_url}}/scoreboard?mode=error_code&amp;amount=30">error codes</a>
     </p>
 {% endblock %}
 {% block footer %}
index f5fd8a7846a63cc0871d14fbff811ed4b68a030c..5b893c0f38ceeab89c82a378ab9ca845cc71e42c 100644 (file)
@@ -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 %}
-        <h1>Top {{blockers}} defederating instances</h1>
-    {% elif blocked %}
-        <h1>Top {{blocked}} defederated instances</h1>
-    {% elif reference %}
-        <h1>Top {{reference}} referencing instances</h1>
-    {% elif software %}
-        <h1>Top {{software}} used software</h1>
-    {% elif command %}
-        <h1>TOP {{command}} scripts</h1>
-    {% elif error_code %}
-        <h1>TOP {{error_code}} error codes</h1>
+    {% if mode == 'blocker' %}
+        <h1>Top {{amount}} defederating instances</h1>
+    {% elif mode == 'blocked' %}
+        <h1>Top {{amount}} defederated instances</h1>
+    {% elif mode == 'reference' %}
+        <h1>Top {{amount}} referencing instances</h1>
+    {% elif mode == 'software' %}
+        <h1>Top {{amount}} used software</h1>
+    {% elif mode == 'command' %}
+        <h1>TOP {{amount}} scripts</h1>
+    {% elif mode == 'error_code' %}
+        <h1>TOP {{amount}} error codes</h1>
     {% endif %}
 {% endblock %}
 
@@ -23,8 +23,8 @@
         <table>
             <thead>
                 <th>№</th>
-                <th>{% if software %}Software{% elif error_code %}Error code{% else %}Instance{% endif %}</th>
-                <th>{% if reference %}References{% elif software or error_code %}Total{% else %}Blocks{% endif %}</th>
+                <th>{% if mode == 'software' %}Software{% elif mode == 'error_code' %}Error code{% else %}Instance{% endif %}</th>
+                <th>{% if mode == 'reference' %}References{% elif mode in('software', 'error_code') %}Total{% else %}Blocks{% endif %}</th>
             </thead>
 
             <tbody>
                 <tr>
                     <td>{{loop.index}}</td>
                     <td>
-                        {% if software or command or error_code %}
+                        {% if mode in ('software', 'command', 'error_code') %}
                             {{entry['domain']}}
                         {% elif entry['domain'] == None %}
                             -
                         {% else %}
-                            <a href="{{base_url}}/top?{% if blockers %}reverse{% elif blocked or reference %}domain{% endif %}={{entry['domain']}}" rel="nofollow noopener noreferrer">{{entry['domain']}}</a>&nbsp;
+                            <a href="{{base_url}}/top?{% if mode == 'blocker' %}reverse{% elif mode in('blocked', 'reference') %}domain{% endif %}={{entry['domain']}}" rel="nofollow noopener noreferrer">{{entry['domain']}}</a>&nbsp;
                             <a class="listlink" href="https://{{entry['domain']}}" rel="external" target="_blank">↗</a>
                         {% endif %}
                     </td>
-                    <td>{{entry['highscore']}}</td>
+                    <td>{{entry['score']}}</td>
                 </tr>
             {% endfor %}
             </tbody>
@@ -50,7 +50,7 @@
 {% endblock %}
 
 {% block footer %}
-    {% if error_code %}
+    {% if mode == 'error_code' %}
         <h2>Please note to error codes:</h2>
         <ul>
             <li>Error code 999 is fake and covers a lot of reasons why the domain/instance is not reachable. Mostly that the domain is not resolvable or the server refused connection.</li>