Skip to content

Commit

Permalink
SDA-4291 - Optimize logic to speed up close time
Browse files Browse the repository at this point in the history
  • Loading branch information
KiranNiranjan authored and Kiran Niranjan committed Sep 21, 2023
1 parent 9a2766f commit 692461a
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/app/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Expand All @@ -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,
},
Expand Down Expand Up @@ -287,13 +293,14 @@ export class AppStats {
* Captures the max CPU & Memory value
* @private
*/
private async captureMaxValue(): Promise<void> {
const mem = await si.mem();
const cpuUsage = await si.currentLoad();
private async retrieveLatestData(): Promise<void> {
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;
Expand Down

0 comments on commit 692461a

Please sign in to comment.