Skip to content

Commit c03466f

Browse files
committed
Change loki connection management
1 parent da814ce commit c03466f

File tree

5 files changed

+88
-26
lines changed

5 files changed

+88
-26
lines changed

ui/src/components/Sim/hooks/useLokiWebSocket.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ITransactionReceived,
1111
} from "@/components/Sim/types";
1212
import { useRef } from "react";
13+
import { EConnectionState } from "@/contexts/SimContext/types";
1314

1415
// FIXME: latency in topology is wrong
1516

@@ -387,11 +388,18 @@ function connectLokiWebSocket(lokiHost: string, dispatch: any): () => void {
387388
'{service="cardano-node"} |~ "BlockFetchServer|MsgBlock|CompletedBlockFetch|MsgLeiosBlock|MsgLeiosBlockTxs"';
388389
const wsUrl = `ws://${lokiHost}/loki/api/v1/tail?query=${encodeURIComponent(query)}&limit=5000`;
389390
console.log("Connecting to Loki:", wsUrl);
391+
dispatch({
392+
type: "SET_LOKI_CONNECTION_STATE",
393+
payload: EConnectionState.Connecting,
394+
});
390395

391396
const ws = new WebSocket(wsUrl);
392397

393398
ws.onopen = () => {
394-
dispatch({ type: "SET_LOKI_CONNECTED", payload: true });
399+
dispatch({
400+
type: "SET_LOKI_CONNECTION_STATE",
401+
payload: EConnectionState.Connected,
402+
});
395403
};
396404

397405
let count = 0;
@@ -438,25 +446,32 @@ function connectLokiWebSocket(lokiHost: string, dispatch: any): () => void {
438446

439447
ws.onerror = (error) => {
440448
console.error("WebSocket error:", error);
441-
dispatch({ type: "SET_LOKI_CONNECTED", payload: false });
449+
dispatch({
450+
type: "SET_LOKI_CONNECTION_STATE",
451+
payload: EConnectionState.NotConnected,
452+
});
442453
};
443454

444455
ws.onclose = () => {
445-
dispatch({ type: "SET_LOKI_CONNECTED", payload: false });
456+
dispatch({
457+
type: "SET_LOKI_CONNECTION_STATE",
458+
payload: EConnectionState.NotConnected,
459+
});
446460
};
447461

448462
return () => ws.close();
449463
}
450464

451465
export const useLokiWebSocket = () => {
452466
const {
453-
state: { lokiHost, lokiConnected },
467+
state: { lokiHost, lokiConnectionState },
454468
dispatch,
455469
} = useSimContext();
470+
456471
const cleanupRef = useRef<(() => void) | null>(null);
457472

458473
const connect = () => {
459-
if (!lokiHost || lokiConnected) return;
474+
if (!lokiHost || lokiConnectionState === EConnectionState.Connected) return;
460475

461476
dispatch({ type: "RESET_TIMELINE" });
462477

@@ -466,7 +481,10 @@ export const useLokiWebSocket = () => {
466481
const disconnect = () => {
467482
cleanupRef.current?.();
468483
cleanupRef.current = null;
469-
dispatch({ type: "SET_LOKI_CONNECTED", payload: false });
484+
dispatch({
485+
type: "SET_LOKI_CONNECTION_STATE",
486+
payload: EConnectionState.NotConnected,
487+
});
470488
};
471489

472490
return { connect, disconnect };

ui/src/components/Sim/modules/Scenario.tsx

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@ import {
22
useSimContext,
33
defaultAggregatedData,
44
} from "@/contexts/SimContext/context";
5-
import { IScenario } from "@/contexts/SimContext/types";
5+
import { EConnectionState, IScenario } from "@/contexts/SimContext/types";
66
import { ChangeEvent, FC, useCallback, useEffect, useState } from "react";
77
import { useStreamMessagesHandler } from "../hooks/useStreamMessagesHandler";
88
import { useLokiWebSocket } from "../hooks/useLokiWebSocket";
99
import { Button } from "@/components/Button";
1010

1111
export const Scenario: FC = () => {
1212
const {
13-
state: { allScenarios, activeScenario, events, lokiHost, lokiConnected, autoStart },
13+
state: {
14+
allScenarios,
15+
activeScenario,
16+
events,
17+
lokiHost,
18+
lokiConnectionState,
19+
autoStart,
20+
},
1421
dispatch,
1522
} = useSimContext();
1623
const { startStream, streaming, stopStream } = useStreamMessagesHandler();
17-
const { connect: connectLoki, disconnect: disconnectLoki, connecting: lokiConnecting } = useLokiWebSocket();
24+
const { connect: connectLoki, disconnect: disconnectLoki } =
25+
useLokiWebSocket();
1826
const [includeTransactions, setIncludeTransactions] = useState(true);
1927

2028
const isLokiMode = !!lokiHost;
@@ -29,7 +37,9 @@ export const Scenario: FC = () => {
2937
scenario.topology,
3038
window.location.toString(),
3139
).toString(),
32-
trace: scenario.trace ? new URL(scenario.trace, window.location.toString()).toString() : undefined,
40+
trace: scenario.trace
41+
? new URL(scenario.trace, window.location.toString()).toString()
42+
: undefined,
3343
}));
3444
dispatch({ type: "SET_SCENARIOS", payload: scenarios });
3545

@@ -40,7 +50,11 @@ export const Scenario: FC = () => {
4050
const scenarioIndex = parseInt(scenarioParam, 10);
4151
if (scenarioIndex >= 0 && scenarioIndex < scenarios.length) {
4252
const targetScenario = scenarios[scenarioIndex];
43-
dispatch({ type: "SET_SCENARIO", payload: targetScenario.name, autoStart: true });
53+
dispatch({
54+
type: "SET_SCENARIO",
55+
payload: targetScenario.name,
56+
autoStart: true,
57+
});
4458
}
4559
}
4660
})();
@@ -55,9 +69,20 @@ export const Scenario: FC = () => {
5569
startStream(true); // Include transactions by default for auto-load
5670
}
5771
// Reset autoStart flag after triggering
58-
dispatch({ type: "SET_SCENARIO", payload: activeScenario, autoStart: false });
72+
dispatch({
73+
type: "SET_SCENARIO",
74+
payload: activeScenario,
75+
autoStart: false,
76+
});
5977
}
60-
}, [autoStart, activeScenario, isLokiMode, connectLoki, startStream, dispatch]);
78+
}, [
79+
autoStart,
80+
activeScenario,
81+
isLokiMode,
82+
connectLoki,
83+
startStream,
84+
dispatch,
85+
]);
6186

