Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different Aggregation periods #496

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions watson/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def _final_print(lines):
pass

# handle special title formatting for aggregate reports
if aggregated:
if report['timespan']['from'] == report['timespan']['to']:
_print('{} - {}'.format(
style('date', '{:ddd DD MMMM YYYY}'.format(
report['timespan']['from']
Expand Down Expand Up @@ -775,6 +775,15 @@ def _final_print(lines):
mutually_exclusive=_SHORTCUT_OPTIONS,
help="The date at which the report should stop (inclusive). "
"Defaults to tomorrow.")
@click.option('-d', '--daily', 'aggregation', cls=MutuallyExclusiveOption,
flag_value='daily', mutually_exclusive=['weekly', 'monthly'],
help="Aggregate time frames by day (default)")
@click.option('-w', '--weekly', 'aggregation', cls=MutuallyExclusiveOption,
flag_value='weekly', mutually_exclusive=['daily', 'monthly'],
help="Aggregate time frames by week")
@click.option('-m', '--monthly', 'aggregation', cls=MutuallyExclusiveOption,
flag_value='monthly', mutually_exclusive=['daily', 'weekly'],
help="Aggregate time frames by month")
@click.option('-p', '--project', 'projects', shell_complete=get_projects,
multiple=True,
help="Reports activity only for the given project. You can add "
Expand All @@ -799,7 +808,7 @@ def _final_print(lines):
@click.pass_context
@catch_watson_error
def aggregate(ctx, watson, current, from_, to, projects, tags, output_format,
pager):
pager, aggregation):
"""
Display a report of the time spent on each project aggregated by day.

Expand Down Expand Up @@ -869,14 +878,31 @@ def aggregate(ctx, watson, current, from_, to, projects, tags, output_format,
2018-11-21 00:00:00,2018-11-21 23:59:59,watson,,77.0
2018-11-21 00:00:00,2018-11-21 23:59:59,watson,docs,77.0
"""
delta = (to - from_).days
lines = []

for i in range(delta + 1):
offset = datetime.timedelta(days=i)
from_offset = from_ + offset
output = ctx.invoke(report, current=current, from_=from_offset,
to=from_offset, projects=projects, tags=tags,
aggregation_ranges = []

if aggregation == 'monthly':
start = from_.replace(day=1)
while start.month <= to.month:
next_month = (start + datetime.timedelta(days=32)).replace(day=1)
end = next_month - datetime.timedelta(days=1)
aggregation_ranges.append((start, end))
start = next_month
elif aggregation == 'weekly':
start = from_ - datetime.timedelta(days=from_.weekday())
while start <= to:
end = start + datetime.timedelta(days=6)
aggregation_ranges.append((start, end))
start += datetime.timedelta(days=7)
else: # daily
delta = (to - from_).days
for i in range(delta + 1):
offset = datetime.timedelta(days=i)
aggregation_ranges.append((from_ + offset, from_ + offset))

for start, end in aggregation_ranges:
output = ctx.invoke(report, current=current, from_=start,
to=end, projects=projects, tags=tags,
output_format=output_format,
pager=pager, aggregated=True,
include_partial_frames=True)
Expand Down