Update monitor.service.ts

Isso permite que as operações assíncronas sejam executadas em paralelo
This commit is contained in:
Wender Teixeira 2023-09-04 15:06:18 -03:00 committed by GitHub
parent 41bea8931f
commit 7d9dd64303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -74,81 +74,62 @@ export class WAMonitoringService {
} }
} }
public async instanceInfo(instanceName?: string) { public async instanceInfo(instanceName?: string) {
this.logger.verbose('get instance info'); this.logger.verbose('get instance info');
if (instanceName && !this.waInstances[instanceName]) {
throw new NotFoundException(`Instance "${instanceName}" not found`);
}
const instances: any[] = []; const urlServer = this.configService.get<HttpServer>('SERVER').URL;
for await (const [key, value] of Object.entries(this.waInstances)) { const instances: any[] = await Promise.all(
if (value) { Object.entries(this.waInstances).map(async ([key, value]) => {
this.logger.verbose('get instance info: ' + key); if (!value || !value.connectionStatus || value.connectionStatus.state !== 'open') {
let chatwoot: any; return {
instance: {
const urlServer = this.configService.get<HttpServer>('SERVER').URL; instanceName: key,
status: value?.connectionStatus?.state || 'unknown',
const findChatwoot = await this.waInstances[key].findChatwoot(); },
if (findChatwoot && findChatwoot.enabled) {
chatwoot = {
...findChatwoot,
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
}; };
} }
if (value.connectionStatus.state === 'open') { this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
const instanceData = { const instanceData: any = {
instance: { instance: {
instanceName: key, instanceName: key,
owner: value.wuid, owner: value.wuid,
profileName: (await value.getProfileName()) || 'not loaded', profileName: (await value.getProfileName()) || 'not loaded',
profilePictureUrl: value.profilePictureUrl, profilePictureUrl: value.profilePictureUrl,
profileStatus: (await value.getProfileStatus()) || '', profileStatus: (await value.getProfileStatus()) || '',
status: value.connectionStatus.state, status: 'open',
}, },
}; };
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) { if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL; instanceData.instance.serverUrl = urlServer;
instanceData.instance.apikey = (await this.repository.auth.find(key))?.apikey;
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey; const findChatwoot = await this.waInstances[key].findChatwoot();
if (findChatwoot && findChatwoot.enabled) {
instanceData.instance['chatwoot'] = chatwoot; instanceData.instance.chatwoot = {
...findChatwoot,
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
};
} }
instances.push(instanceData);
} else {
this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state);
const instanceData = {
instance: {
instanceName: key,
status: value.connectionStatus.state,
},
};
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
instanceData.instance['chatwoot'] = chatwoot;
}
instances.push(instanceData);
} }
}
} return instanceData;
}),
);
this.logger.verbose('return instance info: ' + instances.length); this.logger.verbose('return instance info: ' + instances.length);
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances; if (instanceName) {
} const instance = instances.find((i) => i.instance.instanceName === instanceName);
return instance || [];
}
return instances;
}
private delInstanceFiles() { private delInstanceFiles() {
this.logger.verbose('cron to delete instance files started'); this.logger.verbose('cron to delete instance files started');
setInterval(async () => { setInterval(async () => {