File size: 5,577 Bytes
6d9f36a 1dbfa1e 6d9f36a 1dbfa1e 6d9f36a 1dbfa1e 6d9f36a 1dbfa1e 6d9f36a 1dbfa1e 6d9f36a 1dbfa1e 6d9f36a 1dbfa1e 6d9f36a 1dbfa1e 6d9f36a 1dbfa1e 6d9f36a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
interface EndpointStats {
totalRequests: number;
successRequests: number;
failedRequests: number;
lastAccessed: number;
}
interface VisitorData {
timestamp: number;
count: number;
}
interface IPFailureTracking {
count: number;
resetTime: number;
}
interface GlobalStats {
totalRequests: number;
totalSuccess: number;
totalFailed: number;
uniqueVisitors: Set<string>;
endpoints: Map<string, EndpointStats>;
startTime: number;
visitorsByHour: Map<number, Set<string>>;
}
class StatsTracker {
private stats: GlobalStats;
private ipFailures: Map<string, IPFailureTracking>;
private readonly MAX_FAILS_PER_IP = 1;
private readonly FAIL_WINDOW_MS = 12 * 60 * 60 * 1000;
constructor() {
this.stats = {
totalRequests: 0,
totalSuccess: 0,
totalFailed: 0,
uniqueVisitors: new Set(),
endpoints: new Map(),
startTime: Date.now(),
visitorsByHour: new Map(),
};
this.ipFailures = new Map();
setInterval(() => {
const now = Date.now();
this.ipFailures.forEach((tracking, ip) => {
if (now > tracking.resetTime) {
this.ipFailures.delete(ip);
}
});
}, 5 * 60 * 1000);
}
trackRequest(endpoint: string, statusCode: number, clientIp: string): boolean {
const now = Date.now();
const isFailed = statusCode >= 400;
if (isFailed) {
const ipTracking = this.ipFailures.get(clientIp);
if (!ipTracking) {
this.ipFailures.set(clientIp, {
count: 1,
resetTime: now + this.FAIL_WINDOW_MS,
});
} else {
if (now > ipTracking.resetTime) {
ipTracking.count = 1;
ipTracking.resetTime = now + this.FAIL_WINDOW_MS;
} else {
if (ipTracking.count >= this.MAX_FAILS_PER_IP) {
return false;
}
ipTracking.count++;
}
}
} else {
const ipTracking = this.ipFailures.get(clientIp);
if (ipTracking && ipTracking.count > 0) {
ipTracking.count--;
}
}
this.stats.totalRequests++;
this.stats.uniqueVisitors.add(clientIp);
const currentHour = Math.floor(now / (1000 * 60 * 60));
if (!this.stats.visitorsByHour.has(currentHour)) {
this.stats.visitorsByHour.set(currentHour, new Set());
}
this.stats.visitorsByHour.get(currentHour)!.add(clientIp);
const cutoffHour = currentHour - 24;
Array.from(this.stats.visitorsByHour.keys()).forEach(hour => {
if (hour < cutoffHour) {
this.stats.visitorsByHour.delete(hour);
}
});
if (statusCode >= 200 && statusCode < 400) {
this.stats.totalSuccess++;
} else {
this.stats.totalFailed++;
}
if (!this.stats.endpoints.has(endpoint)) {
this.stats.endpoints.set(endpoint, {
totalRequests: 0,
successRequests: 0,
failedRequests: 0,
lastAccessed: now,
});
}
const endpointStats = this.stats.endpoints.get(endpoint)!;
endpointStats.totalRequests++;
endpointStats.lastAccessed = now;
if (statusCode >= 200 && statusCode < 400) {
endpointStats.successRequests++;
} else {
endpointStats.failedRequests++;
}
return true;
}
getGlobalStats() {
const uptime = Date.now() - this.stats.startTime;
const uptimeHours = Math.floor(uptime / (1000 * 60 * 60));
const uptimeDays = Math.floor(uptimeHours / 24);
return {
totalRequests: this.stats.totalRequests,
totalSuccess: this.stats.totalSuccess,
totalFailed: this.stats.totalFailed,
uniqueVisitors: this.stats.uniqueVisitors.size,
successRate: this.stats.totalRequests > 0
? ((this.stats.totalSuccess / this.stats.totalRequests) * 100).toFixed(2)
: "0.00",
uptime: {
ms: uptime,
hours: uptimeHours,
days: uptimeDays,
formatted: uptimeDays > 0
? `${uptimeDays}d ${uptimeHours % 24}h`
: `${uptimeHours}h`,
},
};
}
getVisitorChartData(): VisitorData[] {
const currentHour = Math.floor(Date.now() / (1000 * 60 * 60));
const data: VisitorData[] = [];
for (let i = 23; i >= 0; i--) {
const hour = currentHour - i;
const visitors = this.stats.visitorsByHour.get(hour);
const timestamp = hour * 1000 * 60 * 60;
data.push({
timestamp,
count: visitors ? visitors.size : 0,
});
}
return data;
}
getEndpointStats(endpoint: string) {
return this.stats.endpoints.get(endpoint) || null;
}
getAllEndpointStats() {
const result: Record<string, EndpointStats> = {};
this.stats.endpoints.forEach((stats, endpoint) => {
result[endpoint] = stats;
});
return result;
}
getTopEndpoints(limit: number = 10) {
return Array.from(this.stats.endpoints.entries())
.map(([endpoint, stats]) => ({ endpoint, ...stats }))
.sort((a, b) => b.totalRequests - a.totalRequests)
.slice(0, limit);
}
reset() {
this.stats = {
totalRequests: 0,
totalSuccess: 0,
totalFailed: 0,
uniqueVisitors: new Set(),
endpoints: new Map(),
startTime: Date.now(),
visitorsByHour: new Map(),
};
this.ipFailures.clear();
}
}
let statsTracker: StatsTracker;
export function initStatsTracker() {
statsTracker = new StatsTracker();
return statsTracker;
}
export function getStatsTracker() {
if (!statsTracker) {
throw new Error("StatsTracker not initialized. Call initStatsTracker() first.");
}
return statsTracker;
} |