Skip to content

Commit 1eb63f0

Browse files
author
Kevin
committed
feat: add Statistics component and integrate with state management; update App component to include Statistics
1 parent b87801d commit 1eb63f0

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Stage } from "./components/Stage";
88
import { Logs } from "Logs";
99
import { Selection } from "leva-controls/Selection";
1010
import { Examples } from "leva-controls/Examples";
11+
import { Statistics } from "leva-controls/Statistics";
1112

1213
function App() {
1314
return (
@@ -18,6 +19,7 @@ function App() {
1819
<Inputs />
1920
<Playback />
2021
<Selection />
22+
<Statistics />
2123
<Logs />
2224
</Suspense>
2325
<div style={{ height: "100vh", width: "100vw" }}>
@@ -29,7 +31,7 @@ function App() {
2931
folderTitleHeight: "32px",
3032
numberInputMinWidth: "90px",
3133
rowHeight: "32px",
32-
rootWidth: "400px",
34+
rootWidth: "420px",
3335
controlWidth: "240px",
3436
},
3537
fonts: {

src/client/run.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import { useRef } from "react";
1717
import { Output } from "smart";
1818
import { id } from "utils";
19-
import { appendAtom, clearAtom, logAtom, State } from "./state";
19+
import { appendAtom, clearAtom, logAtom, State, statsAtom } from "./state";
2020

2121
// ─── Input State ─────────────────────────────────────────────────────────────
2222

@@ -73,6 +73,7 @@ export function useRun() {
7373
const { data: contents } = useSolutionContents();
7474
const append = useSetAtom(appendAtom);
7575
const clear = useSetAtom(clearAtom);
76+
const setStats = useSetAtom(statsAtom);
7677
const setLog = useSetAtom(logAtom);
7778
const abort = useRef<() => void | null>();
7879
const setBuffering = useSetAtom(bufferingAtom);
@@ -115,7 +116,6 @@ export function useRun() {
115116
const progress: State["progress"] = {};
116117
let adg: State["adg"] = undefined;
117118
let prev: State["step"] | undefined = undefined;
118-
let stats: State["stats"] = undefined;
119119

120120
// ─────────────────────────────────────
121121

@@ -167,7 +167,7 @@ export function useRun() {
167167
progress[d.agent] = pick(d, "finished", "total");
168168
break;
169169
case "stats":
170-
stats = omit(d, "type");
170+
setStats(omit(d, "type"));
171171
break;
172172
case "error":
173173
console.error(d.error);
@@ -190,7 +190,6 @@ export function useRun() {
190190
adg,
191191
agentState: structuredClone(agentState),
192192
progress: structuredClone(progress),
193-
stats: structuredClone(stats),
194193
});
195194
}
196195
s.unsubscribe();

src/client/state.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ export type State = {
3232
adg?: AdgProgress;
3333
agentState?: Record<number, StateChange["value"]>;
3434
progress?: Record<number, Pick<ExecProgress, "finished" | "total">>;
35-
stats?: Omit<Stats, "type">;
3635
};
3736

37+
export const statsAtom = atom<Omit<Stats, "type">>();
38+
export const useStats = () => useAtom(statsAtom);
39+
3840
export const logAtom = atom<string[]>([]);
3941
export const useLog = () => useAtom(logAtom);
4042

@@ -241,6 +243,7 @@ export const clearAtom = atom<null, never[], unknown>(null, (_get, set) => {
241243
set(selectionAtom, new Set());
242244
set(lengthAtom, 0);
243245
set(timespanAtom, 0);
246+
set(statsAtom, undefined);
244247
await clear();
245248
await set(setCacheAtom);
246249
}, 0);

src/leva-controls/Selection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useControls } from "leva";
33
import { jsx } from "leva-plugins/jsx";
44
import { thru } from "lodash";
55
import { useEffect } from "react";
6-
import { Status } from "Status";
6+
import { Status } from "components/Status";
77

88
export function Selection() {
99
const [selection] = useSelection();

src/leva-controls/Statistics.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { useSolutionContents } from "client/run";
2+
import { useLength, useStats } from "client/state";
3+
import { useControls } from "leva";
4+
import { jsx } from "leva-plugins/jsx";
5+
import { Components } from "leva/plugin";
6+
import { round, sum, values } from "lodash";
7+
8+
function Statistics1() {
9+
const [stats] = useStats();
10+
const length = useLength();
11+
return stats ? (
12+
<div>
13+
<Components.Row input>
14+
<Components.Label>Sum of costs</Components.Label>
15+
<Components.Label>{stats.mapf_plan_cost}</Components.Label>
16+
</Components.Row>
17+
<Components.Row input>
18+
<Components.Label>Makespan</Components.Label>
19+
<Components.Label>{round(length / 10, 1)}s</Components.Label>
20+
</Components.Row>
21+
<Components.Row input>
22+
<Components.Label>Average time</Components.Label>
23+
<Components.Label>
24+
{round(
25+
sum(values(stats.agent_exec_cost)) /
26+
values(stats.agent_exec_cost).length /
27+
10,
28+
1
29+
)}
30+
s
31+
</Components.Label>
32+
</Components.Row>
33+
</div>
34+
) : (
35+
"You'll see trends here once simulation ends."
36+
);
37+
}
38+
39+
export function Statistics() {
40+
useControls("Statistics", () => ({
41+
selection: jsx({
42+
value: <Statistics1 />,
43+
}),
44+
}));
45+
return null;
46+
}

0 commit comments

Comments
 (0)