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 {
|
|
|
|
#started = false;
|
|
|
|
#telemetryObject: Partial<Telemetry> = {};
|
|
|
|
|
|
|
|
start() {
|
2022-03-09 10:54:42 +00:00
|
|
|
this.#telemetryObject.locale ||= getLocale();
|
|
|
|
this.#telemetryObject.deviceId ||= getDeviceId();
|
|
|
|
this.#telemetryObject.instanceId ||= getInstanceId();
|
|
|
|
this.#telemetryObject.openTime ||= new Date().valueOf();
|
|
|
|
this.#telemetryObject.timeline ??= [];
|
2022-03-09 10:13:17 +00:00
|
|
|
this.#started = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
log(verb: Verb, noun: Noun, more?: Record<string, unknown>) {
|
|
|
|
if (!this.#started) {
|
|
|
|
this.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 10:13:17 +00:00
|
|
|
async stop() {
|
2022-03-09 10:54:42 +00:00
|
|
|
// Will set ids if not set.
|
|
|
|
this.start();
|
|
|
|
|
2022-03-09 10:13:17 +00:00
|
|
|
this.#telemetryObject.counts = await getCounts();
|
|
|
|
this.#telemetryObject.closeTime = new Date().valueOf();
|
|
|
|
}
|
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();
|