Some analytics fixes

This commit is contained in:
Gregory Schier
2024-01-18 20:23:02 -08:00
parent 874c73b50f
commit 60acc86e52
4 changed files with 52 additions and 34 deletions

View File

@@ -12,11 +12,29 @@ use crate::{is_dev, models};
#[derive(Serialize, Deserialize)]
pub enum AnalyticsResource {
App,
Sidebar,
Workspace,
Environment,
Folder,
HttpRequest,
HttpResponse,
KeyValue,
}
impl AnalyticsResource {
pub fn from_str(s: &str) -> Option<AnalyticsResource> {
match s {
"App" => Some(AnalyticsResource::App),
"Sidebar" => Some(AnalyticsResource::Sidebar),
"Workspace" => Some(AnalyticsResource::Workspace),
"Environment" => Some(AnalyticsResource::Environment),
"Folder" => Some(AnalyticsResource::Folder),
"Http_request" => Some(AnalyticsResource::HttpRequest),
"Http_response" => Some(AnalyticsResource::HttpResponse),
"Key_value" => Some(AnalyticsResource::KeyValue),
_ => None,
}
}
}
#[derive(Serialize, Deserialize)]
@@ -30,22 +48,24 @@ pub enum AnalyticsAction {
Delete,
DeleteMany,
Send,
Toggle,
Duplicate,
}
impl AnalyticsAction {
pub fn from_str(s: &str) -> Option<AnalyticsAction> {
match s {
"launch" => Some(AnalyticsAction::Launch),
"launch_first" => Some(AnalyticsAction::LaunchFirst),
"launch_update" => Some(AnalyticsAction::LaunchUpdate),
"create" => Some(AnalyticsAction::Create),
"update" => Some(AnalyticsAction::Update),
"upsert" => Some(AnalyticsAction::Upsert),
"delete" => Some(AnalyticsAction::Delete),
"delete_many" => Some(AnalyticsAction::DeleteMany),
"send" => Some(AnalyticsAction::Send),
"duplicate" => Some(AnalyticsAction::Duplicate),
"Launch" => Some(AnalyticsAction::Launch),
"LaunchFirst" => Some(AnalyticsAction::LaunchFirst),
"LaunchUpdate" => Some(AnalyticsAction::LaunchUpdate),
"Create" => Some(AnalyticsAction::Create),
"Update" => Some(AnalyticsAction::Update),
"Upsert" => Some(AnalyticsAction::Upsert),
"Delete" => Some(AnalyticsAction::Delete),
"DeleteMany" => Some(AnalyticsAction::DeleteMany),
"Send" => Some(AnalyticsAction::Send),
"Duplicate" => Some(AnalyticsAction::Duplicate),
"Toggle" => Some(AnalyticsAction::Toggle),
_ => None,
}
}
@@ -54,11 +74,13 @@ impl AnalyticsAction {
fn resource_name(resource: AnalyticsResource) -> &'static str {
match resource {
AnalyticsResource::App => "app",
AnalyticsResource::Sidebar => "sidebar",
AnalyticsResource::Workspace => "workspace",
AnalyticsResource::Environment => "environment",
AnalyticsResource::Folder => "folder",
AnalyticsResource::HttpRequest => "http_request",
AnalyticsResource::HttpResponse => "http_response",
AnalyticsResource::KeyValue => "key_value",
}
}
@@ -74,6 +96,7 @@ fn action_name(action: AnalyticsAction) -> &'static str {
AnalyticsAction::DeleteMany => "delete_many",
AnalyticsAction::Send => "send",
AnalyticsAction::Duplicate => "duplicate",
AnalyticsAction::Toggle => "toggle",
}
}

View File

@@ -7,7 +7,7 @@ use std::time::Duration;
use base64::Engine;
use http::{HeaderMap, HeaderName, HeaderValue, Method};
use http::header::{ACCEPT, USER_AGENT};
use log::{info, warn};
use log::{error, info, warn};
use reqwest::multipart;
use reqwest::redirect::Policy;
use sqlx::{Pool, Sqlite};
@@ -82,14 +82,14 @@ pub async fn send_http_request(
let header_name = match HeaderName::from_bytes(name.as_bytes()) {
Ok(n) => n,
Err(e) => {
eprintln!("Failed to create header name: {}", e);
error!("Failed to create header name: {}", e);
continue;
}
};
let header_value = match HeaderValue::from_str(value.as_str()) {
Ok(n) => n,
Err(e) => {
eprintln!("Failed to create header value: {}", e);
error!("Failed to create header value: {}", e);
continue;
}
};
@@ -267,7 +267,6 @@ pub async fn send_http_request(
response.url = v.url().to_string();
let body_bytes = v.bytes().await.expect("Failed to get body").to_vec();
response.content_length = Some(body_bytes.len() as i64);
println!("Response: {:?}", body_bytes.len());
{
// Write body to FS
@@ -314,7 +313,6 @@ pub async fn send_http_request(
Ok(response)
}
Err(e) => {
println!("Yo: {}", e);
response_err(response, e.to_string(), app_handle, pool).await
}
}

View File

@@ -294,21 +294,18 @@ async fn track_event(
action: &str,
attributes: Option<Value>,
) -> Result<(), String> {
let action_type = AnalyticsAction::from_str(action);
match (action_type, action_type) {
(Some(t), Some(t)) => {
analytics::track_event(
&window.app_handle(),
resource,
action_type,
attributes,
)
.await;
},
_ => {
error!("Invalid action type: {}", action);
match (
AnalyticsResource::from_str(resource),
AnalyticsAction::from_str(action),
) {
(Some(resource), Some(action)) => {
analytics::track_event(&window.app_handle(), resource, action, attributes).await;
}
}
_ => {
error!("Invalid action/resource for track_event: {action} {resource}");
return Err("Invalid event".to_string());
}
};
Ok(())
}
@@ -785,7 +782,7 @@ fn main() {
let p_string = p.to_string_lossy().replace(' ', "%20");
let url = format!("sqlite://{}?mode=rwc", p_string);
println!("Connecting to database at {}", url);
info!("Connecting to database at {}", url);
tauri::async_runtime::block_on(async move {
let pool = SqlitePool::connect(p.to_str().unwrap())
@@ -1001,7 +998,6 @@ fn create_window(handle: &AppHandle<Wry>, url: Option<&str>) -> Window<Wry> {
WindowEvent::Focused(..) => apply_offset(),
WindowEvent::ScaleFactorChanged { .. } => apply_offset(),
WindowEvent::CloseRequested { .. } => {
println!("CLOSE REQUESTED");
// api.prevent_close();
}
_ => {}

View File

@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::fs;
use log::error;
use rand::distributions::{Alphanumeric, DistString};
use serde::{Deserialize, Serialize};
@@ -213,7 +214,7 @@ pub async fn get_key_value_string(
match result {
Ok(v) => v,
Err(e) => {
println!("Failed to parse string key value: {}", e);
error!("Failed to parse string key value: {}", e);
default.to_string()
}
}
@@ -234,7 +235,7 @@ pub async fn get_key_value_int(
match result {
Ok(v) => v,
Err(e) => {
println!("Failed to parse int key value: {}", e);
error!("Failed to parse int key value: {}", e);
default.clone()
}
}
@@ -961,7 +962,7 @@ pub async fn delete_response(id: &str, pool: &Pool<Sqlite>) -> Result<HttpRespon
// Delete the body file if it exists
if let Some(p) = resp.body_path.clone() {
if let Err(e) = fs::remove_file(p) {
println!("Failed to delete body file: {}", e);
error!("Failed to delete body file: {}", e);
};
}