From 11f192f22acc38fc37c688dfe80d2adea4bc88bf Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 9 Mar 2022 16:24:42 +0530 Subject: [PATCH] fix: allow post start start incase ids not set --- src/telemetry/helpers.ts | 8 ++++++-- src/telemetry/telemetry.ts | 24 +++++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/telemetry/helpers.ts b/src/telemetry/helpers.ts index 17ad9183..1314894d 100644 --- a/src/telemetry/helpers.ts +++ b/src/telemetry/helpers.ts @@ -14,7 +14,7 @@ export function getId(): string { export function getLocale(): Locale { // @ts-ignore - const country: string = frappe.AccountingSettings.country; + const country: string = frappe.AccountingSettings?.country ?? ''; const language: string = config.get('language') as string; return { country, language }; @@ -76,7 +76,11 @@ export function getInstanceId(): UniqueId { const files = config.get(ConfigKeys.Files) as ConfigFile[]; // @ts-ignore - const companyName = frappe.AccountingSettings.companyName; + const companyName = frappe.AccountingSettings?.companyName; + if (companyName === undefined) { + return ''; + } + const file = files.find((f) => f.companyName === companyName); if (file === undefined) { diff --git a/src/telemetry/telemetry.ts b/src/telemetry/telemetry.ts index 080b4a56..bd826eef 100644 --- a/src/telemetry/telemetry.ts +++ b/src/telemetry/telemetry.ts @@ -1,3 +1,4 @@ +import { cloneDeep } from 'lodash'; import { getCounts, getDeviceId, getInstanceId, getLocale } from './helpers'; import { Noun, Telemetry, Verb } from './types'; @@ -6,14 +7,11 @@ class TelemetryManager { #telemetryObject: Partial = {}; start() { - if (this.#started) { - return; - } - this.#telemetryObject.locale = getLocale(); - this.#telemetryObject.deviceId = getDeviceId(); - this.#telemetryObject.instanceId = getInstanceId(); - this.#telemetryObject.openTime = new Date().valueOf(); - this.#telemetryObject.timeline = []; + this.#telemetryObject.locale ||= getLocale(); + this.#telemetryObject.deviceId ||= getDeviceId(); + this.#telemetryObject.instanceId ||= getInstanceId(); + this.#telemetryObject.openTime ||= new Date().valueOf(); + this.#telemetryObject.timeline ??= []; this.#started = true; } @@ -29,10 +27,18 @@ class TelemetryManager { this.#telemetryObject.timeline.push({ time, verb, noun, more }); } + async stop() { + // Will set ids if not set. + this.start(); + this.#telemetryObject.counts = await getCounts(); this.#telemetryObject.closeTime = new Date().valueOf(); } + + get telemetryObject(): Readonly> { + return cloneDeep(this.#telemetryObject); + } } -export const telemetryManager = new TelemetryManager(); +export default new TelemetryManager();