Skip to content

Commit

Permalink
fix: simplify telemetry code, send platform
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Jul 18, 2022
1 parent 1265453 commit c88908d
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 197 deletions.
6 changes: 5 additions & 1 deletion fyo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -226,6 +226,10 @@ export class Fyo {
skipTelemetryLogging: false,
appVersion: '',
platform: '',
language: '',
instanceId: '',
deviceId: '',
openCount: -1,
};
}

Expand Down
101 changes: 0 additions & 101 deletions fyo/telemetry/helpers.ts

This file was deleted.

83 changes: 22 additions & 61 deletions fyo/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -39,17 +31,12 @@ export class TelemetryManager {
#url: string = '';
#token: string = '';
#started = false;
#telemetryObject: Partial<Telemetry> = {};
fyo: Fyo;

constructor(fyo: Fyo) {
this.fyo = fyo;
}

set platform(value: Platform) {
this.#telemetryObject.platform ||= value;
}

get hasCreds() {
return !!this.#url && !!this.#token;
}
Expand All @@ -58,18 +45,11 @@ export class TelemetryManager {
return this.#started;
}

get telemetryObject(): Readonly<Partial<Telemetry>> {
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');
Expand All @@ -83,7 +63,6 @@ export class TelemetryManager {

this.log(Verb.Closed, 'instance');
this.#started = false;
this.#clear();
}

log(verb: Verb, noun: Noun, more?: Record<string, unknown>) {
Expand All @@ -96,7 +75,6 @@ export class TelemetryManager {
}

async logOpened() {
await this.#init();
await this.#setCreds();
this.#sendBeacon(Verb.Opened, 'app');
}
Expand Down Expand Up @@ -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<string, unknown>
): 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;
}
}
6 changes: 2 additions & 4 deletions fyo/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -21,13 +19,13 @@ export type Noun = string;
export interface Telemetry {
deviceId: UniqueId;
instanceId: UniqueId;
platform?: Platform;
platform?: string;
country: string;
language: string;
version: AppVersion;
timestamp: Timestamp;
openCount: number;
verb: Verb;
noun: Noun;
more?: Record<string, unknown>
more?: Record<string, unknown>;
}
4 changes: 1 addition & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
<script>
import { ConfigKeys } from 'fyo/core/types';
import { ModelNameEnum } from 'models/types';
import { incrementOpenCount } from 'src/utils/misc';
import { computed } from 'vue';
import WindowsTitleBar from './components/WindowsTitleBar.vue';
import { handleErrorWithDialog } from './errorHandling';
Expand Down Expand Up @@ -94,8 +93,7 @@ export default {
async setDesk(filePath) {
this.activeScreen = 'Desk';
await this.setDeskRoute();
const openCount = await incrementOpenCount(filePath);
await fyo.telemetry.start(openCount);
await fyo.telemetry.start(true);
await checkForUpdates(false);
this.dbPath = filePath;
this.companyName = await fyo.getValue(
Expand Down
40 changes: 40 additions & 0 deletions src/initFyo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Fyo } from 'fyo';
import { ConfigFile, ConfigKeys } from 'fyo/core/types';
import { getRegionalModels, models } from 'models';
import { ModelNameEnum } from 'models/types';
import { getRandomString, getValueMapFromList } from 'utils';
Expand Down Expand Up @@ -32,7 +33,9 @@ export async function initializeInstance(
await setSingles(fyo);
await setCreds(fyo);
await setVersion(fyo);
setDeviceId(fyo);
await setInstanceId(fyo);
await incrementOpenCount(fyo);
await setCurrencySymbols(fyo);
}

Expand Down Expand Up @@ -63,11 +66,25 @@ async function setVersion(fyo: Fyo) {
}
}

function setDeviceId(fyo: Fyo) {
let deviceId = fyo.config.get(ConfigKeys.DeviceId) as string | undefined;
if (deviceId === undefined) {
deviceId = getRandomString();
fyo.config.set(ConfigKeys.DeviceId, deviceId);
}

fyo.store.deviceId = deviceId;
}
async function setInstanceId(fyo: Fyo) {
const systemSettings = await fyo.doc.getDoc(ModelNameEnum.SystemSettings);
if (!systemSettings.instanceId) {
await systemSettings.setAndSync('instanceId', getRandomString());
}

fyo.store.instanceId = (await fyo.getValue(
ModelNameEnum.SystemSettings,
'instanceId'
)) as string;
}

async function setCurrencySymbols(fyo: Fyo) {
Expand All @@ -81,3 +98,26 @@ async function setCurrencySymbols(fyo: Fyo) {
'symbol'
) as Record<string, string | undefined>;
}

export async function incrementOpenCount(fyo: Fyo) {
const instanceId = (await fyo.getValue(
ModelNameEnum.SystemSettings,
'instanceId'
)) as string;

let openCount = 0;
const files = (fyo.config.get(ConfigKeys.Files) ?? []) as ConfigFile[];
for (const file of files) {
if (file.id !== instanceId) {
continue;
}

file.openCount ??= 0;
file.openCount += 1;
openCount = file.openCount;
break;
}

fyo.config.set(ConfigKeys.Files, files);
fyo.store.openCount = openCount;
}
Loading

0 comments on commit c88908d

Please sign in to comment.