Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CostAndUsageReports.ts with try/catch error handling to enable unknown data to be gracefully handled #1226

Open
wants to merge 1 commit into
base: changeset-release/trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 53 additions & 44 deletions packages/aws/src/lib/CostAndUsageReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,58 +156,67 @@ export default class CostAndUsageReports {
getEstimatesFromInputData(
inputData: LookupTableInput[],
): LookupTableOutput[] {
const result: LookupTableOutput[] = []
const unknownRows: CostAndUsageReportsRow[] = []
const result: LookupTableOutput[] = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can undo these changes where you add the semi-colons. It may not play well with the linter

const unknownRows: CostAndUsageReportsRow[] = [];

inputData.map((inputDataRow: LookupTableInput) => {
const costAndUsageReportRow = new CostAndUsageReportsRow(
null,
'',
'',
inputDataRow.region,
inputDataRow.serviceName,
inputDataRow.usageType,
inputDataRow.usageUnit,
inputDataRow.vCpus != '' ? parseFloat(inputDataRow.vCpus) : null,
1,
1,
{},
)

const footprintEstimate = this.getFootprintEstimateFromUsageRow(
costAndUsageReportRow,
unknownRows,
)

if (footprintEstimate) {
result.push({
serviceName: inputDataRow.serviceName,
region: inputDataRow.region,
usageType: inputDataRow.usageType,
vCpus: inputDataRow.vCpus,
kilowattHours: footprintEstimate.kilowattHours,
co2e: footprintEstimate.co2e,
})
try {
const costAndUsageReportRow = new CostAndUsageReportsRow(
null,
'',
'',
inputDataRow.region,
inputDataRow.serviceName,
inputDataRow.usageType,
inputDataRow.usageUnit,
inputDataRow.vCpus != '' ? parseFloat(inputDataRow.vCpus) : null,
1,
1,
{},
);

const footprintEstimate = this.getFootprintEstimateFromUsageRow(
costAndUsageReportRow,
unknownRows,
);

if (footprintEstimate) {
result.push({
serviceName: inputDataRow.serviceName,
region: inputDataRow.region,
usageType: inputDataRow.usageType,
vCpus: inputDataRow.vCpus,
kilowattHours: footprintEstimate.kilowattHours,
co2e: footprintEstimate.co2e,
});
}
} catch (error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you confirm that this will in fact reach the catch block? i am not sure that the issue here would actually trigger an error, however, I may be wrong.

console.error(`Error processing inputDataRow: ${JSON.stringify(inputDataRow)}. Error: ${error.message}`);
}
})
});

if (result.length > 0) {
unknownRows.map((inputDataRow: CostAndUsageReportsRow) => {
const footprintEstimate = this.getEstimateForUnknownUsage(inputDataRow)
if (footprintEstimate)
result.push({
serviceName: inputDataRow.serviceName,
region: inputDataRow.region,
usageType: inputDataRow.usageType,
vCpus: inputDataRow.vCpus,
kilowattHours: footprintEstimate.kilowattHours,
co2e: footprintEstimate.co2e,
})
})
try {
const footprintEstimate = this.getEstimateForUnknownUsage(inputDataRow);
if (footprintEstimate)
result.push({
serviceName: inputDataRow.serviceName,
region: inputDataRow.region,
usageType: inputDataRow.usageType,
vCpus: inputDataRow.vCpus,
kilowattHours: footprintEstimate.kilowattHours,
co2e: footprintEstimate.co2e,
});
} catch (error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thought as above. I think if the error we are trying to fix is the NaN issue, we may be better to error handle at the root where the arithmetic occurs to aggregate the results. I could also see this potentially being console warnings.

console.error(`Error processing unknownRow: ${JSON.stringify(inputDataRow)}. Error: ${error.message}`);
}
});
}

return result
}
return result;
}


private getFootprintEstimateFromUsageRow(
costAndUsageReportRow: CostAndUsageReportsRow,
Expand Down