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

fix: use server categories in cli report #89

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
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
22 changes: 10 additions & 12 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 All @@ -12,7 +13,7 @@
import aw_client

from . import queries
from .classes import default_classes
from .classes import default_classes, get_classes

now = datetime.now(timezone.utc)
td1day = timedelta(days=1)
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 All @@ -158,10 +161,7 @@ def report(

bid_browsers: List[str] = []

# TODO: Allow loading from toml
logger.info("Using default classes")
classes = default_classes

classes = get_classes()
params = queries.DesktopQueryParams(
bid_browsers=bid_browsers,
classes=classes,
Expand All @@ -185,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 @@ -202,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 @@ -247,44 +247,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
Loading