badmfck-api-server 2.8.5 → 2.8.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -80,6 +80,7 @@ async function Initializer(services) {
80
80
  for (let i of services) {
81
81
  await i.init();
82
82
  activeServices.push(i.getName());
83
+ MonitorService_1.S_STAT_REGISTRATE_SERVICE.invoke(i.getName());
83
84
  }
84
85
  for (let i of services) {
85
86
  i.applicationReady();
@@ -88,7 +89,7 @@ async function Initializer(services) {
88
89
  exports.Initializer = Initializer;
89
90
  class APIService extends BaseService_1.BaseService {
90
91
  static nextLogID = 0;
91
- version = "2.8.5";
92
+ version = "2.8.7";
92
93
  options;
93
94
  monitor = null;
94
95
  monitorIndexFile;
@@ -388,6 +389,7 @@ class APIService extends BaseService_1.BaseService {
388
389
  log.time = data.responseTime;
389
390
  log.referer = ref;
390
391
  }
392
+ MonitorService_1.S_STAT_REGISTRATE_REQUEST.invoke(data);
391
393
  if (res.destroyed || res.closed) {
392
394
  if (log)
393
395
  log.error = "Connection already closed, can't send response for: " + data.endpoint;
@@ -1,4 +1,28 @@
1
+ import Signal, { Req } from "badmfck-signal";
1
2
  import { BaseService } from "./BaseService";
3
+ import { TransferPacketVO } from "./structures/Interfaces";
4
+ export interface IEPStat {
5
+ success: number;
6
+ fail: number;
7
+ }
8
+ export interface IEPStatReqFilter {
9
+ range: "minute" | "hour" | "day";
10
+ from: string;
11
+ to: string;
12
+ ep: string;
13
+ }
14
+ export interface IEPStatResult {
15
+ }
16
+ export declare const S_STAT_REGISTRATE_REQUEST: Signal<TransferPacketVO<any>>;
17
+ export declare const S_STAT_REGISTRATE_SERVICE: Signal<string>;
18
+ export declare const REQ_EP_STAT: Req<IEPStatReqFilter, IEPStatResult>;
2
19
  export declare class MonitorService extends BaseService {
20
+ endpoints: Map<number, Map<string, IEPStat>>;
3
21
  constructor();
22
+ getEPStat(req: IEPStatReqFilter): Promise<IEPStatResult>;
23
+ onStatRegistrate(data: TransferPacketVO): void;
24
+ pad(num: number): string;
25
+ getMinutes(date: Date): number;
26
+ getDate(minutes: number): Date;
27
+ getMinutesInMonth(date: Date): number;
4
28
  }
@@ -1,10 +1,138 @@
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
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MonitorService = void 0;
26
+ exports.MonitorService = exports.REQ_EP_STAT = exports.S_STAT_REGISTRATE_SERVICE = exports.S_STAT_REGISTRATE_REQUEST = void 0;
27
+ const badmfck_signal_1 = __importStar(require("badmfck-signal"));
4
28
  const BaseService_1 = require("./BaseService");
29
+ exports.S_STAT_REGISTRATE_REQUEST = new badmfck_signal_1.default();
30
+ exports.S_STAT_REGISTRATE_SERVICE = new badmfck_signal_1.default();
31
+ exports.REQ_EP_STAT = new badmfck_signal_1.Req(undefined, "REQ_EP_STAT");
5
32
  class MonitorService extends BaseService_1.BaseService {
33
+ endpoints = new Map();
6
34
  constructor() {
7
35
  super("MonitorService");
36
+ exports.S_STAT_REGISTRATE_REQUEST.subscribe(this.onStatRegistrate);
37
+ exports.REQ_EP_STAT.listener = async (req) => this.getEPStat(req);
38
+ }
39
+ async getEPStat(req) {
40
+ const result = new Map();
41
+ if (req.range === "hour") {
42
+ for (let [minute, minuteSlot] of this.endpoints) {
43
+ const date = this.getDate(minute);
44
+ let range = minute + "";
45
+ if (req.range === "hour")
46
+ range = this.pad(this.getDate(minute).getHours());
47
+ else if (req.range === "day")
48
+ range = this.pad(this.getDate(minute).getDate());
49
+ if (req.from) {
50
+ if (range < req.from)
51
+ continue;
52
+ }
53
+ if (req.to) {
54
+ if (range > req.to)
55
+ continue;
56
+ }
57
+ let resultSlot = result.get(range);
58
+ if (!resultSlot) {
59
+ resultSlot = new Map();
60
+ result.set(range, resultSlot);
61
+ }
62
+ let epSlot = resultSlot.get(req.ep);
63
+ if (!epSlot) {
64
+ epSlot = { success: 0, fail: 0 };
65
+ resultSlot.set(req.ep, epSlot);
66
+ }
67
+ for (let [ep, stat] of minuteSlot) {
68
+ epSlot.success += stat.success;
69
+ epSlot.fail += stat.fail;
70
+ }
71
+ }
72
+ }
73
+ const endResult = [];
74
+ for (let [date, obj] of result) {
75
+ const stat = {
76
+ date,
77
+ data: []
78
+ };
79
+ for (let [ep, s] of obj) {
80
+ const r = { ep, ...s };
81
+ stat.data.push(r);
82
+ }
83
+ endResult.push(stat);
84
+ }
85
+ return { filter: req, result: endResult, overallcount: this.endpoints.size };
86
+ }
87
+ onStatRegistrate(data) {
88
+ const minute = this.getMinutes(new Date());
89
+ let minuteSlot = this.endpoints.get(minute);
90
+ if (!minuteSlot) {
91
+ minuteSlot = new Map();
92
+ this.endpoints.set(minute, minuteSlot);
93
+ }
94
+ if (!data.endpoint)
95
+ data.endpoint = "unknown";
96
+ let epSlot = minuteSlot.get(data.endpoint);
97
+ if (!epSlot) {
98
+ epSlot = { success: 0, fail: 0 };
99
+ minuteSlot.set(data.endpoint, epSlot);
100
+ }
101
+ if (data.error) {
102
+ epSlot.fail++;
103
+ }
104
+ else {
105
+ epSlot.success++;
106
+ }
107
+ if (this.endpoints.size > 3000) {
108
+ const firstItem = this.endpoints.keys().next().value;
109
+ if (firstItem)
110
+ this.endpoints.delete(firstItem);
111
+ }
112
+ }
113
+ pad(num) {
114
+ return num.toString().padStart(2, '0');
115
+ }
116
+ getMinutes(date) {
117
+ const year = date.getFullYear();
118
+ const month = this.pad(date.getMonth() + 1);
119
+ const day = this.pad(date.getDate());
120
+ const hours = this.pad(date.getHours());
121
+ const minutes = this.pad(date.getMinutes());
122
+ return parseInt(`${year}${month}${day}${hours}${minutes}`);
123
+ }
124
+ getDate(minutes) {
125
+ const datetime = minutes.toString();
126
+ const year = parseInt(datetime.slice(0, 4));
127
+ const month = parseInt(datetime.slice(4, 6)) - 1;
128
+ const day = parseInt(datetime.slice(6, 8));
129
+ const hours = parseInt(datetime.slice(8, 10));
130
+ const min = parseInt(datetime.slice(10, 12));
131
+ return new Date(year, month, day, hours, min);
132
+ }
133
+ getMinutesInMonth(date) {
134
+ const daysInMonth = date.getDate();
135
+ return daysInMonth * 1440;
8
136
  }
9
137
  }
10
138
  exports.MonitorService = MonitorService;
@@ -15,6 +15,7 @@ export declare class Monitor extends BaseEndpoint {
15
15
  ignoreHttpLogging: boolean;
16
16
  users: Map<string, IStatObject>;
17
17
  constructor();
18
+ controllers(req: HTTPRequestVO): Promise<TransferPacketVO>;
18
19
  logs(req: HTTPRequestVO): Promise<TransferPacketVO>;
19
20
  netlog(req: HTTPRequestVO): Promise<TransferPacketVO>;
20
21
  metrics(req: HTTPRequestVO): Promise<TransferPacketVO>;
@@ -11,6 +11,7 @@ const LogService_1 = require("../LogService");
11
11
  const crypto_1 = __importDefault(require("crypto"));
12
12
  const os_1 = __importDefault(require("os"));
13
13
  const DefaultErrors_1 = __importDefault(require("../structures/DefaultErrors"));
14
+ const MonitorService_1 = require("../MonitorService");
14
15
  exports.S_MONITOR_REGISTRATE_ACTION = new badmfck_signal_1.Signal();
15
16
  class Monitor extends BaseEndpoint_1.BaseEndpoint {
16
17
  ignoreHttpLogging = true;
@@ -21,9 +22,14 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
21
22
  { ignoreInterceptor: true, endpoint: "log", handler: this.logs },
22
23
  { ignoreInterceptor: true, endpoint: "netlog", handler: this.netlog },
23
24
  { ignoreInterceptor: true, endpoint: "metrics", handler: this.metrics },
24
- { ignoreInterceptor: true, endpoint: "server", handler: this.serverStat }
25
+ { ignoreInterceptor: true, endpoint: "server", handler: this.serverStat },
26
+ { ignoreInterceptor: true, endpoint: "controllers", handler: this.controllers }
25
27
  ]);
26
28
  }
29
+ async controllers(req) {
30
+ this.checkAuthentication(req);
31
+ return {};
32
+ }
27
33
  async logs(req) {
28
34
  this.checkAuthentication(req);
29
35
  const services = await LogService_1.REQ_LOG_UNIQUE_SERVICES.request();
@@ -39,11 +45,13 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
39
45
  }
40
46
  async netlog(req) {
41
47
  this.checkAuthentication(req);
42
- return {};
48
+ const log = await APIService_1.REQ_HTTP_LOG.request(req.data);
49
+ return { data: log };
43
50
  }
44
51
  async metrics(req) {
45
52
  this.checkAuthentication(req);
46
- return {};
53
+ const result = await MonitorService_1.REQ_EP_STAT.request(req.data);
54
+ return { data: result };
47
55
  }
48
56
  async serverStat(req) {
49
57
  this.checkAuthentication(req);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.8.5",
3
+ "version": "2.8.7",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",