-
Notifications
You must be signed in to change notification settings - Fork 0
Jan mongo button func #143
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
Open
NelsonDong41
wants to merge
8
commits into
main
Choose a base branch
from
JAN-MongoButtonFunc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e6fb85e
added endpoint for grouped hacker data
Mandy-cyber f5d6fc9
using Next getStaticProps to pass in environment variables
233cee1
Kept copy to clipboard button available at all times
e0829e8
removed dotenv for vercel deployment
e616d81
resolving comments from review!
Mandy-cyber 367a251
added id to destructuring
Mandy-cyber f1a48f9
adding return type to createSortedHacker
Mandy-cyber 3b24ee6
small changes from review
Mandy-cyber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 128 additions & 1 deletion
129
backend/cabin-sorting/data/json_outputs/sortedHackers.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,128 @@ | ||
| [{"question0":"answer1","question1":"answer1","question2":"answer1","question3":"answer1","question4":"answer1","question5":"answer1","question6":"answer1","question7":"answer1","question8":"answer1","question9":"answer1","question10":"answer1","question11":"answer1","id":"64accf3dd36486421bc50e53","email":"test","assignedCabin":"cabin1","secondAssignedCabin":" cabin2"},{"question0":"answer2","question1":"answer2","question2":"answer2","question3":"answer2","question4":"answer2","question5":"answer2","question6":"answer2","question7":"answer2","question8":"answer2","question9":"answer2","question10":"answer2","question11":"answer2","id":"64accf66d36486421bc50e55","email":"test2","assignedCabin":" cabin2","secondAssignedCabin":"cabin1"},{"question0":"answer3","question1":"answer3","question2":"answer3","question3":"answer3","question4":"answer3","question5":"answer3","question6":"answer3","question7":"answer3","question8":"answer3","question9":"answer3","question10":"answer3","question11":"answer3","id":"64accf7fd36486421bc50e57","email":"test3","assignedCabin":" cabin3","secondAssignedCabin":"cabin1"},{"question0":"answer4","question1":"answer4","question2":"answer4","question3":"answer4","question4":"answer4","question5":"answer4","question6":"answer4","question7":"answer4","question8":"answer4","question9":"answer4","question10":"answer4","question11":"answer4","id":"64accf8ed36486421bc50e59","email":"test4","assignedCabin":" cabin4","secondAssignedCabin":"cabin1"}] | ||
| [ | ||
| { | ||
| "id": "64accf3dd36486421bc50e53", | ||
| "email": "test", | ||
| "question0": "answer1", | ||
| "question1": "answer1", | ||
| "question2": "answer1", | ||
| "question3": "answer1", | ||
| "question4": "answer1", | ||
| "question5": "answer1", | ||
| "question6": "answer1", | ||
| "question7": "answer1", | ||
| "question8": "answer1", | ||
| "question9": "answer1", | ||
| "question10": "answer1", | ||
| "question11": "answer1", | ||
| "assignedCabin": "cabin1", | ||
| "secondAssignedCabin": "cabin2" | ||
| }, | ||
| { | ||
| "id": "64accf66d36486421bc50e55", | ||
| "email": "test2", | ||
| "question0": "answer2", | ||
| "question1": "answer2", | ||
| "question2": "answer2", | ||
| "question3": "answer2", | ||
| "question4": "answer2", | ||
| "question5": "answer2", | ||
| "question6": "answer2", | ||
| "question7": "answer2", | ||
| "question8": "answer2", | ||
| "question9": "answer2", | ||
| "question10": "answer2", | ||
| "question11": "answer2", | ||
| "assignedCabin": "cabin2", | ||
| "secondAssignedCabin": "cabin1" | ||
| }, | ||
| { | ||
| "id": "64accf7fd36486421bc50e57", | ||
| "email": "test3", | ||
| "question0": "answer3", | ||
| "question1": "answer3", | ||
| "question2": "answer3", | ||
| "question3": "answer3", | ||
| "question4": "answer3", | ||
| "question5": "answer3", | ||
| "question6": "answer3", | ||
| "question7": "answer3", | ||
| "question8": "answer3", | ||
| "question9": "answer3", | ||
| "question10": "answer3", | ||
| "question11": "answer3", | ||
| "assignedCabin": "cabin3", | ||
| "secondAssignedCabin": "cabin1" | ||
| }, | ||
| { | ||
| "id": "64accf8ed36486421bc50e59", | ||
| "email": "test4", | ||
| "question0": "answer4", | ||
| "question1": "answer4", | ||
| "question2": "answer4", | ||
| "question3": "answer4", | ||
| "question4": "answer4", | ||
| "question5": "answer4", | ||
| "question6": "answer4", | ||
| "question7": "answer4", | ||
| "question8": "answer4", | ||
| "question9": "answer4", | ||
| "question10": "answer4", | ||
| "question11": "answer4", | ||
| "assignedCabin": "cabin4", | ||
| "secondAssignedCabin": "cabin1" | ||
| }, | ||
| { | ||
| "id": "64ae0242cdcfed39b8b59958", | ||
| "email": "test4", | ||
| "question0": "answer4", | ||
| "question1": "answer4", | ||
| "question2": "answer4", | ||
| "question3": "answer4", | ||
| "question4": "answer4", | ||
| "question5": "answer4", | ||
| "question6": "answer4", | ||
| "question7": "answer4", | ||
| "question8": "answer4", | ||
| "question9": "answer4", | ||
| "question10": "answer4", | ||
| "question11": "answer4", | ||
| "assignedCabin": "cabin4", | ||
| "secondAssignedCabin": "cabin1" | ||
| }, | ||
| { | ||
| "id": "64ae024dcdcfed39b8b5995a", | ||
| "email": "test4", | ||
| "question0": "answer4", | ||
| "question1": "answer4", | ||
| "question2": "answer4", | ||
| "question3": "answer4", | ||
| "question4": "answer4", | ||
| "question5": "answer4", | ||
| "question6": "answer4", | ||
| "question7": "answer4", | ||
| "question8": "answer4", | ||
| "question9": "answer4", | ||
| "question10": "answer4", | ||
| "question11": "answer4", | ||
| "assignedCabin": "cabin4", | ||
| "secondAssignedCabin": "cabin1" | ||
| }, | ||
| { | ||
| "id": "64b0a5b67c6e7f3511e8509e", | ||
| "email": "test4", | ||
| "question0": "answer4", | ||
| "question1": "answer4", | ||
| "question2": "answer4", | ||
| "question3": "answer4", | ||
| "question4": "answer4", | ||
| "question5": "answer4", | ||
| "question6": "answer4", | ||
| "question7": "answer4", | ||
| "question8": "answer4", | ||
| "question9": "answer4", | ||
| "question10": "answer4", | ||
| "question11": "answer4", | ||
| "assignedCabin": "cabin4", | ||
| "secondAssignedCabin": "cabin1" | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 160 additions & 24 deletions
184
backend/cabin-sorting/service/sortedHackers-service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,171 @@ | ||
| import { Types } from "mongoose"; | ||
| import * as sortedHackersDao from "../dao/sortedHackers-dao.js"; | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { parse } from 'csv-parse/sync'; | ||
| import { Types } from 'mongoose'; | ||
| import * as sortedHackersDao from '../dao/sortedHackers-dao.js'; | ||
|
|
||
| let hackerList: any[]; | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let answerList: any[]; | ||
| let cabinList: any[]; | ||
| let CABIN_SIZE: number; | ||
| let QUESTIONS_SIZE: number; | ||
Mandy-cyber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| interface HackerDataType { | ||
| id: string, | ||
| email: string, | ||
| [key: string] : string, | ||
| id: string; | ||
| email: string; | ||
| [key: string]: string; | ||
| } | ||
|
|
||
| const getSortedHackers = async () => { | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const rawHackerData = await sortedHackersDao.getSortedHackers(); | ||
| const formattedHackerData = rawHackerData.map(hackerData => { | ||
| const {_id, email, applicationResponses} = hackerData | ||
| const initialHackerData : HackerDataType = { | ||
| id: _id.toString(), | ||
| email: email, | ||
| } | ||
| const rawHackerData = await sortedHackersDao.getSortedHackers(); | ||
| const formattedHackerData = rawHackerData.map((hackerData) => { | ||
| const { _id, email, applicationResponses } = hackerData; | ||
| const initialHackerData: HackerDataType = { | ||
| id: _id.toString(), | ||
| email: email | ||
| }; | ||
|
|
||
| function formatApplicationResponses(accumulatedResponse : HackerDataType, currentResponseKey : any, currentResponseIndex : number) { | ||
| const currentResponseQuestionKey = "question" + currentResponseIndex; | ||
| accumulatedResponse[currentResponseQuestionKey] = applicationResponses[currentResponseKey] | ||
| return accumulatedResponse | ||
| } | ||
| function formatApplicationResponses( | ||
| accumulatedResponse: HackerDataType, | ||
| currentResponseKey: any, | ||
| currentResponseIndex: number | ||
| ) { | ||
| const currentResponseQuestionKey = 'question' + currentResponseIndex; | ||
| accumulatedResponse[currentResponseQuestionKey] = | ||
| applicationResponses[currentResponseKey]; | ||
| return accumulatedResponse; | ||
| } | ||
|
|
||
| return Object.keys(applicationResponses).reduce(formatApplicationResponses, initialHackerData) | ||
| }) | ||
| return formattedHackerData; | ||
| return Object.keys(applicationResponses).reduce( | ||
| formatApplicationResponses, | ||
| initialHackerData | ||
| ); | ||
| }); | ||
| return formattedHackerData; | ||
| }; | ||
|
|
||
| const createSortedHacker = async (hacker : any) => { | ||
| const createResponse = await sortedHackersDao.createdSortedHacker(hacker) | ||
| const createSortedHacker = async (hacker: any) => { | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const createResponse = await sortedHackersDao.createdSortedHacker(hacker); | ||
| return createResponse; | ||
| }; | ||
|
|
||
| export default {getSortedHackers, createSortedHacker}; | ||
|
|
||
| const getGroupedHackers = async () => { | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const hackers = await assignHackerCabins(); | ||
| console.log(hackers) | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let cabinEmails: string[][] = []; | ||
|
|
||
| hackers.forEach((hacker) => { | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| console.log(hacker) | ||
| }) | ||
|
|
||
| const groupedHackers = hackers.reduce((accum, currVal) => { | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const assignedCabin = currVal.assignedCabin; | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const cabinNum = cabinList.indexOf(assignedCabin); | ||
| if (cabinNum === -1) { | ||
| console.error( | ||
| `Cabin assigned to Hacker ${assignedCabin.id} could not be found` | ||
| ); | ||
| return accum; | ||
| } | ||
| const hackerEmail = currVal.email; | ||
| if (!accum[cabinNum]) { | ||
| accum[cabinNum] = [] | ||
| } | ||
| accum[cabinNum].push(hackerEmail); | ||
|
|
||
| console.log(accum) | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return accum; | ||
| }, cabinEmails); | ||
|
|
||
| return groupedHackers | ||
| }; | ||
|
|
||
| function loadCSV(filepath: string, headers: boolean): any[] { | ||
| const csvFileAbsolutePath = path.resolve( | ||
| 'cabin-sorting', | ||
| 'data', | ||
| 'csv_inputs', | ||
| filepath); | ||
| console.log(csvFileAbsolutePath) | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // error handling in case file is missing | ||
| let fileContent; | ||
| try { | ||
| fileContent = fs.readFileSync(csvFileAbsolutePath, { | ||
| encoding: 'utf-8' | ||
| }); | ||
| } catch (err) { | ||
| console.log(`File cannot be found: "${filepath}"`); | ||
| return []; | ||
| } | ||
|
|
||
| const options = { | ||
| delimiter: ',', | ||
| columns: headers | ||
| }; | ||
| return parse(fileContent, options); | ||
| } | ||
|
|
||
| async function assignHackerCabins() { | ||
| hackerList = await getSortedHackers(); | ||
| answerList = loadCSV('answer.csv', true); | ||
| cabinList = loadCSV('cabinTypes.csv', false)[0]; | ||
|
|
||
| CABIN_SIZE = Object.keys(cabinList).length; | ||
| QUESTIONS_SIZE = Object.keys(answerList[0]).length; | ||
|
|
||
| // ensuring the CSV files exists before continuing | ||
| if ( | ||
| hackerList.length === 0 || | ||
| answerList.length === 0 || | ||
| cabinList.length === 0 | ||
| ) { | ||
| console.error( | ||
| 'Please add the respective csv file(s) to the folder to run the sorting algorithm' | ||
| ); | ||
| return []; | ||
| } else { | ||
| matchAnswers(); | ||
| return hackerList; | ||
| } | ||
| } | ||
|
|
||
| function matchAnswers() { | ||
| hackerList.forEach((hacker: any) => { | ||
| // each element = a different cabin, all initialized to 0 | ||
| const cabinScore = Array<number>(CABIN_SIZE).fill(0); | ||
|
|
||
| hydrateCabinScore(hacker, cabinScore); | ||
Mandy-cyber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // create extra column for hacker that determines the cabin they should | ||
| // join (the one with the most points) | ||
| const cabinOptions: string[] = Object.values(cabinList); | ||
| const maxIndex: number = cabinScore.indexOf(Math.max(...cabinScore)); | ||
| hacker.assignedCabin = cabinOptions[maxIndex]; | ||
|
|
||
| // find backup cabin for hacker (in case the first choice fills up) | ||
| const counterCopy = cabinScore.slice(); | ||
| counterCopy[maxIndex] = -1; | ||
| hacker.secondAssignedCabin = | ||
| cabinOptions[counterCopy.indexOf(Math.max(...counterCopy))]; | ||
| }); | ||
| } | ||
|
|
||
| function hydrateCabinScore(hacker: any, cabinScore: number[]) { | ||
| answerList.forEach((cabin: any, cabinIndex: number) => { | ||
| for ( | ||
| let questionIndex = 0; | ||
| questionIndex < QUESTIONS_SIZE; | ||
alyssamui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| questionIndex++ | ||
| ) { | ||
| if ( | ||
| cabin['question' + questionIndex.toString()] === | ||
| hacker['question' + questionIndex.toString()] | ||
| ) { | ||
| cabinScore[cabinIndex]++; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| export default { getSortedHackers, createSortedHacker, getGroupedHackers }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.