badmfck-api-server 1.5.6 → 1.5.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,7 +36,7 @@ var LOG_LEVEL;
36
36
  })(LOG_LEVEL || (exports.LOG_LEVEL = LOG_LEVEL = {}));
37
37
  const getDefaultLogOptions = () => {
38
38
  return {
39
- stackSize: 1000,
39
+ stackSize: 100,
40
40
  textLimit: 1024,
41
41
  level: LOG_LEVEL.ALL
42
42
  };
@@ -157,9 +157,9 @@ class LogService extends BaseService_1.BaseService {
157
157
  this.options.output(logitem.source + " > " + logitem.level + " > " + logitem.date + " > " + text);
158
158
  exports.S_LOG_CREATED.invoke(logitem);
159
159
  this.log.push(logitem);
160
- let limit = this.options.stackSize ?? 1000;
161
- if (limit > 10000)
162
- limit = 10000;
160
+ let limit = this.options.stackSize ?? 100;
161
+ if (limit > 1000)
162
+ limit = 1000;
163
163
  if (this.log.length > limit)
164
164
  this.log.shift();
165
165
  });
@@ -1,23 +1,30 @@
1
1
  import { BaseEndpoint } from "../BaseEndpoint";
2
2
  import { HTTPRequestVO, TransferPacketVO } from "../structures/Interfaces";
3
- export declare const indexFilePath: string;
4
3
  interface IStatObject {
5
4
  requests: Map<string, number>;
6
5
  errors: Map<string, number>;
7
6
  fatalErrors: Map<string, number>;
8
7
  apiErrors: Map<string, number>;
9
8
  }
9
+ interface ISystemStat {
10
+ cpuUsage: number;
11
+ memoryTotal: number;
12
+ memoryUsage: any;
13
+ }
10
14
  export declare class Monitor extends BaseEndpoint {
11
15
  ignoreHttpLogging: boolean;
12
16
  private startedAt;
13
17
  private httpRequests;
18
+ private systemStat;
14
19
  constructor();
20
+ addSystemStat(): void;
15
21
  registrateError(endpoint: string): void;
16
22
  registrateFatalError(endpoint: string): void;
17
23
  registrateAPIError(endpoint: string): void;
18
24
  registrateResponse(endpoint: string, responseTime: number): void;
19
25
  increaseStat(statObject: Map<string, number>, endpoint: string): void;
20
26
  createStatObj(): IStatObject;
27
+ createSystemStatObj(): ISystemStat;
21
28
  getDateIndex(d: Date): number;
22
29
  getHourMinuteIndex(d: Date): string;
23
30
  leadZero(i: number): string;
@@ -25,6 +32,7 @@ export declare class Monitor extends BaseEndpoint {
25
32
  logs(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
26
33
  netlog(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
27
34
  metrics(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
35
+ serverStat(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
28
36
  mapToKeyValue(map: Map<string, number>): {
29
37
  name: string;
30
38
  value: number;
@@ -3,24 +3,34 @@ 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 = exports.indexFilePath = void 0;
6
+ exports.Monitor = void 0;
7
7
  const APIService_1 = require("../APIService");
8
8
  const BaseEndpoint_1 = require("../BaseEndpoint");
9
9
  const LogService_1 = require("../LogService");
10
10
  const crypto_1 = __importDefault(require("crypto"));
11
- const path_1 = __importDefault(require("path"));
12
- exports.indexFilePath = path_1.default.resolve(__dirname, "index.html");
11
+ const os_1 = __importDefault(require("os"));
13
12
  class Monitor extends BaseEndpoint_1.BaseEndpoint {
14
13
  ignoreHttpLogging = true;
15
- startedAt = new Date();
14
+ startedAt = +new Date();
16
15
  httpRequests = new Map();
16
+ systemStat = new Map();
17
17
  constructor() {
18
18
  super("sys-monitor");
19
19
  this.registerEndpoints([
20
20
  { ignoreInterceptor: true, endpoint: "log", handler: this.logs },
21
21
  { ignoreInterceptor: true, endpoint: "netlog", handler: this.netlog },
22
- { ignoreInterceptor: true, endpoint: "metrics", handler: this.metrics }
22
+ { ignoreInterceptor: true, endpoint: "metrics", handler: this.metrics },
23
+ { ignoreInterceptor: true, endpoint: "server/stat", handler: this.serverStat }
23
24
  ]);
25
+ setInterval(() => {
26
+ this.addSystemStat();
27
+ }, 1000 * 60 * 10);
28
+ this.addSystemStat();
29
+ }
30
+ addSystemStat() {
31
+ const so = this.createSystemStatObj();
32
+ so.memoryUsage = process.memoryUsage();
33
+ so.memoryTotal = os_1.default.totalmem();
24
34
  }
25
35
  registrateError(endpoint) {
26
36
  const so = this.createStatObj();
@@ -68,6 +78,33 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
68
78
  fatalErrors: new Map(),
69
79
  apiErrors: new Map()
70
80
  };
81
+ day.set(hourMinute, hm);
82
+ }
83
+ return hm;
84
+ }
85
+ createSystemStatObj() {
86
+ const d = new Date();
87
+ const dtm = this.getDateIndex(d);
88
+ let day = this.systemStat.get(dtm);
89
+ if (!day) {
90
+ day = new Map();
91
+ this.systemStat.set(dtm, day);
92
+ if (this.systemStat.size > 14) {
93
+ for (let i of this.systemStat) {
94
+ this.systemStat.delete(i[0]);
95
+ break;
96
+ }
97
+ }
98
+ }
99
+ const hourMinute = this.getHourMinuteIndex(d);
100
+ let hm = day.get(hourMinute);
101
+ if (!hm) {
102
+ hm = {
103
+ cpuUsage: 0,
104
+ memoryTotal: 0,
105
+ memoryUsage: {}
106
+ };
107
+ day.set(hourMinute, hm);
71
108
  }
72
109
  return hm;
73
110
  }
@@ -90,12 +127,13 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
90
127
  if (!req.headers['authorization'])
91
128
  return { error: { code: 10002, message: "No authorization found", httpStatus: 400 } };
92
129
  const auth = req.headers['authorization'];
93
- if (!Array.isArray(req.precheck)) {
130
+ if (!req.precheck || !Array.isArray(req.precheck.data)) {
94
131
  return { error: { code: 10003, message: "No authorization records found", httpStatus: 400 } };
95
132
  }
96
133
  let authorized = false;
97
- for (let i of req.precheck) {
134
+ for (let i of req.precheck.data) {
98
135
  let expectationStr = "";
136
+ expectationStr += req.endpoint;
99
137
  expectationStr += JSON.stringify(i);
100
138
  expectationStr += JSON.stringify(req.method);
101
139
  expectationStr += JSON.stringify(req.data);
@@ -138,6 +176,15 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
138
176
  }
139
177
  return { data: { stat: result, date: +date } };
140
178
  }
179
+ async serverStat(req) {
180
+ return { data: {
181
+ memory: {
182
+ cpus: os_1.default.cpus(),
183
+ uptime: process.uptime,
184
+ osUptime: os_1.default.uptime
185
+ }
186
+ } };
187
+ }
141
188
  mapToKeyValue(map) {
142
189
  const res = [];
143
190
  for (let i of map)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.5.6",
3
+ "version": "1.5.8",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",