diff --git a/src/app/stats.ts b/src/app/stats.ts index 19dc3daf2..f4df1895c 100644 --- a/src/app/stats.ts +++ b/src/app/stats.ts @@ -25,6 +25,12 @@ export class AppStats { app.getPath('userData'), 'statsAnalytics.json', ); + private cpu: si.Systeminformation.CpuData | undefined; + private mem: si.Systeminformation.MemData | undefined; + private cpuUsage: si.Systeminformation.CurrentLoadData | undefined; + private osInfo: si.Systeminformation.OsData | undefined; + private uuid: si.Systeminformation.UuidData | undefined; + private time: si.Systeminformation.TimeData | undefined; private maxMemoryUsed: number = 0; private maxCPUUsage: number = 0; @@ -40,7 +46,7 @@ export class AppStats { this.logAppEvents(); this.sendAnalytics(SDAUserSessionActionTypes.Start); setInterval(async () => { - await this.captureMaxValue(); + await this.retrieveLatestData(); }, MAX_USAGE_CHECK_INTERVAL); } @@ -54,35 +60,35 @@ export class AppStats { crashProcess: string = '', ) { console.time(`stats ${actionType}`); - const cpu = await si.cpu(); - const mem = await si.mem(); - const cpuUsage = await si.currentLoad(); - const osInfo = await si.osInfo(); - const uuid = await si.uuid(); - const time = await si.time(); + this.cpu = this.cpu ?? (await si.cpu()); + this.mem = this.mem ?? (await si.mem()); + this.cpuUsage = this.cpuUsage ?? (await si.currentLoad()); + this.osInfo = this.osInfo ?? (await si.osInfo()); + this.uuid = this.uuid ?? (await si.uuid()); + this.time = this.time ?? si.time(); const totalMem = this.convertToMB(os.totalmem()); - const usedMem = this.convertToMB(mem.used); + const usedMem = this.convertToMB(this.mem.used); console.timeEnd(`stats ${actionType}`); const event: ISessionData = { element: AnalyticsElements.SDA_SESSION, action_type: actionType, extra_data: { sessionStartDatetime: this.startTime, - machineStartDatetime: this.convertUptime(time.uptime), - machineId: uuid.os, + machineStartDatetime: this.convertUptime(this.time.uptime), + machineId: this.uuid.os, osName: os.platform(), - osVersion: osInfo.release, + osVersion: this.osInfo.release, osLanguage: app.getLocale(), - cpuNumberOfCores: cpu.cores, - cpuMaxFrequency: cpu.speedMax, - cpuUsagePercent: Math.round(cpuUsage.currentLoad), + cpuNumberOfCores: this.cpu.cores, + cpuMaxFrequency: this.cpu.speedMax, + cpuUsagePercent: Math.round(this.cpuUsage.currentLoad), maxCPUUsagePercent: this.maxCPUUsage, memoryTotal: this.convertToMB(os.totalmem()), memoryUsedPercent: this.calculatePercentage(usedMem, totalMem), maxMemoryUsedPercent: this.maxMemoryUsed, sdaUsedMemory: this.convertToMB(process.memoryUsage().heapUsed), - memoryAvailable: this.convertToMB(mem.available), - vdi: !!osInfo.hypervizor, + memoryAvailable: this.convertToMB(this.mem.available), + vdi: !!this.osInfo.hypervizor, endReason: endReason ? endReason : undefined, crashProcess, }, @@ -287,13 +293,14 @@ export class AppStats { * Captures the max CPU & Memory value * @private */ - private async captureMaxValue(): Promise { - const mem = await si.mem(); - const cpuUsage = await si.currentLoad(); + private async retrieveLatestData(): Promise { + this.mem = await si.mem(); + this.cpu = await si.cpu(); + this.cpuUsage = await si.currentLoad(); - const cpuUsagePercent = Math.round(cpuUsage.currentLoad); + const cpuUsagePercent = Math.round(this.cpuUsage.currentLoad); const totalMem = this.convertToMB(os.totalmem()); - const usedMem = this.convertToMB(mem.used); + const usedMem = this.convertToMB(this.mem.used); const memUsedPercentage = this.calculatePercentage(usedMem, totalMem); if (memUsedPercentage > this.maxMemoryUsed) { this.maxMemoryUsed = memUsedPercentage;