Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flask-statistics

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flask-statistics

Package to collect statistics in Flask.

  • 1.0.2
  • PyPI
  • Socket score

Maintainers
1

Flask-Statistics

Flask-Statistics is an extensions that collects data from requests send to the server to then display statistics like response-time, hits, unique hits, etc.

Installation

You can install the extensions with pip:

$ pip install flask-statistics

Usage

Below is a small example on how to use the extension.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_statistics import Statistics

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"

db = SQLAlchemy(app)

class Request(db.Model):
    __tablename__ = "request"

    index = db.Column(db.Integer, primary_key=True, autoincrement=True)
    response_time = db.Column(db.Float)
    date = db.Column(db.DateTime)
    method = db.Column(db.String)
    size = db.Column(db.Integer)
    status_code = db.Column(db.Integer)
    path = db.Column(db.String)
    user_agent = db.Column(db.String)
    remote_address = db.Column(db.String)
    exception = db.Column(db.String)
    referrer = db.Column(db.String)
    browser = db.Column(db.String)
    platform = db.Column(db.String)
    mimetype = db.Column(db.String)

statistics = Statistics(app, db, Request)

@app.route("/")
def index():
    return "Hello World!"

if  __name__ == "__main__":
    app.run()

The script above sets up a basic SQLAlchemy Database. When initalizing the statistics extensions, you will have to provide a model (here: Request) to store the data in.

Now, whenever a user requests an endpoint, it will be stored in the defined model.

If you are in debug mode and an exception occures, teardown of the request will not happen and therefore it won't be stored in the database. If you want to teardown it by force, set PRESERVE_CONTEXT_ON_EXCEPTION to False in the config.

Things that are stored

KeyTypeDescription
response_timefloatThe time it took the server to process the request.
datedatetime.datetimeThe date the request was send to the server (in UTC).
methodstrThe HTTP method that was used (e.g. GET, POST, ..).
sizeintThe body size in bytes that was send to the client.
status_codeintThe status code that was returned by the request.
user_agentstrThe User-Agent that was send with the request.
remote_addressstrThe ip address of the client.
exceptionstrIf an error occured, this field will have the error message and the status_code will automatically be 500.
Example: ZeroDivisionError('division by zero')
referrerstrLink to the website that referred the user to the endpoint.
browserstrThe browser that was used to send the request.
Example: firefox 72.0
platformstrOperating System the request was send from.
mimetypestrMimetype of the response send to the client (e.g. html/text).

View the statistics

Flask-Statistics will add a blueprint with the prefix ../statistics to your app.

preview_image

Config

NameTypeDescriptionDefault
STATISTICS_DEFAULT_DATE_SPANdatetime.timedeltaIf no date was selected, it will be set to today as end and today - option as start.datetime.timedelta(days=7)

Protect the route

Flask-Statistics accepts a fourth argument: A function that is called before any requests to the statistics blueprint.

...

def check_if_user_is_allowed():
    # check if user is allowed to
    # e.g. JWT Token, Flask Login etc.
    # ...
    if not allowed:
        return redirect(url_for("index"))

statistics = Statistics(app, db, Request, check_if_user_is_allowed)

There you can check if the user has the required permissions to view the statistics. If not, redirect him to another page.

Disable statistics

If a user has opt-out, request record can be disabled with custom function disable_f.

statistics = Statistics(app, db, Request, disable_f= lambda: current_user.has_optout() )

or

def has_disable_cookie():
    return request.cookies.get('disable_stat') == 'True'

statistics = Statistics(app, db, Request, disable_f=has_disable_cookie )

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc