forked from akash-network/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientSocketStats.ts
61 lines (52 loc) · 1.39 KB
/
clientSocketStats.ts
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
export class ClientWebSocketStats {
private id: string;
private openedOn: Date;
private closedOn?: Date;
private usage: WebSocketUsage = "Unknown";
private usageStats: {
[key in WebSocketUsage]: { count: number; data: number };
} = {
StreamLogs: { count: 0, data: 0 },
StreamEvents: { count: 0, data: 0 },
Shell: { count: 0, data: 0 },
DownloadLogs: { count: 0, data: 0 },
Unknown: { count: 0, data: 0 }
};
constructor(id: string) {
this.id = id;
this.openedOn = new Date();
}
setUsage(usage: WebSocketUsage) {
this.usage = usage;
if (usage !== "Unknown") {
this.usageStats[usage].count += 1;
}
}
logDataTransfer(dataTransferred: number) {
this.usageStats[this.usage].data += dataTransferred;
}
close() {
this.closedOn = new Date();
}
isClosed() {
return !!this.closedOn;
}
getStats() {
return {
id: this.id,
openedOn: this.openedOn,
closedOn: this.closedOn,
usageStats: this.usageStats,
totalStats: (Object.keys(this.usageStats) as WebSocketUsage[]).reduce(
(s, n) => {
return {
count: s.count + this.usageStats[n].count,
data: s.data + this.usageStats[n].data
};
},
{ count: 0, data: 0 }
)
};
}
}
export type WebSocketUsage = "StreamLogs" | "StreamEvents" | "Shell" | "DownloadLogs" | "Unknown";