Skip to content

Commit

Permalink
refactor: 重构获取昨日数据部分
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Dec 20, 2024
1 parent 5478afa commit ac35eb2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,10 @@ export class Utilities {
}

public static getCurrentDate(): string {
return Utilities.getDateDate(new Date());
return Utilities.getDateString(new Date());
}

public static getDateDate(date: Date): string {
public static getDateString(date: Date): string {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
Expand Down
9 changes: 5 additions & 4 deletions src/routes/ApiStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ export class ApiStats {
});

inst.app.get("/api/stats/yesterday", (req, res) => {
const yesterday = inst.server.centerStats.getYesterday();
res.json({
hits: inst.server.centerStats.getLast30DaysHourlyStats().at(-2)?.map(d => d.hits || 0),
bytes : inst.server.centerStats.getLast30DaysHourlyStats().at(-2)?.map(d => d.bytes || 0),
hits: yesterday.hits,
bytes : yesterday.bytes,
total: {
hits: inst.server.centerStats.getLast30DaysHourlyStats().at(2)?.reduce((acc, d) => acc + d.hits, 0),
bytes: inst.server.centerStats.getLast30DaysHourlyStats().at(2)?.reduce((acc, d) => acc + d.bytes, 0)
hits: yesterday.hits.reduce((acc, d) => acc + d, 0),
bytes: yesterday.bytes.reduce((acc, d) => acc + d, 0)
},
rejected: RateLimiter.rejectedRequest.getLast30DaysHourlyStats().at(-2),
rank: inst.stats.sort((a, b) => ((b.getLast30DaysStats().at(-2)?.bytes || 0) - (a.getLast30DaysStats().at(-2)?.bytes || 0))).filter(s => (s.getLast30DaysStats().at(-2)?.bytes || 0) > 0).map((s, index) => {
Expand Down
12 changes: 11 additions & 1 deletion src/statistics/ClusterStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,24 @@ export class StatsStorage {
}
}

public getYesterday(): { hits: number, bytes: number } {
const yesterday = Utilities.getDateString(new Date(Date.now() - 24 * 60 * 60 * 1000));
const yesterdayData = this.data.find(entry => entry.date === yesterday);
if (yesterdayData) {
return { hits: yesterdayData.hits, bytes: yesterdayData.bytes };
} else {
return { hits: 0, bytes: 0 };
}
}

public getLast30DaysStats(): { date: string, hits: number, bytes: number }[] {
const now = new Date();

const dateMap: { [key: string]: { hits: number, bytes: number } } = {};

// 填充最近30天的数据
for (let i = 0; i < 30; i++) {
const date = Utilities.getDateDate(new Date(now.getTime() - i * 24 * 60 * 60 * 1000));
const date = Utilities.getDateString(new Date(now.getTime() - i * 24 * 60 * 60 * 1000));
dateMap[date] = { hits: 0, bytes: 0 };
}

Expand Down
19 changes: 17 additions & 2 deletions src/statistics/HourlyStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class HourlyStatsStorage {

public addData({ hits, bytes }: { hits: number, bytes: number }): void {
const now = new Date();
const date = Utilities.getDateDate(now);
const date = Utilities.getDateString(now);
const hour = now.getHours().toString().padStart(2, '0');

let dayData = this.data.find(entry => entry.date === date);
Expand Down Expand Up @@ -63,12 +63,27 @@ export class HourlyStatsStorage {
this.dataUpdated = true;
}

public getYesterday(): { hits: number[], bytes: number[] } {
const yesterday = Utilities.getDateString(new Date(Date.now() - 24 * 60 * 60 * 1000));
let yesterdayData = this.data.find(entry => entry.date === yesterday);
if (!yesterdayData) {
return { hits: Array(24).fill(0), bytes: Array(24).fill(0) };
}
const yesterdayStats = { hits: Array(24).fill(0), bytes: Array(24).fill(0) };
yesterdayData.hourlyStats.forEach(hourData => {
const hour = parseInt(hourData.hour);
yesterdayStats.hits[hour] = hourData.hits;
yesterdayStats.bytes[hour] = hourData.bytes;
});
return yesterdayStats;
}

public getLast30DaysHourlyStats(): { date: string, hits: number, bytes: number }[][] {
const result: { date: string, hits: number, bytes: number }[][] = [];
const now = new Date();

for (let i = 0; i < 30; i++) {
const dateString = Utilities.getDateDate(new Date(now.getTime() - i * 24 * 60 * 60 * 1000));
const dateString = Utilities.getDateString(new Date(now.getTime() - i * 24 * 60 * 60 * 1000));

const dayData = this.data.find(entry => entry.date === dateString);
const dayResult: { date: string, hits: number, bytes: number }[] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/statistics/NumberStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class NumberStorage {
if (this.data.length > 30) this.data.shift();

const now = new Date();
const date = Utilities.getDateDate(now);
const date = Utilities.getDateString(now);
const hour = now.getHours();

let todayData = this.data.find(d => d.date === date);
Expand All @@ -45,7 +45,7 @@ export class NumberStorage {

public getTodayStats(): number[] {
const now = new Date();
const date = Utilities.getDateDate(now);
const date = Utilities.getDateString(now);

const todayData = this.data.find(d => d.date === date);
if (!todayData) return new Array(24).fill(0);
Expand Down

0 comments on commit ac35eb2

Please sign in to comment.