Skip to content

Commit

Permalink
Add option to control the order of the days in the log output (#369)
Browse files Browse the repository at this point in the history
* Add option to control the order of the days in the `log` output
* Add `--reverse/--no-reverse` flag to log
* Test sorted_groupby for reverse log flag
* Move reverse-log changelog entry to unreleased
* Also fix previous changelog typo
  • Loading branch information
joelostblom authored Jun 18, 2020
1 parent 2e973c2 commit ac8a23c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Log output order can now be controlled via the `--reverse/--no-reverse` flag
and the `reverse_log` configuration option (#369)

### Changed

- Require latest Arrow version 0.15.6 to support ISO week dates (#380)
Expand All @@ -28,7 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Stylize prompt to create new project or tag (#310).
- Aggregate calculates wrong time if used with `--current` (#293)
- The `start` command now correctly checks if project is empty (#322)
- Aggregate ignores frames that crosses aggreagate boundary (#248)
- Aggregate ignores frames that crosses aggregate boundary (#248)
- The `report` and `aggregate` commands with `--json` option now correctly
encode Arrow objects (#329)

Expand Down
8 changes: 8 additions & 0 deletions docs/user-guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ If `true`, the output of the `report` command will include the currently
running frame (if any) by default. The option can be overridden on the
command line with the `-c/-C` resp. `--current/--no-current` flags.

#### `options.reverse_log` (default: `true`)

If `true`, the output of the `log` command will reverse the order of the days
to display the latest day's entries on top and the oldest day's entries at the
bottom. The option can be overridden on the command line with the `-r/-R` resp.
`--reverse/--no-reverse` flags.

#### `options.stop_on_start` (default: `false`)

If `true`, starting a new project will stop running projects:
Expand Down Expand Up @@ -230,6 +237,7 @@ week_start = monday
log_current = false
pager = true
report_current = false
reverse_log = true
```

## Application folder
Expand Down
28 changes: 28 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import json
import os
import datetime
import operator

try:
from StringIO import StringIO
Expand All @@ -32,6 +33,7 @@
get_start_time_for_period,
make_json_writer,
safe_save,
sorted_groupby,
parse_tags,
PY2,
json_arrow_encoder,
Expand Down Expand Up @@ -270,6 +272,32 @@ def test_build_csv_multiple_cols():
assert build_csv(data) == result


# sorted_groupby

def test_sorted_groupby(watson):
end = arrow.utcnow()
watson.add('foo', end.shift(hours=-25), end.shift(hours=-24), ['A'])
watson.add('bar', end.shift(hours=-1), end, ['A'])

result = list(sorted_groupby(
watson.frames,
operator.attrgetter('day'),
reverse=False))
assert result[0][0] < result[1][0]


def test_sorted_groupby_reverse(watson):
end = arrow.utcnow()
watson.add('foo', end.shift(hours=-25), end.shift(hours=-24), ['A'])
watson.add('bar', end.shift(hours=-1), end, ['A'])

result = list(sorted_groupby(
watson.frames,
operator.attrgetter('day'),
reverse=True))
assert result[0][0] > result[1][0]


# frames_to_csv

def test_frames_to_csv_empty_data(watson):
Expand Down
12 changes: 9 additions & 3 deletions watson/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,8 @@ def aggregate(ctx, watson, current, from_, to, projects, tags, output_format,
@cli.command()
@click.option('-c/-C', '--current/--no-current', 'current', default=None,
help="(Don't) include currently running frame in output.")
@click.option('-r/-R', '--reverse/--no-reverse', 'reverse', default=None,
help="(Don't) reverse the order of the days in output.")
@click.option('-f', '--from', 'from_', type=DateTime,
default=arrow.now().shift(days=-7),
help="The date from when the log should start. Defaults "
Expand Down Expand Up @@ -908,8 +910,8 @@ def aggregate(ctx, watson, current, from_, to, projects, tags, output_format,
help="(Don't) view output through a pager.")
@click.pass_obj
@catch_watson_error
def log(watson, current, from_, to, projects, tags, year, month, week, day,
luna, all, output_format, pager):
def log(watson, current, reverse, from_, to, projects, tags, year, month, week,
day, luna, all, output_format, pager):
"""
Display each recorded session during the given timespan.
Expand Down Expand Up @@ -986,6 +988,9 @@ def log(watson, current, from_, to, projects, tags, year, month, week, day,
watson.frames.add(cur['project'], cur['start'], arrow.utcnow(),
cur['tags'], id="current")

if reverse is None:
reverse = watson.config.getboolean('options', 'reverse_log', True)

span = watson.frames.span(from_, to)
filtered_frames = watson.frames.filter(
projects=projects or None, tags=tags or None, span=span
Expand All @@ -1001,7 +1006,8 @@ def log(watson, current, from_, to, projects, tags, year, month, week, day,

frames_by_day = sorted_groupby(
filtered_frames,
operator.attrgetter('day'), reverse=True
operator.attrgetter('day'),
reverse=reverse
)

lines = []
Expand Down

0 comments on commit ac8a23c

Please sign in to comment.