6287
const chooseScenario = useCallback(
6388
(event: ChangeEvent<HTMLSelectElement>) => {
@@ -89,8 +114,9 @@ export const Scenario: FC = () => {
89114
}
90115
}, [isLokiMode, connectLoki, startStream, includeTransactions]);
91116

92-
const isLoaded = events.length > 0 || streaming || lokiConnected;
93-
const isConnecting = streaming || lokiConnecting;
117+
const isConnected = lokiConnectionState == EConnectionState.Connected;
118+
const isConnecting = lokiConnectionState == EConnectionState.Connecting;
119+
const isLoaded = events.length > 0 || streaming || isConnected;
94120

95121
return (
96122
<div className="flex items-center justify-start gap-4 border-2 rounded-md p-4 border-gray-200 bg-white/80 backdrop-blur-xs">
@@ -130,10 +156,17 @@ export const Scenario: FC = () => {
130156
onClick={handleStartStream}
131157
disabled={isConnecting || isLoaded}
132158
>
133-
{isLokiMode
134-
? (lokiConnecting ? "Connecting..." : lokiConnected ? "Connected" : "Connect")
135-
: (streaming ? "Loading..." : isLoaded ? "Loaded" : "Load Scenario")
136-
}
159+
{isLokiMode
160+
? isConnecting
161+
? "Connecting..."
162+
: isConnected
163+
? "Connected"
164+
: "Connect"
165+
: streaming
166+
? "Loading..."
167+
: isLoaded
168+
? "Loaded"
169+
: "Load"}
137170
</Button>
138171
{isLoaded && (
139172
<Button

ui/src/contexts/SimContext/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ISimContext,
55
ISimContextState,
66
ISimulationAggregatedDataState,
7+
EConnectionState,
78
} from "./types";
89

910
export const defaultAggregatedData: ISimulationAggregatedDataState = {
@@ -34,7 +35,7 @@ export const defaultState: ISimContextState = {
3435
aggregatedData: defaultAggregatedData,
3536
tracePath: "",
3637
lokiHost: undefined,
37-
lokiConnected: false,
38+
lokiConnectionState: EConnectionState.NotConnected,
3839
topography: { links: new Map(), nodes: new Map() },
3940
topologyPath: "",
4041
topologyLoaded: false,

ui/src/contexts/SimContext/reducer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { defaultAggregatedData } from "./context";
2-
import { ISimContextState, TSimContextActions } from "./types";
2+
import {
3+
ISimContextState,
4+
TSimContextActions,
5+
EConnectionState,
6+
} from "./types";
37
import {
48
computeAggregatedDataAtTime,
59
clearLatencyCache,
@@ -37,7 +41,7 @@ export const reducer = (
3741
autoStart: action.autoStart || false,
3842
tracePath: scenario.trace || "",
3943
lokiHost: scenario.loki,
40-
lokiConnected: false,
44+
lokiConnectionState: EConnectionState.NotConnected,
4145
topologyPath: scenario.topology,
4246
topologyLoaded:
4347
state.topologyLoaded && scenario.topology === state.topologyPath,
@@ -199,10 +203,10 @@ export const reducer = (
199203
speedMultiplier: 1,
200204
};
201205

202-
case "SET_LOKI_CONNECTED":
206+
case "SET_LOKI_CONNECTION_STATE":
203207
return {
204208
...state,
205-
lokiConnected: action.payload,
209+
lokiConnectionState: action.payload,
206210
};
207211

208212
default:

ui/src/contexts/SimContext/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ export interface IGraphContextState {
6363
currentNode?: string;
6464
}
6565

66+
export enum EConnectionState {
67+
NotConnected = "NotConnected",
68+
Connecting = "Connecting",
69+
Connected = "Connected",
70+
}
71+
6672
export interface IScenario {
6773
name: string;
6874
topology: string;
@@ -79,7 +85,7 @@ export interface ISimContextState {
7985
aggregatedData: ISimulationAggregatedDataState;
8086
tracePath: string;
8187
lokiHost?: string;
82-
lokiConnected: boolean;
88+
lokiConnectionState: EConnectionState;
8389
topography: ITransformedNodeMap;
8490
topologyPath: string;
8591
topologyLoaded: boolean;
@@ -114,7 +120,7 @@ export type TSimContextActions =
114120
| { type: "SET_TIMELINE_PLAYING"; payload: boolean }
115121
| { type: "SET_TIMELINE_SPEED"; payload: number }
116122
| { type: "RESET_TIMELINE" }
117-
| { type: "SET_LOKI_CONNECTED"; payload: boolean };
123+
| { type: "SET_LOKI_CONNECTION_STATE"; payload: EConnectionState };
118124

119125
export interface ISimContext {
120126
state: ISimContextState;

0 commit comments

Comments
 (0)