2022-03-10 10:31:55 +00:00
|
|
|
import config, { ConfigKeys, TelemetrySetting } from '@/config';
|
2022-03-09 11:20:48 +00:00
|
|
|
import frappe from 'frappe';
|
2022-03-09 10:54:42 +00:00
|
|
|
import { cloneDeep } from 'lodash';
|
2022-03-09 10:13:17 +00:00
|
|
|
import { getCounts, getDeviceId, getInstanceId, getLocale } from './helpers';
|
|
|
|
import { Noun, Telemetry, Verb } from './types';
|
|
|
|
|
|
|
|
class TelemetryManager {
|
2022-03-14 12:56:26 +00:00
|
|
|
#url: string = '';
|
|
|
|
#token: string = '';
|
2022-03-09 10:13:17 +00:00
|
|
|
#started = false;
|
|
|
|
#telemetryObject: Partial<Telemetry> = {};
|
|
|
|
|
|
|
|
start() {
|
2022-03-10 12:06:28 +00:00
|
|
|
this.#telemetryObject.locale = getLocale();
|
2022-03-09 10:54:42 +00:00
|
|
|
this.#telemetryObject.deviceId ||= getDeviceId();
|
|
|
|
this.#telemetryObject.instanceId ||= getInstanceId();
|
|
|
|
this.#telemetryObject.openTime ||= new Date().valueOf();
|
|
|
|
this.#telemetryObject.timeline ??= [];
|
2022-03-09 11:20:48 +00:00
|
|
|
this.#telemetryObject.errors ??= {};
|
2022-03-09 10:13:17 +00:00
|
|
|
this.#started = true;
|
|
|
|
}
|
|
|
|
|
2022-03-10 09:05:47 +00:00
|
|
|
getCanLog(): boolean {
|
|
|
|
const telemetrySetting = config.get(ConfigKeys.Telemetry) as string;
|
2022-03-10 10:31:55 +00:00
|
|
|
return telemetrySetting === TelemetrySetting.allow;
|
2022-03-10 07:51:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 12:56:26 +00:00
|
|
|
setCreds(url: string, token: string) {
|
|
|
|
this.#url ||= url;
|
|
|
|
this.#token ||= token;
|
|
|
|
}
|
|
|
|
|
2022-03-09 10:13:17 +00:00
|
|
|
log(verb: Verb, noun: Noun, more?: Record<string, unknown>) {
|
|
|
|
if (!this.#started) {
|
|
|
|
this.start();
|
|
|
|
}
|
|
|
|
|
2022-03-10 09:05:47 +00:00
|
|
|
if (!this.getCanLog()) {
|
2022-03-10 07:51:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-09 10:13:17 +00:00
|
|
|
const time = new Date().valueOf();
|
|
|
|
if (this.#telemetryObject.timeline === undefined) {
|
|
|
|
this.#telemetryObject.timeline = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#telemetryObject.timeline.push({ time, verb, noun, more });
|
|
|
|
}
|
2022-03-09 10:54:42 +00:00
|
|
|
|
2022-03-09 11:20:48 +00:00
|
|
|
error(name: string) {
|
|
|
|
if (this.#telemetryObject.errors === undefined) {
|
|
|
|
this.#telemetryObject.errors = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#telemetryObject.errors[name] ??= 0;
|
|
|
|
this.#telemetryObject.errors[name] += 1;
|
|
|
|
}
|
|
|
|
|
2022-03-10 12:06:28 +00:00
|
|
|
async setCount() {
|
|
|
|
this.#telemetryObject.counts = this.getCanLog() ? await getCounts() : {};
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2022-03-09 10:54:42 +00:00
|
|
|
// Will set ids if not set.
|
|
|
|
this.start();
|
|
|
|
|
2022-03-09 11:20:48 +00:00
|
|
|
//@ts-ignore
|
|
|
|
this.#telemetryObject.version = frappe.store.appVersion ?? '';
|
2022-03-09 10:13:17 +00:00
|
|
|
this.#telemetryObject.closeTime = new Date().valueOf();
|
2022-03-10 09:05:47 +00:00
|
|
|
|
2022-03-10 12:06:28 +00:00
|
|
|
const telemetryObject = this.#telemetryObject;
|
|
|
|
|
|
|
|
this.#started = false;
|
|
|
|
this.#telemetryObject = {};
|
|
|
|
|
2022-03-10 10:31:55 +00:00
|
|
|
if (config.get(ConfigKeys.Telemetry) === TelemetrySetting.dontLogAnything) {
|
2022-03-10 12:06:28 +00:00
|
|
|
return '';
|
2022-03-10 09:05:47 +00:00
|
|
|
}
|
2022-03-10 12:06:28 +00:00
|
|
|
|
2022-03-14 12:56:26 +00:00
|
|
|
const data = JSON.stringify({
|
|
|
|
token: this.#token,
|
|
|
|
telemetryData: telemetryObject,
|
|
|
|
});
|
|
|
|
|
|
|
|
return { url: this.#url, data };
|
2022-03-09 10:13:17 +00:00
|
|
|
}
|
2022-03-09 10:54:42 +00:00
|
|
|
|
|
|
|
get telemetryObject(): Readonly<Partial<Telemetry>> {
|
|
|
|
return cloneDeep(this.#telemetryObject);
|
|
|
|
}
|
2022-03-09 10:13:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 10:54:42 +00:00
|
|
|
export default new TelemetryManager();
|