Refactor commands and DB

This commit is contained in:
Gregory Schier
2024-02-01 02:29:24 -08:00
parent be8dd107e3
commit 1dd0b69079
46 changed files with 534 additions and 540 deletions

View File

@@ -3,8 +3,7 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::{Pool, Sqlite};
use sqlx::types::JsonValue;
use tauri::{AppHandle, Manager, State};
use tokio::sync::Mutex;
use tauri::{AppHandle, Manager};
use crate::{is_dev, models};
@@ -126,17 +125,15 @@ pub struct LaunchEventInfo {
pub num_launches: i32,
}
pub async fn track_launch_event(app_handle: &AppHandle) -> LaunchEventInfo {
pub async fn track_launch_event(app_handle: &AppHandle, db: &Pool<Sqlite>) -> LaunchEventInfo {
let namespace = "analytics";
let last_tracked_version_key = "last_tracked_version";
let db_instance: State<'_, Mutex<Pool<Sqlite>>> = app_handle.state();
let pool = &*db_instance.lock().await;
let mut info = LaunchEventInfo::default();
info.num_launches = models::get_key_value_int(namespace, "num_launches", 0, pool).await + 1;
info.num_launches = models::get_key_value_int(db, namespace, "num_launches", 0).await + 1;
info.previous_version =
models::get_key_value_string(namespace, last_tracked_version_key, "", pool).await;
models::get_key_value_string(db, namespace, last_tracked_version_key, "").await;
info.current_version = app_handle.package_info().version.to_string();
if info.previous_version.is_empty() {
@@ -167,19 +164,18 @@ pub async fn track_launch_event(app_handle: &AppHandle) -> LaunchEventInfo {
AnalyticsAction::Launch,
Some(json!({ "num_launches": info.num_launches })),
)
.await;
.await;
// Update key values
models::set_key_value_string(
db,
namespace,
last_tracked_version_key,
info.current_version.as_str(),
pool,
)
.await;
models::set_key_value_int(namespace, "num_launches", info.num_launches, pool).await;
models::set_key_value_int(db, namespace, "num_launches", info.num_launches).await;
info
}