Skip to content

Commit

Permalink
add statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Sep 29, 2024
1 parent bdb6f02 commit db65bea
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import asyncio
import json
import os
import pathlib
import traceback
import aiohttp
import uvicorn
import fastapi
import Bot.config
Expand All @@ -17,13 +15,13 @@
import astroidapi.health_check
import astroidapi.read_handler
import astroidapi.surrealdb_handler
import astroidapi.statistics
import beta_users
from fastapi.middleware.cors import CORSMiddleware
import requests
import sentry_sdk
from PIL import Image
from fastapi import HTTPException
import time
import slowapi
from slowapi.errors import RateLimitExceeded
from slowapi import Limiter
Expand Down Expand Up @@ -137,6 +135,12 @@ def root():
}
return fastapi.responses.JSONResponse(status_code=200, content=home_data)


@api.get("/statistics", description="Get the statistics.")
async def get_statistics():
await astroidapi.surrealdb_handler.Statistics.update_messages(1)
return await astroidapi.statistics.get_statistics()

@api.get("/invite/{platform}", description="Get the invite link for the astroid bot.")
def invite(platform: str, token: Annotated[str, fastapi.Query(max_length=85)] = None):
if platform == "discord":
Expand Down Expand Up @@ -705,6 +709,7 @@ async def get_channel_name(platform: str, id: str, token: Annotated[str, fastapi
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This channel does not exist."})



logging.info("[CORE] API started.")

uvicorn.run(api, host="localhost", port=9921)
uvicorn.run(api, host="localhost", port=9921)
38 changes: 37 additions & 1 deletion src/astroidapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,37 @@ class CheckEligibilityError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class ProfileNotFoundError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class ProfileCreationError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class ProfileUpdateError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class ProfileDeletionError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class GetProfileError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class GetStatisticsError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)



class ReadHandlerError:
Expand Down Expand Up @@ -234,4 +265,9 @@ def __init__(self, message):
super().__init__(self.message)




class ProfileProcessorError:
class ProfileNotFoundError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
2 changes: 2 additions & 0 deletions src/astroidapi/sending_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import astroidapi.read_handler as read_handler
import astroidapi.formatter as formatter
import astroidapi.attachment_processor as attachment_processor
import astroidapi.statistics as statistics
import os
import traceback

Expand Down Expand Up @@ -46,6 +47,7 @@ async def distribute(cls, endpoint, updated_json):
if sender == "nerimity":
await cls.send_from_nerimity(updated_json, endpoint, attachments)
await attachment_processor.clear_temporary_attachments()
asyncio.create_task(statistics.update_statistics())
return True
except Exception as e:
traceback.print_exc()
Expand Down
39 changes: 39 additions & 0 deletions src/astroidapi/statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import math
import astroidapi.surrealdb_handler as surrealdb_handler

async def get_statistics():
statistics = await surrealdb_handler.Statistics.getall()
total_messages = statistics["messages"]["total"]
def round_down_to_nearest(x, base):
return base * math.floor(x / base)

if total_messages < 10:
total_messages_rounded = total_messages
elif total_messages < 50:
total_messages_rounded = round_down_to_nearest(total_messages, 10)
elif total_messages < 100:
total_messages_rounded = round_down_to_nearest(total_messages, 50)
elif total_messages < 500:
total_messages_rounded = round_down_to_nearest(total_messages, 100)
else:
total_messages_rounded = round_down_to_nearest(total_messages, 1000)
statistics["messages"]["total_rounded"] = total_messages_rounded

total_monthly_messages = statistics["messages"]["month"]
if total_monthly_messages < 10:
total_monthly_messages_rounded = total_monthly_messages
elif total_monthly_messages < 50:
total_monthly_messages_rounded = round_down_to_nearest(total_monthly_messages, 10)
elif total_monthly_messages < 100:
total_monthly_messages_rounded = round_down_to_nearest(total_monthly_messages, 50)
elif total_monthly_messages < 500:
total_monthly_messages_rounded = round_down_to_nearest(total_monthly_messages, 100)
else:
total_monthly_messages_rounded = round_down_to_nearest(total_monthly_messages, 1000)
statistics["messages"]["month_rounded"] = total_monthly_messages_rounded


return statistics

async def update_statistics():
await surrealdb_handler.Statistics.update_messages(1)
65 changes: 65 additions & 0 deletions src/astroidapi/surrealdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import astroidapi.errors as errors
import traceback
import pathlib
import random
import string
import datetime

async def sync_local_files(folderpath: str, specific: bool = False):
try:
Expand Down Expand Up @@ -197,8 +200,10 @@ async def check_eligibility(cls, endpoint: int):
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
elegible_endpoints = await db.select("attachments:eligible_endpoints")
print(elegible_endpoints)
if endpoint in elegible_endpoints["endpoints"]:
return True
return False

except Exception as e:
raise errors.SurrealDBHandler.CheckEligibilityError(e)
Expand Down Expand Up @@ -270,3 +275,63 @@ async def for_nerimity(cls, endpoint: int, nerimity_id: str):
except Exception as e:
raise errors.SurrealDBHandler.CreateEndpointError(e)





class Statistics:

@staticmethod
async def getall():
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
messages = await db.select("statistics:messages")
entpoints = len(await db.select("endpoints"))

return {
"messages": messages,
"endpoints": entpoints
}
except Exception as e:
raise errors.SurrealDBHandler.GetStatisticsError(e)

@staticmethod
async def get_messages():
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
return await db.select("statistics:messages")
except Exception as e:
raise errors.SurrealDBHandler.GetStatisticsError(e)

@staticmethod
async def update_messages(increment: int, start_period: bool = False):
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
current = datetime.datetime.fromtimestamp(datetime.datetime.now().timestamp())
total = await db.select("statistics:messages")
total = total["total"]
await db.query(f"UPDATE statistics:messages SET total = {total + increment}")
if start_period:
await db.query(f"UPDATE statistics:messages SET periodStart = {datetime.datetime.now().timestamp()}")
await db.query(f"UPDATE statistics:messages SET month = 0")
period_start = await db.select("statistics:messages")
period_start = period_start["periodStart"]
print(datetime.datetime.fromtimestamp(period_start) + datetime.timedelta(weeks=4))
if current >= datetime.datetime.fromtimestamp(period_start) + datetime.timedelta(weeks=4):
await db.query(f"UPDATE statistics:messages SET periodStart = {datetime.datetime.now().timestamp()}")
await db.query(f"UPDATE statistics:messages SET month = 0")
else:
moth = await db.select("statistics:messages")
moth = moth["month"]
await db.query(f"UPDATE statistics:messages SET month = {moth + increment}")

return await db.select("statistics:messages")
except Exception as e:
traceback.print_exc()
raise errors.SurrealDBHandler.GetStatisticsError(e)

0 comments on commit db65bea

Please sign in to comment.