Skip to content

Commit

Permalink
Refactor after review
Browse files Browse the repository at this point in the history
  • Loading branch information
MCozhusheck committed Jan 10, 2025
1 parent de689ed commit 7c5994c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use node_adapter::BaseNodeStatus;
use p2pool::models::Connections;
use serde_json::json;
use std::fs::{remove_dir_all, remove_file};
use std::ops::Deref;
use std::path::Path;
use tauri_plugin_cli::CliExt;
use telemetry_service::TelemetryService;
Expand Down Expand Up @@ -275,8 +274,7 @@ async fn setup_inner(
.init(app_version.to_string(), telemetry_id.clone())
.await?;
let telemetry_service = state.telemetry_service.clone();
let telemetry_service = telemetry_service.read().await;
let telemetry_service = telemetry_service.deref();
let telemetry_service = &telemetry_service.read().await;

let mut binary_resolver = BinaryResolver::current().write().await;
let should_check_for_update = now
Expand Down Expand Up @@ -816,7 +814,6 @@ fn main() {
);
let telemetry_service = TelemetryService::new(
app_config_raw.anon_id().to_string(),
"0.0.0".to_string(),
app_config.clone(),
app_in_memory_config.clone(),
);
Expand Down
74 changes: 45 additions & 29 deletions src-tauri/src/telemetry_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ use tokio::sync::{
use tokio_util::sync::CancellationToken;

use crate::{
app_config::AppConfig, app_in_memory_config::AppInMemoryConfig,
hardware::hardware_status_monitor::HardwareStatusMonitor, process_utils::retry_with_backoff,
utils::platform_utils::PlatformUtils,
app_config::AppConfig,
app_in_memory_config::AppInMemoryConfig,
hardware::hardware_status_monitor::HardwareStatusMonitor,
process_utils::retry_with_backoff,
utils::platform_utils::{CurrentOperatingSystem, PlatformUtils},
};

const LOG_TARGET: &str = "tari::universe::telemetry_service";
Expand Down Expand Up @@ -78,14 +80,13 @@ pub struct TelemetryService {
impl TelemetryService {
pub fn new(
app_id: String,
version: String,
config: Arc<RwLock<AppConfig>>,
in_memory_config: Arc<RwLock<AppInMemoryConfig>>,
) -> Self {
let cancellation_token = CancellationToken::new();
TelemetryService {
app_id,
version,
version: "0.0.0".to_string(),
tx_channel: None,
cancellation_token,
config,
Expand All @@ -97,6 +98,19 @@ impl TelemetryService {
app_version: String,
user: String,
) -> Result<(), TelemetryServiceError> {
let hardware = HardwareStatusMonitor::current();
let cpu_name = hardware.get_cpu_devices().await?;
let cpu_name = match cpu_name.first() {
Some(cpu) => cpu.public_properties.name.clone(),
None => "Unknown".to_string(),
};
let gpu_name = hardware.get_gpu_devices().await?;
let gpu_name = match gpu_name.first() {
Some(gpu) => gpu.public_properties.name.clone(),
None => "Unknown".to_string(),
};
let os = PlatformUtils::detect_current_os();

self.version = app_version;
let cancellation_token = self.cancellation_token.clone();
let config_cloned = self.config.clone();
Expand All @@ -111,6 +125,14 @@ impl TelemetryService {
let (tx, mut rx) = mpsc::channel(128);
self.tx_channel = Some(tx);
tokio::spawn(async move {
let system_info = SystemInfo {
app_id,
version,
user_id: user,
cpu_name,
gpu_name,
os,
};
tokio::select! {
_ = async {
debug!(target: LOG_TARGET, "TelemetryService::init has been started");
Expand All @@ -122,9 +144,7 @@ impl TelemetryService {
Box::pin(send_telemetry_data(
telemetry_data.clone(),
telemetry_api_url.clone(),
app_id.clone(),
version.clone(),
user.clone(),
system_info.clone(),
))
},
3,
Expand Down Expand Up @@ -169,37 +189,33 @@ impl TelemetryService {
}
}

async fn send_telemetry_data(
data: TelemetryData,
api_url: String,
#[derive(Clone)]
struct SystemInfo {
app_id: String,
version: String,
user_id: String,
os: CurrentOperatingSystem,
cpu_name: String,
gpu_name: String,
}

async fn send_telemetry_data(
data: TelemetryData,
api_url: String,
system_info: SystemInfo,
) -> Result<(), TelemetryServiceError> {
let request = reqwest::Client::new();
let hardware = HardwareStatusMonitor::current();
let cpu_name = hardware.get_cpu_devices().await?;
let cpu_name = match cpu_name.first() {
Some(cpu) => cpu.public_properties.name.clone(),
None => "Unknown".to_string(),
};
let gpu_name = hardware.get_gpu_devices().await?;
let gpu_name = match gpu_name.first() {
Some(gpu) => gpu.public_properties.name.clone(),
None => "Unknown".to_string(),
};
let os = PlatformUtils::detect_current_os();

let full_data = FullTelemetryData {
event_name: data.event_name,
event_value: data.event_value,
created_at: SystemTime::now(),
user_id,
app_id,
version,
os: os.to_string(),
cpu_name,
gpu_name,
user_id: system_info.user_id,
app_id: system_info.app_id,
version: system_info.version,
os: system_info.os.to_string(),
cpu_name: system_info.cpu_name,
gpu_name: system_info.gpu_name,
};
let request_builder = request
.post(api_url)
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/utils/platform_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use std::fmt::Display;

#[derive(Clone)]
pub enum CurrentOperatingSystem {
Windows,
Linux,
Expand Down

0 comments on commit 7c5994c

Please sign in to comment.