mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 23:44:12 +01:00
Rust analytics and a few tweaks
This commit is contained in:
75
src-tauri/src/analytics.rs
Normal file
75
src-tauri/src/analytics.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use sqlx::types::JsonValue;
|
||||
|
||||
use crate::is_dev;
|
||||
|
||||
pub enum AnalyticsResource {
|
||||
App,
|
||||
// Workspace,
|
||||
// Environment,
|
||||
// Folder,
|
||||
// HttpRequest,
|
||||
// HttpResponse,
|
||||
}
|
||||
|
||||
pub enum AnalyticsAction {
|
||||
Launch,
|
||||
// Create,
|
||||
// Update,
|
||||
// Upsert,
|
||||
// Delete,
|
||||
// Send,
|
||||
// Duplicate,
|
||||
}
|
||||
|
||||
fn resource_name(resource: AnalyticsResource) -> &'static str {
|
||||
match resource {
|
||||
AnalyticsResource::App => "app",
|
||||
// AnalyticsResource::Workspace => "workspace",
|
||||
// AnalyticsResource::Environment => "environment",
|
||||
// AnalyticsResource::Folder => "folder",
|
||||
// AnalyticsResource::HttpRequest => "http_request",
|
||||
// AnalyticsResource::HttpResponse => "http_response",
|
||||
}
|
||||
}
|
||||
|
||||
fn action_name(action: AnalyticsAction) -> &'static str {
|
||||
match action {
|
||||
AnalyticsAction::Launch => "launch",
|
||||
// AnalyticsAction::Create => "create",
|
||||
// AnalyticsAction::Update => "update",
|
||||
// AnalyticsAction::Upsert => "upsert",
|
||||
// AnalyticsAction::Delete => "delete",
|
||||
// AnalyticsAction::Send => "send",
|
||||
// AnalyticsAction::Duplicate => "duplicate",
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn track_event(
|
||||
resource: AnalyticsResource,
|
||||
action: AnalyticsAction,
|
||||
attributes: Option<JsonValue>,
|
||||
) {
|
||||
let event = format!("{}.{}", resource_name(resource), action_name(action));
|
||||
let attributes_json = attributes.unwrap_or("{}".to_string().into()).to_string();
|
||||
let params = vec![
|
||||
("e", event.clone()),
|
||||
("a", attributes_json.clone()),
|
||||
("id", "site_zOK0d7jeBy2TLxFCnZ".to_string()),
|
||||
];
|
||||
let url = format!("https://t.yaak.app/t/e");
|
||||
let req = reqwest::Client::builder()
|
||||
.build()
|
||||
.unwrap()
|
||||
.get(&url)
|
||||
.query(¶ms);
|
||||
|
||||
if is_dev() {
|
||||
println!("Ignore dev analytics event: {}", event);
|
||||
} else {
|
||||
if let Err(e) = req.send().await {
|
||||
println!("Error sending analytics event: {}", e);
|
||||
} else {
|
||||
println!("Sent analytics event: {}", event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,9 @@ use tokio::sync::Mutex;
|
||||
|
||||
use window_ext::TrafficLightWindowExt;
|
||||
|
||||
use crate::analytics::{AnalyticsAction, AnalyticsResource};
|
||||
|
||||
mod analytics;
|
||||
mod models;
|
||||
mod plugin;
|
||||
mod render;
|
||||
@@ -92,7 +95,7 @@ async fn actually_send_request(
|
||||
let environment_ref = environment.as_ref();
|
||||
let workspace = models::get_workspace(&request.workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to get workspace");
|
||||
.expect("Failed to get Workspace");
|
||||
|
||||
let mut url_string = render::render(&request.url, &workspace, environment.as_ref());
|
||||
|
||||
@@ -363,7 +366,7 @@ async fn create_workspace(
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect("Failed to create workspace");
|
||||
.expect("Failed to create Workspace");
|
||||
|
||||
emit_and_return(&window, "created_model", created_workspace)
|
||||
}
|
||||
@@ -684,7 +687,7 @@ async fn list_workspaces(
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect("Failed to create workspace");
|
||||
.expect("Failed to create Workspace");
|
||||
Ok(vec![workspace])
|
||||
} else {
|
||||
Ok(workspaces)
|
||||
@@ -706,7 +709,7 @@ async fn delete_workspace(
|
||||
let pool = &*db_instance.lock().await;
|
||||
let workspace = models::delete_workspace(workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to delete workspace");
|
||||
.expect("Failed to delete Workspace");
|
||||
emit_and_return(&window, "deleted_model", workspace)
|
||||
}
|
||||
|
||||
@@ -813,6 +816,11 @@ fn main() {
|
||||
let w = create_window(app_handle, None);
|
||||
w.restore_state(StateFlags::all())
|
||||
.expect("Failed to restore window state");
|
||||
|
||||
tauri::async_runtime::block_on(async move {
|
||||
analytics::track_event(AnalyticsResource::App, AnalyticsAction::Launch, None)
|
||||
.await;
|
||||
})
|
||||
}
|
||||
|
||||
// ExitRequested { api, .. } => {
|
||||
|
||||
@@ -659,7 +659,7 @@ pub async fn upsert_workspace(
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
get_workspace(&workspace.id, pool).await
|
||||
get_workspace(&id, pool).await
|
||||
}
|
||||
|
||||
pub async fn update_response(
|
||||
|
||||
Reference in New Issue
Block a user