Data: Stations and pumping (the tracked stations, their position, pump.sh from the app); the weather of the station at Load

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maximeandClaude Opus 5.5 committed 2026-11-17 08:51:48 +01:00
1 parent eaa124f9f5
commit c8c8c1587a
8 files changed
+261 -14

No files matched your search

+79 -5
View File
@@ -1,6 +1,7 @@
import { useEffect, useState } from "react";
import { Button, Card, Group, Select, TextInput } from "@mantine/core";
import { useEffect, useRef, useState } from "react";
import { ActionIcon, Button, Card, Checkbox, Code, Group, Modal, ScrollArea, Select, Stack, Table, Text, TextInput } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { IconPlus, IconX } from "@tabler/icons-react";
// A request to the API; a refusal of the server shows as a notification -> the JSON returned, or null
export async function call(path, method = "GET", body) {
@@ -18,9 +19,9 @@ export async function call(path, method = "GET", body) {
// The station and the range (UTC, To excluded; empty: the whole period), shared by the pages through the address
export function Selector({ selection, choose }) {
const [stations, setStations] = useState([]), [s, setS] = useState(selection);
useEffect(() => { call("/api/stations").then(l => l && setStations(l)); }, []);
useEffect(() => setS(selection), [selection.station, selection.start, selection.end]);
const [stations, setStations] = useState([]), [s, setS] = useState(selection), [editing, setEditing] = useState(false);
useEffect(() => { call("/api/stations").then(l => l && setStations(l)); }, [selection]); // a pumping may add one
useEffect(() => setS(selection), [selection]); // a new object at each change of the address, or at each Load
return (
<Card shadow="sm" withBorder>
<Group align="flex-end" gap="md">
@@ -29,7 +30,80 @@ export function Selector({ selection, choose }) {
<TextInput label="From (UTC)" type="date" value={s.start} onChange={e => setS({ ...s, start: e.currentTarget.value })} />
<TextInput label="To (excluded)" type="date" value={s.end} onChange={e => setS({ ...s, end: e.currentTarget.value })} />
<Button onClick={() => choose(s)} disabled={!s.station}>Load</Button>
<Button variant="default" onClick={() => setEditing(true)}>Stations and pumping</Button>
</Group>
<Stations opened={editing} close={() => setEditing(false)} selection={s} reload={() => choose(selection)} />
</Card>
);
}
const shift = (d, n) => (d ? new Date(Date.parse(d) + n * 864e5).toISOString().slice(0, 10) : "");
// The tracked stations (extraction/stations.txt of ~/aimsight-data): serial and position (for the weather of the Data page);
// the checked ones pumped (extraction/pump.sh) over a range, or without range their missing days up to now. The state of
// the pumping read every 2 s while the window is open or a pumping runs; at its end, the page loads again
function Stations({ opened, close, selection, reload }) {
const [rows, setRows] = useState([]), [checked, setChecked] = useState([]), [range, setRange] = useState({});
const [images, setImages] = useState(true), [state, setState] = useState(null);
useEffect(() => {
if (!opened) return;
call("/api/tracked").then(l => l && setRows(l));
setChecked(selection.station ? [selection.station] : []);
setRange({ start: shift(selection.start, -3), end: selection.end }); // 3 days before: the history of the detectors
}, [opened]);
const watch = opened || !!state?.running;
useEffect(() => {
if (!watch) return;
const read = () => fetch("/api/pump").then(r => r.ok && r.json()).then(p => p && setState(p)).catch(() => {}); // silently
read();
const timer = setInterval(read, 2000);
return () => clearInterval(timer);
}, [watch]);
const running = !!state?.running, was = useRef(false);
useEffect(() => { if (was.current && !running) reload(); was.current = running; }, [running]);
const set = (i, k, v) => setRows(rows.map((r, n) => (n === i ? { ...r, [k]: v } : r)));
const save = () => call("/api/tracked", "PUT", { stations: rows.filter(r => r.serial) }).then(l => l && setRows(l));
const pump = () => call("/api/pump", "POST", { stations: checked, ...range, images }).then(p => p && setState(p));
return (
<Modal opened={opened} onClose={close} title="Stations and pumping" size="xl">
<Stack gap="md">
<Table withRowBorders={false} verticalSpacing={4}>
<Table.Thead><Table.Tr><Table.Th w={40}>Pump</Table.Th><Table.Th>Serial</Table.Th>
<Table.Th>Position (lat,lon or a Swiss place, for the weather)</Table.Th><Table.Th w={40} /></Table.Tr></Table.Thead>
<Table.Tbody>
{rows.map((r, i) => (
<Table.Tr key={i}>
<Table.Td><Checkbox checked={checked.includes(r.serial)} disabled={!r.serial} aria-label={`Pump ${r.serial}`}
onChange={e => setChecked(e.currentTarget.checked ? [...checked, r.serial] : checked.filter(c => c !== r.serial))} /></Table.Td>
<Table.Td><TextInput size="xs" value={r.serial} onChange={e => set(i, "serial", e.currentTarget.value.trim())} /></Table.Td>
<Table.Td><TextInput size="xs" value={r.position} placeholder="46.6895,7.7790"
onChange={e => set(i, "position", e.currentTarget.value)} /></Table.Td>
<Table.Td><ActionIcon variant="subtle" color="gray" aria-label="Remove" onClick={() => setRows(rows.filter((_, n) => n !== i))}>
<IconX size={16} /></ActionIcon></Table.Td>
</Table.Tr>
))}
</Table.Tbody>
</Table>
<Group gap="sm">
<Button size="xs" variant="default" leftSection={<IconPlus size={14} />}
onClick={() => setRows([...rows, { serial: "", position: "" }])}>Add a station</Button>
<Button size="xs" onClick={save}>Save the list</Button>
</Group>
<Group align="flex-end" gap="md">
<TextInput label="From (UTC)" type="date" value={range.start || ""} onChange={e => setRange({ ...range, start: e.currentTarget.value })} />
<TextInput label="To (excluded)" type="date" value={range.end || ""} onChange={e => setRange({ ...range, end: e.currentTarget.value })} />
<Checkbox label="Images" checked={images} onChange={e => setImages(e.currentTarget.checked)} mb={8} />
<Button onClick={pump} loading={running} disabled={!checked.length}>Pump {checked.length || ""}</Button>
</Group>
<Text size="xs" c="dimmed">From is 3 days before the range shown (the history of the detectors); without dates, the missing days of
each station up to now. Read-only on the platform; a long range, outside busy hours.</Text>
{state?.log?.length > 0 && (
<ScrollArea h={220} viewportRef={v => v && (v.scrollTop = v.scrollHeight)}>
<Code block>{state.log.join("\n")}</Code>
</ScrollArea>
)}
</Stack>
</Modal>
);
}