diff --git a/fyo/index.ts b/fyo/index.ts index 877e5e77..5d52e0bc 100644 --- a/fyo/index.ts +++ b/fyo/index.ts @@ -13,7 +13,7 @@ import { TelemetryManager } from './telemetry/telemetry'; import { DEFAULT_CURRENCY, DEFAULT_DISPLAY_PRECISION, - DEFAULT_INTERNAL_PRECISION, + DEFAULT_INTERNAL_PRECISION } from './utils/consts'; import * as errors from './utils/errors'; import { format } from './utils/format'; @@ -226,6 +226,10 @@ export class Fyo { skipTelemetryLogging: false, appVersion: '', platform: '', + language: '', + instanceId: '', + deviceId: '', + openCount: -1, }; } diff --git a/fyo/telemetry/helpers.ts b/fyo/telemetry/helpers.ts deleted file mode 100644 index caa9860c..00000000 --- a/fyo/telemetry/helpers.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { Fyo } from 'fyo'; -import { ConfigFile, ConfigKeys } from 'fyo/core/types'; -import { DEFAULT_COUNTRY_CODE } from 'fyo/utils/consts'; -import { ModelNameEnum } from 'models/types'; -import { getRandomString } from 'utils'; -import { UniqueId } from './types'; - -export function getCountry(fyo: Fyo): string { - return ( - (fyo.singles.SystemSettings?.countryCode as string) ?? DEFAULT_COUNTRY_CODE - ); -} - -export function getLanguage(fyo: Fyo): string { - return fyo.config.get('language') as string; -} - -export function getDeviceId(fyo: Fyo): UniqueId { - let deviceId = fyo.config.get(ConfigKeys.DeviceId) as string | undefined; - if (deviceId === undefined) { - deviceId = getRandomString(); - fyo.config.set(ConfigKeys.DeviceId, deviceId); - } - - return deviceId; -} - -export async function getInstanceId(fyo: Fyo): Promise { - const instanceId = (await fyo.getValue( - ModelNameEnum.SystemSettings, - 'instanceId' - )) as string; - const companyName = (await fyo.getValue( - ModelNameEnum.AccountingSettings, - 'companyName' - )) as string; - const dbPath = fyo.db.dbPath!; - const files = (fyo.config.get(ConfigKeys.Files) ?? []) as ConfigFile[]; - - let file = files.find((f) => f.id === instanceId); - - if (file === undefined) { - file = addNewConfigFile(companyName, dbPath, instanceId, files, fyo); - } - - if (!file.id) { - setIdOnConfigFile(instanceId, companyName, dbPath, files, fyo); - } - - return instanceId; -} - -export function addNewConfigFile( - companyName: string, - dbPath: string, - instanceId: string, - files: ConfigFile[], - fyo: Fyo -): ConfigFile { - const newFile: ConfigFile = { - companyName, - dbPath, - id: instanceId, - openCount: 0, - }; - - files.push(newFile); - fyo.config.set(ConfigKeys.Files, files); - return newFile; -} - -export async function getVersion(fyo: Fyo) { - const version = (await fyo.getValue( - ModelNameEnum.SystemSettings, - 'version' - )) as string | undefined; - - if (version) { - return version; - } - - return fyo.store.appVersion; -} - -function setIdOnConfigFile( - instanceId: string, - companyName: string, - dbPath: string, - files: ConfigFile[], - fyo: Fyo -) { - for (const file of files) { - if (file.companyName !== companyName || file.dbPath !== dbPath) { - continue; - } - - file.id = instanceId; - } - - fyo.config.set(ConfigKeys.Files, files); -} diff --git a/fyo/telemetry/telemetry.ts b/fyo/telemetry/telemetry.ts index 2966f4bb..bb84fcd2 100644 --- a/fyo/telemetry/telemetry.ts +++ b/fyo/telemetry/telemetry.ts @@ -1,14 +1,6 @@ import { Fyo } from 'fyo'; -import { cloneDeep } from 'lodash'; -import { DateTime } from 'luxon'; -import { - getCountry, - getDeviceId, - getInstanceId, - getLanguage, - getVersion, -} from './helpers'; -import { Noun, Platform, Telemetry, Verb } from './types'; +import { ConfigKeys } from 'fyo/core/types'; +import { Noun, Telemetry, Verb } from './types'; /** * # Telemetry @@ -19,11 +11,11 @@ import { Noun, Platform, Telemetry, Verb } from './types'; * Used to initialize state. It should be called before any logging and after an * instance has loaded. * It is called on three events: - * 1. When Desk is opened, i.e. when the usage starts, this also sends a started - * log. - * 2. On visibility change if not started, eg: when user minimizeds Books and + * 1. When Desk is opened, i.e. when the usage starts, this also sends a + * Opened instance log. + * 2. On visibility change if not started, eg: when user minimizes Books and * then comes back later. - * 3. When `log` is called, but telemetry wasn't initialized. + * 3. When `log` is called, but telemetry isn't initialized. * * ## `log` * Used to log activity. @@ -39,17 +31,12 @@ export class TelemetryManager { #url: string = ''; #token: string = ''; #started = false; - #telemetryObject: Partial = {}; fyo: Fyo; constructor(fyo: Fyo) { this.fyo = fyo; } - set platform(value: Platform) { - this.#telemetryObject.platform ||= value; - } - get hasCreds() { return !!this.#url && !!this.#token; } @@ -58,18 +45,11 @@ export class TelemetryManager { return this.#started; } - get telemetryObject(): Readonly> { - return cloneDeep(this.#telemetryObject); - } - - async start(openCount?: number) { - await this.#init(); - + async start(isOpened?: boolean) { this.#started = true; await this.#setCreds(); - if (typeof openCount === 'number') { - this.#telemetryObject.openCount = openCount; + if (isOpened) { this.log(Verb.Opened, 'instance'); } else { this.log(Verb.Resumed, 'instance'); @@ -83,7 +63,6 @@ export class TelemetryManager { this.log(Verb.Closed, 'instance'); this.#started = false; - this.#clear(); } log(verb: Verb, noun: Noun, more?: Record) { @@ -96,7 +75,6 @@ export class TelemetryManager { } async logOpened() { - await this.#init(); await this.#setCreds(); this.#sendBeacon(Verb.Opened, 'app'); } @@ -125,46 +103,29 @@ export class TelemetryManager { this.#token = tokenString; } - async #init() { - this.#telemetryObject.language ??= getLanguage(this.fyo); - this.#telemetryObject.deviceId ||= getDeviceId(this.fyo); - this.#telemetryObject.version = this.fyo.store.appVersion; - - if (this.fyo.db.dbPath) { - this.#telemetryObject.country ||= getCountry(this.fyo); - this.#telemetryObject.instanceId ||= await getInstanceId(this.fyo); - this.#telemetryObject.version = await getVersion(this.fyo); - } else { - this.#telemetryObject.country ||= ''; - this.#telemetryObject.instanceId ||= ''; - } - } - #getTelemtryData( verb: Verb, noun: Noun, more?: Record ): Telemetry { + const countryCode = this.fyo.singles.SystemSettings?.countryCode as + | string + | undefined; + return { - country: this.#telemetryObject.country!, - language: this.#telemetryObject.language!, - deviceId: this.#telemetryObject.deviceId!, - instanceId: this.#telemetryObject.instanceId!, - version: this.#telemetryObject.version!, - openCount: this.#telemetryObject.openCount!, - timestamp: DateTime.now().toMillis().toString(), + country: countryCode ?? '', + language: this.fyo.store.language, + deviceId: + this.fyo.store.deviceId || + (this.fyo.config.get(ConfigKeys.DeviceId) as string), + instanceId: this.fyo.store.instanceId, + version: this.fyo.store.appVersion, + openCount: this.fyo.store.openCount, + timestamp: new Date().toISOString().replace('T', ' ').slice(0, -1), + platform: this.fyo.store.platform, verb, noun, more, }; } - - #clear() { - delete this.#telemetryObject.country; - delete this.#telemetryObject.language; - delete this.#telemetryObject.deviceId; - delete this.#telemetryObject.instanceId; - delete this.#telemetryObject.version; - delete this.#telemetryObject.openCount; - } } diff --git a/fyo/telemetry/types.ts b/fyo/telemetry/types.ts index 507acff0..351cd634 100644 --- a/fyo/telemetry/types.ts +++ b/fyo/telemetry/types.ts @@ -2,8 +2,6 @@ export type AppVersion = string; export type UniqueId = string; export type Timestamp = string; -export type Platform = 'Windows' | 'Mac' | 'Linux'; - export enum Verb { Created = 'created', Deleted = 'deleted', @@ -21,7 +19,7 @@ export type Noun = string; export interface Telemetry { deviceId: UniqueId; instanceId: UniqueId; - platform?: Platform; + platform?: string; country: string; language: string; version: AppVersion; @@ -29,5 +27,5 @@ export interface Telemetry { openCount: number; verb: Verb; noun: Noun; - more?: Record + more?: Record; } diff --git a/src/App.vue b/src/App.vue index a981266a..c9f13e0e 100644 --- a/src/App.vue +++ b/src/App.vue @@ -38,7 +38,6 @@