Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Enable interval/time range querying #118

Open
maennel opened this issue Jul 22, 2016 · 2 comments
Open

Enable interval/time range querying #118

maennel opened this issue Jul 22, 2016 · 2 comments

Comments

@maennel
Copy link
Contributor

maennel commented Jul 22, 2016

Queries for objects during a time interval should be possible.

Right now, objects at a specific point in time can be queried. If one needs objects that lived during a specific time interval, it gets difficult. CleanerVersion should help to do this by adding the following query possibility on VersionedManager and VersionedQuerySet:

Model.objects.in_interval(start, end, unique=<bool>, inclusive=<bool>).filter(...)

Arguments are:

  • start: a timestamp indicating the start of the time interval
  • end: a timestamp indicating the end of the time interval
  • unique: a boolean; if True, return the latest valid version of an object during a time interval; if False, return all versions of an object during a time interval; Default: False
  • inclusive: a boolean: if True, versions that either start or end (or both) during a time interval are considered; if False, only versions that start AND end during a time interval are considered; Default: True

The return value is, as usual an object of type VersionedQuerySet.

Some more details:
inclusive set to True translates to the filtering expression version_start_date < interval_end_date AND version_end_date > interval_start_date, i.e. a version is part of the resulting QuerySet if it starts, ends or exists only during the interval.
inclusive set to False translates to the filtering expression version_start_date >= interval_start_date AND version_start_date < interval_end_date AND version_end_date > interval_start_date AND version_end_date <= interval_end_date, i.e. a version is part of the resulting QuerySet if it starts AND ends during the specified time interval.

Related objects (either O2M, M2O or M2M) should also be limited by the above mentioned filters.

@brki
Copy link
Contributor

brki commented Aug 25, 2016

@maennel I'm not sure I understand the value of having the inclusive parameter. What's the use case for that?

There are these possible cases:

  1. version ended before or started after the interval
  2. version started before and ended after the interval (or never ended)
  3. version started and ended during the interval
  4. version started before and ended during the interval
  5. version started during and ended after the interval.

For me, it would make sense to include cases 2, 3, 4, and 5.

brki added a commit to brki/cleanerversion that referenced this issue Aug 25, 2016
brki added a commit to brki/cleanerversion that referenced this issue Aug 30, 2016
@brki
Copy link
Contributor

brki commented Aug 30, 2016

Current status on brki/cleanerversion/interval-querying:

Works for queried models, including with the unique parameter for in_interval(). The unique parameter only works for Django 1.8+. Since Django 1.7 has been at EOL for quite a while already, this okay imho.

Still TODO:

  • make it work for relations
  • more testing (relations / complex queries with model spanning lookups, etc.)
  • tox testing (to cover python / django versions)
  • clarify use case for inclusive parameter

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants