-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathgetAppsStatistics.ts
More file actions
84 lines (72 loc) · 2.22 KB
/
getAppsStatistics.ts
File metadata and controls
84 lines (72 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Apps } from '@rocket.chat/apps';
import { AppStatus, AppStatusUtils } from '@rocket.chat/apps-engine/definition/AppStatus';
import { AppInstallationSource } from '@rocket.chat/apps-engine/server/storage';
import mem from 'mem';
import { SystemLogger } from '../../../../server/lib/logger/system';
import { Info } from '../../../utils/rocketchat.info';
export type AppsStatistics = {
engineVersion: string;
totalInstalled: number | false;
totalActive: number | false;
totalFailed: number | false;
totalPrivateApps: number | false;
totalPrivateAppsEnabled: number | false;
};
async function _getAppsStatistics(): Promise<AppsStatistics> {
if (!Apps.self?.isInitialized()) {
return {
engineVersion: Info.marketplaceApiVersion,
totalInstalled: false,
totalActive: false,
totalFailed: false,
totalPrivateApps: false,
totalPrivateAppsEnabled: false,
};
}
try {
const apps = await Apps.getManager().get();
let totalInstalled = 0;
let totalActive = 0;
let totalFailed = 0;
let totalPrivateApps = 0;
let totalPrivateAppsEnabled = 0;
await Promise.all(
apps.map(async (app) => {
totalInstalled++;
const status = await app.getStatus();
const storageItem = app.getStorageItem();
if (storageItem.installationSource === AppInstallationSource.PRIVATE) {
totalPrivateApps++;
if (AppStatusUtils.isEnabled(status)) {
totalPrivateAppsEnabled++;
}
}
if (AppStatusUtils.isEnabled(status)) {
totalActive++;
} else if (status !== AppStatus.MANUALLY_DISABLED) {
totalFailed++;
}
}),
);
return {
engineVersion: Info.marketplaceApiVersion,
totalInstalled,
totalActive,
totalFailed,
totalPrivateApps,
totalPrivateAppsEnabled,
};
} catch (err: unknown) {
SystemLogger.error({ msg: 'Exception while getting Apps statistics', err });
return {
engineVersion: Info.marketplaceApiVersion,
totalInstalled: false,
totalActive: false,
totalFailed: false,
totalPrivateApps: false,
totalPrivateAppsEnabled: false,
};
}
}
// since this function is called every 5s by `setPrometheusData` we're memoizing the result since the result won't change that often
export const getAppsStatistics = mem(_getAppsStatistics, { maxAge: 60000 });