Skip to content

Commit 78bbad7

Browse files
committed
fix: remove unnecessary functions in query class Keep feature parity
with aw-client
1 parent 25b5a7b commit 78bbad7

File tree

4 files changed

+22
-122
lines changed

4 files changed

+22
-122
lines changed

aw-client-rust/src/classes.rs

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
//!
33
//! Taken from default classes in aw-webui
44
5-
use log::warn;
6-
use rand::Rng;
75
use serde::{Deserialize, Serialize};
86
use serde_json;
97

10-
use super::blocking::AwClient as ActivityWatchClient;
11-
128
pub type CategoryId = Vec<String>;
139

1410
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -146,61 +142,3 @@ pub fn default_classes() -> Vec<(CategoryId, CategorySpec)> {
146142
),
147143
]
148144
}
149-
150-
/// Get classes from server-side settings using default localhost:5600.
151-
/// Might throw an error if not set yet, in which case we use the default classes as a fallback.
152-
pub fn get_classes() -> Vec<(CategoryId, CategorySpec)> {
153-
get_classes_from_server("localhost", 5600)
154-
}
155-
156-
/// Get classes from server-side settings with custom host and port.
157-
/// Might throw an error if not set yet, in which case we use the default classes as a fallback.
158-
pub fn get_classes_from_server(host: &str, port: u16) -> Vec<(CategoryId, CategorySpec)> {
159-
let mut rng = rand::rng();
160-
let random_int = rng.random_range(0..10001);
161-
let client_id = format!("get-setting-{}", random_int);
162-
163-
// Create a client with a random ID, similar to the Python implementation
164-
let awc = match ActivityWatchClient::new(host, port, &client_id) {
165-
Ok(client) => client,
166-
Err(_) => {
167-
warn!(
168-
"Failed to create ActivityWatch client for {}:{}, using default classes",
169-
host, port
170-
);
171-
return default_classes();
172-
}
173-
};
174-
175-
awc.get_setting("classes")
176-
.map(|setting_value| {
177-
// Try to deserialize the setting into Vec<ClassSetting>
178-
if setting_value.is_null() {
179-
return default_classes();
180-
}
181-
182-
let class_settings: Vec<ClassSetting> = match serde_json::from_value(setting_value) {
183-
Ok(classes) => classes,
184-
Err(e) => {
185-
warn!(
186-
"Failed to deserialize classes setting: {}, using default classes",
187-
e
188-
);
189-
return default_classes();
190-
}
191-
};
192-
193-
// Convert ClassSetting to (CategoryId, CategorySpec) format
194-
class_settings
195-
.into_iter()
196-
.map(|class| (class.name, class.rule))
197-
.collect()
198-
})
199-
.unwrap_or_else(|_| {
200-
warn!(
201-
"Failed to get classes from server {}:{}, using default classes as fallback",
202-
host, port
203-
);
204-
default_classes()
205-
})
206-
}

aw-client-rust/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl std::fmt::Debug for AwClient {
3636
}
3737

3838
fn get_hostname() -> String {
39-
return gethostname::gethostname().to_string_lossy().to_string();
39+
gethostname::gethostname().to_string_lossy().to_string()
4040
}
4141

4242
impl AwClient {
@@ -123,8 +123,6 @@ impl AwClient {
123123
.map(|(start, stop)| format!("{}/{}", start, stop))
124124
.collect();
125125

126-
let query_lines: Vec<&str> = query.split('\n').collect();
127-
128126
// Result is a sequence, one element per timeperiod
129127
self.client
130128
.post(url)

aw-client-rust/src/queries.rs

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@
2828
//! };
2929
//!
3030
//! // Automatically fetches classes from localhost:5600
31-
//! let query = QueryParams::Desktop(params.clone()).canonical_events_with_classes();
31+
//! let query = QueryParams::Desktop(params.clone()).canonical_events();
3232
//!
33-
//! // Or from a custom server
34-
//! let query = QueryParams::Desktop(params)
35-
//! .canonical_events_with_classes_from_server("localhost", 2345);
3633
//! ```
3734
3835
use crate::classes::{CategoryId, CategorySpec};
@@ -78,8 +75,6 @@ pub static BROWSER_APPNAMES: phf::Map<&'static str, &'static [&'static str]> = p
7875
"vivaldi" => &["Vivaldi-stable", "Vivaldi-snapshot", "vivaldi.exe"],
7976
};
8077

81-
pub const DEFAULT_LIMIT: u32 = 100;
82-
8378
/// Type alias for categorization classes
8479
pub type ClassRule = (CategoryId, CategorySpec);
8580

@@ -135,33 +130,6 @@ impl QueryParams {
135130
QueryParams::Android(params) => build_android_canonical_events(params),
136131
}
137132
}
138-
139-
/// Build canonical events query string with automatic class fetching if not provided
140-
pub fn canonical_events_with_classes(&self) -> String {
141-
self.canonical_events_with_classes_from_server("localhost", 5600)
142-
}
143-
144-
/// Build canonical events query string with automatic class fetching from custom server
145-
pub fn canonical_events_with_classes_from_server(&self, host: &str, port: u16) -> String {
146-
match self {
147-
QueryParams::Desktop(params) => {
148-
let mut params_with_classes = params.clone();
149-
if params_with_classes.base.classes.is_empty() {
150-
params_with_classes.base.classes =
151-
crate::classes::get_classes_from_server(host, port);
152-
}
153-
build_desktop_canonical_events(&params_with_classes)
154-
}
155-
QueryParams::Android(params) => {
156-
let mut params_with_classes = params.clone();
157-
if params_with_classes.base.classes.is_empty() {
158-
params_with_classes.base.classes =
159-
crate::classes::get_classes_from_server(host, port);
160-
}
161-
build_android_canonical_events(&params_with_classes)
162-
}
163-
}
164-
}
165133
}
166134

