From 22187996b317129abe1e82fe7284c63f836f1aad Mon Sep 17 00:00:00 2001 From: brysonjbest Date: Fri, 5 Jul 2024 13:13:22 -0700 Subject: [PATCH] Refactor link to button for generation of scenarios template. --- .../ScenarioTester/ScenarioTester.tsx | 16 ++++++++-- app/utils/api.ts | 32 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/components/ScenarioTester/ScenarioTester.tsx b/app/components/ScenarioTester/ScenarioTester.tsx index 59ae6c6..8d9f84a 100644 --- a/app/components/ScenarioTester/ScenarioTester.tsx +++ b/app/components/ScenarioTester/ScenarioTester.tsx @@ -3,7 +3,7 @@ import { Table, Tag, Button, TableProps, Flex, Upload, message } from "antd"; import { CheckCircleOutlined, CloseCircleOutlined } from "@ant-design/icons"; import { UploadOutlined } from "@ant-design/icons"; import styles from "./ScenarioTester.module.css"; -import { runDecisionsForScenarios, uploadCSVAndProcess } from "@/app/utils/api"; +import { runDecisionsForScenarios, uploadCSVAndProcess, getCSVForRuleRun } from "@/app/utils/api"; interface ScenarioTesterProps { jsonFile: string; @@ -232,6 +232,16 @@ export default function ScenarioTester({ jsonFile, uploader }: ScenarioTesterPro } }; + const handleDownloadScenarios = async () => { + try { + const csvContent = await getCSVForRuleRun(jsonFile); + message.success(`Scenario Testing Template: ${csvContent}`); + } catch (error) { + message.error("Error downloading scenarios."); + console.error("Error:", error); + } + }; + return (
{uploader ? ( @@ -239,7 +249,9 @@ export default function ScenarioTester({ jsonFile, uploader }: ScenarioTesterPro
  1. Download a template CSV file:{" "} - Download Scenarios/Template +
  2. Add additional scenarios to the CSV file
  3. diff --git a/app/utils/api.ts b/app/utils/api.ts index 8583c85..22e7a39 100644 --- a/app/utils/api.ts +++ b/app/utils/api.ts @@ -276,6 +276,38 @@ export const runDecisionsForScenarios = async (goRulesJSONFilename: string) => { } }; +/** + * Downloads a CSV file containing scenarios for a rule run. + * @param goRulesJSONFilename The filename for the JSON rule. + * @returns The processed CSV content as a string. + * @throws If an error occurs during file upload or processing. + */ +export const getCSVForRuleRun = async (goRulesJSONFilename: string): Promise => { + try { + const response = await axiosAPIInstance.post( + "/scenario/evaluation", + { goRulesJSONFilename: goRulesJSONFilename }, + { + responseType: "blob", + headers: { "Content-Type": "application/json" }, + } + ); + + const blob = new Blob([response.data], { type: "text/csv" }); + const url = window.URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = `${goRulesJSONFilename.replace(/\.json$/, ".csv")}`; + a.click(); + window.URL.revokeObjectURL(url); + + return "CSV downloaded successfully"; + } catch (error) { + console.error(`Error getting CSV for rule run: ${error}`); + throw new Error("Error getting CSV for rule run"); + } +}; + /** * Uploads a CSV file containing scenarios and processes the scenarios against the specified rule. * @param file The file to be uploaded.