1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: GPL-3.0
use reqwest::Client;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::{json, Value};
use std::{
	env,
	fs::{create_dir_all, File},
	io,
	io::{Read, Write},
	path::PathBuf,
};
use thiserror::Error;

const ENDPOINT: &str = "https://insights.onpop.io/api/send";
const WEBSITE_ID: &str = "0cbea0ba-4752-45aa-b3cd-8fd11fa722f7";
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Error, Debug)]
pub enum TelemetryError {
	#[error("a reqwest error occurred: {0}")]
	NetworkError(reqwest::Error),
	#[error("io error occurred: {0}")]
	IO(io::Error),
	#[error("opt-out has been set, can not report metrics")]
	OptedOut,
	#[error("unable to find config file")]
	ConfigFileNotFound,
	#[error("serialization failed: {0}")]
	SerializeFailed(String),
}

pub type Result<T> = std::result::Result<T, TelemetryError>;

#[derive(Debug, Clone)]
pub struct Telemetry {
	// Endpoint to the telemetry API.
	// This should include the domain and api path (e.g. localhost:3000/api/send)
	endpoint: String,
	// Has the user opted-out to anonymous telemetry
	opt_out: bool,
	// Reqwest client
	client: Client,
}

impl Telemetry {
	/// Create a new Telemetry instance.
	///
	/// parameters:
	/// `config_path`: the path to the configuration file (used for opt-out checks)
	pub fn new(config_path: &PathBuf) -> Self {
		Self::init(ENDPOINT.to_string(), config_path)
	}

	/// Initialize a new Telemetry instance with parameters.
	/// Can be used in tests to provide mock endpoints.
	/// parameters:
	/// `endpoint`: the API endpoint that telemetry will call
	///	`config_path`: the path to the configuration file (used for opt-out checks)
	fn init(endpoint: String, config_path: &PathBuf) -> Self {
		let opt_out = Self::is_opt_out(&config_path);

		Telemetry { endpoint, opt_out, client: Client::new() }
	}

	fn is_opt_out_from_config(config_file_path: &PathBuf) -> bool {
		let config: Config = match read_json_file(config_file_path) {
			Ok(config) => config,
			Err(err) => {
				log::debug!("{:?}", err.to_string());
				return false;
			},
		};

		// if the version is empty, then the user has not opted out
		!config.opt_out.version.is_empty()
	}

	// Checks two env variables, CI & DO_NOT_TRACK. If either are set to true, disable telemetry
	fn is_opt_out_from_env() -> bool {
		// CI first as it is more likely to be set
		let ci = env::var("CI").unwrap_or_default();
		let do_not_track = env::var("DO_NOT_TRACK").unwrap_or_default();
		ci == "true" || ci == "1" || do_not_track == "true" || do_not_track == "1"
	}

	/// Check if the user has opted out of telemetry through two methods:
	/// 1. Check environment variable DO_NOT_TRACK. If not set check...
	/// 2. Configuration file
	fn is_opt_out(config_file_path: &PathBuf) -> bool {
		Self::is_opt_out_from_env() || Self::is_opt_out_from_config(config_file_path)
	}

	/// Send JSON payload to saved api endpoint.
	/// Returns error and will not send anything if opt-out is true.
	/// Returns error from reqwest if the sending fails.
	/// It sends message only once as "best effort". There is no retry on error
	/// in order to keep overhead to a minimal.
	async fn send_json(&self, payload: Value) -> Result<()> {
		if self.opt_out {
			return Err(TelemetryError::OptedOut);
		}

		let request_builder = self.client.post(&self.endpoint);

		request_builder
			.json(&payload)
			.send()
			.await
			.map_err(TelemetryError::NetworkError)?;

		Ok(())
	}
}

/// Generically reports that the CLI was used to the telemetry endpoint.
/// There is explicitly no reqwest retries on failure to ensure overhead
/// stays to a minimum.
pub async fn record_cli_used(tel: Telemetry) -> Result<()> {
	let payload = generate_payload("", json!({}));

	let res = tel.send_json(payload).await;
	log::debug!("send_cli_used result: {:?}", res);

	res
}

