-
Notifications
You must be signed in to change notification settings - Fork 0
refactor ml containers to use tanstack query and refactor api route h… #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
justin-phxm
wants to merge
3
commits into
main
Choose a base branch
from
refactor-to-tanstack-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * API Route Constants for Helios Telemetry Client | ||
| * | ||
| * This file defines all API endpoints used in the application in a structured format. | ||
| * Routes are organized by feature/domain for better maintainability. | ||
| * | ||
| * Usage: | ||
| * ```typescript | ||
| * import { API_ROUTES } from '@/constants/apiRoutes'; | ||
| * const response = await api.get(API_ROUTES.ml.packetCorrelationMatrix); | ||
| * ``` | ||
| */ | ||
|
|
||
| /** | ||
| * Next.js API Routes (client-side proxies) | ||
| * These routes are handled by Next.js API handlers in /pages/api/ | ||
| */ | ||
| export const API_ROUTES = { | ||
| /** | ||
| * Authentication/Security endpoints | ||
| */ | ||
| auth: { | ||
| /** Check MQTT password for driver updates */ | ||
| checkMQTTPassword: "/api/checkMQTTPassword", | ||
| }, | ||
|
|
||
| /** | ||
| * Health check endpoint | ||
| */ | ||
| health: { | ||
| /** Basic health check */ | ||
| hello: "/api/hello", | ||
| }, | ||
|
|
||
| /** | ||
| * Machine Learning endpoints | ||
| */ | ||
| ml: { | ||
| /** Get lap correlation matrix data */ | ||
| lapCorrelationMatrix: "/api/getLapCorrelationMatrix", | ||
| /** Get packet correlation matrix data */ | ||
| packetCorrelationMatrix: "/api/getPacketCorrelationMatrix", | ||
| }, | ||
| } as const; | ||
|
|
||
| /** | ||
| * Backend API Routes (direct server calls) | ||
| * These routes connect directly to the backend server (prodURL) | ||
| * Used when bypassing Next.js API routes | ||
| */ | ||
| export const BACKEND_ROUTES = { | ||
| /** | ||
| * Driver endpoints | ||
| */ | ||
| drivers: { | ||
| /** Get all drivers */ | ||
| base: "/drivers", | ||
| /** Get driver by RFID */ | ||
| byRfid: (rfid: number) => `/driver/${rfid}`, | ||
| }, | ||
|
|
||
| /** | ||
| * Lap data endpoints | ||
| */ | ||
| laps: { | ||
| /** Get all laps */ | ||
| base: "/laps", | ||
| }, | ||
|
|
||
| /** | ||
| * Machine Learning endpoints (backend) | ||
| */ | ||
| ml: { | ||
| /** ML health check */ | ||
| health: "/ml/health", | ||
| /** Invalidate ML cache */ | ||
| invalidateCache: "/ml/invalidate", | ||
| /** Get lap correlation matrix */ | ||
| lapCorrelationMatrix: "/ml/correlation-matrix/lap", | ||
| /** Get packet correlation matrix */ | ||
| packetCorrelationMatrix: "/ml/correlation-matrix/packet", | ||
| }, | ||
|
|
||
| /** | ||
| * Playback endpoints | ||
| */ | ||
| playback: { | ||
| /** Get packets between time range */ | ||
| packetsBetween: "/packetsBetween", | ||
| }, | ||
| } as const; | ||
|
|
||
| /** | ||
| * Type helper to extract route values | ||
| */ | ||
| export type ApiRoute = (typeof API_ROUTES)[keyof typeof API_ROUTES]; | ||
| export type BackendRoute = (typeof BACKEND_ROUTES)[keyof typeof BACKEND_ROUTES]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { useTheme } from "next-themes"; | ||
| import { useMemo } from "react"; | ||
| import type { PlotParams } from "react-plotly.js"; | ||
|
|
||
| import { API_ROUTES } from "@/constants/apiRoutes"; | ||
| import { api } from "@/lib/api"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
|
|
||
| export type PlotTypes = | ||
| | typeof API_ROUTES.ml.packetCorrelationMatrix | ||
| | typeof API_ROUTES.ml.lapCorrelationMatrix; | ||
|
|
||
| interface UseMLCorrelationMatrixOptions { | ||
| plotType: PlotTypes; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches correlation matrix data from the API with a 30-second timeout. | ||
| * | ||
| * Uses the configured axios instance from @/lib/api which includes: | ||
| * - 30-second timeout to prevent hanging requests | ||
| * - Standard JSON headers | ||
| * - Centralized error handling | ||
| * | ||
| * @param plotType - The API endpoint to fetch from | ||
| * @returns Promise resolving to PlotParams data | ||
| * @throws Error if the request fails or times out | ||
| */ | ||
| async function fetchCorrelationMatrix( | ||
| plotType: PlotTypes, | ||
| ): Promise<PlotParams> { | ||
| const response = await api.get<string>(plotType); | ||
|
|
||
| // Parse the JSON string response | ||
| const rawData = JSON.parse(response.data) as PlotParams; | ||
|
|
||
| return rawData; | ||
| } | ||
justin-phxm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Custom hook to fetch and cache ML correlation matrix data using TanStack Query. | ||
| * | ||
| * Features: | ||
| * - 1-hour cache TTL (matches backend cache) | ||
| * - Automatic retry on failure (3 attempts) | ||
| * - No refetch on window focus (expensive ML data) | ||
| * - Theme-aware layout transformation (doesn't refetch on theme change) | ||
| * - 30-second timeout to prevent hanging requests | ||
| * | ||
| * @param options - Configuration options | ||
| * @param options.plotType - The API endpoint to fetch from | ||
| * @returns Query result with theme-transformed plot data | ||
| */ | ||
| export function useMLCorrelationMatrix({ | ||
| plotType, | ||
| }: UseMLCorrelationMatrixOptions) { | ||
| const { resolvedTheme } = useTheme(); | ||
| // Fetch raw data from API | ||
| const query = useQuery({ | ||
| // Only enable query when theme is resolved | ||
| // This prevents unnecessary fetches before theme is ready | ||
| enabled: !!resolvedTheme, | ||
|
|
||
| // Fetch function - uses axios with 30s timeout | ||
| queryFn: () => fetchCorrelationMatrix(plotType), | ||
|
|
||
| // Query key: ['ml', 'correlation-matrix', plotType] | ||
| // Note: theme is NOT in the key to avoid separate cache entries per theme | ||
| queryKey: ["ml", "correlation-matrix", plotType] as const, | ||
|
|
||
| // Throw errors to error boundary (optional, can be removed if you prefer error state) | ||
| throwOnError: false, | ||
| }); | ||
| // Transform layout based on current theme (memoized to avoid unnecessary recalculations) | ||
| const transformedData = useMemo(() => { | ||
| if (!query.data) return null; | ||
|
|
||
| const layout: PlotParams["layout"] = { | ||
| autosize: true, | ||
| font: { | ||
| color: resolvedTheme === "dark" ? "white" : "black", | ||
| }, | ||
| margin: { l: 175, t: 75 }, | ||
| paper_bgcolor: "rgba(0,0,0,0)", | ||
| title: query.data.layout.title, | ||
| }; | ||
|
|
||
| return { | ||
| ...query.data, | ||
| layout, | ||
| }; | ||
| }, [query.data, resolvedTheme]); | ||
|
|
||
| // Return query state with transformed data | ||
| return { | ||
| ...query, | ||
| data: transformedData, | ||
| isLoading: query.isLoading || !resolvedTheme, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.