py-backend-analytics 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- py_backend_analytics/__init__.py +10 -0
- py_backend_analytics/constants.py +1 -0
- py_backend_analytics/db/__init__.py +0 -0
- py_backend_analytics/db/clients/__init__.py +0 -0
- py_backend_analytics/db/clients/abstract_db_client.py +33 -0
- py_backend_analytics/db/clients/sqlite_db_client.py +163 -0
- py_backend_analytics/db/constants.py +20 -0
- py_backend_analytics/db/models.py +25 -0
- py_backend_analytics/db/registry.py +15 -0
- py_backend_analytics/enums.py +5 -0
- py_backend_analytics/extraction/__init__.py +0 -0
- py_backend_analytics/extraction/constants.py +9 -0
- py_backend_analytics/extraction/extractors/__init__.py +0 -0
- py_backend_analytics/extraction/extractors/abstract_extractor.py +17 -0
- py_backend_analytics/extraction/extractors/fastapi_extractor.py +28 -0
- py_backend_analytics/extraction/geo_lookup.py +57 -0
- py_backend_analytics/input_data.py +21 -0
- py_backend_analytics/middlewares/__init__.py +0 -0
- py_backend_analytics/middlewares/fastapi_middleware.py +71 -0
- py_backend_analytics/models.py +10 -0
- py_backend_analytics/visualization/__init__.py +0 -0
- py_backend_analytics/visualization/constants.py +0 -0
- py_backend_analytics/visualization/fastapi_visualizer.py +35 -0
- py_backend_analytics-0.1.0.dist-info/METADATA +118 -0
- py_backend_analytics-0.1.0.dist-info/RECORD +28 -0
- py_backend_analytics-0.1.0.dist-info/WHEEL +5 -0
- py_backend_analytics-0.1.0.dist-info/licenses/LICENSE +674 -0
- py_backend_analytics-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from py_backend_analytics.enums import PyBackendAnalyticsDB as PyBackendAnalyticsDB
|
|
2
|
+
from py_backend_analytics.input_data import (
|
|
3
|
+
PyBackendAnalyticsInputData as PyBackendAnalyticsInputData,
|
|
4
|
+
)
|
|
5
|
+
from py_backend_analytics.middlewares.fastapi_middleware import (
|
|
6
|
+
PyBackendAnalyticsFastAPIMiddleware as PyBackendAnalyticsFastAPIMiddleware,
|
|
7
|
+
)
|
|
8
|
+
from py_backend_analytics.visualization.fastapi_visualizer import (
|
|
9
|
+
py_backend_analytics_fastapi_visualization as py_backend_analytics_fastapi_visualization,
|
|
10
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
PACKAGE_NAME = "py_backend_analytics"
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
|
|
4
|
+
from py_backend_analytics.models import RequestInfo
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass
|
|
8
|
+
class AbstractDBClient(ABC):
|
|
9
|
+
"""Client for interaction with the DB. Methods names are very self-descriptive"""
|
|
10
|
+
|
|
11
|
+
connection_string: str
|
|
12
|
+
|
|
13
|
+
@classmethod
|
|
14
|
+
async def create(cls, connection_string: str):
|
|
15
|
+
self = cls(connection_string)
|
|
16
|
+
await self.db_setup()
|
|
17
|
+
return self
|
|
18
|
+
|
|
19
|
+
async def db_setup(self):
|
|
20
|
+
if not await self._db_table_exists():
|
|
21
|
+
await self._create_db_table()
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
async def insert_request_info(self, model: RequestInfo): ...
|
|
25
|
+
|
|
26
|
+
@abstractmethod
|
|
27
|
+
async def get_analytics_summary(self) -> dict: ...
|
|
28
|
+
|
|
29
|
+
@abstractmethod
|
|
30
|
+
async def _create_db_table(self): ...
|
|
31
|
+
|
|
32
|
+
@abstractmethod
|
|
33
|
+
async def _db_table_exists(self) -> bool: ...
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
from datetime import datetime, timedelta, timezone
|
|
2
|
+
from contextlib import asynccontextmanager
|
|
3
|
+
|
|
4
|
+
import aiosqlite
|
|
5
|
+
|
|
6
|
+
from py_backend_analytics.db.clients.abstract_db_client import AbstractDBClient
|
|
7
|
+
from py_backend_analytics.db.constants import (
|
|
8
|
+
DB_TABLE_NAME,
|
|
9
|
+
DBColumns,
|
|
10
|
+
DB_INDEX_SUFFIX,
|
|
11
|
+
TOP_AGGREGATION_LIMIT,
|
|
12
|
+
UNKNOWN,
|
|
13
|
+
DEFAULT_DB_TIMEOUT,
|
|
14
|
+
)
|
|
15
|
+
from py_backend_analytics.db.models import AnalyticsSummaryFields as F
|
|
16
|
+
from py_backend_analytics.models import RequestInfo
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SQLiteDBClient(AbstractDBClient):
|
|
20
|
+
async def insert_request_info(self, model: RequestInfo):
|
|
21
|
+
async with self._get_connection() as conn:
|
|
22
|
+
cur = await conn.cursor()
|
|
23
|
+
await cur.execute(
|
|
24
|
+
f"""INSERT INTO {DB_TABLE_NAME}
|
|
25
|
+
({DBColumns.location}, {DBColumns.page}, {DBColumns.source}, {DBColumns.datestamp})
|
|
26
|
+
VALUES (?, ?, ?, ?)""",
|
|
27
|
+
(model.location, model.page, model.source, model.datestamp),
|
|
28
|
+
)
|
|
29
|
+
await conn.commit()
|
|
30
|
+
|
|
31
|
+
async def get_analytics_summary(self) -> dict:
|
|
32
|
+
async with self._get_connection() as conn:
|
|
33
|
+
now = datetime.now(timezone.utc)
|
|
34
|
+
|
|
35
|
+
last_month = (now - timedelta(days=30)).isoformat()
|
|
36
|
+
last_year = (now - timedelta(days=365)).isoformat()
|
|
37
|
+
|
|
38
|
+
all_time = {
|
|
39
|
+
F.TOP_COUNTRIES: await self._get_top(conn, DBColumns.location),
|
|
40
|
+
F.TOP_PAGES: await self._get_top(conn, DBColumns.page),
|
|
41
|
+
F.TOP_SOURCES: await self._get_top(conn, DBColumns.source),
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
month_stats = {
|
|
45
|
+
F.TOP_COUNTRIES: await self._get_top(
|
|
46
|
+
conn, DBColumns.location, last_month
|
|
47
|
+
),
|
|
48
|
+
F.TOP_PAGES: await self._get_top(conn, DBColumns.page, last_month),
|
|
49
|
+
F.TOP_SOURCES: await self._get_top(conn, DBColumns.source, last_month),
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
year_stats = {
|
|
53
|
+
F.TOP_COUNTRIES: await self._get_top(
|
|
54
|
+
conn, DBColumns.location, last_year
|
|
55
|
+
),
|
|
56
|
+
F.TOP_PAGES: await self._get_top(conn, DBColumns.page, last_year),
|
|
57
|
+
F.TOP_SOURCES: await self._get_top(conn, DBColumns.source, last_year),
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
recent_query = f"""
|
|
61
|
+
SELECT
|
|
62
|
+
{DBColumns.location},
|
|
63
|
+
{DBColumns.page},
|
|
64
|
+
{DBColumns.source},
|
|
65
|
+
{DBColumns.datestamp}
|
|
66
|
+
FROM {DB_TABLE_NAME}
|
|
67
|
+
ORDER BY {DBColumns.datestamp} DESC
|
|
68
|
+
LIMIT 100
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
cur = await conn.execute(recent_query)
|
|
72
|
+
recent_rows = await cur.fetchall()
|
|
73
|
+
|
|
74
|
+
recent_requests = [
|
|
75
|
+
{
|
|
76
|
+
F.LOCATION: row[0],
|
|
77
|
+
F.PAGE: row[1],
|
|
78
|
+
F.SOURCE: row[2],
|
|
79
|
+
F.DATESTAMP: row[3],
|
|
80
|
+
}
|
|
81
|
+
for row in recent_rows
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
F.ALL_TIME: all_time,
|
|
86
|
+
F.LAST_MONTH: month_stats,
|
|
87
|
+
F.LAST_YEAR: year_stats,
|
|
88
|
+
F.RECENT_REQUESTS: recent_requests,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async def _create_db_table(self):
|
|
92
|
+
async with self._get_connection() as conn:
|
|
93
|
+
cur = await conn.cursor()
|
|
94
|
+
await cur.execute(
|
|
95
|
+
f"""CREATE TABLE {DB_TABLE_NAME}(
|
|
96
|
+
{DBColumns.id} INTEGER PRIMARY KEY,
|
|
97
|
+
{DBColumns.location} TEXT NULL,
|
|
98
|
+
{DBColumns.page} TEXT NOT NULL,
|
|
99
|
+
{DBColumns.source} TEXT NULL,
|
|
100
|
+
{DBColumns.datestamp} TEXT NOT NULL
|
|
101
|
+
) STRICT"""
|
|
102
|
+
)
|
|
103
|
+
await cur.execute(
|
|
104
|
+
f"""CREATE INDEX {DBColumns.datestamp}{DB_INDEX_SUFFIX}
|
|
105
|
+
ON {DB_TABLE_NAME}({DBColumns.datestamp})"""
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
async def _db_table_exists(self) -> bool:
|
|
109
|
+
async with self._get_connection() as conn:
|
|
110
|
+
cur = await conn.cursor()
|
|
111
|
+
result = await cur.execute(
|
|
112
|
+
f"""
|
|
113
|
+
SELECT name
|
|
114
|
+
FROM sqlite_schema
|
|
115
|
+
WHERE type = 'table' AND name = '{DB_TABLE_NAME}'
|
|
116
|
+
"""
|
|
117
|
+
)
|
|
118
|
+
name = await result.fetchone()
|
|
119
|
+
return name is not None
|
|
120
|
+
|
|
121
|
+
@staticmethod
|
|
122
|
+
async def _get_top(
|
|
123
|
+
conn: aiosqlite.Connection,
|
|
124
|
+
column: str,
|
|
125
|
+
min_date: str | None = None,
|
|
126
|
+
limit: int = TOP_AGGREGATION_LIMIT,
|
|
127
|
+
) -> list[dict]:
|
|
128
|
+
query = f"""
|
|
129
|
+
SELECT {column}, COUNT(*) as count
|
|
130
|
+
FROM {DB_TABLE_NAME}
|
|
131
|
+
"""
|
|
132
|
+
params = []
|
|
133
|
+
|
|
134
|
+
if min_date:
|
|
135
|
+
query += f" WHERE {DBColumns.datestamp} >= ?"
|
|
136
|
+
params.append(min_date)
|
|
137
|
+
|
|
138
|
+
query += f"""
|
|
139
|
+
GROUP BY {column}
|
|
140
|
+
ORDER BY count DESC
|
|
141
|
+
LIMIT {limit}
|
|
142
|
+
"""
|
|
143
|
+
|
|
144
|
+
cur = await conn.execute(query, params)
|
|
145
|
+
rows = await cur.fetchall()
|
|
146
|
+
|
|
147
|
+
return [
|
|
148
|
+
{
|
|
149
|
+
F.VALUE: row[0] or UNKNOWN,
|
|
150
|
+
F.COUNT: row[1],
|
|
151
|
+
}
|
|
152
|
+
for row in rows
|
|
153
|
+
]
|
|
154
|
+
|
|
155
|
+
@asynccontextmanager
|
|
156
|
+
async def _get_connection(self):
|
|
157
|
+
connection = await aiosqlite.connect(
|
|
158
|
+
self.connection_string, timeout=DEFAULT_DB_TIMEOUT
|
|
159
|
+
)
|
|
160
|
+
try:
|
|
161
|
+
yield connection
|
|
162
|
+
finally:
|
|
163
|
+
await connection.close()
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from py_backend_analytics.constants import PACKAGE_NAME
|
|
4
|
+
|
|
5
|
+
DB_TABLE_NAME = f"{PACKAGE_NAME}_requests_info"
|
|
6
|
+
DB_INDEX_SUFFIX = "_index"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
TOP_AGGREGATION_LIMIT = 10
|
|
10
|
+
UNKNOWN = "UNKNOWN"
|
|
11
|
+
DEFAULT_DB_TIMEOUT = 60
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class DBColumns:
|
|
16
|
+
id = "id"
|
|
17
|
+
location = "location"
|
|
18
|
+
page = "page"
|
|
19
|
+
source = "source"
|
|
20
|
+
datestamp = "datestamp"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass(frozen=True)
|
|
5
|
+
class AnalyticsSummaryFields:
|
|
6
|
+
# root
|
|
7
|
+
ALL_TIME: str = "all_time"
|
|
8
|
+
LAST_MONTH: str = "last_month"
|
|
9
|
+
LAST_YEAR: str = "last_year"
|
|
10
|
+
RECENT_REQUESTS: str = "recent_requests"
|
|
11
|
+
|
|
12
|
+
# TimeRangeStats
|
|
13
|
+
TOP_COUNTRIES: str = "top_countries"
|
|
14
|
+
TOP_PAGES: str = "top_pages"
|
|
15
|
+
TOP_SOURCES: str = "top_sources"
|
|
16
|
+
|
|
17
|
+
# TopEntry
|
|
18
|
+
VALUE: str = "value"
|
|
19
|
+
COUNT: str = "count"
|
|
20
|
+
|
|
21
|
+
# RequestInfo
|
|
22
|
+
LOCATION: str = "location"
|
|
23
|
+
PAGE: str = "page"
|
|
24
|
+
SOURCE: str = "source"
|
|
25
|
+
DATESTAMP: str = "datestamp"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from py_backend_analytics.db.clients.abstract_db_client import AbstractDBClient
|
|
2
|
+
from py_backend_analytics.db.clients.sqlite_db_client import SQLiteDBClient
|
|
3
|
+
from py_backend_analytics.enums import PyBackendAnalyticsDB
|
|
4
|
+
|
|
5
|
+
DB_CLIENTS = {PyBackendAnalyticsDB.SQLITE: SQLiteDBClient}
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
async def get_db_client(
|
|
9
|
+
connection_string: str, db_type: PyBackendAnalyticsDB
|
|
10
|
+
) -> AbstractDBClient:
|
|
11
|
+
"""Unified interface for getting the correct type of DB Client"""
|
|
12
|
+
db_client_class = DB_CLIENTS.get(db_type)
|
|
13
|
+
if not db_client_class:
|
|
14
|
+
raise ValueError(f"Provided wrong DB type: {db_type}")
|
|
15
|
+
return await db_client_class.create(connection_string)
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from typing import TypeVar, Generic
|
|
3
|
+
|
|
4
|
+
from py_backend_analytics.extraction.geo_lookup import IpCountryLookup
|
|
5
|
+
from py_backend_analytics.models import RequestInfo
|
|
6
|
+
|
|
7
|
+
T = TypeVar("T")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class AbstractExtractor(ABC, Generic[T]):
|
|
11
|
+
geo_lookup: IpCountryLookup
|
|
12
|
+
|
|
13
|
+
def __init__(self):
|
|
14
|
+
self.geo_lookup = IpCountryLookup()
|
|
15
|
+
|
|
16
|
+
@abstractmethod
|
|
17
|
+
def extract(self, request: T) -> RequestInfo: ...
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from datetime import datetime, timezone
|
|
2
|
+
from starlette.routing import Match
|
|
3
|
+
|
|
4
|
+
from py_backend_analytics.extraction.extractors.abstract_extractor import (
|
|
5
|
+
AbstractExtractor,
|
|
6
|
+
T,
|
|
7
|
+
)
|
|
8
|
+
from py_backend_analytics.models import RequestInfo
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FastAPIExtractor(AbstractExtractor):
|
|
12
|
+
def extract(self, request: T) -> RequestInfo:
|
|
13
|
+
"""Extracts info from a FastAPI request. No FastAPI typing, so FastAPI is not needed at import."""
|
|
14
|
+
page = request.url.path # /users/12313 - backup
|
|
15
|
+
# best effort to get /users/{id} instead of /users/12313
|
|
16
|
+
for route in request.app.router.routes:
|
|
17
|
+
match, scope = route.matches(request.scope)
|
|
18
|
+
|
|
19
|
+
if match == Match.FULL:
|
|
20
|
+
page = scope["route"].path
|
|
21
|
+
break
|
|
22
|
+
source = request.headers.get("referer", "direct")
|
|
23
|
+
datestamp = datetime.now(timezone.utc)
|
|
24
|
+
ip = request.client.host
|
|
25
|
+
location = self.geo_lookup.get_country(ip)
|
|
26
|
+
return RequestInfo(
|
|
27
|
+
location=location, page=page, source=source, datestamp=datestamp
|
|
28
|
+
)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import gzip
|
|
2
|
+
import shutil
|
|
3
|
+
import tempfile
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from datetime import date
|
|
6
|
+
from urllib.request import urlopen
|
|
7
|
+
|
|
8
|
+
import maxminddb
|
|
9
|
+
|
|
10
|
+
from py_backend_analytics.extraction.constants import (
|
|
11
|
+
TEMP_DIR_NAME,
|
|
12
|
+
MMDB_FILE_NAME,
|
|
13
|
+
GZ_FILE_NAME,
|
|
14
|
+
DBIP_URL,
|
|
15
|
+
COUNTRY,
|
|
16
|
+
NAMES,
|
|
17
|
+
EN,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class IpCountryLookup:
|
|
22
|
+
"""
|
|
23
|
+
Installs IP DB, if it's not present, in the TEMP folder.
|
|
24
|
+
|
|
25
|
+
The DB is Taken from db-ip.com and is free. Then we use maxminddb to extract data from it.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self):
|
|
29
|
+
self._dir = Path(tempfile.gettempdir()) / TEMP_DIR_NAME
|
|
30
|
+
self._dir.mkdir(exist_ok=True)
|
|
31
|
+
self._mmdb = self._dir / MMDB_FILE_NAME
|
|
32
|
+
if not self._mmdb.exists():
|
|
33
|
+
self._download()
|
|
34
|
+
self._db = maxminddb.open_database(str(self._mmdb))
|
|
35
|
+
|
|
36
|
+
def get_country(self, ip: str) -> str | None:
|
|
37
|
+
data = self._db.get(ip)
|
|
38
|
+
if not data:
|
|
39
|
+
return None
|
|
40
|
+
return data.get(COUNTRY, {}).get(NAMES, {}).get(EN)
|
|
41
|
+
|
|
42
|
+
def _download(self):
|
|
43
|
+
url = self._url()
|
|
44
|
+
gz = self._dir / GZ_FILE_NAME
|
|
45
|
+
|
|
46
|
+
with urlopen(url) as r:
|
|
47
|
+
with open(gz, "wb") as f:
|
|
48
|
+
f.write(r.read())
|
|
49
|
+
|
|
50
|
+
with gzip.open(gz, "rb") as f_in:
|
|
51
|
+
with open(self._mmdb, "wb") as f_out:
|
|
52
|
+
shutil.copyfileobj(f_in, f_out)
|
|
53
|
+
|
|
54
|
+
def _url(self) -> str:
|
|
55
|
+
month = date.today().strftime("%Y-%m")
|
|
56
|
+
|
|
57
|
+
return DBIP_URL.format(month=month)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from typing import List, Any, Set
|
|
3
|
+
|
|
4
|
+
from py_backend_analytics.constants import PACKAGE_NAME
|
|
5
|
+
from py_backend_analytics.enums import PyBackendAnalyticsDB
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class PyBackendAnalyticsInputData:
|
|
10
|
+
db_connection_string: str
|
|
11
|
+
db_type: PyBackendAnalyticsDB = PyBackendAnalyticsDB.SQLITE
|
|
12
|
+
excluded_endpoints: List[str] = field(default_factory=list)
|
|
13
|
+
excluded_path_fragments: List[str] = field(default_factory=list)
|
|
14
|
+
excluded_path_prefixes: Set[str] = field(default_factory=set)
|
|
15
|
+
logger: Any | None = None
|
|
16
|
+
|
|
17
|
+
def __post_init__(self):
|
|
18
|
+
if not self.excluded_endpoints:
|
|
19
|
+
self.excluded_endpoints = ["/favicon.ico", "/style.css"]
|
|
20
|
+
if not self.excluded_path_fragments:
|
|
21
|
+
self.excluded_path_fragments = ["static", PACKAGE_NAME]
|
|
File without changes
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
2
|
+
from fastapi import Request
|
|
3
|
+
|
|
4
|
+
from py_backend_analytics.db.registry import get_db_client
|
|
5
|
+
from py_backend_analytics.extraction.extractors.fastapi_extractor import (
|
|
6
|
+
FastAPIExtractor,
|
|
7
|
+
)
|
|
8
|
+
from py_backend_analytics.input_data import PyBackendAnalyticsInputData
|
|
9
|
+
from py_backend_analytics.models import RequestInfo
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PyBackendAnalyticsFastAPIMiddleware(BaseHTTPMiddleware):
|
|
13
|
+
"""
|
|
14
|
+
DB is initted in the dispatch because there is no reliable way to call async method from __init__, since
|
|
15
|
+
there may already be an event loop running.
|
|
16
|
+
|
|
17
|
+
The flow:
|
|
18
|
+
1) Init DB if needed
|
|
19
|
+
2) Extract info from the request
|
|
20
|
+
3) Save the info to the DB if the request page is not excluded
|
|
21
|
+
4) On error: best effort to log the error message
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, app, input_data: PyBackendAnalyticsInputData):
|
|
25
|
+
super().__init__(app)
|
|
26
|
+
self._initialized = False
|
|
27
|
+
self._data = input_data
|
|
28
|
+
self._logger = input_data.logger
|
|
29
|
+
self._db_client = None
|
|
30
|
+
|
|
31
|
+
async def dispatch(self, request: Request, call_next):
|
|
32
|
+
try:
|
|
33
|
+
if not self._initialized:
|
|
34
|
+
await self._setup_db()
|
|
35
|
+
request_info = FastAPIExtractor().extract(request)
|
|
36
|
+
if self._should_save_request(request_info):
|
|
37
|
+
await self._db_client.insert_request_info(request_info)
|
|
38
|
+
except Exception as e:
|
|
39
|
+
self._debug(
|
|
40
|
+
f"Got an error when trying to extract info from the request: {e}"
|
|
41
|
+
)
|
|
42
|
+
return await call_next(request)
|
|
43
|
+
|
|
44
|
+
async def _setup_db(self):
|
|
45
|
+
self._db_client = await get_db_client(
|
|
46
|
+
self._data.db_connection_string, self._data.db_type
|
|
47
|
+
)
|
|
48
|
+
self._initialized = True
|
|
49
|
+
|
|
50
|
+
def _debug(self, message: str):
|
|
51
|
+
"""Best effort to log"""
|
|
52
|
+
if self._logger is not None:
|
|
53
|
+
try:
|
|
54
|
+
self._logger.debug(message)
|
|
55
|
+
except Exception:
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
def _should_save_request(self, request_info: RequestInfo):
|
|
59
|
+
page = request_info.page
|
|
60
|
+
return (
|
|
61
|
+
page not in self._data.excluded_endpoints
|
|
62
|
+
and not any(
|
|
63
|
+
page.startswith(fragment)
|
|
64
|
+
for fragment in self._data.excluded_path_prefixes
|
|
65
|
+
)
|
|
66
|
+
and not any(
|
|
67
|
+
fragment
|
|
68
|
+
for fragment in self._data.excluded_path_fragments
|
|
69
|
+
if fragment in page
|
|
70
|
+
)
|
|
71
|
+
)
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from fastapi import FastAPI
|
|
4
|
+
from starlette.requests import Request
|
|
5
|
+
from starlette.staticfiles import StaticFiles
|
|
6
|
+
from starlette.templating import Jinja2Templates, _TemplateResponse
|
|
7
|
+
|
|
8
|
+
from py_backend_analytics.constants import PACKAGE_NAME
|
|
9
|
+
from py_backend_analytics.db.registry import get_db_client
|
|
10
|
+
from py_backend_analytics.input_data import PyBackendAnalyticsInputData
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
_TEMPLATES_DIR = Path(__file__).parent / "templates"
|
|
14
|
+
_STATIC_DIR = Path(__file__).parent / "static"
|
|
15
|
+
_TEMPLATES = Jinja2Templates(directory=_TEMPLATES_DIR)
|
|
16
|
+
ANALYTICS_STATIC = PACKAGE_NAME
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def py_backend_analytics_fastapi_visualization(
|
|
20
|
+
app: FastAPI, input_data: PyBackendAnalyticsInputData, request: Request
|
|
21
|
+
) -> _TemplateResponse:
|
|
22
|
+
"""Visualizes the DB data as an HTML file, it also uses CSS."""
|
|
23
|
+
app.mount(
|
|
24
|
+
f"/{ANALYTICS_STATIC}",
|
|
25
|
+
StaticFiles(directory=_STATIC_DIR),
|
|
26
|
+
name=ANALYTICS_STATIC,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
db_client = await get_db_client(input_data.db_connection_string, input_data.db_type)
|
|
30
|
+
|
|
31
|
+
results = await db_client.get_analytics_summary()
|
|
32
|
+
|
|
33
|
+
return _TEMPLATES.TemplateResponse(
|
|
34
|
+
request=request, name="visualization.html", context=results
|
|
35
|
+
)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-backend-analytics
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: easy-to-setup network analytics for python backends
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: aiosqlite>=0.22.1
|
|
9
|
+
Requires-Dist: maxminddb>=3.1.1
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# py-backend-analytics
|
|
13
|
+
|
|
14
|
+
Easy-to-set-up python backend traffic analytics package.
|
|
15
|
+
|
|
16
|
+

|
|
17
|
+

|
|
18
|
+
|
|
19
|
+

|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
py-backend-analytics is a package that saves data about your traffic in a DB and then lets
|
|
24
|
+
you easily visualize your traffic for small to medium apps.
|
|
25
|
+
|
|
26
|
+
Key features:
|
|
27
|
+
- Set-up-once middleware that handles all the logic
|
|
28
|
+
- Saving data about your traffic: location, path/page, source, datetime
|
|
29
|
+
- Async DB client
|
|
30
|
+
- out-of-the-box visualization
|
|
31
|
+
|
|
32
|
+
Currently supported frameworks:
|
|
33
|
+
- FastAPI
|
|
34
|
+
|
|
35
|
+
Currently supported databases:
|
|
36
|
+
- SQLite
|
|
37
|
+
|
|
38
|
+
## Overview flow
|
|
39
|
+
|
|
40
|
+
You specify your data using `PyBackendAnalyticsInputData` object.
|
|
41
|
+
Then you add the middleware. The middleware will save information about each request
|
|
42
|
+
in the chosen database.
|
|
43
|
+
|
|
44
|
+
Then, you can add an endpoint that will visualize this data over time.
|
|
45
|
+
|
|
46
|
+
## Future Plans
|
|
47
|
+
|
|
48
|
+
Support for Django and Postgres is planned for the future releases.
|
|
49
|
+
|
|
50
|
+
Also, improvements for the visualization layers.
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
$ pip install py_backend_analytics
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Quickstart
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
import uvicorn
|
|
62
|
+
from fastapi import FastAPI, Request, APIRouter
|
|
63
|
+
|
|
64
|
+
from py_backend_analytics import PyBackendAnalyticsInputData, PyBackendAnalyticsFastAPIMiddleware, py_backend_analytics_fastapi_visualization
|
|
65
|
+
|
|
66
|
+
app = FastAPI()
|
|
67
|
+
my_router = APIRouter()
|
|
68
|
+
|
|
69
|
+
# Setup py_backend_analytics
|
|
70
|
+
db_string = "test1.db"
|
|
71
|
+
input_data = PyBackendAnalyticsInputData(db_string)
|
|
72
|
+
app.add_middleware(PyBackendAnalyticsFastAPIMiddleware, input_data)
|
|
73
|
+
|
|
74
|
+
# Create endpoint with visualization
|
|
75
|
+
@my_router.get("/")
|
|
76
|
+
async def get(request: Request):
|
|
77
|
+
# This will return HTML page that you can see at the top of this doc
|
|
78
|
+
return await py_backend_analytics_fastapi_visualization(app, input_data, request)
|
|
79
|
+
|
|
80
|
+
# run the ap
|
|
81
|
+
app.include_router(my_router, tags=["my_router"])
|
|
82
|
+
uvicorn.run(app, port=8080)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Usage
|
|
86
|
+
|
|
87
|
+
### Input Data
|
|
88
|
+
|
|
89
|
+
You must create a `PyBackendAnalyticsInputData` object and specify its attributes.
|
|
90
|
+
|
|
91
|
+
The only required attribute is the connection string to the DB, rest is optional.
|
|
92
|
+
|
|
93
|
+
Attributes are:
|
|
94
|
+
- `db_connection_string: str`
|
|
95
|
+
- `db_type: PyBackendAnalyticsDB` - Enum that chooses the database type, defaults to `PyBackendAnalyticsDB.SQLITE`
|
|
96
|
+
- `excluded_endpoints: List[str]` - list of excluded endpoints. defaults are: `["/favicon.ico", "/style.css"]`
|
|
97
|
+
- `excluded_path_fragments: List[str]` - excluded path fragments. defaults are: `["static", "py_backend_analytics"]`
|
|
98
|
+
- `excluded_path_prefixes: Set[str]` - excluded path prefixes. They will be excluded only if the path starts by them.
|
|
99
|
+
- `logger: Any | None` - Optional logger, used if there are any errors. Defaults to None.
|
|
100
|
+
|
|
101
|
+
DB is currently only for SQLite. It is recommended to provide an additional SQLite DB instead of the main you are using
|
|
102
|
+
for safety. The DB client will create a new table if it doesn't exist and one manual index.
|
|
103
|
+
|
|
104
|
+
### Middleware
|
|
105
|
+
|
|
106
|
+
Added middleware takes in the input data and no further setup is needed.
|
|
107
|
+
|
|
108
|
+
If there are any errors it will fail without raising exceptions; If logger is provided, it will try to log the error
|
|
109
|
+
|
|
110
|
+
### Visualization
|
|
111
|
+
|
|
112
|
+
You must provide your app, input_data and a request from inside the endpoint, and you will get an HTML template with the
|
|
113
|
+
visualization of the data in the DB.
|
|
114
|
+
|
|
115
|
+
## IP geo-location data
|
|
116
|
+
|
|
117
|
+
The geo-location data is taken from:
|
|
118
|
+
`db-ip.com` and uses `maxminddb` library to access it
|