From 23ef2bb786d3f4b24aa619ab647cf5ef3f572f31 Mon Sep 17 00:00:00 2001 From: Thad Kerosky Date: Tue, 20 Aug 2024 22:04:40 -0400 Subject: [PATCH] 162 return Rules Engine data to components (#238) * Use replacer/reviver to pass Map of Maps to the browser. link to example. Co-authored-by: Devin Altobello Co-authored-by: plocket * console.log and comments to help #223 and #221 --------- Co-authored-by: Devin Altobello Co-authored-by: plocket --- .../EnergyUseHistory.tsx | 2 + heat-stack/app/routes/_heat+/single.tsx | 71 +++++++++++++++++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/heat-stack/app/components/ui/heat/CaseSummaryComponents/EnergyUseHistory.tsx b/heat-stack/app/components/ui/heat/CaseSummaryComponents/EnergyUseHistory.tsx index 1cff8111..d778191e 100644 --- a/heat-stack/app/components/ui/heat/CaseSummaryComponents/EnergyUseHistory.tsx +++ b/heat-stack/app/components/ui/heat/CaseSummaryComponents/EnergyUseHistory.tsx @@ -33,6 +33,8 @@ export function EnergyUseHistory() { /> + (Get example file here) + diff --git a/heat-stack/app/routes/_heat+/single.tsx b/heat-stack/app/routes/_heat+/single.tsx index 85956308..b2bbca6f 100644 --- a/heat-stack/app/routes/_heat+/single.tsx +++ b/heat-stack/app/routes/_heat+/single.tsx @@ -43,12 +43,13 @@ import WeatherUtil from '#app/utils/WeatherUtil' // - [ ] Validate pyodide data // - [ ] Only use csv data after any time the user uploads csv. When the user adjusts the table, use the table data instead. // - [ ] Disable the submit button when inputs or csv file are invalid -// - [Waiting] Use start_date and end_date from rules-engine output of CSV parsing rather than 2 year window. +// - [x] Use start_date and end_date from rules-engine output of CSV parsing rather than 2 year window. // - [ ] (use data passing function API from PR#172 from rules engine) to Build table component form -// - [ ] Proposition: always set form default values when run in development +// - [x] Proposition: always set form default values when run in development // - [ ] Pass modified table back to rules engine for full cycle revalidation // - [ ] Feature v2: How about a dropdown? census geocoder address form picker component to choose which address from several, if ambigous or bad. // - [ ] Treat design_temperature distinctly from design_temperature_override, and design_temperature_override should be kept in state like name or address +// - [ ] Will weather service take timestamp instead of timezone date data? // Ours import { Home, Location, Case, type NaturalGasUsageData, /* validateNaturalGasUsageData, HeatLoadAnalysisZod */ } from '../../../types/index.ts' @@ -253,7 +254,7 @@ export async function action({ request, params }: ActionFunctionArgs) { type NaturalGasUsageData = z.infer; const pyodideResultsFromTextFile: NaturalGasUsageData = executeParseGasBillPy(uploadedTextFile).toJs() - console.log('result', pyodideResultsFromTextFile )//, validateNaturalGasUsageData(pyodideResultsFromTextFile)) + // console.log('result', pyodideResultsFromTextFile )//, validateNaturalGasUsageData(pyodideResultsFromTextFile)) const startDateString = pyodideResultsFromTextFile.get('overall_start_date'); const endDateString = pyodideResultsFromTextFile.get('overall_end_date'); @@ -421,13 +422,74 @@ Traceback (most recent call last): File "", line 32, // const otherResult = executePy(summaryInput, convertedDatesTIWD, exampleNationalGridCSV); - return redirect(`/single`) + const str_version = JSON.stringify(foo2, replacer); + // const json_version = JSON.parse(str_version); + // console.log("str_version", str_version); + + // Consider adding to form data + return json({data: str_version}); + // return redirect(`/single`) +} + +// https://stackoverflow.com/a/56150320 +function replacer(key: any, value: any) { + if(value instanceof Map) { + return { + dataType: 'Map', + value: Array.from(value.entries()), // or with spread: value: [...value] + }; + } else { + return value; + } +} + +function reviver(key: any, value: any) { + if(typeof value === 'object' && value !== null) { + if (value.dataType === 'Map') { + return new Map(value.value); + } + } + return value; } export default function Inputs() { // const location = useLocation(); // console.log(`location:`, location); // `.state` is `null` const lastResult = useActionData() + + /* @ts-ignore */ + console.log("lastResult (all Rules Engine data)", lastResult !== undefined ? JSON.parse(lastResult.data, reviver): undefined) + + /** + * Where temp1 is a temporary variable with the main Map of Maps (or undefined if page not yet submitted). + * + * temp1.get('summary_output'): Map(9) { estimated_balance_point → 61.5, other_fuel_usage → 0.2857142857142857, average_indoor_temperature → 67, difference_between_ti_and_tbp → 5.5, design_temperature → 1, whole_home_heat_loss_rate → 48001.81184312083, standard_deviation_of_heat_loss_rate → 0.08066745182677547, average_heat_load → 3048115.0520381727, maximum_heat_load → 3312125.0171753373 } + */ + /* @ts-ignore */ + console.log("Summary Output", lastResult !== undefined ? JSON.parse(lastResult.data, reviver)?.get('summary_output'): undefined) + + /** + * Where temp1 is a temporary variable with the main Map of Maps (or undefined if page not yet submitted). + * temp1.get('billing_records') + * Array(25) [ Map(9), Map(9), Map(9), Map(9), Map(9), Map(9), Map(9), Map(9), Map(9), Map(9), … ] + * temp1.get('billing_records')[0] + * Map(9) { period_start_date → "2020-10-02", period_end_date → "2020-11-04", usage → 29, analysis_type_override → null, inclusion_override → true, analysis_type → 0, default_inclusion_by_calculation → false, eliminated_as_outlier → false, whole_home_heat_loss_rate → null } + * temp1.get('billing_records')[0].get('period_start_date') + * "2020-10-02" + */ + /* @ts-ignore */ + console.log("EnergyUseHistoryChart table data", lastResult !== undefined ? JSON.parse(lastResult.data, reviver)?.get('billing_records'): undefined) + + /** + * Where temp1 is a temporary variable with the main Map of Maps (or undefined if page not yet submitted). + * temp1.get('balance_point_graph').get('records') + Array(23) [ Map(5), Map(5), Map(5), Map(5), Map(5), Map(5), Map(5), Map(5), Map(5), Map(5), … ] + temp1.get('balance_point_graph').get('records')[0] + Map(5) { balance_point → 60, heat_loss_rate → 51056.8007761249, change_in_heat_loss_rate → 0, percent_change_in_heat_loss_rate → 0, standard_deviation → 0.17628334816871494 } + temp1.get('balance_point_graph').get('records')[0].get('heat_loss_rate') + *//* @ts-ignore */ + console.log("HeatLoad chart", lastResult !== undefined ? JSON.parse(lastResult.data, reviver)?.get('balance_point_graph')?.get('records'): undefined) + type SchemaZodFromFormType = z.infer const [form, fields] = useForm({ lastResult, @@ -450,6 +512,7 @@ export default function Inputs() { return ( <> +
{JSON.stringify(lastResult, null, 2)}