167135
/// Helper function to serialize classes in the format expected by the categorize function
@@ -201,7 +169,7 @@ fn serialize_classes(classes: &[ClassRule]) -> String {
201169
format!("[{}]", parts.join(", "))
202170
}
203171

204-
fn build_desktop_canonical_events(params: &DesktopQueryParams) -> String {
172+
pub fn build_desktop_canonical_events(params: &DesktopQueryParams) -> String {
205173
let mut query = Vec::new();
206174

207175
// Fetch window events
@@ -256,7 +224,7 @@ not_afk = period_union(not_afk, audible_events)"
256224
query.join(";\n")
257225
}
258226

259-
fn build_android_canonical_events(params: &AndroidQueryParams) -> String {
227+
pub fn build_android_canonical_events(params: &AndroidQueryParams) -> String {
260228
let mut query = Vec::new();
261229

262230
// Fetch app events
@@ -287,7 +255,7 @@ fn build_android_canonical_events(params: &AndroidQueryParams) -> String {
287255
query.join(";\n")
288256
}
289257

290-
fn build_browser_events(params: &DesktopQueryParams) -> String {
258+
pub fn build_browser_events(params: &DesktopQueryParams) -> String {
291259
let mut query = String::from("browser_events = [];");
292260

293261
for browser_bucket in &params.base.bid_browsers {
@@ -311,38 +279,34 @@ browser_events = sort_by_timestamp(browser_events)",
311279
query
312280
}
313281

314-
/// Build a full desktop query
282+
/// Build a full desktop query using default localhost:5600 configuration
315283
pub fn full_desktop_query(params: &DesktopQueryParams) -> String {
316-
let mut query = QueryParams::Desktop(params.clone()).canonical_events_with_classes();
284+
let mut query = QueryParams::Desktop(params.clone()).canonical_events();
317285

318286
// Add basic event aggregations
319-
query.push_str(&format!(
320-
"
287+
query.push_str(
288+
&"
321289
title_events = sort_by_duration(merge_events_by_keys(events, [\"app\", \"title\"]));
322290
app_events = sort_by_duration(merge_events_by_keys(title_events, [\"app\"]));
323291
cat_events = sort_by_duration(merge_events_by_keys(events, [\"$category\"]));
324-
app_events = limit_events(app_events, {});
325-
title_events = limit_events(title_events, {});
326292
duration = sum_durations(events);
327-
",
328-
DEFAULT_LIMIT, DEFAULT_LIMIT
329-
));
293+
"
294+
.to_string(),
295+
);
330296

331297
// Add browser-specific query parts if browser buckets exist
332298
if !params.base.bid_browsers.is_empty() {
333-
query.push_str(&format!(
334-
"
299+
query.push_str(
300+
&"
335301
browser_events = split_url_events(browser_events);
336302
browser_urls = merge_events_by_keys(browser_events, [\"url\"]);
337303
browser_urls = sort_by_duration(browser_urls);
338-
browser_urls = limit_events(browser_urls, {});
339304
browser_domains = merge_events_by_keys(browser_events, [\"$domain\"]);
340305
browser_domains = sort_by_duration(browser_domains);
341-
browser_domains = limit_events(browser_domains, {});
342306
browser_duration = sum_durations(browser_events);
343-
",
344-
DEFAULT_LIMIT, DEFAULT_LIMIT
345-
));
307+
"
308+
.to_string(),
309+
);
346310
} else {
347311
query.push_str(
348312
"
@@ -443,7 +407,7 @@ mod tests {
443407
let params = DesktopQueryParams {
444408
base: QueryParamsBase {
445409
bid_browsers: vec![],
446-
classes: vec![], // Empty classes - should trigger server fetch
410+
classes: vec![],
447411
filter_classes: vec![],
448412
filter_afk: true,
449413
include_audible: true,
@@ -453,9 +417,9 @@ mod tests {
453417
};
454418

455419
let query_params = QueryParams::Desktop(params);
456-
let query = query_params.canonical_events_with_classes();
420+
let query = query_params.canonical_events();
457421

458-
// Should contain basic query structure even if server fetch fails
422+
// Should contain basic query structure
459423
assert!(query.contains("events = flood"));
460424
assert!(query.contains("test-window"));
461425
}
@@ -484,7 +448,7 @@ mod tests {
484448
};
485449

486450
let query_params = QueryParams::Desktop(params);
487-
let query = query_params.canonical_events_with_classes();
451+
let query = query_params.canonical_events();
488452

489453
// Should contain categorization
490454
assert!(query.contains("events = categorize"));

0 commit comments

Comments
 (0)