Skip to content

Commit

Permalink
Merge pull request #11 from bcgov/feature/multi-scenario
Browse files Browse the repository at this point in the history
Refactor link to button for generation of scenarios template.
  • Loading branch information
brysonjbest authored Jul 5, 2024
2 parents 48e6571 + 2218799 commit 6eef5a3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app/components/ScenarioTester/ScenarioTester.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -232,14 +232,26 @@ 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 (
<div>
{uploader ? (
<Flex gap={"small"}>
<ol className={styles.instructionsList}>
<li>
Download a template CSV file:{" "}
<a href={`/api/scenario/evaluation/${encodeURIComponent(jsonFile)}`}>Download Scenarios/Template</a>
<Button onClick={handleDownloadScenarios} size="large" type="primary">
Generate Scenarios/Template
</Button>
</li>
<li>Add additional scenarios to the CSV file</li>
<li>
Expand Down
32 changes: 32 additions & 0 deletions app/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
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.
Expand Down

0 comments on commit 6eef5a3

Please sign in to comment.