/// Reports what CLI command was called to telemetry.
///
/// parameters:
/// `command_name`: the name of the command entered (new, up, build, etc)
/// `data`: the JSON representation of subcommands. This should never include any user inputted
/// data like a file name.
pub async fn record_cli_command(tel: Telemetry, command_name: &str, data: Value) -> Result<()> {
	let payload = generate_payload(command_name, data);

	let res = tel.send_json(payload).await;
	log::debug!("send_cli_used result: {:?}", res);

	res
}

#[derive(PartialEq, Serialize, Deserialize, Debug)]
struct OptOut {
	// what telemetry version did they opt-out for
	version: String,
}

/// Type to represent pop cli configuration.
/// This will be written as json to a config.json file.
#[derive(PartialEq, Serialize, Deserialize, Debug)]
pub struct Config {
	opt_out: OptOut,
}

/// Returns the configuration file path based on OS's default config directory.
pub fn config_file_path() -> Result<PathBuf> {
	let config_path = dirs::config_dir().ok_or(TelemetryError::ConfigFileNotFound)?.join("pop");
	// Creates pop dir if needed
	create_dir_all(config_path.as_path()).map_err(|err| TelemetryError::IO(err))?;
	Ok(config_path.join("config.json"))
}

/// Writes opt-out to the configuration file at the specified path.
/// opt-out is currently the only config type. Hence, if the file exists, it will be overwritten.
///
/// parameters:
/// `config_path`: the path to write the config file to
pub fn write_config_opt_out(config_path: &PathBuf) -> Result<()> {
	let config = Config { opt_out: OptOut { version: CARGO_PKG_VERSION.to_string() } };

	let config_json = serde_json::to_string_pretty(&config)
		.map_err(|err| TelemetryError::SerializeFailed(err.to_string()))?;

	// overwrites file if it exists
	let mut file = File::create(&config_path).map_err(|err| TelemetryError::IO(err))?;
	file.write_all(config_json.as_bytes()).map_err(|err| TelemetryError::IO(err))?;

	Ok(())
}

fn read_json_file<T>(file_path: &PathBuf) -> std::result::Result<T, io::Error>
where
	T: DeserializeOwned,
{
	let mut file = File::open(file_path)?;

	let mut json = String::new();
	file.read_to_string(&mut json)?;

	let deserialized: T = serde_json::from_str(&json)?;

	Ok(deserialized)
}

fn generate_payload(event_name: &str, data: Value) -> Value {
	json!({
		"payload": {
			"hostname": "cli",
			"language": "en-US",
			"referrer": "",
			"screen": "1920x1080",
			"title": CARGO_PKG_VERSION,
			"url": "/",
			"website": WEBSITE_ID,
			"name": event_name,
			"data": data
		},
		"type": "event"
	})
}

#[cfg(test)]
mod tests {

	use super::*;
	use mockito::{Matcher, Mock, Server};
	use serde_json::json;
	use tempfile::TempDir;

	fn create_temp_config(temp_dir: &TempDir) -> Result<PathBuf> {
		let config_path = temp_dir.path().join("config.json");
		write_config_opt_out(&config_path)?;
		Ok(config_path)
	}
	async fn default_mock(mock_server: &mut Server, payload: String) -> Mock {
		mock_server
			.mock("POST", "/api/send")
			.match_header("content-type", "application/json")
			.match_header("accept", "*/*")
			.match_body(Matcher::JsonString(payload.clone()))
			.match_header("content-length", payload.len().to_string().as_str())
			.match_header("host", mock_server.host_with_port().trim())
			.create_async()
			.await
	}

	#[tokio::test]
	async fn write_config_opt_out_works() -> Result<()> {
		// Mock config file path function to return a temporary path
		let temp_dir = TempDir::new().unwrap();
		let config_path = create_temp_config(&temp_dir)?;

		let actual_config: Config = read_json_file(&config_path).unwrap();
		let expected_config = Config { opt_out: OptOut { version: CARGO_PKG_VERSION.to_string() } };

		assert_eq!(actual_config, expected_config);
		Ok(())
	}

