Skip to content

Commit

Permalink
162 return Rules Engine data to components (#238)
Browse files Browse the repository at this point in the history
* Use replacer/reviver to pass Map of Maps to the browser. link to example.

Co-authored-by: Devin Altobello <[email protected]>
Co-authored-by: plocket <[email protected]>

* console.log and comments to help #223 and #221

---------

Co-authored-by: Devin Altobello <[email protected]>
Co-authored-by: plocket <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2024
1 parent d813a53 commit 23ef2bb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export function EnergyUseHistory() {
/>
<Button type="submit"> <Upload className="h-4 w-4 mr-2" /> Upload</Button>
</Suspense>
(<a className="inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80 gap-1" href="https://github.com/codeforboston/home-energy-analysis-tool/issues/162#issuecomment-2246594484">Get example file here</a>)

</div>
<AnalysisHeader />
<EnergyUseHistoryChart />
Expand Down
71 changes: 67 additions & 4 deletions heat-stack/app/routes/_heat+/single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -253,7 +254,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
type NaturalGasUsageData = z.infer<typeof NaturalGasUsageData>;
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');

Expand Down Expand Up @@ -421,13 +422,74 @@ Traceback (most recent call last): File "<exec>", 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<typeof action>()

/* @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<typeof Schema>
const [form, fields] = useForm({
lastResult,
Expand All @@ -450,6 +512,7 @@ export default function Inputs() {

return (
<>
<pre>{JSON.stringify(lastResult, null, 2)}</pre>
<Form
id={form.id}
method="post"
Expand Down

0 comments on commit 23ef2bb

Please sign in to comment.