Files
TJWaterFrontend_Refine/src/components/olmap/MonitoringPlaceOptimization/schemeApi.ts
T
jiang 29b8babd68 fix(api): align frontend with project-scoped backend
Use project-scoped endpoints and run IDs for current project, SCADA metadata, historical time series, migrated DMA results, and sensor placement runs.

The regressions recurred because tests mocked pre-interceptor and new-only payload shapes, while history queries relied on implicit global scheme state and unbounded request fan-out. Cover post-interceptor pages, migrated result_rows, explicit run_id routing, multi-element requests, request limits, and cancellation.
2026-09-01 14:37:51 +08:00

125 lines
3.3 KiB
TypeScript

import { api } from "@/lib/api";
import { config } from "@/config/config";
import type {
AdjustmentStatus,
SensorPlacementScheme,
SensorPoint,
} from "./types";
export interface OptimizeSchemeInput {
run_name: string;
sensor_type: "pressure";
method: "sensitivity" | "kmeans";
sensor_count: number;
min_diameter: number;
}
type SensorPlacementRunResponse = {
run_id: string;
name: string;
sensor_count: number;
min_diameter: number;
created_by: string;
created_at: string;
sensor_locations: string[];
sensor_points: SensorPoint[];
can_edit: boolean;
};
type SensorPlacementRunPage = {
items: SensorPlacementRunResponse[];
total: number;
limit: number;
offset: number;
};
const toSensorPlacementScheme = (
run: SensorPlacementRunResponse,
): SensorPlacementScheme => ({
id: run.run_id,
scheme_name: run.name,
sensor_number: run.sensor_count,
min_diameter: run.min_diameter,
username: run.created_by,
create_time: run.created_at,
sensor_location: run.sensor_locations,
sensor_points: run.sensor_points,
can_edit: run.can_edit,
});
export const optimizeSensorPlacement = async (
input: OptimizeSchemeInput,
): Promise<SensorPlacementScheme> => {
const response = await api.post<SensorPlacementRunResponse>(
`${config.BACKEND_URL}/api/v1/sensor-placement-runs`,
input,
);
return toSensorPlacementScheme(response.data);
};
export const listSensorPlacementSchemes = async (): Promise<
SensorPlacementScheme[]
> => {
const response = await api.get<
SensorPlacementRunPage | SensorPlacementRunResponse[]
>(
`${config.BACKEND_URL}/api/v1/sensor-placement-runs`,
{ params: { limit: 1000, offset: 0 } },
);
const runs = Array.isArray(response.data)
? response.data
: response.data.items;
return runs.map(toSensorPlacementScheme);
};
export const getSensorPlacementScheme = async (
schemeId: string,
): Promise<SensorPlacementScheme> => {
const response = await api.get<SensorPlacementRunResponse>(
`${config.BACKEND_URL}/api/v1/sensor-placement-runs/${encodeURIComponent(schemeId)}`,
);
return toSensorPlacementScheme(response.data);
};
export const getSensorPlacementCandidate = async (
nodeId: string,
): Promise<SensorPoint> => {
const response = await api.get<SensorPoint>(
`${config.BACKEND_URL}/api/v1/sensor-placement-candidates/${encodeURIComponent(nodeId)}`,
);
return response.data;
};
export const overwriteSensorPlacementScheme = async (
schemeId: string,
expectedSensorLocation: string[],
sensorLocation: string[],
): Promise<SensorPlacementScheme> => {
const response = await api.put<SensorPlacementRunResponse>(
`${config.BACKEND_URL}/api/v1/sensor-placement-runs/${encodeURIComponent(schemeId)}`,
{
expected_sensor_locations: expectedSensorLocation,
sensor_locations: sensorLocation,
},
);
return toSensorPlacementScheme(response.data);
};
export const exportSensorPlacementExcel = async (
schemeId: string,
sensorLocation: string[],
adjustmentStatus: Record<string, AdjustmentStatus>,
) => {
const response = await api.post<Blob>(
`${config.BACKEND_URL}/api/v1/sensor-placement-runs/${encodeURIComponent(schemeId)}/exports/excel`,
{
sensor_locations: sensorLocation,
adjustment_status: adjustmentStatus,
},
{
responseType: "blob",
},
);
return response.data;
};