refactor(frontend): align TJWater Next integrations
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import { api } from "@/lib/api";
|
||||
|
||||
export type AnalysisRun = {
|
||||
run_id: string;
|
||||
name: string;
|
||||
run_type: string;
|
||||
created_by: string;
|
||||
created_at: string;
|
||||
started_at: string;
|
||||
status: string;
|
||||
parameters: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type AnalysisResult = {
|
||||
result_id: string;
|
||||
run_id: string;
|
||||
result_type: string;
|
||||
node_id?: string | null;
|
||||
link_id?: string | null;
|
||||
payload: Record<string, unknown>;
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
type AnalysisRunListResponse = {
|
||||
success: boolean;
|
||||
data: AnalysisRun[];
|
||||
count: number;
|
||||
};
|
||||
|
||||
export type AnalysisSchemeRecord = AnalysisRun & {
|
||||
id: string;
|
||||
scheme_id: string;
|
||||
scheme_name: string;
|
||||
scheme_type: string;
|
||||
username: string;
|
||||
create_time: string;
|
||||
scheme_start_time: string;
|
||||
scheme_detail: Record<string, unknown>;
|
||||
schemeName: string;
|
||||
type: string;
|
||||
startTime: string;
|
||||
schemeDetail: Record<string, unknown>;
|
||||
};
|
||||
|
||||
const normalizeParameters = (value: unknown): Record<string, unknown> =>
|
||||
value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
|
||||
export const toAnalysisSchemeRecord = (run: AnalysisRun): AnalysisSchemeRecord => {
|
||||
const parameters = normalizeParameters(run.parameters);
|
||||
return {
|
||||
...run,
|
||||
parameters,
|
||||
id: run.run_id,
|
||||
scheme_id: run.run_id,
|
||||
scheme_name: run.name,
|
||||
scheme_type: run.run_type,
|
||||
username: run.created_by,
|
||||
create_time: run.created_at,
|
||||
scheme_start_time: run.started_at,
|
||||
scheme_detail: parameters,
|
||||
schemeName: run.name,
|
||||
type: run.run_type,
|
||||
startTime: run.started_at,
|
||||
schemeDetail: parameters,
|
||||
};
|
||||
};
|
||||
|
||||
export const listAnalysisSchemes = async ({
|
||||
runType,
|
||||
queryDate,
|
||||
}: {
|
||||
runType?: string;
|
||||
queryDate?: string;
|
||||
} = {}): Promise<AnalysisSchemeRecord[]> => {
|
||||
const response = await api.get<AnalysisRunListResponse>(
|
||||
"/api/v1/analysis/runs",
|
||||
);
|
||||
const runs = Array.isArray(response.data.data) ? response.data.data : [];
|
||||
return runs
|
||||
.filter((run) => !runType || run.run_type === runType)
|
||||
.filter((run) => !queryDate || run.created_at.slice(0, 10) === queryDate)
|
||||
.map(toAnalysisSchemeRecord);
|
||||
};
|
||||
|
||||
export const getAnalysisScheme = async (
|
||||
runId: string,
|
||||
): Promise<AnalysisSchemeRecord> => {
|
||||
const response = await api.get<AnalysisRun>(
|
||||
`/api/v1/analysis/runs/${encodeURIComponent(runId)}`,
|
||||
);
|
||||
return toAnalysisSchemeRecord(response.data);
|
||||
};
|
||||
|
||||
export const getAnalysisResults = async (
|
||||
runId: string,
|
||||
resultType?: string,
|
||||
): Promise<AnalysisResult[]> => {
|
||||
const response = await api.get<AnalysisResult[]>(
|
||||
`/api/v1/analysis/runs/${encodeURIComponent(runId)}/results`,
|
||||
{ params: resultType ? { result_type: resultType } : undefined },
|
||||
);
|
||||
return Array.isArray(response.data) ? response.data : [];
|
||||
};
|
||||
Reference in New Issue
Block a user