badmfck-api-server 2.8.7 → 2.8.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -89,7 +89,7 @@ async function Initializer(services) {
89
89
  exports.Initializer = Initializer;
90
90
  class APIService extends BaseService_1.BaseService {
91
91
  static nextLogID = 0;
92
- version = "2.8.7";
92
+ version = "2.8.9";
93
93
  options;
94
94
  monitor = null;
95
95
  monitorIndexFile;
@@ -108,6 +108,7 @@ class APIService extends BaseService_1.BaseService {
108
108
  if (this.options.monitor && this.options.monitor.length > 0) {
109
109
  exports.REQ_MONITOR_USERS.listener = async () => this.options.monitor ?? [];
110
110
  this.monitor = new MonitorService_1.MonitorService();
111
+ this.monitor.init();
111
112
  this.options.endpoints.push(new Monitor_1.Monitor());
112
113
  }
113
114
  this.options.endpoints.push(new ExternalServiceEndpoint_1.ExternalServiceEndpoint());
@@ -238,7 +239,7 @@ class APIService extends BaseService_1.BaseService {
238
239
  files: req.files ?? null,
239
240
  referer: req.get("Referer"),
240
241
  };
241
- if (this.options.preproducer) {
242
+ if (this.options.preproducer && ep !== "--sys-monitor") {
242
243
  try {
243
244
  const preproducerResult = await this.options.preproducer(httpRequest);
244
245
  if (preproducerResult)
@@ -5,20 +5,29 @@ export interface IEPStat {
5
5
  success: number;
6
6
  fail: number;
7
7
  }
8
+ export interface ICustomEvent {
9
+ event: string;
10
+ data?: any;
11
+ project?: string;
12
+ }
8
13
  export interface IEPStatReqFilter {
9
- range: "minute" | "hour" | "day";
10
- from: string;
11
- to: string;
12
- ep: string;
14
+ range?: "minute" | "hour" | "day";
15
+ from?: string;
16
+ to?: string;
17
+ ep?: string;
18
+ project?: string;
19
+ event?: string;
13
20
  }
14
21
  export interface IEPStatResult {
15
22
  }
16
23
  export declare const S_STAT_REGISTRATE_REQUEST: Signal<TransferPacketVO<any>>;
24
+ export declare const S_STAT_REGISTRATE_CUSTOM_EVENT: Signal<ICustomEvent>;
17
25
  export declare const S_STAT_REGISTRATE_SERVICE: Signal<string>;
18
26
  export declare const REQ_EP_STAT: Req<IEPStatReqFilter, IEPStatResult>;
19
27
  export declare class MonitorService extends BaseService {
20
- endpoints: Map<number, Map<string, IEPStat>>;
28
+ projects: Map<string, Map<number, Map<string, IEPStat>>>;
21
29
  constructor();
30
+ init(): Promise<void>;
22
31
  getEPStat(req: IEPStatReqFilter): Promise<IEPStatResult>;
23
32
  onStatRegistrate(data: TransferPacketVO): void;
24
33
  pad(num: number): string;
@@ -23,24 +23,34 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.MonitorService = exports.REQ_EP_STAT = exports.S_STAT_REGISTRATE_SERVICE = exports.S_STAT_REGISTRATE_REQUEST = void 0;
26
+ exports.MonitorService = exports.REQ_EP_STAT = exports.S_STAT_REGISTRATE_SERVICE = exports.S_STAT_REGISTRATE_CUSTOM_EVENT = exports.S_STAT_REGISTRATE_REQUEST = void 0;
27
27
  const badmfck_signal_1 = __importStar(require("badmfck-signal"));
28
28
  const BaseService_1 = require("./BaseService");
29
29
  exports.S_STAT_REGISTRATE_REQUEST = new badmfck_signal_1.default();
30
+ exports.S_STAT_REGISTRATE_CUSTOM_EVENT = new badmfck_signal_1.default();
30
31
  exports.S_STAT_REGISTRATE_SERVICE = new badmfck_signal_1.default();
31
32
  exports.REQ_EP_STAT = new badmfck_signal_1.Req(undefined, "REQ_EP_STAT");
32
33
  class MonitorService extends BaseService_1.BaseService {
33
- endpoints = new Map();
34
+ projects = new Map();
34
35
  constructor() {
35
36
  super("MonitorService");
36
- exports.S_STAT_REGISTRATE_REQUEST.subscribe(this.onStatRegistrate);
37
+ exports.S_STAT_REGISTRATE_REQUEST.subscribe(req => this.onStatRegistrate(req));
37
38
  exports.REQ_EP_STAT.listener = async (req) => this.getEPStat(req);
38
39
  }
40
+ async init() {
41
+ super.init();
42
+ }
39
43
  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
+ const result = [];
45
+ for (let [project, projectSlot] of this.projects) {
46
+ if (req.project && req.project !== project)
47
+ continue;
48
+ let p = result.find(p => p.project === project);
49
+ if (!p) {
50
+ p = { project, dates: [] };
51
+ result.push(p);
52
+ }
53
+ for (let [minute, minuteSlot] of projectSlot) {
44
54
  let range = minute + "";
45
55
  if (req.range === "hour")
46
56
  range = this.pad(this.getDate(minute).getHours());
@@ -54,42 +64,43 @@ class MonitorService extends BaseService_1.BaseService {
54
64
  if (range > req.to)
55
65
  continue;
56
66
  }
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);
67
+ let dateSlot = p.dates.find(p => p.date === range);
68
+ if (!dateSlot) {
69
+ dateSlot = {
70
+ date: range,
71
+ endpoints: []
72
+ };
73
+ p.dates.push(dateSlot);
66
74
  }
67
- for (let [ep, stat] of minuteSlot) {
68
- epSlot.success += stat.success;
69
- epSlot.fail += stat.fail;
75
+ for (let [epName, epStatSlot] of minuteSlot) {
76
+ let epSlot = dateSlot.endpoints.find(p => p.ep === epName);
77
+ if (!epSlot) {
78
+ epSlot = {
79
+ ep: epName,
80
+ success: 0,
81
+ fail: 0
82
+ };
83
+ dateSlot.endpoints.push(epSlot);
84
+ }
85
+ epSlot.fail += epStatSlot.fail;
86
+ epSlot.success += epStatSlot.success;
70
87
  }
71
88
  }
72
89
  }
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 };
90
+ return { filter: req, result: result };
86
91
  }
87
92
  onStatRegistrate(data) {
88
93
  const minute = this.getMinutes(new Date());
89
- let minuteSlot = this.endpoints.get(minute);
94
+ const project = data.project ?? "unknown";
95
+ let projectSlot = this.projects.get(project);
96
+ if (!projectSlot) {
97
+ projectSlot = new Map();
98
+ this.projects.set(project, projectSlot);
99
+ }
100
+ let minuteSlot = projectSlot.get(minute);
90
101
  if (!minuteSlot) {
91
102
  minuteSlot = new Map();
92
- this.endpoints.set(minute, minuteSlot);
103
+ projectSlot.set(minute, minuteSlot);
93
104
  }
94
105
  if (!data.endpoint)
95
106
  data.endpoint = "unknown";
@@ -104,10 +115,10 @@ class MonitorService extends BaseService_1.BaseService {
104
115
  else {
105
116
  epSlot.success++;
106
117
  }
107
- if (this.endpoints.size > 3000) {
108
- const firstItem = this.endpoints.keys().next().value;
118
+ if (projectSlot.size > 3000) {
119
+ const firstItem = projectSlot.keys().next().value;
109
120
  if (firstItem)
110
- this.endpoints.delete(firstItem);
121
+ projectSlot.delete(firstItem);
111
122
  }
112
123
  }
113
124
  pad(num) {
@@ -16,6 +16,7 @@ export interface TransferPacketVO<T = any> {
16
16
  };
17
17
  file?: string;
18
18
  blockResponse?: boolean;
19
+ project?: string;
19
20
  }
20
21
  export interface HTTPRequestVO<T = any> {
21
22
  raw: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.8.7",
3
+ "version": "2.8.9",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",