Skip to content

Commit

Permalink
added "--teach-me" to track_usage()
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Sep 26, 2024
1 parent 4ebdcb1 commit 8d36e3d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
13 changes: 4 additions & 9 deletions cargo-near/src/commands/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ impl NewContext {
.wrap_err_with(|| format!("Failed to write to file: {}", new_file_path.display()))?;
}

track_request();
let _detached_thread_handle = std::thread::Builder::new()
.spawn(mixpanel_tracking::track_usage)
.unwrap();

execute_git_commands(project_dir)?;

println!("New project is created at '{}'.\n", project_dir.display());
Expand All @@ -81,14 +84,6 @@ impl NewContext {
}
}

#[tracing::instrument(target = "tracing_instrument", name = "Sending a tracking request ...")]
fn track_request() {
tracing::Span::current();
let _detached_thread_handle = std::thread::Builder::new()
.spawn(mixpanel_tracking::track_usage)
.unwrap();
}

#[tracing::instrument(
target = "tracing_instrument",
name = "The process of executing",
Expand Down
49 changes: 45 additions & 4 deletions cargo-near/src/mixpanel_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use base64::{engine::general_purpose, Engine as _};
use reqwest::{header::HeaderMap, Client};
use serde::Serialize;
use std::{env, str};
use tracing::debug;
use tracing_indicatif::span_ext::IndicatifSpanExt;

const SEND_TRACKING_REQUEST_ERROR: &str = "Can't send tracking usage event";

Expand All @@ -19,7 +19,14 @@ struct TrackingData {
properties: MixpanelProperties,
}

#[tracing::instrument(
target = "tracing_instrument",
name = "Sending a tracking request using Mixpanel via url"
)]
pub(crate) fn track_usage() {
tracing::Span::current().pb_set_message("https://api.mixpanel.com/track ...");
tracing::info!(target: "near_teach_me", "https://api.mixpanel.com/track ...");

let properties = MixpanelProperties {
token: "24177ef1ec09ffea5cb6f68909c66a61".to_string(),
pkg_version: env!("CARGO_PKG_VERSION").to_string(),
Expand All @@ -34,6 +41,26 @@ pub(crate) fn track_usage() {

let client = Client::new();

let request_payload = serde_json::json!({
"base64_encoded_tracking_data": base64_encoded_data
});
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"I am making HTTP call to broadcast the tracking data, learn more https://mixpanel.com/"
);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"HTTP GET https://api.mixpanel.com/track",
);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"JSON Body:\n{}",
near_cli_rs::common::indent_payload(&format!("{:#}", request_payload))
);

let mut headers = HeaderMap::new();
headers.insert("accept", "text/plain".parse().unwrap());
headers.insert("content-type", "application/json".parse().unwrap());
Expand All @@ -43,13 +70,27 @@ pub(crate) fn track_usage() {
.block_on(
client
.get("https://api.mixpanel.com/track")
.query(&[("data", base64_encoded_data)])
.headers(headers)
.query(&[("data", &base64_encoded_data)])
.headers(headers.clone())
.send(),
)
.inspect(|response| {
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"JSON Response:\n{}",
near_cli_rs::common::indent_payload(&format!("{:#?}", response))
);
})
.is_err()
{
debug!(SEND_TRACKING_REQUEST_ERROR)
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"JSON RPC Response:\n{}",
near_cli_rs::common::indent_payload(SEND_TRACKING_REQUEST_ERROR)
);
tracing::debug!(SEND_TRACKING_REQUEST_ERROR)
}
}

Expand Down

0 comments on commit 8d36e3d

Please sign in to comment.