Skip to content

Commit f4e87fa

Browse files
Ravjot BrarRavjot Brar
authored andcommitted
refactor index.js
1 parent a90ef71 commit f4e87fa

3 files changed

Lines changed: 109 additions & 100 deletions

File tree

apps/server/src/index.ts

Lines changed: 3 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { WebSocket, WebSocketServer } from "ws"
2-
import { Decoder, GlideClient, GlideClusterClient } from "@valkey/valkey-glide"
2+
import { GlideClient, GlideClusterClient } from "@valkey/valkey-glide"
33
import { VALKEY } from "../../../common/src/constants.ts"
44
import {
55
getKeys,
@@ -9,7 +9,8 @@ import {
99
updateKey
1010
} from "./keys-browser.ts"
1111
import { connectToValkey } from "./connection.ts"
12-
import { parseInfo, parseClusterInfo } from "./utils.ts"
12+
import { setDashboardData, setClusterDashboardData } from "./setDashboardData.ts"
13+
import { sendValkeyRunCommand } from "./sendCommand.ts"
1314

1415
const wss = new WebSocketServer({ port: 8080 })
1516

@@ -181,101 +182,3 @@ wss.on("connection", (ws: WebSocket) => {
181182
console.log("Client disconnected. Reason:", code, reason.toString())
182183
})
183184
})
184-
185-
async function setDashboardData(
186-
connectionId: string,
187-
client: GlideClient,
188-
ws: WebSocket,
189-
) {
190-
const rawInfo = await client.info()
191-
const info = parseInfo(rawInfo)
192-
const rawMemoryStats = (await client.customCommand(["MEMORY", "STATS"], {
193-
decoder: Decoder.String,
194-
})) as Array<{
195-
key: string;
196-
value: string;
197-
}>
198-
199-
const memoryStats = rawMemoryStats.reduce((acc, { key, value }) => {
200-
acc[key] = value
201-
return acc
202-
}, {} as Record<string, string>)
203-
204-
ws.send(
205-
JSON.stringify({
206-
type: VALKEY.STATS.setData,
207-
payload: {
208-
connectionId,
209-
info: info,
210-
memory: memoryStats,
211-
},
212-
}),
213-
)
214-
}
215-
216-
async function setClusterDashboardData(
217-
clusterId: string,
218-
client: GlideClusterClient,
219-
ws: WebSocket,
220-
) {
221-
const rawInfo = await client.info({ route:"allNodes" })
222-
const info = parseClusterInfo(rawInfo)
223-
224-
ws.send(
225-
JSON.stringify({
226-
type: VALKEY.CLUSTER.setClusterData,
227-
payload: {
228-
clusterId,
229-
info: info,
230-
},
231-
}),
232-
)
233-
}
234-
235-
async function sendValkeyRunCommand(
236-
client: GlideClient | GlideClusterClient,
237-
ws: WebSocket,
238-
payload: { command: string; connectionId: string },
239-
) {
240-
try {
241-
let response = (await client.customCommand(
242-
payload.command.split(" "),
243-
))
244-
245-
if (typeof response === "string") {
246-
if (response.includes("ResponseError")) {
247-
ws.send(
248-
JSON.stringify({
249-
meta: { command: payload.command },
250-
type: VALKEY.COMMAND.sendFailed,
251-
payload: response,
252-
}),
253-
)
254-
}
255-
response = parseInfo(response)
256-
}
257-
258-
ws.send(
259-
JSON.stringify({
260-
meta: {
261-
connectionId: payload.connectionId,
262-
command: payload.command,
263-
},
264-
type: VALKEY.COMMAND.sendFulfilled,
265-
payload: response,
266-
}),
267-
)
268-
} catch (err) {
269-
ws.send(
270-
JSON.stringify({
271-
meta: {
272-
connectionId: payload.connectionId,
273-
command: payload.command,
274-
},
275-
type: VALKEY.COMMAND.sendFailed,
276-
payload: err,
277-
}),
278-
)
279-
console.log("Error sending command to Valkey", err)
280-
}
281-
}

apps/server/src/sendCommand.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { GlideClient, GlideClusterClient } from "@valkey/valkey-glide"
2+
import { VALKEY } from "common/src/constants"
3+
import WebSocket from "ws"
4+
import { parseInfo } from "./utils"
5+
6+
export async function sendValkeyRunCommand(
7+
client: GlideClient | GlideClusterClient,
8+
ws: WebSocket,
9+
payload: { command: string; connectionId: string },
10+
) {
11+
try {
12+
let response = (await client.customCommand(
13+
payload.command.split(" "),
14+
))
15+
16+
if (typeof response === "string") {
17+
if (response.includes("ResponseError")) {
18+
ws.send(
19+
JSON.stringify({
20+
meta: { command: payload.command },
21+
type: VALKEY.COMMAND.sendFailed,
22+
payload: response,
23+
}),
24+
)
25+
}
26+
response = parseInfo(response)
27+
}
28+
29+
ws.send(
30+
JSON.stringify({
31+
meta: {
32+
connectionId: payload.connectionId,
33+
command: payload.command,
34+
},
35+
type: VALKEY.COMMAND.sendFulfilled,
36+
payload: response,
37+
}),
38+
)
39+
} catch (err) {
40+
ws.send(
41+
JSON.stringify({
42+
meta: {
43+
connectionId: payload.connectionId,
44+
command: payload.command,
45+
},
46+
type: VALKEY.COMMAND.sendFailed,
47+
payload: err,
48+
}),
49+
)
50+
console.log("Error sending command to Valkey", err)
51+
}
52+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { GlideClient, GlideClusterClient, Decoder } from "@valkey/valkey-glide"
2+
import { VALKEY } from "common/src/constants"
3+
import WebSocket from "ws"
4+
import { parseClusterInfo, parseInfo } from "./utils"
5+
6+
export async function setDashboardData(
7+
connectionId: string,
8+
client: GlideClient,
9+
ws: WebSocket,
10+
) {
11+
const rawInfo = await client.info()
12+
const info = parseInfo(rawInfo)
13+
const rawMemoryStats = (await client.customCommand(["MEMORY", "STATS"], {
14+
decoder: Decoder.String,
15+
})) as Array<{
16+
key: string;
17+
value: string;
18+
}>
19+
20+
const memoryStats = rawMemoryStats.reduce((acc, { key, value }) => {
21+
acc[key] = value
22+
return acc
23+
}, {} as Record<string, string>)
24+
25+
ws.send(
26+
JSON.stringify({
27+
type: VALKEY.STATS.setData,
28+
payload: {
29+
connectionId,
30+
info: info,
31+
memory: memoryStats,
32+
},
33+
}),
34+
)
35+
}
36+
37+
export async function setClusterDashboardData(
38+
clusterId: string,
39+
client: GlideClusterClient,
40+
ws: WebSocket,
41+
) {
42+
const rawInfo = await client.info({ route:"allNodes" })
43+
const info = parseClusterInfo(rawInfo)
44+
45+
ws.send(
46+
JSON.stringify({
47+
type: VALKEY.CLUSTER.setClusterData,
48+
payload: {
49+
clusterId,
50+
info: info,
51+
},
52+
}),
53+
)
54+
}

0 commit comments

Comments
 (0)