Skip to content

Commit

Permalink
refactor: 修改 /api/stats/center 返回值格式
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Nov 2, 2024
1 parent 14c5957 commit bdbf263
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/routes/ApiStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,36 @@ export class ApiStats {
inst.app.get("/api/stats/center", (req, res) => {
res.setHeader('Content-Type', 'application/json');
const data = inst.server.centerStats.getLast30DaysHourlyStats();
const daily = data.map(d => {
let hits = 0;
let bytes = 0;
d.filter(h => h !== null).forEach(h => {
hits += h.hits;
bytes += h.bytes;
});
return { hits, bytes };
});

const dailyHits: number[] = [];
const dailyBytes: number[] = [];

data.forEach(d => {
const { hits, bytes } = d.filter(h => h !== null).reduce((acc, h) => {
acc.hits += h.hits;
acc.bytes += h.bytes;
return acc;
}, { hits: 0, bytes: 0 });

dailyHits.push(hits);
dailyBytes.push(bytes);
});

const hourlyData = data.at(0) || [];
const hourlyHits = hourlyData.map(d => d?.hits || 0);
const hourlyBytes = hourlyData.map(d => d?.bytes || 0);

res.status(200).json({
daily,
hourly: data.at(0)?.map(hour => ([hour.hits, hour.bytes])) || [],
daily: [dailyHits, dailyBytes],
hourly: [hourlyHits, hourlyBytes],
rejected: RateLimiter.rejectedRequest.getLast30DaysHourlyStats().at(0)?.map(hour => hour.hits) || [],
today: inst.server.centerStats.today(),
onlines: inst.clusters.filter(c => c.isOnline).length,
sources: inst.server.sources.length,
totalFiles: inst.files.length,
totalSize: inst.files.reduce((acc, f) => acc + f.size, 0),
startTime: inst.server.startAt.getTime()
});
});
});

inst.app.get("/api/stats/source", (req, res) => {
Expand Down

0 comments on commit bdbf263

Please sign in to comment.