Skip to main content

Simple timeseries data type and field.

Project description

django-simple-timeseries

Serializes small, simple timeseries to a database with Django. Provides the Timeseries class for manipulating timeseries, and the TimeseriesField custom field type for serializing.

Status: Experimental.

Lint status Test status

Example

Define a TimeseriesField on a model:

from django.db import models
from django_simple_timeseries import TimeseriesField

class Appliance(models.Model):
    name = models.CharField(max_length=64)
    temperature = TimeseriesField(
        resolution_seconds=60 * 60,
        max_points=24,
        help_text='Last 24 hours of temperature data',
    )

You can then access Timeseries methods on it:

>>> a = Appliance(name='fridge')
>>> a.temperature.add(23.2)
>>> a.save()
>>> # Wait some time.
>>> a.temperature.add(26.5)
>>> a.save()
>>> print(list(a.iter_points()))
[
    (datetime(2020, 1, 1, 2, 30, 0, tzinfo=<UTC>), 23.2),
    (datetime(2020, 1, 2, 2, 30, 0, tzinfo=<UTC>), 26.5),
]

Requirements

This package supports and is tested against the latest patch versions of:

  • Python: 3.7, 3.8, 3.9
  • Django: 2.2, 3.0, 3.1
  • MariaDB: 10.2, 10.3, 10.4, 10.5
  • MySQL: 5.7, 8.0
  • Oracle: 12.2+ (only tested against 12.2.0.1 SE)
  • PostgreSQL: 9.5, 10, 11, 12
  • SQLite: 3.9.0+

All database backends are tested with the latest versions of their drivers. SQLite is also tested on GitHub Actions' latest macOS and Windows virtual environments.

Installation

pip install django_simple_timeseries

For Django 3.1 and newer, no additional dependencies are required. For earlier versions, this project requires django_jsonfield_backport:

pip install django_jsonfield_backport

How it works

Timeseries

The Timeseries class implements a simple vector-like timeseries. Timeseries data is always contiguous.

Internally, all timeseries instances have:

  • .start_time, a datetime.datetime corresponding to the first data point;
  • .data, the recorded data points (or y-values); and
  • .resolution, a timedelta which describes the fixed interval between samples.

Samples are added by calling the add() method. The add() method ensures contiguousness with the following policy:

  • If fewer than resolution seconds have elapsed since the most recent sample, the most recent sample is replaced.
  • If more than resolution seconds have elapsed since the last sample, the vector is extended by the appropriate number of samples (time_delta % resolution - 1), each which will be recorded as gaps with the value None.
  • In all cases, the vector is trimmed to no more than max_points samples.

TimeseriesField

TimeseriesField is implemented as, and extends, a JSONField. The Timeseries methods .to_object() and .from_object() serialize a Timeseries instance to and from plain python objects, which the custom field type transparently implements.

Usage Notes

This module is experimental and hasn't been exhaustively tested. It is not intended for large timeseries. Use at your own risk!

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-simple-timeseries-0.2.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

django_simple_timeseries-0.2.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file django-simple-timeseries-0.2.0.tar.gz.

File metadata

  • Download URL: django-simple-timeseries-0.2.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django-simple-timeseries-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0eec0bd0088c537d1aed6ba5e72a9b28e95135946e125f49451d530bf0a92ec4
MD5 d84d24c6137569591538a527110acb8c
BLAKE2b-256 5953cb0d8f395dd571eaf75ab29d8de7f1b7f87b8cc123f69041d8f53d417ebf

See more details on using hashes here.

File details

Details for the file django_simple_timeseries-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_simple_timeseries-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django_simple_timeseries-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8917f63a506d8ba1fcdfe8a62a0e819325163ccadd1cad75880a01861b6149eb
MD5 06387aef7c6f9691e0c1f581ddcd835b
BLAKE2b-256 b12586a778a5dc27be320ffdea76db0409893d13a021abdb233d739d298c0475

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