-
Notifications
You must be signed in to change notification settings - Fork 0
chore: some math #27
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
chore: some math #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -121,7 +121,7 @@ async function parseFileAndUpdateData(file: MultiPartData) { | |||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const parsedKitchens: { name: string, total: number, checks: number }[] = [] | ||||||||||||||||||||||||||||
| const parsedKitchens: { name: string, total: number, checks: number, averageCheck: number, commonAverageCheck: number, commonTotal: number }[] = [] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for (const row of dataRows) { | ||||||||||||||||||||||||||||
| const name = row[indexOfName] | ||||||||||||||||||||||||||||
|
|
@@ -132,13 +132,27 @@ async function parseFileAndUpdateData(file: MultiPartData) { | |||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const averageCheck = Math.round(total / checks) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| parsedKitchens.push({ | ||||||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||||||
| total, | ||||||||||||||||||||||||||||
| checks, | ||||||||||||||||||||||||||||
| averageCheck, | ||||||||||||||||||||||||||||
| commonAverageCheck: 0, | ||||||||||||||||||||||||||||
| commonTotal: 0, | ||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Update common data | ||||||||||||||||||||||||||||
| const commonAverageCheck = Math.round(parsedKitchens.reduce((acc, curr) => acc + curr.averageCheck, 0) / parsedKitchens.length) | ||||||||||||||||||||||||||||
| const commonTotal = Math.round(parsedKitchens.reduce((acc, curr) => acc + curr.total, 0) / parsedKitchens.length) | ||||||||||||||||||||||||||||
|
Comment on lines
+148
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add validation for empty kitchen arrays. The common value calculations could fail with division by zero if Add validation to handle empty arrays: + if (parsedKitchens.length === 0) {
+ return { rowsUpdated: 0, errors: ['No valid kitchen data found'] }
+ }
+
// Update common data
const commonAverageCheck = Math.round(parsedKitchens.reduce((acc, curr) => acc + curr.averageCheck, 0) / parsedKitchens.length)
const commonTotal = Math.round(parsedKitchens.reduce((acc, curr) => acc + curr.total, 0) / parsedKitchens.length)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for (const kitchen of parsedKitchens) { | ||||||||||||||||||||||||||||
| kitchen.commonAverageCheck = commonAverageCheck | ||||||||||||||||||||||||||||
| kitchen.commonTotal = commonTotal | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Every kitchen: find in DB and add amount for this day | ||||||||||||||||||||||||||||
| const kitchens = await repository.kitchen.list() | ||||||||||||||||||||||||||||
| let rowsUpdated = 0 | ||||||||||||||||||||||||||||
|
|
@@ -155,11 +169,17 @@ async function parseFileAndUpdateData(file: MultiPartData) { | |||||||||||||||||||||||||||
| date: dateOnly, | ||||||||||||||||||||||||||||
| total: kitchen.total, | ||||||||||||||||||||||||||||
| checks: kitchen.checks, | ||||||||||||||||||||||||||||
| averageCheck: kitchen.averageCheck, | ||||||||||||||||||||||||||||
| commonAverageCheck: kitchen.commonAverageCheck, | ||||||||||||||||||||||||||||
| commonTotal: kitchen.commonTotal, | ||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| await repository.kitchen.updateRevenue(revenue.id, { | ||||||||||||||||||||||||||||
| total: kitchen.total, | ||||||||||||||||||||||||||||
| checks: kitchen.checks, | ||||||||||||||||||||||||||||
| averageCheck: kitchen.averageCheck, | ||||||||||||||||||||||||||||
| commonAverageCheck: kitchen.commonAverageCheck, | ||||||||||||||||||||||||||||
| commonTotal: kitchen.commonTotal, | ||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add validation for division by zero.
The average check calculation could fail if
checksis 0, leading toInfinityorNaNvalues.Add validation to handle zero checks:
📝 Committable suggestion
🤖 Prompt for AI Agents