Skip to content

Commit b04d38b

Browse files
aw-transform: Add documentation to transforms
1 parent 4e4b364 commit b04d38b

File tree

9 files changed

+131
-1
lines changed

9 files changed

+131
-1
lines changed

aw-transform/src/chunk.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
use aw_models::Event;
22

3+
/// Chunks together events with the same key
4+
///
5+
/// NOTE: In most cases you should use merge_events_by_keys instead, this
6+
/// transform is mostly just for backwards compatibility with older versions
7+
/// of aw-webui
8+
/// NOTE: Does not support sub-chunking which aw-server-python supports
9+
/// Without sub-chunking it is pretty much the same as merge_events_by_key
10+
///
11+
/// # Example
12+
/// ```ignore
13+
/// key: a
14+
/// input:
15+
/// { duration: 1.0, data: { "a": 1, "b": 1 } }
16+
/// { duration: 1.0, data: { "a": 1, "b": 2 } }
17+
/// { duration: 1.0, data: { "a": 2, "b": 1 } }
18+
/// output:
19+
/// { duration: 2.0, data: { "a": 1 } }
20+
/// { duration: 1.0, data: { "a": 2 } }
21+
/// ```
322
pub fn chunk_events_by_key(events: Vec<Event>, key: &str) -> Vec<Event> {
423
let mut chunked_events: Vec<Event> = Vec::new();
524
for event in events {

aw-transform/src/filter_keyvals.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ use serde_json::value::Value;
33

44
use aw_models::Event;
55

6+
/// Drops events not matching the specified key and value(s)
7+
///
8+
/// # Example
9+
/// ```ignore
10+
/// key: a
11+
/// vals: [1,2]
12+
/// input: [a:1][a:2][a:3][b:4]
13+
/// output: [a:1][a:2]
14+
/// ```
615
pub fn filter_keyvals(mut events: Vec<Event>, key: &str, vals: &[Value]) -> Vec<Event> {
716
let mut filtered_events = Vec::new();
817
for event in events.drain(..) {
@@ -18,6 +27,16 @@ pub fn filter_keyvals(mut events: Vec<Event>, key: &str, vals: &[Value]) -> Vec<
1827
filtered_events
1928
}
2029

30+
/// Drops events not matching the regex on the value for a specified key
31+
/// Will only match if the value is a string
32+
///
33+
/// # Example
34+
/// ```ignore
35+
/// key: a
36+
/// regex: "[A-Z]+"
37+
/// input: [a:"HELLO"][a:"hello"][a:3][b:"HELLO"]
38+
/// output: [a:"HELLO"]
39+
/// ```
2140
pub fn filter_keyvals_regex(mut events: Vec<Event>, key: &str, regex: &Regex) -> Vec<Event> {
2241
let mut filtered_events = Vec::new();
2342

aw-transform/src/filter_period.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
use aw_models::Event;
22

3+
/// Removes events not intersecting with the provided filter_events
4+
///
5+
/// Usually used to filter buckets unaware if the user is making any activity with an bucket which
6+
/// is aware if the user is at the computer or not.
7+
/// For example the events from aw-watcher-window should be called with filter_period_intersect
8+
/// with the "not-afk" events from aw-watcher-afk to give events with durations of only when the
9+
/// user is at the computer.
10+
///
11+
/// # Example
12+
/// ```ignore
13+
/// events: [a ][b ]
14+
/// filter_events: [ ] [ ]
15+
/// output: [a ] [b ]
16+
/// ```
317
pub fn filter_period_intersect(events: &[Event], filter_events: &[Event]) -> Vec<Event> {
418
let mut filtered_events = Vec::new();
519
for filter in filter_events {

aw-transform/src/find_bucket.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// Finds the first bucket which starts with the specified string
12
pub fn find_bucket<'a>(
23
bucket_filter: &str,
34
bucketnames: impl IntoIterator<Item = &'a String>,

aw-transform/src/flood.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ use aw_models::Event;
22

33
use crate::sort_by_timestamp;
44

5+
/// Floods event to the nearest neighbouring event if within the specified pulsetime
6+
///
7+
/// Also merges events if they have the same data and are within the pulsetime
8+
///
9+
/// # Example
10+
/// ```ignore
11+
/// pulsetime: 1 second (one space)
12+
/// input: [a] [a] [b][b] [b][c]
13+
/// output: [a ][b ] [b][c]
14+
/// ```
515
pub fn flood(events: Vec<Event>, pulsetime: chrono::Duration) -> Vec<Event> {
616
let mut warned_negative_gap_safe = false;
717
let mut warned_negative_gap_unsafe = false;

aw-transform/src/heartbeat.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
use aw_models::Event;
22

3+
/// Returns a merged event if two events have the same data and are within the pulsetime
4+
///
5+
/// # Example
6+
///
7+
/// ```ignore
8+
/// pulsetime: 1 second (one space)
9+
/// input: [a] [a] [a][b]
10+
/// output: [a ] [a][b]
11+
/// ```
312
pub fn heartbeat(last_event: &Event, heartbeat: &Event, pulsetime: f64) -> Option<Event> {
413
// Verify that data is the same
514
if heartbeat.data != last_event.data {

aw-transform/src/merge.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@ use std::collections::HashMap;
22

33
use aw_models::Event;
44

5+
/// Merge events with the same values at the specified keys
6+
///
7+
/// Doesn't care about if events are neighbouring or not, this transform merges
8+
/// all events with the same key.
9+
/// The timestamp will be the timestamp of the first event with a specific key value
10+
///
11+
/// # Example 1
12+
/// A simple example only using one key
13+
///
14+
/// ```ignore
15+
/// keys: ["a"]
16+
/// input:
17+
/// { duration: 1.0, data: { "a": 1 } }
18+
/// { duration: 1.0, data: { "a": 1 } }
19+
/// { duration: 1.0, data: { "a": 2 } }
20+
/// { duration: 1.0, data: { "b": 1 } }
21+
/// { duration: 1.0, data: { "a": 1 } }
22+
/// output:
23+
/// { duration: 3.0, data: { "a": 1 } }
24+
/// { duration: 1.0, data: { "a": 2 } }
25+
/// { duration: 1.0, data: { "b": 1 } }
26+
/// ```
27+
///
28+
/// # Example 2
29+
/// A more complex example only using two keys
30+
/// ```ignore
31+
/// keys: ["a", "b"]
32+
/// input:
33+
/// { duration: 1.0, data: { "a": 1, "b": 1 } }
34+
/// { duration: 1.0, data: { "a": 2, "b": 2 } }
35+
/// { duration: 1.0, data: { "a": 1, "b": 1 } }
36+
/// { duration: 1.0, data: { "a": 1, "b": 2 } }
37+
/// output:
38+
/// { duration: 2.0, data: { "a": 1, "b": 1 } }
39+
/// { duration: 1.0, data: { "a": 2, "b": 2 } }
40+
/// { duration: 1.0, data: { "a": 1, "b": 2 } }
41+
/// ```
542
#[allow(clippy::map_entry)]
643
pub fn merge_events_by_keys(events: Vec<Event>, keys: Vec<String>) -> Vec<Event> {
744
if keys.is_empty() {

aw-transform/src/sort.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use aw_models::Event;
22

3+
/// Sort a list of events by timestamp
34
pub fn sort_by_timestamp(mut events: Vec<Event>) -> Vec<Event> {
45
events.sort_by(|e1, e2| e1.timestamp.cmp(&e2.timestamp));
56
events
67
}
78

8-
/* Highest first */
9+
/// Sort a list of events by duration with the highest duration first
910
pub fn sort_by_duration(mut events: Vec<Event>) -> Vec<Event> {
1011
events.sort_by(|e1, e2| e2.duration.cmp(&e1.duration));
1112
events

aw-transform/src/split_url.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
use aw_models::Event;
22
use serde_json::value::Value;
33

4+
/// Adds $protocol, $domain, $path and $params keys for events with an "url" key
5+
///
6+
/// But it only adds the generated field if it exists, for example if a url does not have a path
7+
/// the path value will not be set at all.
8+
///
9+
/// # Example
10+
/// ```ignore
11+
/// input: {
12+
/// "data": {
13+
/// "url": "http://google.com/test"
14+
/// }
15+
/// }
16+
/// output: {
17+
/// "data": {
18+
/// "$domain": "google.com",
19+
/// "$path": "/test",
20+
/// "$protocol": "http"
21+
/// }
22+
/// }
23+
/// ```
424
pub fn split_url_event(event: &mut Event) {
525
use rocket::http::uri::Absolute;
626
let uri_str = match event.data.get("url") {

0 commit comments

Comments
 (0)