@bmfk/api 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +90 -0
- package/LICENSE +21 -0
- package/README.md +1293 -2
- package/dist/apiServer/APIService.d.ts +125 -0
- package/dist/apiServer/APIService.js +769 -0
- package/dist/apiServer/BaseEndpoint.d.ts +90 -0
- package/dist/apiServer/BaseEndpoint.js +314 -0
- package/dist/apiServer/BaseService.d.ts +17 -0
- package/dist/apiServer/BaseService.js +39 -0
- package/dist/apiServer/DBService.d.ts +131 -0
- package/dist/apiServer/DBService.js +290 -0
- package/dist/apiServer/DocumentService.d.ts +9 -0
- package/dist/apiServer/DocumentService.js +65 -0
- package/dist/apiServer/LocalRequest.d.ts +3 -0
- package/dist/apiServer/LocalRequest.js +6 -0
- package/dist/apiServer/LogService.d.ts +78 -0
- package/dist/apiServer/LogService.js +349 -0
- package/dist/apiServer/MonitorService.d.ts +102 -0
- package/dist/apiServer/MonitorService.js +615 -0
- package/dist/apiServer/StatService.d.ts +4 -0
- package/dist/apiServer/StatService.js +10 -0
- package/dist/apiServer/TimeframeService.d.ts +28 -0
- package/dist/apiServer/TimeframeService.js +97 -0
- package/dist/apiServer/WebSocketServer.d.ts +39 -0
- package/dist/apiServer/WebSocketServer.js +173 -0
- package/dist/apiServer/db/IDBAdapter.d.ts +14 -0
- package/dist/apiServer/db/IDBAdapter.js +2 -0
- package/dist/apiServer/db/MysqlAdapter.d.ts +72 -0
- package/dist/apiServer/db/MysqlAdapter.js +869 -0
- package/dist/apiServer/db/PostgresAdapter.d.ts +41 -0
- package/dist/apiServer/db/PostgresAdapter.js +73 -0
- package/dist/apiServer/deployment/AccessConfigWizard.d.ts +5 -0
- package/dist/apiServer/deployment/AccessConfigWizard.js +254 -0
- package/dist/apiServer/deployment/Activate.d.ts +4 -0
- package/dist/apiServer/deployment/Activate.js +124 -0
- package/dist/apiServer/deployment/BackupManager.d.ts +33 -0
- package/dist/apiServer/deployment/BackupManager.js +174 -0
- package/dist/apiServer/deployment/Backups.d.ts +2 -0
- package/dist/apiServer/deployment/Backups.js +44 -0
- package/dist/apiServer/deployment/BlueGreenState.d.ts +31 -0
- package/dist/apiServer/deployment/BlueGreenState.js +150 -0
- package/dist/apiServer/deployment/Bundler.d.ts +12 -0
- package/dist/apiServer/deployment/Bundler.js +178 -0
- package/dist/apiServer/deployment/ConfigService.d.ts +203 -0
- package/dist/apiServer/deployment/ConfigService.js +195 -0
- package/dist/apiServer/deployment/Deploy.d.ts +4 -0
- package/dist/apiServer/deployment/Deploy.js +404 -0
- package/dist/apiServer/deployment/DeployKeyWizard.d.ts +2 -0
- package/dist/apiServer/deployment/DeployKeyWizard.js +84 -0
- package/dist/apiServer/deployment/Deployer.d.ts +13 -0
- package/dist/apiServer/deployment/Deployer.js +96 -0
- package/dist/apiServer/deployment/DeployerInit.d.ts +25 -0
- package/dist/apiServer/deployment/DeployerInit.js +214 -0
- package/dist/apiServer/deployment/DeployerService.d.ts +378 -0
- package/dist/apiServer/deployment/DeployerService.js +1553 -0
- package/dist/apiServer/deployment/HashPassword.d.ts +3 -0
- package/dist/apiServer/deployment/HashPassword.js +126 -0
- package/dist/apiServer/deployment/MigrateConfigsWizard.d.ts +1 -0
- package/dist/apiServer/deployment/MigrateConfigsWizard.js +202 -0
- package/dist/apiServer/deployment/MigrateProject.d.ts +31 -0
- package/dist/apiServer/deployment/MigrateProject.js +335 -0
- package/dist/apiServer/deployment/NginxHelper.d.ts +10 -0
- package/dist/apiServer/deployment/NginxHelper.js +111 -0
- package/dist/apiServer/deployment/Notifier.d.ts +30 -0
- package/dist/apiServer/deployment/Notifier.js +82 -0
- package/dist/apiServer/deployment/ProjectConfigWizard.d.ts +1 -0
- package/dist/apiServer/deployment/ProjectConfigWizard.js +148 -0
- package/dist/apiServer/deployment/Restore.d.ts +2 -0
- package/dist/apiServer/deployment/Restore.js +89 -0
- package/dist/apiServer/deployment/ReviewStore.d.ts +62 -0
- package/dist/apiServer/deployment/ReviewStore.js +186 -0
- package/dist/apiServer/deployment/SecretsInjector.d.ts +21 -0
- package/dist/apiServer/deployment/SecretsInjector.js +189 -0
- package/dist/apiServer/deployment/SetupConfigWizard.d.ts +1 -0
- package/dist/apiServer/deployment/SetupConfigWizard.js +238 -0
- package/dist/apiServer/deployment/Status.d.ts +2 -0
- package/dist/apiServer/deployment/Status.js +83 -0
- package/dist/apiServer/deployment/TelegramNotifier.d.ts +8 -0
- package/dist/apiServer/deployment/TelegramNotifier.js +23 -0
- package/dist/apiServer/deployment/Watchdog.d.ts +15 -0
- package/dist/apiServer/deployment/Watchdog.js +65 -0
- package/dist/apiServer/deployment/Wizards.d.ts +1 -0
- package/dist/apiServer/deployment/Wizards.js +83 -0
- package/dist/apiServer/deployment/cli.d.ts +2 -0
- package/dist/apiServer/deployment/cli.js +224 -0
- package/dist/apiServer/deployment/cliCommon.d.ts +27 -0
- package/dist/apiServer/deployment/cliCommon.js +110 -0
- package/dist/apiServer/deployment/cliPrompt.d.ts +5 -0
- package/dist/apiServer/deployment/cliPrompt.js +38 -0
- package/dist/apiServer/deployment/resolveConfig.d.ts +19 -0
- package/dist/apiServer/deployment/resolveConfig.js +139 -0
- package/dist/apiServer/documentation/Documentation.d.ts +18 -0
- package/dist/apiServer/documentation/Documentation.js +143 -0
- package/dist/apiServer/documentation/index.html +460 -0
- package/dist/apiServer/external/MicroserviceClient.d.ts +28 -0
- package/dist/apiServer/external/MicroserviceClient.js +144 -0
- package/dist/apiServer/external/MicroserviceHost.d.ts +52 -0
- package/dist/apiServer/external/MicroserviceHost.js +183 -0
- package/dist/apiServer/external/MicroserviceHostController.d.ts +6 -0
- package/dist/apiServer/external/MicroserviceHostController.js +65 -0
- package/dist/apiServer/helper/AppDataPath.d.ts +9 -0
- package/dist/apiServer/helper/AppDataPath.js +81 -0
- package/dist/apiServer/helper/ClientIp.d.ts +3 -0
- package/dist/apiServer/helper/ClientIp.js +33 -0
- package/dist/apiServer/helper/DataProvider.d.ts +18 -0
- package/dist/apiServer/helper/DataProvider.js +57 -0
- package/dist/apiServer/helper/IpAllowList.d.ts +8 -0
- package/dist/apiServer/helper/IpAllowList.js +40 -0
- package/dist/apiServer/helper/JSONStableStringify.d.ts +1 -0
- package/dist/apiServer/helper/JSONStableStringify.js +15 -0
- package/dist/apiServer/helper/PasswordHash.d.ts +5 -0
- package/dist/apiServer/helper/PasswordHash.js +52 -0
- package/dist/apiServer/helper/Sleep.d.ts +1 -0
- package/dist/apiServer/helper/Sleep.js +5 -0
- package/dist/apiServer/helper/Trace.d.ts +8 -0
- package/dist/apiServer/helper/Trace.js +30 -0
- package/dist/apiServer/helper/UID.d.ts +16 -0
- package/dist/apiServer/helper/UID.js +98 -0
- package/dist/apiServer/helper/Validator.d.ts +86 -0
- package/dist/apiServer/helper/Validator.js +560 -0
- package/dist/apiServer/helper/YYYYMMDDHH.d.ts +2 -0
- package/dist/apiServer/helper/YYYYMMDDHH.js +31 -0
- package/dist/apiServer/helper/ZipUtils.d.ts +6 -0
- package/dist/apiServer/helper/ZipUtils.js +28 -0
- package/dist/apiServer/http/Http.d.ts +183 -0
- package/dist/apiServer/http/Http.js +627 -0
- package/dist/apiServer/interceptors/InterceptorPayAuth.d.ts +19 -0
- package/dist/apiServer/interceptors/InterceptorPayAuth.js +124 -0
- package/dist/apiServer/interceptors/InterceptorSignedAuth.d.ts +40 -0
- package/dist/apiServer/interceptors/InterceptorSignedAuth.js +150 -0
- package/dist/apiServer/monitor/AccessStore.d.ts +27 -0
- package/dist/apiServer/monitor/AccessStore.js +163 -0
- package/dist/apiServer/monitor/LogFiles.d.ts +34 -0
- package/dist/apiServer/monitor/LogFiles.js +109 -0
- package/dist/apiServer/monitor/Monitor.d.ts +54 -0
- package/dist/apiServer/monitor/Monitor.js +447 -0
- package/dist/apiServer/monitor/MonitorAuth.d.ts +87 -0
- package/dist/apiServer/monitor/MonitorAuth.js +474 -0
- package/dist/apiServer/monitor/ProcessMonitor.d.ts +177 -0
- package/dist/apiServer/monitor/ProcessMonitor.js +468 -0
- package/dist/apiServer/monitor/SystemInfo.d.ts +70 -0
- package/dist/apiServer/monitor/SystemInfo.js +217 -0
- package/dist/apiServer/monitor/ThreatMonitor.d.ts +73 -0
- package/dist/apiServer/monitor/ThreatMonitor.js +239 -0
- package/dist/apiServer/monitor/index.html +591 -0
- package/dist/apiServer/review/Review.d.ts +23 -0
- package/dist/apiServer/review/Review.js +149 -0
- package/dist/apiServer/review/ReviewAuth.d.ts +56 -0
- package/dist/apiServer/review/ReviewAuth.js +147 -0
- package/dist/apiServer/review/ReviewDiff.d.ts +43 -0
- package/dist/apiServer/review/ReviewDiff.js +175 -0
- package/dist/apiServer/review/index.html +501 -0
- package/dist/apiServer/routes/Liveness.d.ts +7 -0
- package/dist/apiServer/routes/Liveness.js +27 -0
- package/dist/apiServer/routes/Readiness.d.ts +13 -0
- package/dist/apiServer/routes/Readiness.js +20 -0
- package/dist/apiServer/security/BlockedIps.d.ts +46 -0
- package/dist/apiServer/security/BlockedIps.js +185 -0
- package/dist/apiServer/structures/DefaultErrors.d.ts +36 -0
- package/dist/apiServer/structures/DefaultErrors.js +52 -0
- package/dist/apiServer/structures/HTTPStatus.d.ts +7 -0
- package/dist/apiServer/structures/HTTPStatus.js +11 -0
- package/dist/apiServer/structures/Interfaces.d.ts +55 -0
- package/dist/apiServer/structures/Interfaces.js +2 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +110 -0
- package/dist/signal/Signal.d.ts +23 -0
- package/dist/signal/Signal.js +85 -0
- package/package.json +88 -4
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MonitorService = exports.REQ_STAT_WEEK = exports.REQ_STAT_WEEKS = exports.REQ_STAT = exports.S_STAT_REGISTRATE_SQL_REQUEST = exports.S_STAT_REGISTRATE_HTTP_RESULT = exports.S_STAT_REGISTRATE_HTTP_REQUEST = exports.S_STAT_REGISTRATE_SERVICE = exports.S_STAT_REGISTRATE_CUSTOM_EVENT = exports.S_STAT_REGISTRATE_REQUEST = void 0;
|
|
7
|
+
const Signal_1 = require("../signal/Signal");
|
|
8
|
+
const BaseService_1 = require("./BaseService");
|
|
9
|
+
const YYYYMMDDHH_1 = require("./helper/YYYYMMDDHH");
|
|
10
|
+
const LogService_1 = require("./LogService");
|
|
11
|
+
const os_1 = __importDefault(require("os"));
|
|
12
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
13
|
+
const path_1 = __importDefault(require("path"));
|
|
14
|
+
const AppDataPath_1 = require("./helper/AppDataPath");
|
|
15
|
+
exports.S_STAT_REGISTRATE_REQUEST = new Signal_1.Signal();
|
|
16
|
+
exports.S_STAT_REGISTRATE_CUSTOM_EVENT = new Signal_1.Signal();
|
|
17
|
+
exports.S_STAT_REGISTRATE_SERVICE = new Signal_1.Signal();
|
|
18
|
+
exports.S_STAT_REGISTRATE_HTTP_REQUEST = new Signal_1.Signal();
|
|
19
|
+
exports.S_STAT_REGISTRATE_HTTP_RESULT = new Signal_1.Signal();
|
|
20
|
+
exports.S_STAT_REGISTRATE_SQL_REQUEST = new Signal_1.Signal();
|
|
21
|
+
exports.REQ_STAT = new Signal_1.Req(undefined, "REQ_STAT");
|
|
22
|
+
exports.REQ_STAT_WEEKS = new Signal_1.Req(undefined, "REQ_STAT_WEEKS");
|
|
23
|
+
exports.REQ_STAT_WEEK = new Signal_1.Req(undefined, "REQ_STAT_WEEK");
|
|
24
|
+
class MonitorService extends BaseService_1.BaseService {
|
|
25
|
+
slots = new Map();
|
|
26
|
+
intervalMs = 59_000;
|
|
27
|
+
lastCpuUsage = null;
|
|
28
|
+
lastTimestamp = null;
|
|
29
|
+
metricsDir;
|
|
30
|
+
_live = false;
|
|
31
|
+
static MAX_WEEKS = 10;
|
|
32
|
+
static SCHEMA_VERSION = 2;
|
|
33
|
+
_persistReady = false;
|
|
34
|
+
_flushedUpTo = 0;
|
|
35
|
+
_lastSeenWeekKey = null;
|
|
36
|
+
constructor(metricsDir) {
|
|
37
|
+
super("MonitorService");
|
|
38
|
+
this.metricsDir = metricsDir === false || metricsDir === null ? null
|
|
39
|
+
: metricsDir && metricsDir.length > 0 ? metricsDir
|
|
40
|
+
: MonitorService.defaultDir();
|
|
41
|
+
setInterval(() => this.collectUsage(), this.intervalMs);
|
|
42
|
+
}
|
|
43
|
+
static defaultDir() {
|
|
44
|
+
const dir = AppDataPath_1.AppData.dir("metrics");
|
|
45
|
+
if (dir)
|
|
46
|
+
return dir;
|
|
47
|
+
const fallback = (0, AppDataPath_1.appDataPath)("metrics");
|
|
48
|
+
(0, LogService_1.logWarn)(`Metrics persistence: can't create ${AppDataPath_1.AppData.root()}/${AppDataPath_1.AppData.project()}/metrics (create ${AppDataPath_1.AppData.root()} and give the app user write access, or set ${AppDataPath_1.APP_DATA_ROOT_ENV}) — using ${fallback}`);
|
|
49
|
+
return fallback;
|
|
50
|
+
}
|
|
51
|
+
async init() {
|
|
52
|
+
super.init();
|
|
53
|
+
exports.REQ_STAT.listener = async (req) => this.getEPStat(req);
|
|
54
|
+
exports.REQ_STAT_WEEKS.listener = async () => this.listPersistedWeeks();
|
|
55
|
+
exports.REQ_STAT_WEEK.listener = async (req) => this.readPersistedWeek(req.weekKey);
|
|
56
|
+
exports.S_STAT_REGISTRATE_HTTP_REQUEST.subscribe((data) => this.onHttpRequestRegistrate(data));
|
|
57
|
+
exports.S_STAT_REGISTRATE_HTTP_RESULT.subscribe((data) => this.onHttpResultRegistrate(data));
|
|
58
|
+
exports.S_STAT_REGISTRATE_SQL_REQUEST.subscribe((data) => this.onSQLRequestRegistrate(data));
|
|
59
|
+
exports.S_STAT_REGISTRATE_REQUEST.subscribe((data) => this.onStatRegistrate(data));
|
|
60
|
+
if (!this.metricsDir) {
|
|
61
|
+
(0, LogService_1.logInfo)("Metrics persistence: OFF (metricsDir: false) — history is in memory only and lost on restart.", "MonitorService");
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
this.metricsDir = path_1.default.resolve(this.metricsDir);
|
|
65
|
+
const appDir = path_1.default.resolve(process.cwd());
|
|
66
|
+
if (this.metricsDir === appDir || this.metricsDir.startsWith(appDir + path_1.default.sep))
|
|
67
|
+
(0, LogService_1.logWarn)(`Metrics persistence: metricsDir "${this.metricsDir}" is inside the application directory — a deploy that cleans it will erase the history. Use an absolute path outside the project.`);
|
|
68
|
+
else
|
|
69
|
+
(0, LogService_1.logInfo)(`Metrics persistence: ON → ${this.metricsDir}`, "MonitorService");
|
|
70
|
+
}
|
|
71
|
+
if (this.metricsDir) {
|
|
72
|
+
try {
|
|
73
|
+
await promises_1.default.mkdir(this.metricsDir, { recursive: true });
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
(0, LogService_1.logWarn)("metrics: cannot create metricsDir, disk persistence disabled: " + (e?.message ?? e));
|
|
77
|
+
this.metricsDir = null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (this.metricsDir) {
|
|
81
|
+
await this.hydrate();
|
|
82
|
+
this._lastSeenWeekKey = this.weekKey(new Date());
|
|
83
|
+
this._persistReady = true;
|
|
84
|
+
this.followLive();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
followLive() {
|
|
88
|
+
const canWrite = (0, AppDataPath_1.onWriterChange)(live => {
|
|
89
|
+
if (live === this._live)
|
|
90
|
+
return;
|
|
91
|
+
this._live = live;
|
|
92
|
+
if (live)
|
|
93
|
+
this.hydrate().then(() => this.rotate()).catch(() => { });
|
|
94
|
+
});
|
|
95
|
+
if (!canWrite)
|
|
96
|
+
(0, LogService_1.logInfo)(`Metrics persistence: pm2 worker ${process.env.NODE_APP_INSTANCE} — only instance 0 writes ${this.metricsDir}`, "MonitorService");
|
|
97
|
+
}
|
|
98
|
+
async finishService() {
|
|
99
|
+
await super.finishService();
|
|
100
|
+
if (this.metricsDir && this._persistReady && this._live)
|
|
101
|
+
await this.persistTick(true).catch(() => { });
|
|
102
|
+
}
|
|
103
|
+
async getEPStat(req) {
|
|
104
|
+
const allSlots = [];
|
|
105
|
+
const sortedDayKeys = Array.from(this.slots.keys()).sort((a, b) => a - b);
|
|
106
|
+
for (const dayKey of sortedDayKeys) {
|
|
107
|
+
const dayMap = this.slots.get(dayKey);
|
|
108
|
+
const sortedMinKeys = Array.from(dayMap.keys()).sort((a, b) => a - b);
|
|
109
|
+
for (const minKey of sortedMinKeys) {
|
|
110
|
+
allSlots.push(dayMap.get(minKey));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const steps = { hour: 1, day: 10, week: 30 };
|
|
114
|
+
const step = steps[req.range] || 1;
|
|
115
|
+
const limits = { hour: 60, day: 1440, week: 7200 };
|
|
116
|
+
const limitedSlots = allSlots.slice(-(limits[req.range] || 60));
|
|
117
|
+
return this.aggregate(limitedSlots, step);
|
|
118
|
+
}
|
|
119
|
+
aggregate(allSlots, step) {
|
|
120
|
+
const result = [];
|
|
121
|
+
for (let i = 0; i < allSlots.length; i += step) {
|
|
122
|
+
const chunk = allSlots.slice(i, i + step);
|
|
123
|
+
const first = chunk[0];
|
|
124
|
+
const agg = {
|
|
125
|
+
yyyymmddhhii: first.yyyymmddhhii,
|
|
126
|
+
cpu: 0,
|
|
127
|
+
memory: { rss: 0, heapUsed: 0, heapTotal: 0, external: 0 },
|
|
128
|
+
successEp: {},
|
|
129
|
+
failEp: {},
|
|
130
|
+
http: {},
|
|
131
|
+
http_ok: {},
|
|
132
|
+
http_fail: {},
|
|
133
|
+
httpP95: {},
|
|
134
|
+
success_sql: {},
|
|
135
|
+
fail_sql: {}
|
|
136
|
+
};
|
|
137
|
+
const sum = (dst, src) => {
|
|
138
|
+
if (!src)
|
|
139
|
+
return;
|
|
140
|
+
for (const [k, v] of Object.entries(src))
|
|
141
|
+
dst[k] = (dst[k] || 0) + v;
|
|
142
|
+
};
|
|
143
|
+
const maxInto = (dst, src) => {
|
|
144
|
+
if (!src)
|
|
145
|
+
return;
|
|
146
|
+
for (const [k, v] of Object.entries(src))
|
|
147
|
+
dst[k] = Math.max(dst[k] || 0, v);
|
|
148
|
+
};
|
|
149
|
+
chunk.forEach(s => {
|
|
150
|
+
agg.cpu += s.cpu;
|
|
151
|
+
agg.memory.rss += s.memory.rss;
|
|
152
|
+
agg.memory.heapUsed += s.memory.heapUsed;
|
|
153
|
+
agg.memory.heapTotal += s.memory.heapTotal;
|
|
154
|
+
agg.memory.external += s.memory.external;
|
|
155
|
+
sum(agg.successEp, s.successEp);
|
|
156
|
+
sum(agg.failEp, s.failEp);
|
|
157
|
+
sum(agg.http, s.http);
|
|
158
|
+
sum(agg.http_ok, s.http_ok);
|
|
159
|
+
sum(agg.http_fail, s.http_fail);
|
|
160
|
+
sum(agg.success_sql, s.success_sql);
|
|
161
|
+
sum(agg.fail_sql, s.fail_sql);
|
|
162
|
+
maxInto(agg.httpP95, s.httpP95);
|
|
163
|
+
});
|
|
164
|
+
const len = chunk.length;
|
|
165
|
+
agg.cpu = Number((agg.cpu / len).toFixed(2));
|
|
166
|
+
agg.memory.rss = Math.round(agg.memory.rss / len);
|
|
167
|
+
agg.memory.heapUsed = Math.round(agg.memory.heapUsed / len);
|
|
168
|
+
agg.memory.heapTotal = Math.round(agg.memory.heapTotal / len);
|
|
169
|
+
agg.memory.external = Math.round(agg.memory.external / len);
|
|
170
|
+
result.push({
|
|
171
|
+
timestamp: this.slotToTimestamp(agg.yyyymmddhhii),
|
|
172
|
+
slot: agg
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
slotToTimestamp(val) {
|
|
178
|
+
const s = val.toString();
|
|
179
|
+
return new Date(+s.substring(0, 4), +s.substring(4, 6) - 1, +s.substring(6, 8), +s.substring(8, 10), +s.substring(10, 12)).getTime();
|
|
180
|
+
}
|
|
181
|
+
onStatRegistrate(data) {
|
|
182
|
+
const date = new Date();
|
|
183
|
+
const yyyymmdd = parseInt((0, YYYYMMDDHH_1.YYYYMMDDHH)(date, "yyyymmdd"));
|
|
184
|
+
const yyyymmddhhmm = parseInt((0, YYYYMMDDHH_1.YYYYMMDDHH)(date, "yyyymmddhhmm"));
|
|
185
|
+
const ep = data.endpoint || "unknown";
|
|
186
|
+
const slot = this.getSlot(yyyymmddhhmm);
|
|
187
|
+
if (!data.error)
|
|
188
|
+
slot.successEp[ep] = (slot.successEp[ep] || 0) + 1;
|
|
189
|
+
else
|
|
190
|
+
slot.failEp[ep] = (slot.failEp[ep] || 0) + 1;
|
|
191
|
+
}
|
|
192
|
+
async collectUsage() {
|
|
193
|
+
const usage = process.cpuUsage();
|
|
194
|
+
const now = Date.now();
|
|
195
|
+
const yyyymmddhhmm = parseInt((0, YYYYMMDDHH_1.YYYYMMDDHH)(new Date(now), "yyyymmddhhmm"));
|
|
196
|
+
this.finalizeLatency(yyyymmddhhmm);
|
|
197
|
+
const slot = this.getSlot(yyyymmddhhmm);
|
|
198
|
+
if (this.lastCpuUsage && this.lastTimestamp) {
|
|
199
|
+
const diff = usage.user - this.lastCpuUsage.user;
|
|
200
|
+
const sysdiff = usage.system - this.lastCpuUsage.system;
|
|
201
|
+
const timeDiffMs = now - this.lastTimestamp;
|
|
202
|
+
slot.cpu = Number(((diff + sysdiff) / 1000 / timeDiffMs / os_1.default.cpus().length * 100).toFixed(2));
|
|
203
|
+
}
|
|
204
|
+
this.lastCpuUsage = usage;
|
|
205
|
+
this.lastTimestamp = now;
|
|
206
|
+
const mem = process.memoryUsage();
|
|
207
|
+
slot.memory = {
|
|
208
|
+
rss: Math.round(mem.rss / 1024 / 1024),
|
|
209
|
+
heapUsed: Math.round(mem.heapUsed / 1024 / 1024),
|
|
210
|
+
heapTotal: Math.round(mem.heapTotal / 1024 / 1024),
|
|
211
|
+
external: Math.round(mem.external / 1024 / 1024),
|
|
212
|
+
};
|
|
213
|
+
if (this.metricsDir && this._persistReady && this._live)
|
|
214
|
+
await this.persistTick().catch(() => { });
|
|
215
|
+
}
|
|
216
|
+
static MAX_SLOT_KEYS = 100;
|
|
217
|
+
static normalizeQuery(query) {
|
|
218
|
+
const normalized = query
|
|
219
|
+
.replace(/'(?:\\.|''|[^'])*'/g, "?")
|
|
220
|
+
.replace(/"(?:\\.|""|[^"])*"/g, "?")
|
|
221
|
+
.replace(/\b\d+(?:\.\d+)?\b/g, "?")
|
|
222
|
+
.replace(/\(\s*\?(?:\s*,\s*\?)+\s*\)/g, "(?)")
|
|
223
|
+
.replace(/\s+/g, " ")
|
|
224
|
+
.trim();
|
|
225
|
+
return normalized.length > 300 ? normalized.substring(0, 300) : normalized;
|
|
226
|
+
}
|
|
227
|
+
static normalizeUrl(url) {
|
|
228
|
+
const q = url.indexOf("?");
|
|
229
|
+
const h = url.indexOf("#");
|
|
230
|
+
let end = url.length;
|
|
231
|
+
if (q >= 0)
|
|
232
|
+
end = Math.min(end, q);
|
|
233
|
+
if (h >= 0)
|
|
234
|
+
end = Math.min(end, h);
|
|
235
|
+
const path = url.substring(0, end);
|
|
236
|
+
return path.length > 200 ? path.substring(0, 200) : path;
|
|
237
|
+
}
|
|
238
|
+
bumpCappedKey(record, key) {
|
|
239
|
+
if (key in record) {
|
|
240
|
+
record[key]++;
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (Object.keys(record).length >= MonitorService.MAX_SLOT_KEYS) {
|
|
244
|
+
record["__other__"] = (record["__other__"] || 0) + 1;
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
record[key] = 1;
|
|
248
|
+
}
|
|
249
|
+
onHttpRequestRegistrate(data) {
|
|
250
|
+
const date = new Date();
|
|
251
|
+
const yyyymmddhhmm = parseInt((0, YYYYMMDDHH_1.YYYYMMDDHH)(date, "yyyymmddhhmm"));
|
|
252
|
+
const slot = this.getSlot(yyyymmddhhmm);
|
|
253
|
+
this.bumpCappedKey(slot.http, MonitorService.normalizeUrl(data.url));
|
|
254
|
+
}
|
|
255
|
+
_latByMinute = new Map();
|
|
256
|
+
static LAT_CAP_PER_MIN = 3000;
|
|
257
|
+
onHttpResultRegistrate(data) {
|
|
258
|
+
const minuteKey = parseInt((0, YYYYMMDDHH_1.YYYYMMDDHH)(new Date(), "yyyymmddhhmm"));
|
|
259
|
+
const slot = this.getSlot(minuteKey);
|
|
260
|
+
this.bumpCappedKey(data.ok ? slot.http_ok : slot.http_fail, data.host);
|
|
261
|
+
let hosts = this._latByMinute.get(minuteKey);
|
|
262
|
+
if (!hosts) {
|
|
263
|
+
hosts = new Map();
|
|
264
|
+
this._latByMinute.set(minuteKey, hosts);
|
|
265
|
+
}
|
|
266
|
+
let arr = hosts.get(data.host);
|
|
267
|
+
if (!arr) {
|
|
268
|
+
arr = [];
|
|
269
|
+
hosts.set(data.host, arr);
|
|
270
|
+
}
|
|
271
|
+
if (arr.length < MonitorService.LAT_CAP_PER_MIN)
|
|
272
|
+
arr.push(data.ms);
|
|
273
|
+
}
|
|
274
|
+
finalizeLatency(currentMinute, includeCurrent = false) {
|
|
275
|
+
for (const [minuteKey, hosts] of this._latByMinute) {
|
|
276
|
+
if (includeCurrent ? minuteKey > currentMinute : minuteKey >= currentMinute)
|
|
277
|
+
continue;
|
|
278
|
+
const slot = this.slots.get(Math.floor(minuteKey / 100))?.get(minuteKey);
|
|
279
|
+
if (slot) {
|
|
280
|
+
for (const [host, samples] of hosts) {
|
|
281
|
+
if (!samples.length)
|
|
282
|
+
continue;
|
|
283
|
+
samples.sort((a, b) => a - b);
|
|
284
|
+
const idx = Math.min(samples.length - 1, Math.max(0, Math.ceil(0.95 * samples.length) - 1));
|
|
285
|
+
slot.httpP95[host] = samples[idx];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
this._latByMinute.delete(minuteKey);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
onSQLRequestRegistrate(data) {
|
|
292
|
+
const date = new Date();
|
|
293
|
+
const yyyymmddhhmm = parseInt((0, YYYYMMDDHH_1.YYYYMMDDHH)(date, "yyyymmddhhmm"));
|
|
294
|
+
const slot = this.getSlot(yyyymmddhhmm);
|
|
295
|
+
const key = MonitorService.normalizeQuery(data.query);
|
|
296
|
+
this.bumpCappedKey(data.success ? slot.success_sql : slot.fail_sql, key);
|
|
297
|
+
}
|
|
298
|
+
getSlot(yyyymmddhhii) {
|
|
299
|
+
const yyymmdd = Math.floor(yyyymmddhhii / 100);
|
|
300
|
+
let daySlots = this.slots.get(yyymmdd);
|
|
301
|
+
if (!daySlots) {
|
|
302
|
+
daySlots = new Map();
|
|
303
|
+
this.slots.set(yyymmdd, daySlots);
|
|
304
|
+
if (this.slots.size > 7) {
|
|
305
|
+
let key = this.slots.keys().next().value;
|
|
306
|
+
if (key)
|
|
307
|
+
this.slots.delete(key);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
let slot = daySlots.get(yyyymmddhhii);
|
|
311
|
+
if (!slot) {
|
|
312
|
+
slot = {
|
|
313
|
+
yyyymmddhhii,
|
|
314
|
+
cpu: 0,
|
|
315
|
+
memory: {
|
|
316
|
+
rss: 0,
|
|
317
|
+
heapUsed: 0,
|
|
318
|
+
heapTotal: 0,
|
|
319
|
+
external: 0,
|
|
320
|
+
},
|
|
321
|
+
successEp: {},
|
|
322
|
+
failEp: {},
|
|
323
|
+
http: {},
|
|
324
|
+
http_ok: {},
|
|
325
|
+
http_fail: {},
|
|
326
|
+
httpP95: {},
|
|
327
|
+
success_sql: {},
|
|
328
|
+
fail_sql: {}
|
|
329
|
+
};
|
|
330
|
+
daySlots.set(yyyymmddhhii, slot);
|
|
331
|
+
}
|
|
332
|
+
return slot;
|
|
333
|
+
}
|
|
334
|
+
liveFile(weekKey) {
|
|
335
|
+
return path_1.default.join(this.metricsDir, `metrics_${weekKey}.jsonl`);
|
|
336
|
+
}
|
|
337
|
+
archiveFile(weekKey) {
|
|
338
|
+
return path_1.default.join(this.metricsDir, `metrics_${weekKey}.json`);
|
|
339
|
+
}
|
|
340
|
+
weekKey(date) {
|
|
341
|
+
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
|
342
|
+
const dayNum = (d.getUTCDay() + 6) % 7;
|
|
343
|
+
d.setUTCDate(d.getUTCDate() - dayNum + 3);
|
|
344
|
+
const firstThursday = new Date(Date.UTC(d.getUTCFullYear(), 0, 4));
|
|
345
|
+
const firstDayNum = (firstThursday.getUTCDay() + 6) % 7;
|
|
346
|
+
firstThursday.setUTCDate(firstThursday.getUTCDate() - firstDayNum + 3);
|
|
347
|
+
const week = 1 + Math.round((d.getTime() - firstThursday.getTime()) / (7 * 24 * 3600 * 1000));
|
|
348
|
+
return `${d.getUTCFullYear()}-W${String(week).padStart(2, "0")}`;
|
|
349
|
+
}
|
|
350
|
+
async atomicWrite(file, content) {
|
|
351
|
+
const tmp = file + ".tmp";
|
|
352
|
+
await promises_1.default.writeFile(tmp, content, "utf-8");
|
|
353
|
+
await promises_1.default.rename(tmp, file);
|
|
354
|
+
}
|
|
355
|
+
normalizeSlot(raw) {
|
|
356
|
+
if (!raw || typeof raw !== "object")
|
|
357
|
+
return null;
|
|
358
|
+
const key = Number(raw.yyyymmddhhii);
|
|
359
|
+
if (!Number.isFinite(key))
|
|
360
|
+
return null;
|
|
361
|
+
const num = (v) => (typeof v === "number" && Number.isFinite(v) ? v : 0);
|
|
362
|
+
const rec = (v) => {
|
|
363
|
+
const out = {};
|
|
364
|
+
if (v && typeof v === "object" && !Array.isArray(v))
|
|
365
|
+
for (const [k, val] of Object.entries(v))
|
|
366
|
+
if (typeof val === "number" && Number.isFinite(val))
|
|
367
|
+
out[k] = val;
|
|
368
|
+
return out;
|
|
369
|
+
};
|
|
370
|
+
const m = raw.memory && typeof raw.memory === "object" ? raw.memory : {};
|
|
371
|
+
return {
|
|
372
|
+
yyyymmddhhii: key,
|
|
373
|
+
cpu: num(raw.cpu),
|
|
374
|
+
memory: { rss: num(m.rss), heapUsed: num(m.heapUsed), heapTotal: num(m.heapTotal), external: num(m.external) },
|
|
375
|
+
successEp: rec(raw.successEp), failEp: rec(raw.failEp),
|
|
376
|
+
http: rec(raw.http), http_ok: rec(raw.http_ok), http_fail: rec(raw.http_fail), httpP95: rec(raw.httpP95),
|
|
377
|
+
success_sql: rec(raw.success_sql), fail_sql: rec(raw.fail_sql),
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
async readJsonl(file) {
|
|
381
|
+
const txt = await promises_1.default.readFile(file, "utf-8");
|
|
382
|
+
const out = [];
|
|
383
|
+
for (const line of txt.split("\n")) {
|
|
384
|
+
const t = line.trim();
|
|
385
|
+
if (!t)
|
|
386
|
+
continue;
|
|
387
|
+
try {
|
|
388
|
+
const s = this.normalizeSlot(JSON.parse(t));
|
|
389
|
+
if (s)
|
|
390
|
+
out.push(s);
|
|
391
|
+
}
|
|
392
|
+
catch { }
|
|
393
|
+
}
|
|
394
|
+
return out;
|
|
395
|
+
}
|
|
396
|
+
async persistTick(includeCurrentMinute = false) {
|
|
397
|
+
if (!this.metricsDir)
|
|
398
|
+
return;
|
|
399
|
+
const nowKey = parseInt((0, YYYYMMDDHH_1.YYYYMMDDHH)(new Date(), "yyyymmddhhmm"));
|
|
400
|
+
this.finalizeLatency(nowKey, includeCurrentMinute);
|
|
401
|
+
const pending = [];
|
|
402
|
+
for (const dayMap of this.slots.values()) {
|
|
403
|
+
for (const slot of dayMap.values()) {
|
|
404
|
+
const k = slot.yyyymmddhhii;
|
|
405
|
+
if (k <= this._flushedUpTo)
|
|
406
|
+
continue;
|
|
407
|
+
if (!includeCurrentMinute && k >= nowKey)
|
|
408
|
+
continue;
|
|
409
|
+
pending.push(slot);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
pending.sort((a, b) => a.yyyymmddhhii - b.yyyymmddhhii);
|
|
413
|
+
if (pending.length) {
|
|
414
|
+
try {
|
|
415
|
+
const byWeek = new Map();
|
|
416
|
+
for (const s of pending) {
|
|
417
|
+
const wk = this.weekKey(new Date(this.slotToTimestamp(s.yyyymmddhhii)));
|
|
418
|
+
byWeek.set(wk, (byWeek.get(wk) ?? "") + JSON.stringify(s) + "\n");
|
|
419
|
+
}
|
|
420
|
+
for (const [wk, text] of byWeek)
|
|
421
|
+
await promises_1.default.appendFile(this.liveFile(wk), text, "utf-8");
|
|
422
|
+
this._flushedUpTo = pending[pending.length - 1].yyyymmddhhii;
|
|
423
|
+
}
|
|
424
|
+
catch (e) {
|
|
425
|
+
(0, LogService_1.logWarn)("metrics: persist append failed: " + (e?.message ?? e));
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
const curWeek = this.weekKey(new Date());
|
|
429
|
+
if (this._lastSeenWeekKey && this._lastSeenWeekKey !== curWeek)
|
|
430
|
+
await this.rotate().catch(() => { });
|
|
431
|
+
this._lastSeenWeekKey = curWeek;
|
|
432
|
+
}
|
|
433
|
+
async rotate() {
|
|
434
|
+
if (!this.metricsDir)
|
|
435
|
+
return;
|
|
436
|
+
try {
|
|
437
|
+
const curWeek = this.weekKey(new Date());
|
|
438
|
+
const files = await promises_1.default.readdir(this.metricsDir);
|
|
439
|
+
for (const f of files) {
|
|
440
|
+
const m = f.match(/^metrics_(\d{4}-W\d{2})\.jsonl$/);
|
|
441
|
+
if (m && m[1] !== curWeek)
|
|
442
|
+
await this.downsampleWeek(m[1]);
|
|
443
|
+
}
|
|
444
|
+
const after = await promises_1.default.readdir(this.metricsDir);
|
|
445
|
+
const byWeek = new Map();
|
|
446
|
+
for (const f of after) {
|
|
447
|
+
const m = f.match(/^metrics_(\d{4}-W\d{2})\.(jsonl|json)$/);
|
|
448
|
+
if (!m)
|
|
449
|
+
continue;
|
|
450
|
+
const arr = byWeek.get(m[1]) ?? [];
|
|
451
|
+
arr.push(f);
|
|
452
|
+
byWeek.set(m[1], arr);
|
|
453
|
+
}
|
|
454
|
+
const weeks = Array.from(byWeek.keys()).sort();
|
|
455
|
+
const excess = weeks.slice(0, Math.max(0, weeks.length - MonitorService.MAX_WEEKS));
|
|
456
|
+
for (const wk of excess)
|
|
457
|
+
for (const f of byWeek.get(wk))
|
|
458
|
+
await promises_1.default.unlink(path_1.default.join(this.metricsDir, f)).catch(() => { });
|
|
459
|
+
}
|
|
460
|
+
catch (e) {
|
|
461
|
+
(0, LogService_1.logWarn)("metrics: rotate failed: " + (e?.message ?? e));
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
async downsampleWeek(weekKey) {
|
|
465
|
+
const live = this.liveFile(weekKey);
|
|
466
|
+
let slots;
|
|
467
|
+
try {
|
|
468
|
+
slots = await this.readJsonl(live);
|
|
469
|
+
}
|
|
470
|
+
catch {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
if (!slots.length) {
|
|
474
|
+
await promises_1.default.unlink(live).catch(() => { });
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const byHour = new Map();
|
|
478
|
+
for (const s of slots) {
|
|
479
|
+
const hourKey = Math.floor(s.yyyymmddhhii / 100) * 100;
|
|
480
|
+
const arr = byHour.get(hourKey) ?? [];
|
|
481
|
+
arr.push(s);
|
|
482
|
+
byHour.set(hourKey, arr);
|
|
483
|
+
}
|
|
484
|
+
const hourly = Array.from(byHour.entries())
|
|
485
|
+
.sort((a, b) => a[0] - b[0])
|
|
486
|
+
.map(([hourKey, arr]) => this.mergeSlots(hourKey, arr));
|
|
487
|
+
await this.atomicWrite(this.archiveFile(weekKey), JSON.stringify({ version: MonitorService.SCHEMA_VERSION, weekKey, resolution: "hour", slots: hourly }));
|
|
488
|
+
await promises_1.default.unlink(live).catch(() => { });
|
|
489
|
+
}
|
|
490
|
+
mergeSlots(key, arr) {
|
|
491
|
+
const out = {
|
|
492
|
+
yyyymmddhhii: key, cpu: 0,
|
|
493
|
+
memory: { rss: 0, heapUsed: 0, heapTotal: 0, external: 0 },
|
|
494
|
+
successEp: {}, failEp: {}, http: {}, http_ok: {}, http_fail: {}, httpP95: {},
|
|
495
|
+
success_sql: {}, fail_sql: {},
|
|
496
|
+
};
|
|
497
|
+
const add = (dst, src) => {
|
|
498
|
+
if (!src)
|
|
499
|
+
return;
|
|
500
|
+
for (const [k, v] of Object.entries(src))
|
|
501
|
+
dst[k] = (dst[k] || 0) + v;
|
|
502
|
+
};
|
|
503
|
+
const maxInto = (dst, src) => {
|
|
504
|
+
if (!src)
|
|
505
|
+
return;
|
|
506
|
+
for (const [k, v] of Object.entries(src))
|
|
507
|
+
dst[k] = Math.max(dst[k] || 0, v);
|
|
508
|
+
};
|
|
509
|
+
for (const s of arr) {
|
|
510
|
+
out.cpu += s.cpu;
|
|
511
|
+
out.memory.rss += s.memory.rss;
|
|
512
|
+
out.memory.heapUsed += s.memory.heapUsed;
|
|
513
|
+
out.memory.heapTotal += s.memory.heapTotal;
|
|
514
|
+
out.memory.external += s.memory.external;
|
|
515
|
+
add(out.successEp, s.successEp);
|
|
516
|
+
add(out.failEp, s.failEp);
|
|
517
|
+
add(out.http, s.http);
|
|
518
|
+
add(out.http_ok, s.http_ok);
|
|
519
|
+
add(out.http_fail, s.http_fail);
|
|
520
|
+
add(out.success_sql, s.success_sql);
|
|
521
|
+
add(out.fail_sql, s.fail_sql);
|
|
522
|
+
maxInto(out.httpP95, s.httpP95);
|
|
523
|
+
}
|
|
524
|
+
const n = arr.length || 1;
|
|
525
|
+
out.cpu = Number((out.cpu / n).toFixed(2));
|
|
526
|
+
out.memory.rss = Math.round(out.memory.rss / n);
|
|
527
|
+
out.memory.heapUsed = Math.round(out.memory.heapUsed / n);
|
|
528
|
+
out.memory.heapTotal = Math.round(out.memory.heapTotal / n);
|
|
529
|
+
out.memory.external = Math.round(out.memory.external / n);
|
|
530
|
+
return out;
|
|
531
|
+
}
|
|
532
|
+
async hydrate() {
|
|
533
|
+
if (!this.metricsDir)
|
|
534
|
+
return;
|
|
535
|
+
try {
|
|
536
|
+
const cutoff = parseInt((0, YYYYMMDDHH_1.YYYYMMDDHH)(new Date(Date.now() - 7 * 24 * 3600 * 1000), "yyyymmddhhmm"));
|
|
537
|
+
const weeks = new Set([
|
|
538
|
+
this.weekKey(new Date()),
|
|
539
|
+
this.weekKey(new Date(Date.now() - 7 * 24 * 3600 * 1000)),
|
|
540
|
+
]);
|
|
541
|
+
let maxKey = 0;
|
|
542
|
+
for (const wk of weeks) {
|
|
543
|
+
let slots;
|
|
544
|
+
try {
|
|
545
|
+
slots = await this.readJsonl(this.liveFile(wk));
|
|
546
|
+
}
|
|
547
|
+
catch {
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
for (const s of slots) {
|
|
551
|
+
if (s.yyyymmddhhii < cutoff)
|
|
552
|
+
continue;
|
|
553
|
+
const dayKey = Math.floor(s.yyyymmddhhii / 100);
|
|
554
|
+
let dayMap = this.slots.get(dayKey);
|
|
555
|
+
if (!dayMap) {
|
|
556
|
+
dayMap = new Map();
|
|
557
|
+
this.slots.set(dayKey, dayMap);
|
|
558
|
+
}
|
|
559
|
+
dayMap.set(s.yyyymmddhhii, s);
|
|
560
|
+
if (s.yyyymmddhhii > maxKey)
|
|
561
|
+
maxKey = s.yyyymmddhhii;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
this._flushedUpTo = maxKey;
|
|
565
|
+
while (this.slots.size > 7) {
|
|
566
|
+
const oldest = Array.from(this.slots.keys()).sort((a, b) => a - b)[0];
|
|
567
|
+
this.slots.delete(oldest);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
catch (e) {
|
|
571
|
+
(0, LogService_1.logWarn)("metrics: hydrate failed: " + (e?.message ?? e));
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
async listPersistedWeeks() {
|
|
575
|
+
if (!this.metricsDir)
|
|
576
|
+
return [];
|
|
577
|
+
try {
|
|
578
|
+
const files = await promises_1.default.readdir(this.metricsDir);
|
|
579
|
+
const weeks = new Set();
|
|
580
|
+
for (const f of files) {
|
|
581
|
+
const m = f.match(/^metrics_(\d{4}-W\d{2})\.(jsonl|json)$/);
|
|
582
|
+
if (m)
|
|
583
|
+
weeks.add(m[1]);
|
|
584
|
+
}
|
|
585
|
+
return Array.from(weeks).sort();
|
|
586
|
+
}
|
|
587
|
+
catch {
|
|
588
|
+
return [];
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
async readPersistedWeek(weekKey) {
|
|
592
|
+
if (!this.metricsDir || !/^\d{4}-W\d{2}$/.test(weekKey))
|
|
593
|
+
return [];
|
|
594
|
+
let slots = [];
|
|
595
|
+
try {
|
|
596
|
+
try {
|
|
597
|
+
slots = await this.readJsonl(this.liveFile(weekKey));
|
|
598
|
+
}
|
|
599
|
+
catch {
|
|
600
|
+
const txt = await promises_1.default.readFile(this.archiveFile(weekKey), "utf-8");
|
|
601
|
+
const rawSlots = JSON.parse(txt)?.slots;
|
|
602
|
+
slots = Array.isArray(rawSlots)
|
|
603
|
+
? rawSlots.map((r) => this.normalizeSlot(r)).filter((s) => s !== null)
|
|
604
|
+
: [];
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
catch {
|
|
608
|
+
return [];
|
|
609
|
+
}
|
|
610
|
+
return slots
|
|
611
|
+
.sort((a, b) => a.yyyymmddhhii - b.yyyymmddhhii)
|
|
612
|
+
.map(s => ({ timestamp: this.slotToTimestamp(s.yyyymmddhhii), slot: s }));
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
exports.MonitorService = MonitorService;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StatService = void 0;
|
|
4
|
+
const BaseService_1 = require("./BaseService");
|
|
5
|
+
class StatService extends BaseService_1.BaseService {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("StatService");
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.StatService = StatService;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Signal, Req } from "../signal/Signal";
|
|
2
|
+
import { BaseService } from "./BaseService";
|
|
3
|
+
export declare const REQ_TIMEFRAME_TASK_ADD: Req<ITimeframeTask<any>, void>;
|
|
4
|
+
export declare const S_TIMEFRAME_TASK_EXECUTED: Signal<ITimeframeTask<any>>;
|
|
5
|
+
export interface ITimeframeTask<T = any> {
|
|
6
|
+
name: string;
|
|
7
|
+
start: number;
|
|
8
|
+
params?: T;
|
|
9
|
+
repeat: number;
|
|
10
|
+
callback: (params?: T) => Promise<void>;
|
|
11
|
+
executed: number;
|
|
12
|
+
retries?: number;
|
|
13
|
+
maxRetries?: number;
|
|
14
|
+
processing?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare class TimeframeService extends BaseService {
|
|
17
|
+
TICK_INTERVAL_MS: number;
|
|
18
|
+
RETRY_DELAY_MS: number;
|
|
19
|
+
GRACE_WINDOW_MS: number;
|
|
20
|
+
tasks: ITimeframeTask<any>[];
|
|
21
|
+
private intervalId;
|
|
22
|
+
static wait(ms: number): Promise<void>;
|
|
23
|
+
constructor();
|
|
24
|
+
init(): Promise<void>;
|
|
25
|
+
loop(): Promise<void>;
|
|
26
|
+
private runTask;
|
|
27
|
+
finishService(): Promise<void>;
|
|
28
|
+
}
|