cleanerversion icon indicating copy to clipboard operation
cleanerversion copied to clipboard

Enable interval/time range querying

Open maennel opened this issue 9 years ago • 2 comments

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.

maennel avatar Jul 22 '16 12:07 maennel

@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 avatar Aug 25 '16 09:08 brki

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

brki avatar Aug 30 '16 07:08 brki