Skip to content

Commit

Permalink
feat: 仅显示特定天数内有活动的节点
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Nov 10, 2024
1 parent 3de76bb commit dfb319e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class Config {
public readonly failAttemptsDuration: number = env.get('FAIL_ATTEMPTS_DURATION').default(0).asIntPositive();
public readonly requestRateLimit: number = env.get('REQUEST_RATE_LIMIT').default(0).asIntPositive();
public readonly autoUpdateDuration: number = env.get('AUTO_UPDATE_DURATION').default(0).asIntPositive();
public readonly lastActivityDays: number = env.get('LAST_ACTIVITY_DAYS').default(15).asIntPositive();

// 开发变量
public readonly sourceIpHeader: string = env.get('SOURCE_IP_HEADER').default("x-real-ip").asString();
Expand Down
5 changes: 5 additions & 0 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ export class Server {
socket.onAny((event: string, data) => {
if (this.sessionToClusterMap.has(socket.id)) {
console.log(`SOCKET [${this.sessionToClusterMap.get(socket.id)?.clusterId}] <${event?.toUpperCase() || 'UNKNOWN'}> - [${getRealIP(socket.handshake.headers) || socket.handshake.address}] <${socket.handshake.headers['user-agent'] || 'null'}> ${`<WITH ${Object.keys(data || []).length || 'NO'} PARAMS>`}`);
const cluster = this.sessionToClusterMap.get(socket.id);
if (cluster) {
cluster.lastSeen = Date.now();
this.db.update(cluster);
}
}
});

Expand Down
3 changes: 3 additions & 0 deletions src/database/Cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FileList } from '../FileList.js';
sponsorBanner TEXT,
version TEXT,
downTime INTEGER,
lastSeen INTEGER,
shards INTEGER,
isProxy INTEGER,
isMasterStats INTEGER
Expand Down Expand Up @@ -71,6 +72,8 @@ export class ClusterEntity {

public downTime: number = 0;

public lastSeen: number = 0;

public shards: number = 1000;

@Ignore()
Expand Down
9 changes: 7 additions & 2 deletions src/routes/ApiClusters.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Config } from "../Config.js";
import { ClusterEntity } from "../database/Cluster.js";
import { UserEntity } from "../database/User.js";
import { StatsStorage } from "../statistics/ClusterStats.js";
Expand All @@ -7,9 +8,13 @@ import { ApiFactory } from "./ApiFactory.js";
export class ApiClusters {
public static register(inst: ApiFactory) {
inst.app.get("/api/clusters", async (req, res) => {
// 仅显示特定天数内有活动的节点
// 先把节点按照在线和离线分成两部分,然后各自按照 traffic 从大到小排序,最后返回 JSON 字符串
const onlineClusters = inst.clusters.filter(c => c.isOnline);
const offlineClusters = inst.clusters.filter(c => !c.isOnline);
const clusters = Config.instance.lastActivityDays > 0
? inst.clusters.filter(c => c.lastSeen > Utilities.getDate(-Config.instance.lastActivityDays, "day").getTime())
: inst.clusters;
const onlineClusters = clusters.filter(c => c.isOnline);
const offlineClusters = clusters.filter(c => !c.isOnline);

const onlineClustersSorted = onlineClusters
.sort((a, b) => {
Expand Down

0 comments on commit dfb319e

Please sign in to comment.