Skip to content

Commit

Permalink
fix: fixed cli report command, adding --limit, and cleaned up query c…
Browse files Browse the repository at this point in the history
…onstruction (fixes #88)
  • Loading branch information
ErikBjare committed Jan 9, 2025
1 parent 8209103 commit 81a57df
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 45 deletions.
15 changes: 8 additions & 7 deletions aw_client/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3

import json
import logging
import textwrap
Expand Down Expand Up @@ -138,6 +139,7 @@ def query(
@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("--limit", default=10)
@click.pass_obj
def report(
obj: _Context,
Expand All @@ -146,6 +148,7 @@ def report(
start: datetime,
stop: datetime,
name: Optional[str] = None,
limit: int = 10,
):
logger.info(f"Querying between {start} and {stop}")
bid_window = f"aw-watcher-window_{hostname}"
Expand Down Expand Up @@ -182,11 +185,12 @@ def report(
print_top(
cat_events,
lambda e: " > ".join(e.data["$category"]),
title="Top categories",
title="Categories",
n=limit,
)

title_events = _parse_events(period["window"]["title_events"])
print_top(title_events, lambda e: e.data["title"], title="Top titles")
print_top(title_events, lambda e: e.data["title"], title="Titles", n=limit)

active_events = _parse_events(period["window"]["title_events"])
print(
Expand All @@ -199,11 +203,8 @@ def _parse_events(events: List[dict]) -> List[Event]:
return [Event(**event) for event in events]


def print_top(events: List[Event], key=lambda e: e.data, title="Events"):
print(
title
+ (f" (showing 10 out of {len(events)} events)" if len(events) > 10 else "")
)
def print_top(events: List[Event], key=lambda e: e.data, title="Events", n=10):
print(f"Top {n} {title}" + (f" (out of {len(events)})" if len(events) > 10 else ""))
print(
tabulate(
[
Expand Down
88 changes: 50 additions & 38 deletions aw_client/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,44 +248,56 @@ def fullDesktopQuery(
params.bid_afk = escape_doublequote(params.bid_afk)
params.bid_browsers = [escape_doublequote(bucket) for bucket in params.bid_browsers]

return (
f"""
{canonicalEvents(params)}
title_events = sort_by_duration(merge_events_by_keys(events, ["app", "title"]));
app_events = sort_by_duration(merge_events_by_keys(title_events, ["app"]));
cat_events = sort_by_duration(merge_events_by_keys(events, ["$category"]));
app_events = limit_events(app_events, {default_limit});
title_events = limit_events(title_events, {default_limit});
duration = sum_durations(events);
""" # Browser events are retrieved in canonicalQuery
+ f"""
browser_events = split_url_events(browser_events);
browser_urls = merge_events_by_keys(browser_events, ["url"]);
browser_urls = sort_by_duration(browser_urls);
browser_urls = limit_events(browser_urls, {default_limit});
browser_domains = merge_events_by_keys(browser_events, ["$domain"]);
browser_domains = sort_by_duration(browser_domains);
browser_domains = limit_events(browser_domains, {default_limit});
browser_duration = sum_durations(browser_events);
"""
+ """
RETURN = {
"events": events,
"window": {
"app_events": app_events,
"title_events": title_events,
"cat_events": cat_events,
"active_events": not_afk,
"duration": duration
},
"browser": {
"domains": browser_domains,
"urls": browser_urls,
"duration": browser_duration
}
};
"""
)
# Build the base query
query = f"""
{canonicalEvents(params)}
title_events = sort_by_duration(merge_events_by_keys(events, ["app", "title"]));
app_events = sort_by_duration(merge_events_by_keys(title_events, ["app"]));
cat_events = sort_by_duration(merge_events_by_keys(events, ["$category"]));
app_events = limit_events(app_events, {default_limit});
title_events = limit_events(title_events, {default_limit});
duration = sum_durations(events);
"""

# Add browser-related query parts if browser buckets exist
if params.bid_browsers:
query += """
browser_events = split_url_events(browser_events);
browser_urls = merge_events_by_keys(browser_events, ["url"]);
browser_urls = sort_by_duration(browser_urls);
browser_urls = limit_events(browser_urls, {default_limit});
browser_domains = merge_events_by_keys(browser_events, ["$domain"]);
browser_domains = sort_by_duration(browser_domains);
browser_domains = limit_events(browser_domains, {default_limit});
browser_duration = sum_durations(browser_events);
"""
else:
query += """
browser_events = [];
browser_urls = [];
browser_domains = [];
browser_duration = 0;
"""

# Add the return statement
query += """
RETURN = {
"events": events,
"window": {
"app_events": app_events,
"title_events": title_events,
"cat_events": cat_events,
"active_events": not_afk,
"duration": duration
},
"browser": {
"domains": browser_domains,
"urls": browser_urls,
"duration": browser_duration
}
};
"""
return query


def test_fullDesktopQuery():
Expand Down

0 comments on commit 81a57df

Please sign in to comment.