From 49fc3b02ff87ce2e92518a0705e8fdaeeb800366 Mon Sep 17 00:00:00 2001 From: Bob Date: Tue, 24 Nov 2026 11:09:30 +0000 Subject: [PATCH 1/2] docs(query): make the query snippets run as written - drop // comments: both servers reject them with a 400 parse error (Python accepts no comments, aw-server-rust only #) - __CATEGORIES__ is substituted only by the web UI Query page; via /api/0/query/ it is an undefined variable. find_bucket is a server function, not a web UI magic variable - fix the dead query-snapshot link (renamed to queriesSnapshot.test.node.ts.snap) Git-Session-Id: bed1476c-0b54-4dde-9c03-8080d9dde1e7 --- src/examples/working-with-data.rst | 40 ++++++++++++------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/examples/working-with-data.rst b/src/examples/working-with-data.rst index 3e797fa..ea65d95 100644 --- a/src/examples/working-with-data.rst +++ b/src/examples/working-with-data.rst @@ -65,7 +65,7 @@ For more advanced usage, see the actual canonical query implementations: * `Python canonical queries `_ - Production-ready functions * `Web UI implementation `_ - TypeScript version used by the interface -* `Query examples `_ - Human-readable query snapshots +* `Query examples `_ - Human-readable query snapshots Comprehensive Examples @@ -97,24 +97,23 @@ For specialized analysis beyond canonical events, you can write custom queries u Query Language Basics ~~~~~~~~~~~~~~~~~~~~~~ -Queries use a simple scripting language for server-side data processing: +Queries use a simple scripting language for server-side data processing. +This one gets the window events, keeps only the periods where you were not AFK, +groups them by application and sorts them by time spent: .. code-block:: javascript - // Get events from a bucket events = query_bucket("aw-watcher-window_hostname"); - - // Filter to only active periods afk_events = query_bucket("aw-watcher-afk_hostname"); not_afk = filter_keyvals(afk_events, "status", ["not-afk"]); events = filter_period_intersect(events, not_afk); - - // Group by application events = merge_events_by_keys(events, ["app"]); - - // Sort by time spent RETURN = sort_by_duration(events); +.. note:: + Query statements must not contain ``//`` comments: both servers reject them with a ``400`` parse error. + The Python aw-server accepts no comments at all, and aw-server-rust accepts only ``#`` comments. + Key Transform Functions ~~~~~~~~~~~~~~~~~~~~~~~ @@ -130,12 +129,12 @@ Common query functions include: Magic Variables ~~~~~~~~~~~~~~~ -The web UI provides special variables you can use: +* ``__CATEGORIES__`` - Your configured categorization rules. Only the web UI's Query page substitutes this + variable. Sent to ``/api/0/query/`` directly (for example via ``aw-client``), it fails with an + undefined-variable error, so pass the category rules to ``categorize`` yourself. +* ``find_bucket(pattern)`` - A server-side function that returns the first bucket id matching a pattern. -* ``__CATEGORIES__`` - Your configured categorization rules -* ``find_bucket(pattern)`` - Find bucket names matching a pattern - -Example using categories to filter work activities: +Example using categories to filter work activities (run it from the web UI's Query page): .. code-block:: javascript @@ -176,25 +175,18 @@ Query Examples Complete Example ~~~~~~~~~~~~~~~~ -Here's a complete custom query for analyzing productivity: +Here's a complete custom query for analyzing productivity. It counts only active (not-AFK) time, +applies your categories, keeps the ``Work`` category and groups by application. +Because it uses ``__CATEGORIES__``, run it from the web UI's Query page: .. code-block:: javascript - // Get window and AFK events events = flood(query_bucket(find_bucket("aw-watcher-window_"))); not_afk = flood(query_bucket(find_bucket("aw-watcher-afk_"))); not_afk = filter_keyvals(not_afk, "status", ["not-afk"]); - - // Only count active time events = filter_period_intersect(events, not_afk); - - // Apply categories events = categorize(events, __CATEGORIES__); - - // Filter to work categories only events = filter_keyvals(events, "$category", [["Work"]]); - - // Group by application and sort events = merge_events_by_keys(events, ["app"]); RETURN = sort_by_duration(events); From 81fde232e385551a7b391594400d21a4b187f0ff Mon Sep 17 00:00:00 2001 From: Bob Date: Tue, 24 Nov 2026 11:16:08 +0000 Subject: [PATCH 2/2] docs(query): say the Basics bucket IDs are placeholders Git-Session-Id: bed1476c-0b54-4dde-9c03-8080d9dde1e7 --- src/examples/working-with-data.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/examples/working-with-data.rst b/src/examples/working-with-data.rst index ea65d95..78bc18b 100644 --- a/src/examples/working-with-data.rst +++ b/src/examples/working-with-data.rst @@ -98,6 +98,8 @@ Query Language Basics ~~~~~~~~~~~~~~~~~~~~~~ Queries use a simple scripting language for server-side data processing. +Before running this example, replace both sample bucket IDs with the full window and AFK bucket IDs +for the same device (listed on the web UI's Raw Data page). This one gets the window events, keeps only the periods where you were not AFK, groups them by application and sorts them by time spent: