Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: multiple fixes around analytics
1. fix for multiple calls happening to analytics server global variable to hold latest version sets when server starts get from global variable in about api call 2. send `X-P-Stream` header in POST call to analytics server resolves the issue of hardcoded stream in analytics server code
  • Loading branch information
nikhilsinhaparseable committed Nov 10, 2024
commit a8ef16efef75ae9daa17819c832cad0680c33c0f
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
8 changes: 7 additions & 1 deletion server/src/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,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("X-P-Stream", "serverusageevent")
.json(&self)
.send()
.await;
}
}

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