Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions frontend/testing-view/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Testing } from "./pages/Testing";
import { useStore } from "./store/store";
import type { Connection } from "./types/common/connection";
import type { MessagePacket } from "./types/data/message";
import type { ProtectionUpdate } from "./types/data/protection";
import type { TelemetryData } from "./types/telemetry/telemetry";

function App() {
Expand Down Expand Up @@ -63,6 +64,14 @@ function App() {
});
});

// Callback executed when a protection triggers
const addProtection = useStore((s) => s.addProtection);

// Subscribe to protections, to highlight the measurement that triggered them
useTopic<ProtectionUpdate>("protection/update", (protection) => {
addProtection(protection);
});

return (
<ErrorBoundary onError={reportError}>
<AppLayout backendConnected={isConnected}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const MessagesSection = () => {
variant="secondary"
size="icon"
className="text-muted-foreground hover:text-destructive h-6 w-6"
title="Clear messages and protections"
aria-label="Clear messages and protections"
onClick={clearMessages}
>
<Trash2 className="h-3.5 w-3.5" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useDraggable } from "@dnd-kit/core";
import { Badge } from "@hyperloopupv/frontend-kit";
import { cn } from "@hyperloopupv/frontend-kit/lib";
import { Shield } from "lucide-react";
import { useShallow } from "zustand/shallow";
import {
canAddSeriesToChart,
Expand Down Expand Up @@ -34,6 +35,13 @@ export const VariableItem = ({ packetId, variable }: VariableItemProps) => {
})),
);

// A protection triggered on this measurement and the messages weren't cleared since
const isProtected = useStore(
(s) =>
variable.adjId !== undefined &&
s.activeProtections[variable.adjId] !== undefined,
);

