Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multiple issues around analytics #985

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions server/src/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
*
*/

use crate::analytics;
use crate::option::Config;
use crate::storage::StorageMetadata;
use crate::utils::update::{self, LatestRelease};
use chrono::Duration;
use chrono_humanize::{Accuracy, Tense};
use crossterm::style::Stylize;
use once_cell::sync::OnceCell;
use std::env;
use std::path::Path;
use sysinfo::System;
use ulid::Ulid;

use crate::analytics;
use crate::option::Config;
use crate::storage::StorageMetadata;
use crate::utils::update;

// Expose some static variables for internal usage
pub static LATEST_RELEASE: OnceCell<Option<LatestRelease>> = OnceCell::new();
static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST";

fn is_docker() -> bool {
Expand All @@ -50,6 +51,18 @@ pub fn platform() -> &'static str {
}
}

pub fn set_latest_release(latest_release: Option<LatestRelease>) {
LATEST_RELEASE
.set(latest_release.clone())
.expect("only set once")
}

pub fn get_latest_release() -> &'static Option<LatestRelease> {
LATEST_RELEASE
.get()
.expect("latest release is fetched from global state")
}

// User Agent for Download API call
// Format: Parseable/<UID>/<version>/<commit_hash> (<OS>; <Platform>)
pub fn user_agent(uid: &Ulid) -> String {
Expand Down Expand Up @@ -122,7 +135,7 @@ pub async fn print(config: &Config, meta: &StorageMetadata) {
} else {
None
};

set_latest_release(latest_release.clone());
print_about(
current.released_version,
latest_release,
Expand Down
9 changes: 8 additions & 1 deletion server/src/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use crate::about::{current, platform};
use crate::handlers::http::cluster::utils::check_liveness;
use crate::handlers::http::{base_path_without_preceding_slash, cluster};
use crate::handlers::STREAM_NAME_HEADER_KEY;
use crate::option::{Mode, CONFIG};
use crate::storage;
use crate::{metadata, stats};
Expand Down Expand Up @@ -131,7 +132,13 @@ impl Report {

pub async fn send(&self) {
let client = reqwest::Client::new();
let _ = client.post(ANALYTICS_SERVER_URL).json(&self).send().await;

let _ = client
.post(ANALYTICS_SERVER_URL)
.header(STREAM_NAME_HEADER_KEY, "serverusageevent")
.json(&self)
.send()
.await;
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod livetail;

const PREFIX_TAGS: &str = "x-p-tag-";
const PREFIX_META: &str = "x-p-meta-";
const STREAM_NAME_HEADER_KEY: &str = "x-p-stream";
pub const STREAM_NAME_HEADER_KEY: &str = "x-p-stream";
const CACHE_RESULTS_HEADER_KEY: &str = "x-p-cache-results";
const CACHE_VIEW_HEADER_KEY: &str = "x-p-show-cached";
const USER_ID_HEADER_KEY: &str = "x-p-user-id";
Expand Down
10 changes: 4 additions & 6 deletions server/src/handlers/http/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ use actix_web::web::Json;
use serde_json::json;

use crate::{
about,
about::{self, get_latest_release},
option::{Mode, CONFIG},
storage::StorageMetadata,
utils::update,
};
use std::path::PathBuf;

Expand Down Expand Up @@ -51,14 +50,13 @@ pub async fn about() -> Json<serde_json::Value> {
let meta = StorageMetadata::global();

let current_release = about::current();
let latest_release = update::get_latest(&meta.deployment_id).await;

let latest_release = get_latest_release();
let (update_available, latest_release) = match latest_release {
Ok(latest_release) => (
Some(latest_release) => (
latest_release.version > current_release.released_version,
Some(format!("v{}", latest_release.version)),
),
Err(_) => (false, None),
None => (false, None),
};

let current_version = format!("v{}", current_release.released_version);
Expand Down
5 changes: 1 addition & 4 deletions server/src/utils/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::about;

use super::uid;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct LatestRelease {
pub version: semver::Version,
pub date: DateTime<Utc>,
Expand All @@ -37,20 +37,17 @@ pub async fn get_latest(deployment_id: &uid::Uid) -> Result<LatestRelease, anyho
.timeout(Duration::from_secs(8))
.build()
.expect("client can be built on this system");

let json: serde_json::Value = agent
.get("https://download.parseable.io/latest-version")
.send()
.await?
.json()
.await?;

let version = json["tag_name"]
.as_str()
.and_then(|ver| ver.strip_prefix('v'))
.and_then(|ver| semver::Version::parse(ver).ok())
.ok_or_else(|| anyhow!("Failed parsing version"))?;

let date = json["published_at"]
.as_str()
.ok_or_else(|| anyhow!("Failed parsing published date"))?;
Expand Down
Loading