/django-live-dashboard

Shows current requests to a Django application. Inspired by Phoenix's LiveDashboard.

Primary LanguageHTML

django_live_dashboard

Inspired by Phoenix's LiveView Dashboard. Uses redis pub-sub, chart.js, alpine.js, and websockets for streaming realtime updates of all current requests to a Django application.

Requires

  1. Python 3.6+
  2. Django 3.0+
  3. Django to be run via ASGI for websockets support

Install

  1. pip install django-live-dashboard or poetry add django-live-dashboard
  2. Update current asgi.py or use the asgi.py file in the repo as an example
  3. Add "django_live_dashboard", to INSTALLED_APPS in settings file
  4. Add "django_live_dashboard.middleware.DjangoLiveDashboardMiddleware", to beginning of the MIDDLEWARE list in settings file
  5. Add DJANGO_LIVE_DASHBOARD configuration to settings file (see details below)
  6. Add to default urls.py: path("admin/", include(django_live_dashboard_urls))
  7. Run your server with something like uvicorn project.asgi:application --port=8000

Settings

Configuration is in a DJANGO_LIVE_DASHBOARD setting.

Example

DJANGO_LIVE_DASHBOARD = {
    "ENABLED": True,
    "WEBSOCKET_HOST": 'localhost:8000',
    "TOTAL_TIME_CUTOFF": 0.5,
    "REDIS": {
        "HOST": "localhost",
        "PORT": 6379,
        "DB": 0,
        "PUBSUB_CHANNEL": "django_live_dashboard:stats"
    },
    "CHART": {
        "REFRESH": 1000,
        "DELAY": 1000,
        "DURATION": 100000,
    }
}

ENABLED

Whether any statistics should be logged. Defaults to DEBUG.

WEBSOCKET_HOST

The host that the websocket should listen to. Defaults to localhost.

TOTAL_TIME_CUTOFF

How long a request should take in seconds before getting logged. For example, if the value is 0.75, any request that takes longer than three quarters of a second will get graphed. Defaults to 0.5.

REDIS

Redis configuration with standard HOST, PORT, and DB settings in a dictionary.

HOST

The host of the redis server. Defaults to localhost.

PORT

THe port of the redis server. Defaults to 6379.

DB

The database of the redis server. Defaults to 0.

CHART

Configuration for the streaming chart.js plugin.

DELAY

Delay in milliseconds of the chart.js which helps to make the drawing more fluid and smooth. Defaults to 1000.

DURATION

Time duration in milliseconds of how much data should be charted. Defaults to 100000.

REFRESH

How often the chart should try to refresh its data. Defaults to 1000.

Example Django project

Found in the example_project directory in this repo.

How to run

  1. redis-server
  2. poetry install
  3. poetry run example_project/manage.py migrate
  4. poetry run example_project/manage.py createsuperuser
  5. poetry run uvicorn example_project.project.asgi:application
  6. Go to http://localhost:8000/monitoring/
  7. Open up http://localhost:8000/admin/ in a new tab; go back to the first tab and you should see some updated information