-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.tsx
204 lines (185 loc) · 5.06 KB
/
stats.tsx
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
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { ComponentChildren } from "preact";
import { renderToString } from "preact-render-to-string";
import type { NetworkStatistics, User } from "mtkruto/mod.ts";
import type { WorkerStats } from "./worker.ts";
import type { ClientStats } from "./client_manager.ts";
const startTime = Date.now();
function NewLine() {
return <>{"\n"}</>;
}
function BlankLine() {
return (
<>
<NewLine />
<NewLine />
</>
);
}
function Bool({ children: value }: { children: boolean }) {
return <>{value ? "Yes" : "No"}</>;
}
function Field(
{ name, children: value }: { name: string; children?: ComponentChildren },
) {
return (
<>
{name}: {value}
<NewLine />
</>
);
}
function Section(
{ title, children }: { title: string; children?: ComponentChildren },
) {
return (
<>
{title}
<NewLine />
<Indent>
{children}
</Indent>
</>
);
}
function User({ children: user }: { children: User }) {
return (
<Section title="User">
<Field name="ID">{user.id}</Field>
{user.username && <Field name="Username">@{user.username}</Field>}
<Field name="Bot">
<Bool>{user.isBot}</Bool>
</Field>
<Field name="First Name">{user.firstName}</Field>
{user.lastName && <Field name="Last Name">{user.lastName}</Field>}
</Section>
);
}
function NetworkStats({ children: stats }: { children: NetworkStatistics }) {
return (
<>
<Section title="Messages">
<Field name="Up">{stats.messages.sent}</Field>
<Field name="Down">{stats.messages.received}</Field>
</Section>
<BlankLine />
<Section title="CDN">
<Field name="Up">{stats.cdn.sent}</Field>
<Field name="Down">{stats.cdn.received}</Field>
</Section>
</>
);
}
function Indent({ children }: { children?: ComponentChildren }) {
const v = renderToString(<>{children}</>);
return (
<>
{v.split("\n").map((v) => ` ${v}`).map((v) => v.trim().length == 0 ? "" : v).join("\n")}
</>
);
}
function ClientStats({ children: v }: { children: ClientStats }) {
return (
<>
<User>{v.me}</User>
<BlankLine />
<NetworkStats>{v.network}</NetworkStats>
</>
);
}
export function WorkerStats({ children: v }: { children: WorkerStats }) {
return (
<>
<Field name="Clients">{v.clientCount}</Field>
<BlankLine />
{v.clients
.map((v) => (
<Indent>
<ClientStats>{v}</ClientStats>
</Indent>
))
.map((v, i) => (
<>
<Section title={`Client ${i + 1}`}>{v}</Section>
<BlankLine />
</>
))}
</>
);
}
function MemoryStats() {
const memoryInfo = Deno.systemMemoryInfo();
const total = Math.ceil(memoryInfo.total / 1024 / 1024);
const free = Math.ceil(memoryInfo.available / 1024);
const used = total - free;
const thisProcess = Math.ceil(Deno.memoryUsage().rss / 1024 / 1024);
const uptime = `${(Date.now() - startTime) / 1_000 / 60 / 60}h`;
const totalMemory = `${total} MB`;
const totalMemoryUsed = `${used} MB`;
const memoryUsed = `${thisProcess} MB (${Math.round(thisProcess / total * 100)}% of total, ${Math.round(thisProcess / used * 100)}% of used)`;
return (
<>
<Field name="Uptime">{uptime}</Field>
<Field name="Total Memory">{totalMemory}</Field>
<Field name="Total Memory Used">{totalMemoryUsed}</Field>
<Field name="Memory Used">{memoryUsed}</Field>
</>
);
}
function Hr() {
return <>{"-".repeat(50)}</>;
}
function Stats({ children: workers }: { children: WorkerStats[] }) {
return (
<>
<MemoryStats />
<Field name="Workers">{workers.length}</Field>
<BlankLine />
{workers
.map((v, i) => (
<Section title={`Worker ${i + 1}`}>
<WorkerStats>{v}</WorkerStats>
</Section>
))
.map((v) => (
<>
<BlankLine />
{v}
<BlankLine />
<Hr />
</>
))}
<BlankLine />
SPECIAL PATHS
<NewLine />
<Indent>
/write-logs{"\n"}
/clear-cache
</Indent>
<BlankLine />
<BlankLine />
<BlankLine />
</>
);
}
export function displayStats(workers: WorkerStats[]) {
return renderToString(<Stats>{workers}</Stats>);
}