badmfck-api-server 1.5.9 → 1.6.2
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/dist/apiServer/APIService.js +6 -6
- package/dist/apiServer/LogService.d.ts +1 -1
- package/dist/apiServer/LogService.js +3 -2
- package/dist/apiServer/MysqlService.d.ts +2 -1
- package/dist/apiServer/MysqlService.js +27 -2
- package/dist/apiServer/monitor/Monitor.d.ts +8 -1
- package/dist/apiServer/monitor/Monitor.js +73 -17
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/package.json +1 -1
@@ -47,7 +47,7 @@ async function Initializer(services) {
|
|
47
47
|
exports.Initializer = Initializer;
|
48
48
|
class APIService extends BaseService_1.BaseService {
|
49
49
|
static nextLogID = 0;
|
50
|
-
version = "1.
|
50
|
+
version = "1.6.2";
|
51
51
|
options;
|
52
52
|
monitor;
|
53
53
|
monitorIndexFile;
|
@@ -63,11 +63,11 @@ class APIService extends BaseService_1.BaseService {
|
|
63
63
|
if (this.options.monitor && this.options.monitor.length > 0) {
|
64
64
|
this.monitor = new Monitor_1.Monitor();
|
65
65
|
this.options.endpoints.push(this.monitor);
|
66
|
-
console.
|
67
|
-
console.
|
66
|
+
console.warn("Service Monitor initialized");
|
67
|
+
console.warn("monitor links:");
|
68
68
|
for (let i of this.options.monitor) {
|
69
69
|
const hash = crypto_1.default.createHash("sha256").update(i.login + i.password).digest().toString("hex");
|
70
|
-
console.
|
70
|
+
console.warn(i.login + " -> /sm-" + hash);
|
71
71
|
}
|
72
72
|
}
|
73
73
|
}
|
@@ -252,9 +252,9 @@ class APIService extends BaseService_1.BaseService {
|
|
252
252
|
log.time = data.responseTime;
|
253
253
|
if (res.destroyed || res.closed) {
|
254
254
|
if (log)
|
255
|
-
log.error = "Connection already closed, can't send response";
|
255
|
+
log.error = "Connection already closed, can't send response for: " + data.endpoint;
|
256
256
|
if (this.options.onError)
|
257
|
-
this.options.onError("Connection already closed, can't send response", data);
|
257
|
+
this.options.onError("Connection already closed, can't send response: " + data.endpoint, data);
|
258
258
|
if (this.monitor)
|
259
259
|
this.monitor.registrateError(data.endpoint);
|
260
260
|
}
|
@@ -153,8 +153,9 @@ class LogService extends BaseService_1.BaseService {
|
|
153
153
|
date: this.createDate(d),
|
154
154
|
source: source
|
155
155
|
};
|
156
|
-
if (this.options.output)
|
157
|
-
this.options.output(logitem
|
156
|
+
if (this.options.output) {
|
157
|
+
this.options.output(logitem);
|
158
|
+
}
|
158
159
|
exports.S_LOG_CREATED.invoke(logitem);
|
159
160
|
this.log.push(logitem);
|
160
161
|
let limit = this.options.stackSize ?? 100;
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { FieldInfo, MysqlError, Pool, PoolConnection } from "mysql";
|
2
2
|
import { BaseService } from "./BaseService";
|
3
|
-
import { Req } from "badmfck-signal";
|
3
|
+
import Signal, { Req } from "badmfck-signal";
|
4
|
+
export declare const S_MYSQL_STARTED: Signal<void>;
|
4
5
|
export declare const REQ_MYSQL_QUERY: Req<MySqlQuery | MySqlQuery[], MysqlResult[]>;
|
5
6
|
export declare const executeQuery: (query: MySqlQuery | MySqlQuery[]) => Promise<MysqlResult[]>;
|
6
7
|
export interface MysqlServiceOptions {
|
@@ -1,14 +1,38 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
2
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
27
|
};
|
5
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_QUERY = void 0;
|
29
|
+
exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_QUERY = exports.S_MYSQL_STARTED = void 0;
|
7
30
|
const mysql_1 = __importDefault(require("mysql"));
|
8
31
|
const BaseService_1 = require("./BaseService");
|
9
|
-
const badmfck_signal_1 = require("badmfck-signal");
|
32
|
+
const badmfck_signal_1 = __importStar(require("badmfck-signal"));
|
10
33
|
const crypto_1 = require("crypto");
|
11
34
|
const LogService_1 = require("./LogService");
|
35
|
+
exports.S_MYSQL_STARTED = new badmfck_signal_1.default();
|
12
36
|
exports.REQ_MYSQL_QUERY = new badmfck_signal_1.Req("REQ_MYSQL_QUERY");
|
13
37
|
const executeQuery = async (query) => { return await exports.REQ_MYSQL_QUERY.request(query); };
|
14
38
|
exports.executeQuery = executeQuery;
|
@@ -60,6 +84,7 @@ class MysqlService extends BaseService_1.BaseService {
|
|
60
84
|
else {
|
61
85
|
this.serviceStarted = true;
|
62
86
|
(0, LogService_1.logInfo)("${MysqlService.js}", "Mysql Service started!");
|
87
|
+
exports.S_MYSQL_STARTED.invoke();
|
63
88
|
}
|
64
89
|
}
|
65
90
|
async onApplicationReady() { }
|
@@ -1,5 +1,9 @@
|
|
1
|
+
import { Signal } from "badmfck-signal";
|
1
2
|
import { BaseEndpoint } from "../BaseEndpoint";
|
2
3
|
import { HTTPRequestVO, TransferPacketVO } from "../structures/Interfaces";
|
4
|
+
export interface IUserAction {
|
5
|
+
action: string;
|
6
|
+
}
|
3
7
|
interface IStatObject {
|
4
8
|
requests: Map<string, number>;
|
5
9
|
errors: Map<string, number>;
|
@@ -11,12 +15,15 @@ interface ISystemStat {
|
|
11
15
|
memoryTotal: number;
|
12
16
|
memoryUsage: any;
|
13
17
|
}
|
18
|
+
export declare const S_MONITOR_REGISTRATE_ACTION: Signal<IUserAction>;
|
14
19
|
export declare class Monitor extends BaseEndpoint {
|
15
20
|
ignoreHttpLogging: boolean;
|
16
|
-
private startedAt;
|
17
21
|
private httpRequests;
|
18
22
|
private systemStat;
|
23
|
+
private userActions;
|
24
|
+
private registeredActions;
|
19
25
|
constructor();
|
26
|
+
registrateAction(data: IUserAction): void;
|
20
27
|
addSystemStat(): void;
|
21
28
|
registrateError(endpoint: string): void;
|
22
29
|
registrateFatalError(endpoint: string): void;
|
@@ -3,19 +3,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.Monitor = void 0;
|
6
|
+
exports.Monitor = exports.S_MONITOR_REGISTRATE_ACTION = void 0;
|
7
|
+
const badmfck_signal_1 = require("badmfck-signal");
|
7
8
|
const APIService_1 = require("../APIService");
|
8
9
|
const BaseEndpoint_1 = require("../BaseEndpoint");
|
9
10
|
const LogService_1 = require("../LogService");
|
10
11
|
const crypto_1 = __importDefault(require("crypto"));
|
11
12
|
const os_1 = __importDefault(require("os"));
|
13
|
+
exports.S_MONITOR_REGISTRATE_ACTION = new badmfck_signal_1.Signal();
|
12
14
|
class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
13
15
|
ignoreHttpLogging = true;
|
14
|
-
startedAt = +new Date();
|
15
16
|
httpRequests = new Map();
|
16
17
|
systemStat = new Map();
|
18
|
+
userActions = new Map();
|
19
|
+
registeredActions = 0;
|
17
20
|
constructor() {
|
18
21
|
super("sys-monitor");
|
22
|
+
exports.S_MONITOR_REGISTRATE_ACTION.subscribe(data => this.registrateAction(data));
|
19
23
|
this.registerEndpoints([
|
20
24
|
{ ignoreInterceptor: true, endpoint: "log", handler: this.logs },
|
21
25
|
{ ignoreInterceptor: true, endpoint: "netlog", handler: this.netlog },
|
@@ -27,6 +31,38 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
27
31
|
}, 1000 * 60 * 10);
|
28
32
|
this.addSystemStat();
|
29
33
|
}
|
34
|
+
registrateAction(data) {
|
35
|
+
if (!data.action)
|
36
|
+
return;
|
37
|
+
if (typeof data.action !== "string")
|
38
|
+
return;
|
39
|
+
if (data.action.length > 20)
|
40
|
+
data.action = data.action.substring(0, 20);
|
41
|
+
const d = new Date();
|
42
|
+
const doy = this.getDateIndex(d);
|
43
|
+
let day = this.userActions.get(doy);
|
44
|
+
if (!day) {
|
45
|
+
day = new Map();
|
46
|
+
this.userActions.set(doy, day);
|
47
|
+
if (this.userActions.size > 7) {
|
48
|
+
for (let i of this.userActions) {
|
49
|
+
this.userActions.delete(i[0]);
|
50
|
+
break;
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
const minute = this.getHourMinuteIndex(d);
|
55
|
+
let m = day.get(minute);
|
56
|
+
if (!m) {
|
57
|
+
m = new Map();
|
58
|
+
day.set(minute, m);
|
59
|
+
}
|
60
|
+
let cnt = m.get(data.action);
|
61
|
+
if (!cnt)
|
62
|
+
cnt = 0;
|
63
|
+
cnt++;
|
64
|
+
m.set(data.action, cnt);
|
65
|
+
}
|
30
66
|
addSystemStat() {
|
31
67
|
const so = this.createSystemStatObj();
|
32
68
|
so.memoryUsage = process.memoryUsage();
|
@@ -54,6 +90,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
54
90
|
reqep = 0;
|
55
91
|
reqep += 1;
|
56
92
|
statObject.set(endpoint, reqep);
|
93
|
+
this.registeredActions++;
|
57
94
|
}
|
58
95
|
createStatObj() {
|
59
96
|
const d = new Date();
|
@@ -62,7 +99,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
62
99
|
if (!day) {
|
63
100
|
day = new Map();
|
64
101
|
this.httpRequests.set(dtm, day);
|
65
|
-
if (this.httpRequests.size >
|
102
|
+
if (this.httpRequests.size > 7) {
|
66
103
|
for (let i of this.httpRequests) {
|
67
104
|
this.httpRequests.delete(i[0]);
|
68
105
|
break;
|
@@ -89,7 +126,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
89
126
|
if (!day) {
|
90
127
|
day = new Map();
|
91
128
|
this.systemStat.set(dtm, day);
|
92
|
-
if (this.systemStat.size >
|
129
|
+
if (this.systemStat.size > 7) {
|
93
130
|
for (let i of this.systemStat) {
|
94
131
|
this.systemStat.delete(i[0]);
|
95
132
|
break;
|
@@ -162,19 +199,37 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
162
199
|
const date = new Date();
|
163
200
|
const di = this.getDateIndex(date);
|
164
201
|
const stat = this.httpRequests.get(di);
|
165
|
-
if (!stat)
|
166
|
-
return { data: [] };
|
167
202
|
let result = [];
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
203
|
+
if (stat) {
|
204
|
+
for (let minutes of stat) {
|
205
|
+
result.push({
|
206
|
+
m: minutes[0],
|
207
|
+
ae: this.mapToKeyValue(minutes[1].apiErrors),
|
208
|
+
e: this.mapToKeyValue(minutes[1].errors),
|
209
|
+
r: this.mapToKeyValue(minutes[1].requests),
|
210
|
+
fe: this.mapToKeyValue(minutes[1].fatalErrors),
|
211
|
+
});
|
212
|
+
}
|
213
|
+
}
|
214
|
+
const ua = this.userActions.get(di);
|
215
|
+
let uaResult = [];
|
216
|
+
if (ua) {
|
217
|
+
for (let i of ua) {
|
218
|
+
const minutes = i[1];
|
219
|
+
const mArray = [];
|
220
|
+
for (let j of i[0]) {
|
221
|
+
mArray.push({
|
222
|
+
action: j[0],
|
223
|
+
count: j[1]
|
224
|
+
});
|
225
|
+
}
|
226
|
+
result.push({
|
227
|
+
m: i[0],
|
228
|
+
data: mArray
|
229
|
+
});
|
230
|
+
}
|
176
231
|
}
|
177
|
-
return { data: { stat: result, date: +date } };
|
232
|
+
return { data: { stat: result, date: +date, userActions: uaResult } };
|
178
233
|
}
|
179
234
|
async serverStat(req) {
|
180
235
|
const date = new Date();
|
@@ -192,8 +247,9 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
192
247
|
});
|
193
248
|
}
|
194
249
|
return { data: {
|
195
|
-
|
196
|
-
|
250
|
+
registeredActions: this.registeredActions,
|
251
|
+
uptime: process.uptime(),
|
252
|
+
osUptime: os_1.default.uptime(),
|
197
253
|
stat: result
|
198
254
|
} };
|
199
255
|
}
|
package/dist/index.d.ts
CHANGED
@@ -3,4 +3,5 @@ import { LocalRequest } from "./apiServer/LocalRequest";
|
|
3
3
|
import { MysqlService } from "./apiServer/MysqlService";
|
4
4
|
import { Validator } from "./apiServer/helper/Validator";
|
5
5
|
import { LogService } from "./apiServer/LogService";
|
6
|
-
|
6
|
+
import { S_MONITOR_REGISTRATE_ACTION } from "./apiServer/monitor/Monitor";
|
7
|
+
export { APIService, Initializer, LocalRequest, MysqlService, Validator, LogService, S_MONITOR_REGISTRATE_ACTION };
|
package/dist/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.LogService = exports.Validator = exports.MysqlService = exports.LocalRequest = exports.Initializer = exports.APIService = void 0;
|
3
|
+
exports.S_MONITOR_REGISTRATE_ACTION = exports.LogService = exports.Validator = exports.MysqlService = exports.LocalRequest = exports.Initializer = exports.APIService = void 0;
|
4
4
|
const APIService_1 = require("./apiServer/APIService");
|
5
5
|
Object.defineProperty(exports, "APIService", { enumerable: true, get: function () { return APIService_1.APIService; } });
|
6
6
|
Object.defineProperty(exports, "Initializer", { enumerable: true, get: function () { return APIService_1.Initializer; } });
|
@@ -12,3 +12,5 @@ const Validator_1 = require("./apiServer/helper/Validator");
|
|
12
12
|
Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return Validator_1.Validator; } });
|
13
13
|
const LogService_1 = require("./apiServer/LogService");
|
14
14
|
Object.defineProperty(exports, "LogService", { enumerable: true, get: function () { return LogService_1.LogService; } });
|
15
|
+
const Monitor_1 = require("./apiServer/monitor/Monitor");
|
16
|
+
Object.defineProperty(exports, "S_MONITOR_REGISTRATE_ACTION", { enumerable: true, get: function () { return Monitor_1.S_MONITOR_REGISTRATE_ACTION; } });
|