Сделал аналитику в сервисе для генерации чартов

This commit is contained in:
Данил 2024-10-17 20:56:10 +03:00
parent 5b267e9069
commit 93b6fabbd2
2 changed files with 44 additions and 1 deletions

View file

@ -3,13 +3,13 @@ import psycopg2
import uvicorn import uvicorn
import yaml import yaml
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import locale
import datetime import datetime
import dateutil.relativedelta import dateutil.relativedelta
from fastapi import FastAPI, Response, status from fastapi import FastAPI, Response, status
from psycopg2.extras import DictCursor from psycopg2.extras import DictCursor
from starlette.staticfiles import StaticFiles from starlette.staticfiles import StaticFiles
from middleware.plausible_analytics import PlausibleAnalytics
app = FastAPI() app = FastAPI()
@ -30,6 +30,7 @@ if not os.path.exists('../charts'):
app.mount('/static/charts', StaticFiles(directory='../charts/')) app.mount('/static/charts', StaticFiles(directory='../charts/'))
app.middleware('http')(PlausibleAnalytics())
@app.get("/api/getChart/") @app.get("/api/getChart/")
async def get_chart(response: Response, async def get_chart(response: Response,

View file

@ -0,0 +1,42 @@
import httpx
import yaml
from user_agents import parse as ua_parse
config = yaml.safe_load(open('../config.yaml'))
class PlausibleAnalytics:
async def __call__(self, request, call_next):
response = await call_next(request)
user_agent = request.headers.get('user-agent', 'unknown')
user_agent_parsed = ua_parse(user_agent)
event = {
"domain": config['analytics']['plausible_domain'],
"name": request.url.path or '404 - Not Found',
"url": str(request.url),
"props": {
"method": request.method,
"statusCode": response.status_code,
"browser": f"{user_agent_parsed.browser.family} {user_agent_parsed.browser.version_string}",
"os": f"{user_agent_parsed.os.family} {user_agent_parsed.os.version_string}",
"source": request.headers.get('referer', 'direct'),
},
}
async with httpx.AsyncClient() as client:
try:
await client.post(
config['analytics']['plausible_api'],
json=event,
headers={
"Authorization": f"Bearer {config['analytics']['plausible_token']}",
"Content-Type": "application/json",
"User-Agent": request.headers.get('user-agent', 'unknown'),
},
)
except Exception as e:
print(f"Error sending event to Plausible: {e}")
return response