Skip to main content

Django Datawatch runs automated data checks in your Django installation

Project description

PyPI version GitHub build status Coverage Status Open Source Love MIT Licence

Django Datawatch

With Django Datawatch you are able to implement arbitrary checks on data, review their status and even describe what to do to resolve them. Think of nagios/icinga for data.

Check execution backends

Synchronous

Will execute all tasks synchronously which is not recommended but the most simple way to get started.

Celery

Will execute the tasks asynchronously using celery as a task broker and executor. Celery is supported from 3.1.25.

Other backends

Feel free to implement other task execution backends and send a pull request.

Install

$ pip install django-datawatch

Add django_datawatch to your INSTALLED_APPS

Celery beat database scheduler

If the datawatch scheduler should be run using the celery beat database scheduler, you need to install django_celery_beat for celery >= 4 or django-celery for celery < 4.

Add django_datawatch.tasks.django_datawatch_scheduler to the CELERYBEAT_SCHEDULE of your app. This task should be executed every minute e.g. crontab(minute='*/1'), see example app.

Write a custom check

Create checks.py inside your module.

from datetime import datetime

from celery.schedules import crontab

from django_datawatch.datawatch import datawatch
from django_datawatch.base import BaseCheck, CheckResponse
from django_datawatch.models import Result


@datawatch.register
class CheckTime(BaseCheck):
    run_every = crontab(minute='*/5')  # scheduler will execute this check every 5 minutes

    def generate(self):
        yield datetime.now()

    def check(self, payload):
        response = CheckResponse()
        if payload.hour <= 7:
            response.set_status(Result.STATUS.ok)
        elif payload.hour <= 12:
            response.set_status(Result.STATUS.warning)
        else:
            response.set_status(Result.STATUS.critical)
        return response

    def get_identifier(self, payload):
        # payload will be our datetime object that we are getting from generate method
        return payload

    def get_payload(self, identifier):
        # as get_identifier returns the object we don't need to process it
        # we can return identifier directly
        return identifier

.generate

Must yield payloads to be checked. The check method will then be called for every payload.

.check

Must return an instance of CheckResponse.

.get_identifier

Must return a unique identifier for the payload.

trigger check updates

Check updates for individual payloads can also be triggered when related datasets are changed. The map for update triggers is defined in the Check class' trigger_update attribute.

trigger_update = dict(subproduct=models_customer.SubProduct)

The key is a slug to define your trigger while the value is the model that issues the trigger when saved. You must implement a resolver function for each entry with the name of get__payload which returns the payload to check (same datatype as .check would expect or .generate would yield).

def get_subproduct_payload(self, instance):
    return instance.product

Exceptions

DatawatchCheckSkipException

raise this exception to skip current check. The result will not appear in the checks results.

Run your checks

A management command is provided to queue the execution of all checks based on their schedule. Add a crontab to run this command every minute and it will check if there's something to do.

$ ./manage.py datawatch_run_checks
$ ./manage.py datawatch_run_checks --slug=example.checks.UserHasEnoughBalance

Refresh your check results

A management command is provided to forcefully refresh all existing results for a check. This comes in handy if you changes the logic of your check and don't want to wait until the periodic execution or an update trigger.

$ ./manage.py datawatch_refresh_results
$ ./manage.py datawatch_refresh_results --slug=example.checks.UserHasEnoughBalance

Get a list of registered checks

$ ./manage.py datawatch_list_checks

Clean up your database

Remove the unnecessary check results and executions if you've removed the code for a check.

$ ./manage.py datawatch_clean_up

Settings

DJANGO_DATAWATCH_BACKEND = 'django_datawatch.backends.synchronous'
DJANGO_DATAWATCH_RUN_SIGNALS = True

DJANGO_DATAWATCH_BACKEND

You can chose the backend to run the tasks. Supported are 'django_datawatch.backends.synchronous' and 'django_datawatch.backends.celery'.

Default: 'django_datawatch.backends.synchronous'

DJANGO_DATAWATCH_RUN_SIGNALS

Use this setting to disable running post_save updates during unittests if required.

Default: True

celery task queue

Datawatch supported setting a specific queue in release < 0.4.0

With the switch to celery 4, you should use task routing to define the queue for your tasks, see http://docs.celeryproject.org/en/latest/userguide/routing.html

CONTRIBUTE

Dev environment

  • docker (at least 17.12.0+)
  • docker-compose (at least 1.18.0)

Please make sure that no other container is using port 8000 as this is the one you're install gets exposed to: http://localhost:8000/

Setup

We've included an example app to show how django_datawatch works. Start by launching the included docker container.

docker-compose up -d

Then setup the example app environment.

docker-compose run --rm django migrate
docker-compose run --rm django loaddata example

The installed superuser is "example" with password "datawatch".

Run checks

Open http://localhost:8000/, log in and then go back to http://localhost:8000/. You'll be prompted with an empty dashboard. That's because we didn't run any checks yet. Let's enqueue an update.

docker-compose run --rm django datawatch_run_checks --force

The checks for the example app are run synchronously and should be updated immediately. If you decide to switch to the celery backend, you should now start a celery worker to process the checks.

docker-compose run --rm --entrypoint=celery django worker -A example -l DEBUG

To execute the celery beat scheduler which runs the datawatch scheduler every minute, just run:

docker-compose run --rm --entrypoint=celery django beat --scheduler django_celery_beat.schedulers:DatabaseScheduler -A example

You will see some failed check now after you refreshed the dashboard view.

Django Datawatch dashboard

Run the tests

docker-compose run --rm django test

Requirements upgrades

Check for upgradeable packages by running

docker-compose up -d
docker-compose exec django pip-check

Translations

Collect and compile translations for all registered locales

docker-compose run --rm django makemessages --no-location --all
docker-compose run --rm django compilemessages

Making a new release

bumpversion is used to manage releases.

Add your changes to the CHANGELOG, run

docker-compose exec django bumpversion <major|minor|patch>

then push (including tags).

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

django-datawatch-2.2.5.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

django_datawatch-2.2.5-py3-none-any.whl (35.5 kB view details)

Uploaded Python 3

File details

Details for the file django-datawatch-2.2.5.tar.gz.

File metadata

  • Download URL: django-datawatch-2.2.5.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for django-datawatch-2.2.5.tar.gz
Algorithm Hash digest
SHA256 7e03cd1a0b76eaddad85b130e187bbb75477beca0bae7d559ea2d016354ee636
MD5 65b1b5fb84e38c00ef2994d16b966e42
BLAKE2b-256 6543570a53f70b74662f8c6750020eb1b296ca70bbc838891d44407f39a277b6

See more details on using hashes here.

File details

Details for the file django_datawatch-2.2.5-py3-none-any.whl.

File metadata

  • Download URL: django_datawatch-2.2.5-py3-none-any.whl
  • Upload date:
  • Size: 35.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for django_datawatch-2.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 751388197c99ac77c0ae0c06a6f3ab1ea6cba8ff783f5ca26834c349a826ba87
MD5 083659f3b194ef6a901578b6421d5584
BLAKE2b-256 4b71d8e5e0732e1424770a48f8fdeade777e0192b5558e8d48ac03b184f1af04

See more details on using hashes here.

Supported by

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