Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/rust/src/stats.rs
2885 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
use crate::format_one_arg;
19
use reqwest::header::CONTENT_TYPE;
20
use reqwest::header::USER_AGENT;
21
use reqwest::Client;
22
use serde::{Deserialize, Serialize};
23
use std::sync::mpsc::Sender;
24
use std::time::Duration;
25
26
const PLAUSIBLE_URL: &str = "https://plausible.io/api/event";
27
const SM_USER_AGENT: &str = "Selenium Manager {}";
28
const APP_JSON: &str = "application/json";
29
const PAGE_VIEW: &str = "pageview";
30
const SELENIUM_DOMAIN: &str = "manager.selenium.dev";
31
const SM_STATS_URL: &str = "https://{}/sm-usage";
32
const REQUEST_TIMEOUT_SEC: u64 = 3;
33
34
#[derive(Default, Serialize, Deserialize)]
35
pub struct Data {
36
pub name: String,
37
pub url: String,
38
pub domain: String,
39
pub props: Props,
40
}
41
42
#[derive(Default, Debug, Serialize, Deserialize)]
43
pub struct Props {
44
pub browser: String,
45
pub browser_version: String,
46
pub os: String,
47
pub arch: String,
48
pub lang: String,
49
pub selenium_version: String,
50
}
51
52
#[tokio::main]
53
pub async fn send_stats_to_plausible(http_client: Client, props: Props, sender: Sender<String>) {
54
let user_agent = format_one_arg(SM_USER_AGENT, &props.selenium_version);
55
let sm_stats_url = format_one_arg(SM_STATS_URL, SELENIUM_DOMAIN);
56
let data = Data {
57
name: PAGE_VIEW.to_string(),
58
url: sm_stats_url,
59
domain: SELENIUM_DOMAIN.to_string(),
60
props,
61
};
62
let body = serde_json::to_string(&data).unwrap_or_default();
63
64
let request = http_client
65
.post(PLAUSIBLE_URL)
66
.header(USER_AGENT, user_agent)
67
.header(CONTENT_TYPE, APP_JSON)
68
.timeout(Duration::from_secs(REQUEST_TIMEOUT_SEC))
69
.body(body);
70
71
if let Err(err) = request.send().await {
72
sender
73
.send(format!("Error sending stats to Plausible: {}", err))
74
.unwrap_or_default();
75
}
76
}
77
78