-
Notifications
You must be signed in to change notification settings - Fork 0
Two team compare #46
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
base: master
Are you sure you want to change the base?
Two team compare #46
Changes from all commits
dd96708
9b0340f
7eb9d65
45bb4c5
2979565
bd28d68
50f2e91
b41aa79
df72baf
8abe11f
3c25d0b
81460f9
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 |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| //בס"ד | ||
|
|
||
| import type { ScoutingForm, TeleClimbLevel } from "@repo/scouting_types"; | ||
| import { Router } from "express"; | ||
| import { getFormsCollection } from "./forms-router"; | ||
| import { pipe } from "fp-ts/lib/function"; | ||
| import { | ||
| flatMap, | ||
| left, | ||
| map, | ||
| right, | ||
| tryCatch, | ||
| fold, | ||
| } from "fp-ts/lib/TaskEither"; | ||
| import { mongofyQuery } from "../middleware/query"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { firstElement, isEmpty } from "@repo/array-functions"; | ||
| import { calcAverageGeneralFuelData } from "./general-router"; | ||
| import { generalCalculateFuel } from "../fuel/fuel-general"; | ||
| import { getBPSes } from "./teams-router"; | ||
|
|
||
| export const compareRouter = Router(); | ||
|
|
||
| type GamePeriod = "auto" | "fullGame"; | ||
|
Contributor
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. What about autonomous? Consider adding it
Contributor
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. What about teleop? Consider adding it |
||
|
|
||
| const DIGITS_AFTER_DOT = 2; | ||
|
Contributor
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. Consider renaming it to 'DIFITS_AFTER_DECIMAL_DOT' |
||
| const INITIAL_COUNTER_VALUE = 0; | ||
| const INCREMENT = 1; | ||
|
|
||
| const calculateAverageScoredFuel = ( | ||
| forms: ScoutingForm[], | ||
| gamePeriod: GamePeriod, | ||
| ) => { | ||
|
Contributor
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. Consider declaring a type 'L1 | L2 | L3 | L4' that the function will return |
||
| const generalFuelData = forms.map((form) => | ||
| generalCalculateFuel(form, getBPSes()), | ||
| ); | ||
| const averagedFuelData = calcAverageGeneralFuelData(generalFuelData); | ||
|
|
||
| return parseFloat( | ||
| averagedFuelData[gamePeriod].scored.toFixed(DIGITS_AFTER_DOT), | ||
| ); | ||
| }; | ||
|
|
||
| const findMaxClimbLevel = (forms: ScoutingForm[]) => { | ||
| const fullGameClimbedLevels = forms | ||
| .map((form) => form.tele.climb.level) | ||
| .concat(forms.map((form) => form.auto.climb.level)); | ||
karnishein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return fullGameClimbedLevels.includes("L3") | ||
| ? "L3" | ||
| : fullGameClimbedLevels.includes("L2") | ||
| ? "L2" | ||
| : fullGameClimbedLevels.includes("L1") | ||
| ? "L1" | ||
| : "L0"; | ||
| }; | ||
|
|
||
| const findTimesClimbedToMax = ( | ||
|
Contributor
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. Consider changing it to 'findTimesClimbedToMax' |
||
| forms: ScoutingForm[], | ||
| maxLevel: TeleClimbLevel, | ||
| ) => { | ||
| return forms.reduce( | ||
| (counter, form) => | ||
| form.tele.climb.level === maxLevel ? counter + INCREMENT : counter, | ||
| INITIAL_COUNTER_VALUE, | ||
| ); | ||
| }; | ||
|
|
||
| const findTimesClimbedInAuto = (forms: ScoutingForm[]) => { | ||
| return forms.reduce( | ||
| (counter, form) => | ||
| form.auto.climb.level === "L1" ? counter + INCREMENT : counter, | ||
| INITIAL_COUNTER_VALUE, | ||
| ); | ||
karnishein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| const timesClimedToLevel = ( | ||
| level: TeleClimbLevel, | ||
| climbedLevels: TeleClimbLevel[], | ||
| ) => { | ||
| return climbedLevels.filter((currentLevel) => currentLevel === level).length; | ||
| }; | ||
karnishein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const findTimesClimbedToLevels = (forms: ScoutingForm[]) => { | ||
|
Contributor
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. Consider declaring an interface that the function will return |
||
| const climbedLevels = forms.map((form) => form.tele.climb.level); | ||
| return { | ||
| L1: timesClimedToLevel("L1", climbedLevels), | ||
|
Contributor
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. Same here, consider using a type consisting the possible climbing levels |
||
| L2: timesClimedToLevel("L2", climbedLevels), | ||
| L3: timesClimedToLevel("L3", climbedLevels), | ||
| }; | ||
| }; | ||
|
|
||
| compareRouter.get("/teams", async (req, res) => { | ||
| await pipe( | ||
| getFormsCollection(), | ||
| flatMap((collection) => | ||
| tryCatch( | ||
| () => | ||
| collection | ||
| .find(mongofyQuery({ "match.type": "qualification" })) | ||
| .toArray(), | ||
| (error) => ({ | ||
| status: StatusCodes.INTERNAL_SERVER_ERROR, | ||
| reason: `DB Error: ${error}`, | ||
| }), | ||
| ), | ||
| ), | ||
| map((forms) => forms.map((form) => form.teamNumber)), | ||
| fold( | ||
| (error) => () => | ||
| Promise.resolve(res.status(error.status).send(error.reason)), | ||
| (teamNumbers) => () => | ||
| Promise.resolve(res.status(StatusCodes.OK).json({ teamNumbers })), | ||
| ), | ||
| )(); | ||
| }); | ||
karnishein marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+92
to
+115
Contributor
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. Consider putting it in a function |
||
|
|
||
| compareRouter.get("/", async (req, res) => { | ||
| await pipe( | ||
| getFormsCollection(), | ||
| flatMap((collection) => | ||
| tryCatch( | ||
| () => collection.find(mongofyQuery(req.query)).toArray(), | ||
| (error) => ({ | ||
| status: StatusCodes.INTERNAL_SERVER_ERROR, | ||
| reason: `DB Error: ${error}`, | ||
| }), | ||
| ), | ||
| ), | ||
| flatMap((forms) => { | ||
| if (isEmpty(forms)) return right(forms); | ||
|
|
||
| const firstTeam = firstElement(forms).teamNumber; | ||
| const isSameTeam = forms.every((f) => f.teamNumber === firstTeam); | ||
|
|
||
| return isSameTeam | ||
| ? right(forms) | ||
| : left({ | ||
| status: StatusCodes.BAD_REQUEST, | ||
| reason: | ||
| "Compare Two Validation Error: Forms contain data from multiple different teams.", | ||
| }); | ||
| }), | ||
|
Contributor
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. is this really needed? find out if this is redundant and if not, consider type checking the query instead or maybe even moving to a body. |
||
| map((teamForms) => ({ | ||
| teamNumber: firstElement(teamForms).teamNumber, | ||
| averageFuelInGame: calculateAverageScoredFuel(teamForms, "fullGame"), | ||
| averageFuelInAuto: calculateAverageScoredFuel(teamForms, "auto"), | ||
| maxClimbLevel: findMaxClimbLevel(teamForms), | ||
| timesClimbedToMax: findTimesClimbedToMax( | ||
| teamForms, | ||
| findMaxClimbLevel(teamForms), | ||
| ), | ||
| timesClimbedInAuto: findTimesClimbedInAuto(teamForms), | ||
| timesClimbedToLevels: findTimesClimbedToLevels(teamForms), | ||
karnishein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| })), | ||
|
|
||
| fold( | ||
| (error) => () => | ||
| Promise.resolve(res.status(error.status).send(error.reason)), | ||
| (teamCompareData) => () => | ||
| Promise.resolve(res.status(StatusCodes.OK).json({ teamCompareData })), | ||
| ), | ||
| )(); | ||
| }); | ||
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.
remove before merging