mirror of
https://github.com/finos/SymphonyElectron.git
synced 2024-12-27 17:31:36 -06:00
fix: Typescript (Fix throttle issue and update stats logs) (#671)
* Typescript - Fix throttle issue and update stats logs * Typescript - Fix unit tests
This commit is contained in:
parent
74a2d7fb37
commit
10d3d49c44
@ -110,8 +110,20 @@ describe('utils', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('`throttle`', () => {
|
describe('`throttle`', () => {
|
||||||
|
let origNow;
|
||||||
|
let now;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.useFakeTimers();
|
origNow = Date.now;
|
||||||
|
// mock date func
|
||||||
|
Date.now = () => {
|
||||||
|
return now;
|
||||||
|
};
|
||||||
|
now = 10000;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
// restore original
|
||||||
|
Date.now = origNow;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail when wait is invalid', () => {
|
it('should fail when wait is invalid', () => {
|
||||||
@ -124,10 +136,17 @@ describe('utils', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should call `throttle` correctly', () => {
|
it('should call `throttle` correctly', () => {
|
||||||
const validTime = 3;
|
jest.useFakeTimers();
|
||||||
const tempFn = throttle(jest.fn(), validTime);
|
const validTime = 1000;
|
||||||
|
const functionMock = jest.fn();
|
||||||
|
const tempFn = throttle(functionMock, validTime);
|
||||||
|
for (let i = 0; i < 3; i++) {
|
||||||
|
tempFn();
|
||||||
|
}
|
||||||
|
now += 1000;
|
||||||
|
jest.runTimersToTime(1000);
|
||||||
tempFn();
|
tempFn();
|
||||||
expect(setTimeout).toBeCalledWith(expect.any(Function), validTime);
|
expect(functionMock).toBeCalledTimes(2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -206,7 +206,7 @@ class Config {
|
|||||||
await this.updateUserConfig({ configVersion: app.getVersion().toString(), buildNumber } as IConfig);
|
await this.updateUserConfig({ configVersion: app.getVersion().toString(), buildNumber } as IConfig);
|
||||||
}
|
}
|
||||||
this.userConfig = this.parseConfigData(fs.readFileSync(this.userConfigPath, 'utf8'));
|
this.userConfig = this.parseConfigData(fs.readFileSync(this.userConfigPath, 'utf8'));
|
||||||
logger.info(`config-handler: user config exists with data ${JSON.stringify(this.userConfig)}`);
|
logger.info(`config-handler: User configuration: `, this.userConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -214,7 +214,7 @@ class Config {
|
|||||||
*/
|
*/
|
||||||
private readGlobalConfig() {
|
private readGlobalConfig() {
|
||||||
this.globalConfig = this.parseConfigData(fs.readFileSync(this.globalConfigPath, 'utf8'));
|
this.globalConfig = this.parseConfigData(fs.readFileSync(this.globalConfigPath, 'utf8'));
|
||||||
logger.info(`config-handler: global config exists with data ${JSON.stringify(this.globalConfig)}`);
|
logger.info(`config-handler: Global configuration: `, this.globalConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,16 +13,18 @@ const userDataPath = userDataPathArg && userDataPathArg.substring(userDataPathAr
|
|||||||
|
|
||||||
// Set user data path before app ready event
|
// Set user data path before app ready event
|
||||||
if (isDevEnv) {
|
if (isDevEnv) {
|
||||||
const appDataPath = app.getPath('appData');
|
const devDataPath = path.join(app.getPath('appData'), 'Symphony-dev');
|
||||||
logger.info(`init: Setting app data path to ${appDataPath}`);
|
logger.info(`init: Setting user data path to`, devDataPath);
|
||||||
app.setPath('userData', path.join(appDataPath, 'Symphony-dev'));
|
app.setPath('userData', devDataPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userDataPath) {
|
if (userDataPath) {
|
||||||
logger.info(`init: Setting user data path to ${userDataPath}`);
|
logger.info(`init: Setting user data path to`, userDataPath);
|
||||||
app.setPath('userData', userDataPath);
|
app.setPath('userData', userDataPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info(`init: Fetch user data path`, app.getPath('userData'));
|
||||||
|
|
||||||
// Log app statistics
|
// Log app statistics
|
||||||
appStats.logStats();
|
appStats.logStats();
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { app } from 'electron';
|
import { app } from 'electron';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { logger } from '../common/logger';
|
import { logger } from '../common/logger';
|
||||||
import { config } from './config-handler';
|
|
||||||
|
|
||||||
export class AppStats {
|
export class AppStats {
|
||||||
|
|
||||||
@ -23,7 +22,7 @@ export class AppStats {
|
|||||||
* Logs system related statistics
|
* Logs system related statistics
|
||||||
*/
|
*/
|
||||||
private logSystemStats() {
|
private logSystemStats() {
|
||||||
logger.info(`stats: -----------------Gathering system information-----------------`);
|
logger.info(`-----------------Gathering system information-----------------`);
|
||||||
logger.info( `Network Info -> `, os.networkInterfaces());
|
logger.info( `Network Info -> `, os.networkInterfaces());
|
||||||
logger.info( `CPU Info -> `, os.cpus());
|
logger.info( `CPU Info -> `, os.cpus());
|
||||||
logger.info( `Operating System -> `, os.type());
|
logger.info( `Operating System -> `, os.type());
|
||||||
@ -51,29 +50,15 @@ export class AppStats {
|
|||||||
* Logs Configuration Data
|
* Logs Configuration Data
|
||||||
*/
|
*/
|
||||||
private logConfigurationData() {
|
private logConfigurationData() {
|
||||||
const configItems = [
|
logger.info(`-----------------App Configuration Information-----------------`);
|
||||||
'url', 'minimizeOnClose', 'launchOnStartup', 'alwaysOnTop', 'bringToFront', 'whitelistUrl',
|
|
||||||
'isCustomTitleBar', 'memoryRefresh', 'devToolsEnabled', 'ctWhitelist', 'configVersion',
|
|
||||||
'buildNumber', 'autoLaunchPath', 'notificationSettings', 'permissions', 'customFlags',
|
|
||||||
'crashReporter', 'mainWinPos',
|
|
||||||
];
|
|
||||||
|
|
||||||
logger.info(`stats: -----------------App Configuration Information-----------------`);
|
|
||||||
logger.info(`stats: Is app packaged? ${app.isPackaged}`);
|
logger.info(`stats: Is app packaged? ${app.isPackaged}`);
|
||||||
|
|
||||||
const globalConfiguration = config.getGlobalConfigFields(configItems);
|
|
||||||
logger.info(`stats: Global configuration: `, globalConfiguration);
|
|
||||||
|
|
||||||
const userConfiguration = config.getUserConfigFields(configItems);
|
|
||||||
logger.info(`stats: -----------------Gathering User Configuration Information-----------------`);
|
|
||||||
logger.info(`stats: User configuration: `, userConfiguration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs App metrics
|
* Logs App metrics
|
||||||
*/
|
*/
|
||||||
private logAppMetrics() {
|
private logAppMetrics() {
|
||||||
logger.info(`stats: -----------------Gathering App Metrics-----------------`);
|
logger.info(`-----------------Gathering App Metrics-----------------`);
|
||||||
const metrics = app.getAppMetrics();
|
const metrics = app.getAppMetrics();
|
||||||
metrics.forEach((metric) => {
|
metrics.forEach((metric) => {
|
||||||
logger.info(`stats: PID -> ${metric.pid}, Type -> ${metric.type}, CPU Usage -> `, metric.cpu);
|
logger.info(`stats: PID -> ${metric.pid}, Type -> ${metric.type}, CPU Usage -> `, metric.cpu);
|
||||||
@ -102,6 +87,7 @@ export class AppStats {
|
|||||||
* Logs process info
|
* Logs process info
|
||||||
*/
|
*/
|
||||||
private logProcessInfo() {
|
private logProcessInfo() {
|
||||||
|
logger.info(`-----------------Gathering Process Info-----------------`);
|
||||||
logger.info(`stats: Is default app? ${process.defaultApp}`);
|
logger.info(`stats: Is default app? ${process.defaultApp}`);
|
||||||
logger.info(`stats: Is Mac Store app? ${process.mas}`);
|
logger.info(`stats: Is Mac Store app? ${process.mas}`);
|
||||||
logger.info(`stats: Is Windows Store app? ${process.windowsStore}`);
|
logger.info(`stats: Is Windows Store app? ${process.windowsStore}`);
|
||||||
|
@ -165,13 +165,21 @@ export const throttle = (func: (...args) => void, wait: number): (...args) => vo
|
|||||||
throw Error('throttle: invalid throttleTime arg, must be a number: ' + wait);
|
throw Error('throttle: invalid throttleTime arg, must be a number: ' + wait);
|
||||||
}
|
}
|
||||||
|
|
||||||
let isCalled: boolean = false;
|
let timer: NodeJS.Timer;
|
||||||
|
let lastRan = 0;
|
||||||
|
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
if (!isCalled) {
|
if (!lastRan) {
|
||||||
func(...args);
|
func.apply(null, args);
|
||||||
isCalled = true;
|
lastRan = Date.now();
|
||||||
setTimeout(() => isCalled = false, wait);
|
} else {
|
||||||
|
clearTimeout(timer);
|
||||||
|
timer = setTimeout(() => {
|
||||||
|
if ((Date.now() - lastRan) >= wait) {
|
||||||
|
func.apply(null, args);
|
||||||
|
lastRan = Date.now();
|
||||||
|
}
|
||||||
|
}, wait - (Date.now() - lastRan));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user