nova-api/api/db/stats.py

63 lines
2 KiB
Python
Raw Permalink Normal View History

2023-08-04 03:30:56 +02:00
import os
import pytz
import asyncio
import datetime
from dotenv import load_dotenv
from motor.motor_asyncio import AsyncIOMotorClient
load_dotenv()
2023-08-12 18:20:18 +02:00
## Statistics
2023-08-04 03:30:56 +02:00
class StatsManager:
"""
### The manager for all statistics tracking
"""
def __init__(self):
2023-08-19 16:26:14 +02:00
self.conn = AsyncIOMotorClient(os.environ['MONGO_URI'])
async def _get_collection(self, collection_name: str):
2023-08-17 13:13:20 +02:00
return self.conn[os.getenv('MONGO_NAME', 'nova-test')][collection_name]
async def add_date(self):
date = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y.%m.%d')
year, month, day = date.split('.')
2023-08-04 03:30:56 +02:00
db = await self._get_collection('stats')
await db.update_one({}, {'$inc': {f'dates.{year}.{month}.{day}': 1}}, upsert=True)
2023-08-04 03:30:56 +02:00
async def add_ip_address(self, ip_address: str):
ip_address = ip_address.replace('.', '_')
db = await self._get_collection('stats')
await db.update_one({}, {'$inc': {f'ips.{ip_address}': 1}}, upsert=True)
2023-08-04 03:30:56 +02:00
async def add_target(self, url: str):
db = await self._get_collection('stats')
await db.update_one({}, {'$inc': {f'targets.{url}': 1}}, upsert=True)
2023-08-04 03:30:56 +02:00
async def add_tokens(self, tokens: int, model: str):
db = await self._get_collection('stats')
await db.update_one({}, {'$inc': {f'tokens.{model}': tokens}}, upsert=True)
2023-08-04 03:30:56 +02:00
async def add_model(self, model: str):
db = await self._get_collection('stats')
await db.update_one({}, {'$inc': {f'models.{model}': 1}}, upsert=True)
2023-08-04 03:30:56 +02:00
async def add_path(self, path: str):
path = path.replace('/', '_')
db = await self._get_collection('stats')
await db.update_one({}, {'$inc': {f'paths.{path}': 1}}, upsert=True)
2023-08-04 03:30:56 +02:00
async def get_value(self, obj_filter):
db = await self._get_collection('stats')
return await db.find_one({obj_filter})
2023-08-04 03:30:56 +02:00
manager = StatsManager()
2023-08-04 03:30:56 +02:00
if __name__ == '__main__':
stats = StatsManager()
asyncio.run(stats.add_date())
asyncio.run(stats.add_path('/__demo/test'))