OhMyDitzzy commited on
Commit
499175b
·
1 Parent(s): e84b48e

fix: success rate

Browse files
Files changed (1) hide show
  1. src/server/lib/stats-tracker.ts +35 -28
src/server/lib/stats-tracker.ts CHANGED
@@ -97,8 +97,8 @@ class StatsTracker {
97
  this.stats.totalFailed = data.totalFailed || 0;
98
  this.stats.uniqueVisitors = new Set(data.uniqueVisitors || []);
99
  this.stats.startTime = data.startTime || Date.now();
 
100
  this.stats.endpoints = new Map();
101
-
102
  if (data.endpoints) {
103
  Object.entries(data.endpoints).forEach(([endpoint, stats]) => {
104
  this.stats.endpoints.set(endpoint, stats);
@@ -120,7 +120,7 @@ class StatsTracker {
120
 
121
  private async saveStats(): Promise<void> {
122
  if (!this.redis) {
123
- return;
124
  }
125
 
126
  try {
@@ -144,8 +144,7 @@ class StatsTracker {
144
 
145
  await this.redis.set(this.REDIS_KEY, serialized);
146
 
147
- // We can use this for auto clean up
148
- // However, this will be considered later.
149
  // await this.redis.expire(this.REDIS_KEY, 90 * 24 * 60 * 60);
150
  } catch (error) {
151
  console.error('Error saving stats to Redis:', error);
@@ -156,7 +155,7 @@ class StatsTracker {
156
  if (this.saveTimeout) {
157
  clearTimeout(this.saveTimeout);
158
  }
159
-
160
  this.saveTimeout = setTimeout(() => {
161
  this.saveStats();
162
  }, 5000);
@@ -193,7 +192,19 @@ class StatsTracker {
193
  }
194
  }
195
 
196
- this.stats.totalRequests++;
 
 
 
 
 
 
 
 
 
 
 
 
197
  this.stats.uniqueVisitors.add(clientIp);
198
 
199
  const dateKey = new Date(now).toISOString().split('T')[0];
@@ -202,31 +213,27 @@ class StatsTracker {
202
  }
203
  this.stats.visitorsByDay.get(dateKey)!.add(clientIp);
204
 
205
- if (statusCode >= 200 && statusCode < 400) {
206
- this.stats.totalSuccess++;
207
- } else if (statusCode >= 500) {
208
- this.stats.totalFailed++;
209
- }
210
-
211
- if (!this.stats.endpoints.has(endpoint)) {
212
- this.stats.endpoints.set(endpoint, {
213
- totalRequests: 0,
214
- successRequests: 0,
215
- failedRequests: 0,
216
- lastAccessed: now,
217
- });
218
- }
219
 
220
- const endpointStats = this.stats.endpoints.get(endpoint)!;
221
- endpointStats.totalRequests++;
222
- endpointStats.lastAccessed = now;
223
 
224
- if (statusCode >= 200 && statusCode < 400) {
225
- endpointStats.successRequests++;
226
- } else if (statusCode >= 500) {
227
- endpointStats.failedRequests++;
 
228
  }
229
-
230
  this.scheduleSave();
231
 
232
  return true;
 
97
  this.stats.totalFailed = data.totalFailed || 0;
98
  this.stats.uniqueVisitors = new Set(data.uniqueVisitors || []);
99
  this.stats.startTime = data.startTime || Date.now();
100
+
101
  this.stats.endpoints = new Map();
 
102
  if (data.endpoints) {
103
  Object.entries(data.endpoints).forEach(([endpoint, stats]) => {
104
  this.stats.endpoints.set(endpoint, stats);
 
120
 
121
  private async saveStats(): Promise<void> {
122
  if (!this.redis) {
123
+ return;
124
  }
125
 
126
  try {
 
144
 
145
  await this.redis.set(this.REDIS_KEY, serialized);
146
 
147
+ // use this if we need auto cleanup
 
148
  // await this.redis.expire(this.REDIS_KEY, 90 * 24 * 60 * 60);
149
  } catch (error) {
150
  console.error('Error saving stats to Redis:', error);
 
155
  if (this.saveTimeout) {
156
  clearTimeout(this.saveTimeout);
157
  }
158
+
159
  this.saveTimeout = setTimeout(() => {
160
  this.saveStats();
161
  }, 5000);
 
192
  }
193
  }
194
 
195
+ const isSuccess = statusCode >= 200 && statusCode < 400;
196
+ const isServerError = statusCode >= 500;
197
+
198
+ if (isSuccess || isServerError) {
199
+ this.stats.totalRequests++;
200
+
201
+ if (isSuccess) {
202
+ this.stats.totalSuccess++;
203
+ } else if (isServerError) {
204
+ this.stats.totalFailed++;
205
+ }
206
+ }
207
+
208
  this.stats.uniqueVisitors.add(clientIp);
209
 
210
  const dateKey = new Date(now).toISOString().split('T')[0];
 
213
  }
214
  this.stats.visitorsByDay.get(dateKey)!.add(clientIp);
215
 
216
+ if (isSuccess || isServerError) {
217
+ if (!this.stats.endpoints.has(endpoint)) {
218
+ this.stats.endpoints.set(endpoint, {
219
+ totalRequests: 0,
220
+ successRequests: 0,
221
+ failedRequests: 0,
222
+ lastAccessed: now,
223
+ });
224
+ }
 
 
 
 
 
225
 
226
+ const endpointStats = this.stats.endpoints.get(endpoint)!;
227
+ endpointStats.totalRequests++;
228
+ endpointStats.lastAccessed = now;
229
 
230
+ if (isSuccess) {
231
+ endpointStats.successRequests++;
232
+ } else if (isServerError) {
233
+ endpointStats.failedRequests++;
234
+ }
235
  }
236
+
237
  this.scheduleSave();
238
 
239
  return true;