badmfck-api-server 1.5.7 → 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
  });
@@ -6,17 +6,25 @@ interface IStatObject {
6
6
  fatalErrors: Map<string, number>;
7
7
  apiErrors: Map<string, number>;
8
8
  }
9
+ interface ISystemStat {
10
+ cpuUsage: number;
11
+ memoryTotal: number;
12
+ memoryUsage: any;
13
+ }
9
14
  export declare class Monitor extends BaseEndpoint {
10
15
  ignoreHttpLogging: boolean;
11
16
  private startedAt;
12
17
  private httpRequests;
18
+ private systemStat;
13
19
  constructor();
20
+ addSystemStat(): void;
14
21
  registrateError(endpoint: string): void;
15
22
  registrateFatalError(endpoint: string): void;
16
23
  registrateAPIError(endpoint: string): void;
17
24
  registrateResponse(endpoint: string, responseTime: number): void;
18
25
  increaseStat(statObject: Map<string, number>, endpoint: string): void;
19
26
  createStatObj(): IStatObject;
27
+ createSystemStatObj(): ISystemStat;
20
28
  getDateIndex(d: Date): number;
21
29
  getHourMinuteIndex(d: Date): string;
22
30
  leadZero(i: number): string;
@@ -24,6 +32,7 @@ export declare class Monitor extends BaseEndpoint {
24
32
  logs(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
25
33
  netlog(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
26
34
  metrics(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
35
+ serverStat(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
27
36
  mapToKeyValue(map: Map<string, number>): {
28
37
  name: string;
29
38
  value: number;
@@ -8,17 +8,29 @@ 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 os_1 = __importDefault(require("os"));
11
12
  class Monitor extends BaseEndpoint_1.BaseEndpoint {
12
13
  ignoreHttpLogging = true;
13
- startedAt = new Date();
14
+ startedAt = +new Date();
14
15
  httpRequests = new Map();
16
+ systemStat = new Map();
15
17
  constructor() {
16
18
  super("sys-monitor");
17
19
  this.registerEndpoints([
18
20
  { ignoreInterceptor: true, endpoint: "log", handler: this.logs },
19
21
  { ignoreInterceptor: true, endpoint: "netlog", handler: this.netlog },
20
- { ignoreInterceptor: true, endpoint: "metrics", handler: this.metrics }
22
+ { ignoreInterceptor: true, endpoint: "metrics", handler: this.metrics },
23
+ { ignoreInterceptor: true, endpoint: "server/stat", handler: this.serverStat }
21
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();
22
34
  }
23
35
  registrateError(endpoint) {
24
36
  const so = this.createStatObj();
@@ -66,6 +78,33 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
66
78
  fatalErrors: new Map(),
67
79
  apiErrors: new Map()
68
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);
69
108
  }
70
109
  return hm;
71
110
  }
@@ -94,6 +133,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
94
133
  let authorized = false;
95
134
  for (let i of req.precheck.data) {
96
135
  let expectationStr = "";
136
+ expectationStr += req.endpoint;
97
137
  expectationStr += JSON.stringify(i);
98
138
  expectationStr += JSON.stringify(req.method);
99
139
  expectationStr += JSON.stringify(req.data);
@@ -136,6 +176,15 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
136
176
  }
137
177
  return { data: { stat: result, date: +date } };
138
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
+ }
139
188
  mapToKeyValue(map) {
140
189
  const res = [];
141
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.7",
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",