Skip to content

Commit da57e2a

Browse files
Listen to snake info and poll accordingly (#21)
This change picks out the 'snake' info from the SSE event and uses it when polling the /map endpoint.
1 parent cdd6f48 commit da57e2a

File tree

4 files changed

+63
-42
lines changed

4 files changed

+63
-42
lines changed

src/components/RawSpectroscopyData.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ interface MapResponse {
5858
values: (number | null)[][];
5959
}
6060

61-
async function fetchMap(filepath: string, datapath: string, colour: RGBColour) {
62-
const url = `/api/data/map?filepath=${encodeURIComponent(filepath)}&datapath=${encodeURIComponent(datapath)}`;
61+
async function fetchMap(
62+
filepath: string,
63+
datapath: string,
64+
colour: RGBColour,
65+
snake: boolean,
66+
) {
67+
const url = `/api/data/map?filepath=${encodeURIComponent(filepath)}&datapath=${encodeURIComponent(datapath)}&snake=${encodeURIComponent(snake)}`;
6368
const resp = await fetch(url);
6469
if (!resp.ok) throw new Error(resp.statusText);
6570
const mapResponse: MapResponse = await resp.json();

src/components/useSpectroscopyData.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type FetchMapFunction = (
77
filepath: string,
88
datapath: string,
99
colour: RGBColour,
10+
snake: boolean,
1011
) => Promise<NDT>;
1112

1213
export interface DataChannels {
@@ -19,11 +20,13 @@ export interface ScanEventMessage {
1920
status: "running" | "finished" | "failed";
2021
filepath: string;
2122
uuid: string;
23+
snake: boolean;
2224
}
2325

2426
export function useSpectroscopyData(fetchMap: FetchMapFunction) {
2527
const [running, setRunning] = useState<boolean>(false);
2628
const [filepath, setFilepath] = useState<string | null>(null);
29+
const [snake, setSnake] = useState<boolean>(false);
2730
const [data, setData] = useState<DataChannels>({
2831
red: null,
2932
green: null,
@@ -45,6 +48,7 @@ export function useSpectroscopyData(fetchMap: FetchMapFunction) {
4548
if (msg.status === "running") {
4649
setRunning(true);
4750
setFilepath(msg.filepath);
51+
setSnake(msg.snake);
4852
} else if (msg.status === "finished" || msg.status === "failed") {
4953
setRunning(false); // triggers final poll below
5054
}
@@ -71,9 +75,9 @@ export function useSpectroscopyData(fetchMap: FetchMapFunction) {
7175
try {
7276
const basePath = "/entry/instrument/spectroscopy_detector/";
7377
const [red, green, blue] = await Promise.all([
74-
fetchMap(filepath, basePath + "RedTotal", "red"),
75-
fetchMap(filepath, basePath + "GreenTotal", "green"),
76-
fetchMap(filepath, basePath + "BlueTotal", "blue"),
78+
fetchMap(filepath, basePath + "RedTotal", "red", snake),
79+
fetchMap(filepath, basePath + "GreenTotal", "green", snake),
80+
fetchMap(filepath, basePath + "BlueTotal", "blue", snake),
7781
]);
7882
setData({ red, green, blue });
7983
} catch (err) {
@@ -98,7 +102,7 @@ export function useSpectroscopyData(fetchMap: FetchMapFunction) {
98102
pollInterval.current = null;
99103
}
100104
};
101-
}, [running, filepath, fetchMap]);
105+
}, [running, filepath, snake, fetchMap]);
102106

103107
return { data, running };
104108
}

src/mocks/handlers.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,10 @@ import workflowsResponse from "./workflows-response.json";
33
import plansResponse from "./plans-response.json";
44
import instrumentSessionsResponse from "./instrumentSessions-response.json";
55
import type { ScanEventMessage } from "../components/useSpectroscopyData";
6+
import { mapData } from "./mock_data";
67

78
const fakeTaskId = "7304e8e0-81c6-4978-9a9d-9046ab79ce3c";
89

9-
function mapData(): (number | null)[][] {
10-
return [
11-
[
12-
5467227.0, 5467227.0, 5480663.0, 5478486.0, 5477020.0, 5474645.0,
13-
5472603.0, 5470330.0, 5468827.0, 5467346.0,
14-
],
15-
[
16-
5465947.0, 5464940.0, 5483401.0, 5480384.0, 5477378.0, 5474776.0,
17-
5471462.0, 5450634.0, 5454651.0, 5465208.0,
18-
],
19-
[
20-
5463907.0, 5463469.0, 5481975.0, 5479338.0, 5476649.0, 5474993.0,
21-
5473529.0, 5471431.0, 5470030.0, 5468721.0,
22-
],
23-
[
24-
5466907.0, 5466023.0, 5483679.0, 5480875.0, 5478044.0, 5475180.0,
25-
5473196.0, 5471168.0, 5469539.0, 5468190.0,
26-
],
27-
[
28-
5466626.0, 5465494.0, 5483503.0, 5480586.0, 5477659.0, 5475069.0,
29-
5473287.0, 5471380.0, 5469737.0, 5468496.0,
30-
],
31-
[
32-
5466713.0, 5465920.0, 5483587.0, 5480582.0, 5477829.0, 5475202.0,
33-
5473244.0, 5471103.0, 5468010.0, 5468222.0,
34-
],
35-
[5467700.0, null, null, null, null, null, null, null, null, null],
36-
[null, null, null, null, null, null, null, null, null, null],
37-
[null, null, null, null, null, null, null, null, null, null],
38-
[null, null, null, null, null, null, null, null, null, null],
39-
];
40-
}
41-
4210
export const handlers = [
4311
http.post("/api/graphql", request => {
4412
const referrer = request.request.referrer;
@@ -69,8 +37,9 @@ export const handlers = [
6937
const url = new URL(request.url);
7038
const filepath = url.searchParams.get("filepath");
7139
const datapath = url.searchParams.get("datapath");
72-
console.log("Mock /api/data/map called", { filepath, datapath });
73-
const data = mapData();
40+
const snake: boolean = JSON.parse(url.searchParams.get("snake")!);
41+
console.log("Mock /api/data/map called", { filepath, datapath, snake });
42+
const data = mapData(snake);
7443
return HttpResponse.json({ values: data });
7544
}),
7645

@@ -91,20 +60,22 @@ export const handlers = [
9160
uuid: "fake-scan-uuid",
9261
filepath: "/mock/path/fake.nxs",
9362
status: "running",
63+
snake: true,
9464
});
9565

9666
// simulate data collection for ~5 seconds
9767
let counter = 0;
9868
const interval = setInterval(() => {
9969
counter++;
100-
console.log("Mock event tick", counter);
70+
//console.log("Mock event tick", counter);
10171
if (counter >= 25) {
10272
clearInterval(interval);
10373
// Scan stops
10474
send({
10575
uuid: "fake-scan-uuid",
10676
filepath: "/mock/path/fake.nxs",
10777
status: "finished",
78+
snake: true,
10879
});
10980
}
11081
}, 200);

src/mocks/mock_data.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export function mapData(snake: boolean): (number | null)[][] {
2+
const data: (number | null)[][] = [
3+
[
4+
5467227.0, 5467227.0, 5480663.0, 5478486.0, 5477020.0, 5474645.0,
5+
5472603.0, 5470330.0, 5468827.0, 5467346.0,
6+
],
7+
[
8+
5465947.0, 5464940.0, 5483401.0, 5480384.0, 5477378.0, 5474776.0,
9+
5471462.0, 5450634.0, 5454651.0, 5465208.0,
10+
],
11+
[
12+
5463907.0, 5463469.0, 5481975.0, 5479338.0, 5476649.0, 5474993.0,
13+
5473529.0, 5471431.0, 5470030.0, 5468721.0,
14+
],
15+
[
16+
5466907.0, 5466023.0, 5483679.0, 5480875.0, 5478044.0, 5475180.0,
17+
5473196.0, 5471168.0, 5469539.0, 5468190.0,
18+
],
19+
[
20+
5466626.0, 5465494.0, 5483503.0, 5480586.0, 5477659.0, 5475069.0,
21+
5473287.0, 5471380.0, 5469737.0, 5468496.0,
22+
],
23+
[
24+
5466713.0, 5465920.0, 5483587.0, 5480582.0, 5477829.0, 5475202.0,
25+
5473244.0, 5471103.0, 5468010.0, 5468222.0,
26+
],
27+
[5467700.0, null, null, null, null, null, null, null, null, null],
28+
[null, null, null, null, null, null, null, null, null, null],
29+
[null, null, null, null, null, null, null, null, null, null],
30+
[null, null, null, null, null, null, null, null, null, null],
31+
];
32+
33+
if (snake) {
34+
console.log("Snaking!");
35+
for (let y = 1; y < data.length; y += 2) {
36+
data[y].reverse();
37+
}
38+
}
39+
40+
return data;
41+
}

0 commit comments

Comments
 (0)