Skip to main content

easy-to-setup network analytics for python backends

Project description

py-backend-analytics

Easy-to-set-up python backend traffic analytics package.

CI PyPI

img.png


py-backend-analytics is a package that saves data about your traffic in a DB and then lets you easily visualize your traffic for small to medium apps.

Key features:

  • Set-up-once middleware that handles all the logic
  • Saving data about your traffic: location, path/page, source, datetime
  • Async DB client
  • out-of-the-box visualization

Currently supported frameworks:

  • FastAPI

Currently supported databases:

  • SQLite

Overview flow

You specify your data using PyBackendAnalyticsInputData object. Then you add the middleware. The middleware will save information about each request in the chosen database.

Then, you can add an endpoint that will visualize this data over time.

Future Plans

Support for Django and Postgres is planned for the future releases.

Also, improvements for the visualization layers.

Installation

$ pip install py_backend_analytics

Quickstart

import uvicorn
from fastapi import FastAPI, Request, APIRouter

from py_backend_analytics import PyBackendAnalyticsInputData, PyBackendAnalyticsFastAPIMiddleware, py_backend_analytics_fastapi_visualization

app = FastAPI()
my_router = APIRouter()

# Setup py_backend_analytics
db_string = "test1.db"
input_data = PyBackendAnalyticsInputData(db_string)
app.add_middleware(PyBackendAnalyticsFastAPIMiddleware, input_data)

# Create endpoint with visualization
@my_router.get("/")
async def get(request: Request):
    # This will return HTML page that you can see at the top of this doc
    return await py_backend_analytics_fastapi_visualization(app, input_data, request)

# run the ap
app.include_router(my_router, tags=["my_router"])
uvicorn.run(app, port=8080)

Usage

Input Data

You must create a PyBackendAnalyticsInputData object and specify its attributes.

The only required attribute is the connection string to the DB, rest is optional.

Attributes are:

  • db_connection_string: str
  • db_type: PyBackendAnalyticsDB - Enum that chooses the database type, defaults to PyBackendAnalyticsDB.SQLITE
  • excluded_endpoints: List[str] - list of excluded endpoints. defaults are: ["/favicon.ico", "/style.css"]
  • excluded_path_fragments: List[str] - excluded path fragments. defaults are: ["static", "py_backend_analytics"]
  • excluded_path_prefixes: Set[str] - excluded path prefixes. They will be excluded only if the path starts by them.
  • logger: Any | None - Optional logger, used if there are any errors. Defaults to None.

DB is currently only for SQLite. It is recommended to provide an additional SQLite DB instead of the main you are using for safety. The DB client will create a new table if it doesn't exist and one manual index.

Middleware

Added middleware takes in the input data and no further setup is needed.

If there are any errors it will fail without raising exceptions; If logger is provided, it will try to log the error

Visualization

You must provide your app, input_data and a request from inside the endpoint, and you will get an HTML template with the visualization of the data in the DB.

IP geo-location data

The geo-location data is taken from: db-ip.com and uses maxminddb library to access it

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_backend_analytics-0.3.0.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_backend_analytics-0.3.0-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file py_backend_analytics-0.3.0.tar.gz.

File metadata

  • Download URL: py_backend_analytics-0.3.0.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_backend_analytics-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a03a06388d1fb3786490fb33c76dac00366283f22c471c19d95386345bfd5fd3
MD5 7c20ff24c7122354cbe6313d898a8868
BLAKE2b-256 d9c6006f3f33247d6ea30276f2daae51f1897ef3e38edd3a32e3ae681bb2e7fb

See more details on using hashes here.

File details

Details for the file py_backend_analytics-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: py_backend_analytics-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_backend_analytics-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f76e1a31d059d3b0d87d5757f5c80090c0cf98f2459e7467cba9a787060aa563
MD5 8cafb6cd5b319c3a6eac5f422ae1598c
BLAKE2b-256 0f348584a05cf0a0c8a4b076eae9f211425df14dd93d9bea9c8965e8036e4bc0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page