diff --git a/app.py b/app.py index 851a518..d204201 100644 --- a/app.py +++ b/app.py @@ -175,5 +175,28 @@ def render_terms(): return html_content +@app.route("/stats") +def render_stats(): + """ + Statistics Requests + --- + tags: + - Statistics Endpoint + responses: + 200: + description: Success + """ + db = MasterServer.get_db() + + # Get a list of collection names in the database + collection_names = db.list_collection_names() + + # Create a dictionary to store collection counts + collection_counts = { + name.lower(): db[name].count_documents({}) for name in collection_names} + + return collection_counts + + if __name__ == '__main__': app.run(debug=True) diff --git a/protocols/MasterServer.py b/protocols/MasterServer.py index 4858956..3e092cb 100644 --- a/protocols/MasterServer.py +++ b/protocols/MasterServer.py @@ -15,11 +15,16 @@ class MasterServer(ABC): def __init__(self, key: str) -> None: self._key = key - uri = os.getenv('DATABASE_URL') - self.client = MongoClient(uri) - self.db = self.client['MasterServer'] + self.db = self.get_db() self.collection = self.db[key] + @staticmethod + def get_db(): + uri = os.getenv('DATABASE_URL') + client = MongoClient(uri) + db = client['MasterServer'] + return db + @property def key(self): return self._key @@ -29,7 +34,7 @@ def job(self): pass @abstractmethod - def find(self, *, host: str, port: int): + def find(self, *, host: str, port: int) -> dict: pass def run(self):