-
Notifications
You must be signed in to change notification settings - Fork 11
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
Calculating and Storing Requirement Statistics #866
Open
PabloRaigoza
wants to merge
10
commits into
main
Choose a base branch
from
pablo/fulfillment-stats
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 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ca6a39d
Adding new file in scripts and debugging credentials
PabloRaigoza f11b11e
Implementing the script to determine course frequencies to fulfill given
PabloRaigoza 06a026f
Fixing formatting and linting errors
PabloRaigoza d06218c
Implmenting gen-req-full-stats
PabloRaigoza 6aab6fe
Ran format
PabloRaigoza ccbfee3
Adding documention and removing console.log
PabloRaigoza 7b5ba5c
Ran format
PabloRaigoza 4f326fd
Merge branch 'main' of github.com:cornell-dti/course-plan into pablo/…
PabloRaigoza a8a5630
Cleaning code and replacing backslash with doc id
PabloRaigoza 95acbc1
Resolving all promises before callback
PabloRaigoza 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 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 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 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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { | ||
onboardingDataCollection, | ||
semestersCollection, | ||
toggleableRequirementChoicesCollection, | ||
overriddenFulfillmentChoicesCollection, | ||
courseFulfillmentStats, | ||
} from './firebase-config'; | ||
|
||
import computeGroupedRequirementFulfillmentReports from '../src/requirements/requirement-frontend-computation'; | ||
import computeFulfillmentStats from '../src/requirements/fulfillment-stats'; | ||
import { createAppOnboardingData } from '../src/user-data-converter'; | ||
|
||
import '../src/requirements/decorated-requirements.json'; | ||
|
||
// idRequirementFrequency is a hashmap where the key is the requirement ID and the value is | ||
// an array of maps. Each element in the array represents a slot in the requirement. | ||
// The map is a hashmap where the key is the course ID and the value is the frequency of the course | ||
// in the slot. | ||
let idRequirementFrequency = new Map<string, Map<number, number>[]>(); | ||
/** | ||
* Computes the requirement fulfillment statistics for all users. This is done by iterating through | ||
* all the users and computing the computeGroupedRequirementFulfillmentReports for each user. | ||
* This returns groupedRequirementFulfillmentReport which is then passed to computeFulfillmentStats. | ||
* GroupedRequirementFulfillmentReport is a list of RequirementFulfillmentReport where each | ||
* RequirementFulfillmentReport represents a requirement and a list of courses that fulfill the | ||
* requirement. computeFulfillmentStats then computes the frequency of each course in each slot | ||
* of the requirement and stores it in idRequirementFrequency. | ||
* @param _callback | ||
* @throws Error when computeGroupedRequirementFulfillmentReports fails to compute the fulfillment stats | ||
*/ | ||
async function computeRequirementFullfillmentStatistics(_callback) { | ||
const semQuerySnapshot = await semestersCollection.get(); | ||
semQuerySnapshot.forEach(async doc => { | ||
// obtain the user's semesters, onboarding data, etc... | ||
const semestersAndPlans = await doc.data(); | ||
const onboardingData = (await onboardingDataCollection.doc(doc.id).get()).data(); | ||
const toggleableRequirementChoices = | ||
(await toggleableRequirementChoicesCollection.doc(doc.id).get()).data() ?? {}; | ||
const overriddenFulfillmentChoices = | ||
(await overriddenFulfillmentChoicesCollection.doc(doc.id).get()).data() ?? {}; | ||
|
||
// if the user has obboarding data, semesters, etc... | ||
if ( | ||
onboardingData !== undefined && | ||
semestersAndPlans.semesters !== undefined && | ||
toggleableRequirementChoices !== undefined && | ||
overriddenFulfillmentChoices !== undefined | ||
PabloRaigoza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
// Attempt to compute the fulfillment stats for the user | ||
try { | ||
// use createAppOnboardingData to convert the onboarding data to the format used by the frontend | ||
const newOnboardingData = await createAppOnboardingData(onboardingData); | ||
|
||
// compute the fulfillment stats | ||
const res = await computeGroupedRequirementFulfillmentReports( | ||
semestersAndPlans.semesters, | ||
newOnboardingData, | ||
toggleableRequirementChoices, | ||
overriddenFulfillmentChoices | ||
); | ||
|
||
idRequirementFrequency = await computeFulfillmentStats( | ||
res.groupedRequirementFulfillmentReport, | ||
idRequirementFrequency | ||
); | ||
} catch { | ||
// There was an error computing the fulfillment stats for the user | ||
} | ||
} | ||
}); | ||
|
||
setTimeout(_callback, 120 * 1000); // wait 2 minutes before storing the computed stats | ||
PabloRaigoza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Stores the computed requirement fulfillment statistics in firestore. This is done by iterating | ||
* through all the keys in the idRequirementFrequency hashmap and storing the fulfillment stats | ||
* for each requirement in firestore. We have to keep only the top fifty courses for each slot | ||
* to reduce the size of the data stored in firestore. This is done by sorting the hashmap by | ||
* frequency and keeping only the top fifty courses for each slot. | ||
* @throws Error when courseFulfillmentStats.doc().set(data) fails to store the data in firestore | ||
*/ | ||
async function storeComputedRequirementFullfillmentStatistics() { | ||
// Change the hashmap to only keep the top fifty courses for each slot | ||
|
||
for (const [reqID, slots] of idRequirementFrequency) { | ||
const newSlots: Map<number, number>[] = []; | ||
for (const slot of slots) { | ||
const newSlot = new Map<number, number>(); | ||
const sorted = [...slot.entries()].sort((a, b) => b[1] - a[1]); | ||
|
||
for (let i = 0; i < 50; i += 1) { | ||
const [course, freq] = sorted[i]; | ||
newSlot.set(course, freq); | ||
} | ||
|
||
newSlots.push(newSlot); | ||
} | ||
idRequirementFrequency.set(reqID, newSlots); | ||
} | ||
|
||
// Storing fulfillment stats in firestore by iterating through the hashmap | ||
for (const [reqID, slots] of idRequirementFrequency) { | ||
const json = {}; | ||
|
||
let i = 0; | ||
for (const slot of slots) { | ||
const tempJson = {}; | ||
for (const [course, freq] of slot) { | ||
tempJson[course] = freq; | ||
} | ||
json[i] = tempJson; | ||
i += 1; | ||
} | ||
PabloRaigoza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const data = { | ||
reqID, | ||
slots: json, | ||
}; | ||
courseFulfillmentStats.doc().set(data); // store the data in firestore | ||
} | ||
} | ||
|
||
// Run the script | ||
computeRequirementFullfillmentStatistics(storeComputedRequirementFullfillmentStatistics); |
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @brief This function computes the frequency of courses taken to fulfill a requirement | ||
* | ||
* @param groups A list of requirement groups containing the courses taken to fulfill the requirements | ||
* @param idRequirementFrequency A hashmap of requirement id to a list of frequency maps | ||
* @returns A hashmap of requirement id to a list of frequency maps | ||
* | ||
* @details | ||
* This function computes the frequency of courses taken to fulfill a requirement. | ||
* The hashmap is of the form: requirement id -> list of frequency maps | ||
* The list of frequency maps is of the form: slot number -> course id -> frequency | ||
* | ||
* @note | ||
* The hashmap is passed in as a parameter to avoid creating a new hashmap every time this function is called. | ||
* This function is called multiple times in the main algorithm. | ||
* | ||
* @warning | ||
* This function assumes that the hashmap passed in is empty. | ||
* | ||
* @bug | ||
* None known | ||
* | ||
* @todo | ||
* None | ||
*/ | ||
export default function computeFulfillmentStats( | ||
groups: readonly GroupedRequirementFulfillmentReport[], | ||
idRequirementFrequency: Map<string, Map<number, number>[]> | ||
) { | ||
// Iterate over all groups | ||
const res = idRequirementFrequency; | ||
PabloRaigoza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
groups.forEach(currentGroup => { | ||
// Iterate over all requirements in the group | ||
const { reqs } = currentGroup; | ||
reqs.forEach(reqFulfillment => { | ||
// Obtain the requirement ID and the list of courses taken to fulfill the requirement | ||
const key: string = reqFulfillment.requirement.id; | ||
const { safeCourses } = reqFulfillment.fulfillment; | ||
|
||
// Obtain the frequency list for this particular group's requirements | ||
const freqList = res.get(key) ?? []; | ||
|
||
// Iterate over all slots in the requirement group | ||
// console.log(safeCourses.length); | ||
for (let slotNumber = 0; slotNumber < safeCourses.length; slotNumber += 1) { | ||
if (freqList.length === slotNumber) { | ||
freqList.push(new Map<number, number>()); | ||
PabloRaigoza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
const currentCourseSlot = safeCourses[slotNumber]; | ||
const currentRequirementSlotFreq = freqList[slotNumber]; | ||
|
||
// Iterate over all courses taken to fulfill the req-slot | ||
for (let j = 0; j < currentCourseSlot.length; j += 1) { | ||
const currentCourseId = currentCourseSlot[j].courseId; | ||
const pastFreq = currentRequirementSlotFreq.get(currentCourseId) ?? 0; | ||
currentRequirementSlotFreq.set(currentCourseId, pastFreq + 1); | ||
} | ||
Comment on lines
+42
to
+46
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. Can you convert this into a for each loop? |
||
freqList[slotNumber] = currentRequirementSlotFreq; // Update the frequency list | ||
} | ||
res.set(key, freqList); // Update the hashmap with the new frequency list | ||
}); | ||
}); | ||
|
||
return res; // return the hashmap | ||
} |
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.
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.
Why do we need this
_callback
argument?