Skip to content
Merged
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
59 changes: 40 additions & 19 deletions aw_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,20 @@ def print_top(events: List[Event], key=lambda e: e.data, title="Events", n=10):
@click.option("--cache", is_flag=True)
@click.option("--start", default=now - td1day, type=click.DateTime())
@click.option("--stop", default=now + td1yr, type=click.DateTime())
@click.option(
"--format",
type=click.Choice(["table", "json"]),
default="table",
help="Output format (table or json)",
)
@click.pass_obj
def canonical(
obj: _Context,
hostname: str,
cache: bool,
start: datetime,
stop: datetime,
format: str,
name: Optional[str] = None,
):
logger.info(f"Querying between {start} and {stop}")
Expand Down Expand Up @@ -270,29 +277,43 @@ def canonical(

# TODO: Print titles, apps, categories, with most time
for period in result:
print()
events = _parse_events(period)
print(f"Showing last 10 out of {len(events)} events:")

print(
tabulate(
[
(
str(e.timestamp).split(".")[0],
str(e.duration).split(".")[0],
f'[{e.data["app"]}] {textwrap.shorten(e.data["title"], 60, placeholder="...")}',
)
for e in events[-10:]
],
headers=["Timestamp", "Duration", "Data"],
if format == "json":
# Output as JSON array with ISO-8601 timestamps
json_events = [
{
"timestamp": e.timestamp.isoformat(),
"duration": e.duration.total_seconds(),
"data": e.data,
}
for e in events
]
print(json.dumps(json_events))
else:
# Table format (original behavior)
print()
print(f"Showing last 10 out of {len(events)} events:")

print(
tabulate(
[
(
str(e.timestamp).split(".")[0],
str(e.duration).split(".")[0],
f'[{e.data["app"]}] {textwrap.shorten(e.data["title"], 60, placeholder="...")}',
)
for e in events[-10:]
],
headers=["Timestamp", "Duration", "Data"],
)
)
)

print()
print(
"Total duration:\t",
timedelta(seconds=sum(e["duration"] for e in period)),
)
print()
print(
"Total duration:\t",
timedelta(seconds=sum(e["duration"] for e in period)),
)


if __name__ == "__main__":
Expand Down
Loading