From f38718b59b61b616e12a0b01de7e05fac3d53892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boni=20Garc=C3=ADa?= Date: Thu, 8 Feb 2024 17:48:03 +0100 Subject: [PATCH] [rust] Selenium Manager decrease frequency of statistics reporting (#13555) --- rust/src/lib.rs | 22 ++++++++++++++++++---- rust/src/metadata.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ rust/src/stats.rs | 4 ++-- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 226a37dc4dda4..6a9e5463b23bb 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -31,7 +31,8 @@ use crate::grid::GRID_NAME; use crate::iexplorer::{IExplorerManager, IEDRIVER_NAME, IE_NAMES}; use crate::logger::Logger; use crate::metadata::{ - create_browser_metadata, get_browser_version_from_metadata, get_metadata, write_metadata, + create_browser_metadata, create_stats_metadata, get_browser_version_from_metadata, + get_metadata, is_stats_in_metadata, write_metadata, }; use crate::safari::{SafariManager, SAFARIDRIVER_NAME, SAFARI_NAME}; use crate::safaritp::{SafariTPManager, SAFARITP_NAMES}; @@ -846,9 +847,22 @@ pub trait SeleniumManager { }; let http_client = self.get_http_client().to_owned(); let sender = self.get_sender().to_owned(); - thread::spawn(move || { - send_stats_to_plausible(http_client, props, sender); - }); + let cache_path = self.get_cache_path()?; + let mut metadata = get_metadata(self.get_logger(), &cache_path); + if !is_stats_in_metadata(&metadata.stats, &props) { + self.get_logger() + .debug(format!("Sending stats to Plausible: {:?}", props,)); + let stats_ttl = self.get_ttl(); + if stats_ttl > 0 { + metadata + .stats + .push(create_stats_metadata(&props, stats_ttl)); + write_metadata(&metadata, self.get_logger(), cache_path); + } + thread::spawn(move || { + send_stats_to_plausible(http_client, props, sender); + }); + } } Ok(()) } diff --git a/rust/src/metadata.rs b/rust/src/metadata.rs index 10b1d203f7f70..52971774f5e17 100644 --- a/rust/src/metadata.rs +++ b/rust/src/metadata.rs @@ -15,6 +15,7 @@ // specific language governing permissions and limitations // under the License. +use crate::stats::Props; use crate::Logger; use serde::{Deserialize, Serialize}; use std::fs; @@ -25,6 +26,17 @@ use std::time::{SystemTime, UNIX_EPOCH}; const METADATA_FILE_OLD: &str = "selenium-manager.json"; const METADATA_FILE: &str = "se-metadata.json"; +#[derive(Serialize, Deserialize)] +pub struct Stats { + pub browser: String, + pub browser_version: String, + pub os: String, + pub arch: String, + pub lang: String, + pub selenium_version: String, + pub stats_ttl: u64, +} + #[derive(Serialize, Deserialize)] pub struct Browser { pub browser_name: String, @@ -45,6 +57,7 @@ pub struct Driver { pub struct Metadata { pub browsers: Vec, pub drivers: Vec, + pub stats: Vec, } fn get_metadata_path(cache_path: PathBuf) -> PathBuf { @@ -67,6 +80,7 @@ fn new_metadata(log: &Logger) -> Metadata { Metadata { browsers: Vec::new(), drivers: Vec::new(), + stats: Vec::new(), } } @@ -82,6 +96,7 @@ pub fn get_metadata(log: &Logger, cache_path: &Option) -> Metadata { let now = now_unix_timestamp(); meta.browsers.retain(|b| b.browser_ttl > now); meta.drivers.retain(|d| d.driver_ttl > now); + meta.stats.retain(|s| s.stats_ttl > now); meta } Err(_e) => new_metadata(log), // Empty metadata @@ -128,6 +143,21 @@ pub fn get_driver_version_from_metadata( } } +pub fn is_stats_in_metadata(stats_metadata: &[Stats], props: &Props) -> bool { + let props: Vec<&Stats> = stats_metadata + .iter() + .filter(|p| { + p.browser.eq(&props.browser) + && p.browser_version.eq(&props.browser_version) + && p.os.eq(&props.os) + && p.arch.eq(&props.arch) + && p.lang.eq(&props.lang) + && p.selenium_version.eq(&props.selenium_version) + }) + .collect(); + !props.is_empty() +} + pub fn create_browser_metadata( browser_name: &str, major_browser_version: &str, @@ -156,6 +186,18 @@ pub fn create_driver_metadata( } } +pub fn create_stats_metadata(props: &Props, stats_ttl: u64) -> Stats { + Stats { + browser: props.browser.to_string(), + browser_version: props.browser_version.to_string(), + os: props.os.to_string(), + arch: props.arch.to_string(), + lang: props.lang.to_string(), + selenium_version: props.selenium_version.to_string(), + stats_ttl: now_unix_timestamp() + stats_ttl, + } +} + pub fn write_metadata(metadata: &Metadata, log: &Logger, cache_path: Option) { if let Some(cache) = cache_path { let metadata_path = get_metadata_path(cache.clone()); diff --git a/rust/src/stats.rs b/rust/src/stats.rs index ff2ca7517133d..8e747ec6c8465 100644 --- a/rust/src/stats.rs +++ b/rust/src/stats.rs @@ -28,7 +28,7 @@ const SM_USER_AGENT: &str = "Selenium Manager {}"; const APP_JSON: &str = "application/json"; const PAGE_VIEW: &str = "pageview"; const SELENIUM_DOMAIN: &str = "manager.selenium.dev"; -const SM_STATS_URL: &str = "https://{}/sm-stats"; +const SM_STATS_URL: &str = "https://{}/sm-usage"; const REQUEST_TIMEOUT_SEC: u64 = 3; #[derive(Default, Serialize, Deserialize)] @@ -39,7 +39,7 @@ pub struct Data { pub props: Props, } -#[derive(Default, Serialize, Deserialize)] +#[derive(Default, Debug, Serialize, Deserialize)] pub struct Props { pub browser: String, pub browser_version: String,