	#[tokio::test]
	async fn new_telemetry_works() -> Result<()> {
		let _ = env_logger::try_init();

		// Mock config file path function to return a temporary path
		let temp_dir = TempDir::new().unwrap();
		// write a config file with opt-out set
		let config_path = create_temp_config(&temp_dir)?;

		let _: Config = read_json_file(&config_path).unwrap();

		let tel = Telemetry::init("127.0.0.1".to_string(), &config_path);
		let expected_telemetry = Telemetry {
			endpoint: "127.0.0.1".to_string(),
			opt_out: true,
			client: Default::default(),
		};

		assert_eq!(tel.endpoint, expected_telemetry.endpoint);
		assert_eq!(tel.opt_out, expected_telemetry.opt_out);

		let tel = Telemetry::new(&config_path);

		let expected_telemetry =
			Telemetry { endpoint: ENDPOINT.to_string(), opt_out: true, client: Default::default() };

		assert_eq!(tel.endpoint, expected_telemetry.endpoint);
		assert_eq!(tel.opt_out, expected_telemetry.opt_out);
		Ok(())
	}

	#[test]
	fn new_telemetry_env_vars_works() {
		let _ = env_logger::try_init();

		// assert that no config file, and env vars not existing sets opt-out to false
		env::remove_var("DO_NOT_TRACK");
		env::set_var("CI", "false");
		assert!(!Telemetry::init("".to_string(), &PathBuf::new()).opt_out);

		// assert that if DO_NOT_TRACK env var is set, opt-out is true
		env::set_var("DO_NOT_TRACK", "true");
		assert!(Telemetry::init("".to_string(), &PathBuf::new()).opt_out);
		env::remove_var("DO_NOT_TRACK");

		// assert that if CI env var is set, opt-out is true
		env::set_var("CI", "true");
		assert!(Telemetry::init("".to_string(), &PathBuf::new()).opt_out);
		env::remove_var("CI");
	}

	#[tokio::test]
	async fn test_record_cli_used() -> Result<()> {
		let _ = env_logger::try_init();
		let mut mock_server = Server::new_async().await;

		let mut endpoint = mock_server.url();
		endpoint.push_str("/api/send");

		// Mock config file path function to return a temporary path
		let temp_dir = TempDir::new().unwrap();
		let config_path = temp_dir.path().join("config.json");

		let expected_payload = generate_payload("", json!({})).to_string();

		let mock = default_mock(&mut mock_server, expected_payload).await;

		let mut tel = Telemetry::init(endpoint.clone(), &config_path);
		tel.opt_out = false; // override as endpoint is mocked

		record_cli_used(tel).await?;
		mock.assert_async().await;
		Ok(())
	}

	#[tokio::test]
	async fn test_record_cli_command() -> Result<()> {
		let _ = env_logger::try_init();
		let mut mock_server = Server::new_async().await;

		let mut endpoint = mock_server.url();
		endpoint.push_str("/api/send");

		// Mock config file path function to return a temporary path
		let temp_dir = TempDir::new().unwrap();

		let config_path = temp_dir.path().join("config.json");

		let expected_payload = generate_payload("new", json!("parachain")).to_string();

		let mock = default_mock(&mut mock_server, expected_payload).await;

		let mut tel = Telemetry::init(endpoint.clone(), &config_path);
		tel.opt_out = false; // override as endpoint is mocked

		record_cli_command(tel, "new", json!("parachain")).await?;
		mock.assert_async().await;
		Ok(())
	}

	#[tokio::test]
	async fn opt_out_set_fails() {
		let _ = env_logger::try_init();
		let mut mock_server = Server::new_async().await;

		let endpoint = mock_server.url();

		let mock = mock_server.mock("POST", "/").create_async().await;
		let mock = mock.expect_at_most(0);

		let mut tel = Telemetry::init(endpoint.clone(), &PathBuf::new());
		tel.opt_out = true;

		assert!(matches!(tel.send_json(Value::Null).await, Err(TelemetryError::OptedOut)));
		assert!(matches!(record_cli_used(tel.clone()).await, Err(TelemetryError::OptedOut)));
		assert!(matches!(
			record_cli_command(tel.clone(), "foo", Value::Null).await,
			Err(TelemetryError::OptedOut)
		));
		mock.assert_async().await;
	}
}