const { attributes, listeners, setNodeRef, transform, isDragging } =
useDraggable({
id: `draggable-${packetId}-${variable.id}`,
Expand Down Expand Up @@ -89,6 +97,7 @@ export const VariableItem = ({ packetId, variable }: VariableItemProps) => {
{...listeners}
className={cn(
"hover:bg-accent/30 group flex items-center justify-between gap-3 border-t py-2 pl-6 pr-3",
isProtected && "bg-red-500/15 hover:bg-red-500/25",
opacityStyle,
cursorStyle,
)}
Expand All @@ -105,9 +114,21 @@ export const VariableItem = ({ packetId, variable }: VariableItemProps) => {
{variable.type}
</Badge>

{/* Protection Icon */}
{isProtected && (
<span title="Protection triggered" className="shrink-0 text-red-500">
<Shield className="h-3.5 w-3.5" />
</span>
)}

{/* Variable Name */}
<div className="flex min-w-0 flex-col">
<span className="text-foreground truncate text-xs font-semibold leading-tight">
<span
className={cn(
"text-foreground truncate text-xs font-semibold leading-tight",
isProtected && "text-red-600 dark:text-red-400",
)}
>
{variable.name}
</span>

Expand Down
106 changes: 106 additions & 0 deletions frontend/testing-view/src/store/slices/__tests__/messagesSlice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { beforeEach, describe, expect, it } from "vitest";
import { create } from "zustand";
import { createChartsSlice } from "../../../features/charts/store/chartsSlice";
import { createFilteringSlice } from "../../../features/filtering/store/filteringSlice";
import { createRightSidebarSlice } from "../../../features/workspace/store/rightSidebarSlice";
import { createWorkspacesSlice } from "../../../features/workspace/store/workspacesSlice";
import type { Message } from "../../../types/data/message";
import type { ProtectionUpdate } from "../../../types/data/protection";
import type { Store } from "../../store";
import { createAppSlice } from "../appSlice";
import { createCatalogSlice } from "../catalogSlice";
import { createConnectionsSlice } from "../connectionsSlice";
import { createMessagesSlice } from "../messagesSlice";
import { createTelemetrySlice } from "../telemetrySlice";

const createTestStore = () =>
create<Store>()((...a) => ({
...createAppSlice(...a),
...createCatalogSlice(...a),
...createWorkspacesSlice(...a),
...createTelemetrySlice(...a),
...createRightSidebarSlice(...a),
...createConnectionsSlice(...a),
...createMessagesSlice(...a),
...createChartsSlice(...a),
...createFilteringSlice(...a),
}));

/** Protection number `position` (1-7) of the measurement `measurementId`. */
const protection = (
position: number,
measurementId: number,
value: ProtectionUpdate["value"] = 105.5,
): ProtectionUpdate => ({
id: (position << 13) | measurementId,
value,
timestamp: { hour: 12, minute: 30, second: 6, subsecond: 0 },
});

const MESSAGE: Message = {
id: "message-1",
kind: "info",
board: "LCU",
name: "Example",
payload: "Example message",
timestamp: {
counter: 0,
second: 0,
minute: 0,
hour: 12,
day: 1,
month: 1,
year: 2026,
},
};

let store: ReturnType<typeof createTestStore>;

beforeEach(() => {
store = createTestStore();
});

describe("addProtection", () => {
it("activates the measurement in the low 13 bits of the protection id", () => {
store.getState().addProtection(protection(1, 3000));

expect(Object.keys(store.getState().activeProtections)).toEqual(["3000"]);
expect(store.getState().activeProtections[3000].id).toBe(11192);
});

it("keeps the last protection when several protections of one measurement trigger", () => {
store.getState().addProtection(protection(1, 3000, 105.5));
store.getState().addProtection(protection(2, 3000, 5));

expect(Object.keys(store.getState().activeProtections)).toEqual(["3000"]);
expect(store.getState().activeProtections[3000].value).toBe(5);
});

it("keeps the protections of different measurements apart", () => {
store.getState().addProtection(protection(1, 3000));
store.getState().addProtection(protection(1, 3001));

expect(Object.keys(store.getState().activeProtections)).toEqual([
"3000",
"3001",
]);
});

it("does not add messages", () => {
store.getState().addProtection(protection(1, 3000));

expect(store.getState().messages).toEqual([]);
});
});

describe("clearMessages", () => {
it("clears the messages and the active protections", () => {
store.getState().addMessage(MESSAGE);
store.getState().addProtection(protection(1, 3000));

store.getState().clearMessages();

expect(store.getState().messages).toEqual([]);
expect(store.getState().activeProtections).toEqual({});
});
});
21 changes: 20 additions & 1 deletion frontend/testing-view/src/store/slices/messagesSlice.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import type { StateCreator } from "zustand";
import { config } from "../../../config";
import type { Message } from "../../types/data/message";
import type { ProtectionUpdate } from "../../types/data/protection";
import type { Store } from "../store";

/** The low 13 bits of a protection id are the id of the measurement it watches. */
const MEASUREMENT_ID_MASK = 0x1fff;

export interface MessagesSlice {
messages: Message[];
addMessage: (message: Message) => void;
/**
* Last protection received for each measurement, by the measurement's numeric ADJ id.\
* Those measurements are highlighted until the messages are cleared.
*/
activeProtections: Record<number, ProtectionUpdate>;
addProtection: (protection: ProtectionUpdate) => void;
/** Clears the messages and the active protections */
clearMessages: () => void;
}

Expand All @@ -20,5 +31,13 @@ export const createMessagesSlice: StateCreator<Store, [], [], MessagesSlice> = (
config.MAX_MESSAGES_COUNT,
),
})),
clearMessages: () => set({ messages: [] }),
activeProtections: {},
addProtection: (protection) =>
set((state) => ({
activeProtections: {
...state.activeProtections,
[protection.id & MEASUREMENT_ID_MASK]: protection,
},
})),
clearMessages: () => set({ messages: [], activeProtections: {} }),
});
25 changes: 25 additions & 0 deletions frontend/testing-view/src/types/data/protection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Time of day at which the board detected the protection.\
* It carries no date: it is the day the protection was received.
*/
export type ProtectionTimestamp = {
hour: number;
minute: number;
second: number;
/** Raw sub-second byte from the board (its unit is not settled yet) */
subsecond: number;
};

/**
* Protection event as it arrives from the backend on `protection/update`.
*/
export type ProtectionUpdate = {
/**
* Compound id: `(position << 13) | measurementId`.\
* The low 13 bits are the numeric ADJ id of the measurement that triggered it.
*/
id: number;
/** Value of the measurement that triggered the protection */
value: number | boolean | string;
timestamp: ProtectionTimestamp;
};
5 changes: 5 additions & 0 deletions frontend/testing-view/src/types/data/telemetryCatalogItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type Variable = {
name: string;
type: string;
units?: string;
/**
* Numeric id of the measurement in the ADJ.\
* Protections point to the measurement they watch by this id.
*/
adjId?: number;
};

export interface NumericVariable extends Variable {
Expand Down