Skip to content

Commit

Permalink
add support for quarters
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Feb 23, 2021
1 parent dd1f3c4 commit 17b4cb0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 12 additions & 7 deletions admin_tools_stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@
('days', 'Days'),
('weeks', 'Weeks'),
('months', 'Months'),
('quarters', 'Quarters'),
('years', 'Years'),
)
freqs = {
'years': YEARLY,
'months': MONTHLY,
'weeks': WEEKLY,
'days': DAILY,
'hours': HOURLY
rrule_freqs = {
'years': {'freq': YEARLY},
'quarters': {'freq': MONTHLY, 'interval': 3},
'months': {'freq': MONTHLY},
'weeks': {'freq': WEEKLY},
'days': {'freq': DAILY},
'hours': {'freq': HOURLY},
}


Expand All @@ -94,6 +96,9 @@ def truncate(dt, interval):
return day - relativedelta(weekday=MO(-1))
elif interval == 'months':
return datetime.datetime(dt.year, dt.month, 1, tzinfo=dt.tzinfo)
elif interval == 'quarters':
qmonth = dt.month - (dt.month - 1) % 3
return datetime.datetime(dt.year, qmonth, 1, tzinfo=dt.tzinfo)
elif interval == 'years':
return datetime.datetime(dt.year, 1, 1, tzinfo=dt.tzinfo)

Expand Down Expand Up @@ -455,7 +460,7 @@ def get_multi_time_series(self, configuration, time_since, time_until, interval,
# fill with zeros where the records are missing
start = truncate(time_since, interval)

dates = list(rrule(freq=freqs[interval], dtstart=start, until=time_until))
dates = list(rrule(**rrule_freqs[interval], dtstart=start, until=time_until))
for time in dates:
if self.get_date_field().__class__ == DateField:
time = time.date()
Expand Down
2 changes: 2 additions & 0 deletions admin_tools_stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AdminChartsView(TemplateView):

interval_dateformat_map_bar_chart = {
'years': ("%Y", "%Y"),
'quarters': ("%b %Y", "%b"),
'months': ("%b %Y", "%b"),
'weeks': ("%a %d %b %Y", "%W"),
'days': ("%a %d %b %Y", "%a"),
Expand All @@ -28,6 +29,7 @@ class AdminChartsView(TemplateView):

interval_dateformat_map = {
'years': ("%Y", "%Y"),
'quarters': ("%b %Y", "%b %Y"),
'months': ("%b %Y", "%b %Y"),
'weeks': ("%W (%d %b %Y)", "%W"),
'days': ("%a %d %b %Y", "%d %b %Y"),
Expand Down

0 comments on commit 17b4cb0

Please sign in to comment.