Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 175x 175x 175x 175x 175x 175x 175x 175x 175x 175x 175x 175x 175x 5x 5x 5x 1x 5x 175x 175x 175x | import { VisuallyHidden } from "@radix-ui/react-visually-hidden";
import { useState } from "react";
import { toast } from "sonner";
import { Button } from "@/ahuora-design-system/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogTitle,
DialogTrigger,
} from "@/ahuora-design-system/ui/dialog";
import { Spinner } from "@/ahuora-design-system/ui/spinner";
import {
type MlModelRead,
ModelTypeEnum,
ResultStateEnum,
TransitionEnum,
useCoreMlcolumnmappingListQuery,
useCoreMlListQuery,
useCoreMlPartialUpdateMutation,
useCoreMlTransitionWizardCreateMutation,
} from "@/api/apiStore.gen";
import { useSearchParam } from "@/hooks/searchParams";
import { cn } from "@/lib/utils";
import CancelPanel from "./CancelPanel";
import ColumnMappings from "./ColumnMappings";
import ProgressBar from "./ProgressBar";
import SuccesslyTrained from "./SuccesslyTrained";
import TrainingFailed from "./TrainingFailed";
import UploadCSV from "./UploadCSV";
/** Local or backend-confirmed header information shown during the ML upload flow. */
type CsvHeaderPreviewState = {
headers: string[];
delimiter: string | null;
warnings: string[];
source: "local" | "backend";
};
export default function MachineLearningData({
open,
onOpenChange,
hideTrigger = false,
model,
}: {
open?: boolean;
onOpenChange?: (open: boolean) => void;
hideTrigger?: boolean;
model: MlModelRead;
}) {
const [objectId] = useSearchParam("object");
const [csvHeaderPreview, setCsvHeaderPreview] =
useState<CsvHeaderPreviewState | null>(null);
const simulationObject = +(objectId || 0);
const { refetch } = useCoreMlListQuery({
simulationObject: simulationObject,
});
const [updateMLModel] = useCoreMlPartialUpdateMutation();
const [transitionWizard] = useCoreMlTransitionWizardCreateMutation();
const {
data: columnMappings,
isFetching: columnMappingsFetching,
isLoading: columnMappingsLoading,
refetch: refetchColumnMappings,
} = useCoreMlcolumnmappingListQuery();
const modelId = model?.id ?? 0;
const activeStep = model?.active_step ?? 0;
const completedSteps = model?.completed_steps ?? [];
const columnMappingsReady =
columnMappings && !(activeStep === 1 && columnMappingsFetching);
const columnMappingsLoadingContent = (
<div className="flex h-full w-full items-center justify-center p-4">
<Spinner className="icon-medium" />
</div>
);
Iif (!model) return null;
async function handleUpdateMLModelType(modelType: ModelTypeEnum) {
await updateMLModel({
id: modelId,
patchedPatchMlModel: {
model_type: modelType,
},
}).unwrap();
refetch();
}
async function handleUpdateActiveStep(activeStep: number) {
await transitionWizard({
mlWizardTransition: {
model: modelId,
transition: TransitionEnum.SelectStep,
active_step: activeStep,
},
}).unwrap();
refetch();
}
async function transitionModel({
transition,
errorTitle,
errorDescription,
beforeUpdate,
}: {
transition:
| TransitionEnum.Cancel
| TransitionEnum.Complete
| TransitionEnum.StartMappingUpdate
| TransitionEnum.StartReset;
errorTitle: string;
errorDescription: string;
beforeUpdate?: () => unknown;
}) {
try {
await beforeUpdate?.();
await transitionWizard({
mlWizardTransition: {
model: modelId,
transition,
},
}).unwrap();
refetch();
} catch (error) {
toast.error(errorTitle, {
description: error instanceof Error ? error.message : errorDescription,
});
}
}
async function handleResetModel() {
await transitionModel({
transition: TransitionEnum.StartReset,
errorTitle: "Failed to start retraining",
errorDescription: "An error has occured with starting retraining.",
});
}
async function handleUpdateColumnMappings() {
await transitionModel({
beforeUpdate: refetchColumnMappings,
transition: TransitionEnum.StartMappingUpdate,
errorTitle: "Failed to start model update",
errorDescription: "An error has occured with starting the model update.",
});
}
async function handleCancelResetOrUpdate() {
await transitionModel({
transition: TransitionEnum.Cancel,
errorTitle: "Failed to cancel the model update",
errorDescription: "The model update could not be cancelled.",
});
}
async function handleResetModelComplete() {
await transitionModel({
transition: TransitionEnum.Complete,
errorTitle: "Failed to complete the model update",
errorDescription: "The model update could not be completed.",
});
}
const contentByProgress: Record<number, React.ReactNode> = {
0: (
<UploadCSV
model={model}
simulationObject={simulationObject}
onUpdateModelType={handleUpdateMLModelType}
onCsvHeaderPreviewChange={setCsvHeaderPreview}
/>
),
1: columnMappingsReady ? (
<ColumnMappings
model={model}
simulationObject={simulationObject}
columnMappings={columnMappings}
csvFileName={model?.csv_file_name}
csvHeaderPreview={csvHeaderPreview}
onResetModelComplete={handleResetModelComplete}
/>
) : columnMappingsLoading || columnMappingsFetching ? (
columnMappingsLoadingContent
) : null,
2: (
<SuccesslyTrained
model={model}
onResetModel={handleResetModel}
onUpdateColumnMappings={handleUpdateColumnMappings}
/>
),
3: <TrainingFailed />,
};
const dialogContent =
model.result_state === ResultStateEnum.Failed ? (
<TrainingFailed />
) : (
contentByProgress[activeStep]
);
return (
<div className="flex w-full">
<Dialog key={modelId} open={open} onOpenChange={onOpenChange}>
<div className="w-full flex justify-center p-4">
{!hideTrigger && (
<DialogTrigger asChild>
<Button>{activeStep !== 0 ? "View" : "Set-up"}</Button>
</DialogTrigger>
)}
</div>
<DialogContent
preventDefault
className={cn(
"flex flex-col w-[calc(100vw-2rem)] max-w-[880px] h-[75%]",
)}
onInteractOutside={(event) => event.preventDefault()}
>
<VisuallyHidden>
<DialogTitle />
</VisuallyHidden>
<VisuallyHidden>
<DialogDescription />
</VisuallyHidden>
<ProgressBar
activeStep={activeStep}
completedSteps={completedSteps}
onChangeActiveStep={handleUpdateActiveStep}
/>
{dialogContent}
<CancelPanel model={model} onCancel={handleCancelResetOrUpdate} />
</DialogContent>
</Dialog>
</div>
);
}
|