Skip to content

Commit

Permalink
[rust] Selenium Manager decrease frequency of statistics reporting (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia authored Feb 8, 2024
1 parent a6da4aa commit f38718b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
22 changes: 18 additions & 4 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(())
}
Expand Down
42 changes: 42 additions & 0 deletions rust/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -45,6 +57,7 @@ pub struct Driver {
pub struct Metadata {
pub browsers: Vec<Browser>,
pub drivers: Vec<Driver>,
pub stats: Vec<Stats>,
}

fn get_metadata_path(cache_path: PathBuf) -> PathBuf {
Expand All @@ -67,6 +80,7 @@ fn new_metadata(log: &Logger) -> Metadata {
Metadata {
browsers: Vec::new(),
drivers: Vec::new(),
stats: Vec::new(),
}
}

Expand All @@ -82,6 +96,7 @@ pub fn get_metadata(log: &Logger, cache_path: &Option<PathBuf>) -> 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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<PathBuf>) {
if let Some(cache) = cache_path {
let metadata_path = get_metadata_path(cache.clone());
Expand Down
4 changes: 2 additions & 2 deletions rust/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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,
Expand Down

0 comments on commit f38718b

Please sign in to comment.