badmfck-api-server 1.5.9 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.5.5";
50
+ version = "1.6.0";
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.log("Service Monitor initialized");
67
- console.log("monitor links:");
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.log("/sm-" + hash);
70
+ console.warn(i.login + " -> /sm-" + hash);
71
71
  }
72
72
  }
73
73
  }
@@ -16,7 +16,7 @@ export interface ILogItem {
16
16
  source: string;
17
17
  }
18
18
  export interface ILogServiceOptions {
19
- output?: (data?: any) => void;
19
+ output?: (data: ILogItem) => void;
20
20
  stackSize: number;
21
21
  level: LOG_LEVEL;
22
22
  textLimit: number;
@@ -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.source + " > " + logitem.level + " > " + logitem.date + " > " + text);
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,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 > 14) {
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 > 14) {
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
- for (let minutes of stat) {
169
- result.push({
170
- m: minutes[0],
171
- ae: this.mapToKeyValue(minutes[1].apiErrors),
172
- e: this.mapToKeyValue(minutes[1].errors),
173
- r: this.mapToKeyValue(minutes[1].requests),
174
- fe: this.mapToKeyValue(minutes[1].fatalErrors),
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
- uptime: process.uptime,
196
- osUptime: os_1.default.uptime,
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
- export { APIService, Initializer, LocalRequest, MysqlService, Validator, LogService };
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; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.5.9",
3
+ "version": "1.6.0",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",