From 388a605937df021b2d0b5761e7a861022f4c6a30 Mon Sep 17 00:00:00 2001 From: Valtteri Kantanen Date: Tue, 5 Nov 2024 12:36:33 +0200 Subject: [PATCH] [Faculties] Move `calculateStats` into `facultyHelpers` to enable hot reloading --- .../EvaluationOverview/FacultyProgress.jsx | 3 +- .../EvaluationOverview/ProgrammeView.jsx | 2 +- .../FacultyProgrammeOverview/index.jsx | 64 +------------------ .../FacultyStatistics/facultyHelpers.js | 63 ++++++++++++++++++ .../StudyTrackOverview/index.jsx | 2 +- 5 files changed, 67 insertions(+), 67 deletions(-) diff --git a/services/frontend/src/components/EvaluationOverview/FacultyProgress.jsx b/services/frontend/src/components/EvaluationOverview/FacultyProgress.jsx index 0660b6322f..4e1a77c25b 100644 --- a/services/frontend/src/components/EvaluationOverview/FacultyProgress.jsx +++ b/services/frontend/src/components/EvaluationOverview/FacultyProgress.jsx @@ -1,7 +1,6 @@ import { Message } from 'semantic-ui-react' -import { sortProgrammeKeys } from '@/components/FacultyStatistics/facultyHelpers' -import { calculateStats } from '@/components/FacultyStatistics/FacultyProgrammeOverview' +import { calculateStats, sortProgrammeKeys } from '@/components/FacultyStatistics/facultyHelpers' import { FacultyBarChart } from '@/components/FacultyStatistics/FacultyProgrammeOverview/FacultyBarChart' import { FacultyProgressTable } from '@/components/FacultyStatistics/FacultyProgrammeOverview/FacultyProgressTable' diff --git a/services/frontend/src/components/EvaluationOverview/ProgrammeView.jsx b/services/frontend/src/components/EvaluationOverview/ProgrammeView.jsx index f2fde2e860..a2279b8f71 100644 --- a/services/frontend/src/components/EvaluationOverview/ProgrammeView.jsx +++ b/services/frontend/src/components/EvaluationOverview/ProgrammeView.jsx @@ -3,7 +3,7 @@ import { Divider, Header, Loader, Message, Segment } from 'semantic-ui-react' import { getTargetCreditsForProgramme } from '@/common' import { studyProgrammeToolTips } from '@/common/InfoToolTips' -import { calculateStats } from '@/components/FacultyStatistics/FacultyProgrammeOverview' +import { calculateStats } from '@/components/FacultyStatistics/facultyHelpers' import { InfoBox } from '@/components/InfoBox' import { useLanguage } from '@/components/LanguagePicker/useLanguage' import { DataTable } from '@/components/StudyProgramme/BasicOverview/DataTable' diff --git a/services/frontend/src/components/FacultyStatistics/FacultyProgrammeOverview/index.jsx b/services/frontend/src/components/FacultyStatistics/FacultyProgrammeOverview/index.jsx index 8a0088f0e9..5fea95b0ac 100644 --- a/services/frontend/src/components/FacultyStatistics/FacultyProgrammeOverview/index.jsx +++ b/services/frontend/src/components/FacultyStatistics/FacultyProgrammeOverview/index.jsx @@ -1,8 +1,7 @@ import { Button, Divider, Loader, Message, Popup } from 'semantic-ui-react' -import { getCreditCategories } from '@/common' import { facultyToolTips } from '@/common/InfoToolTips' -import { sortProgrammeKeys } from '@/components/FacultyStatistics/facultyHelpers' +import { calculateStats, sortProgrammeKeys } from '@/components/FacultyStatistics/facultyHelpers' import { downloadProgressTable, downloadStudentTable } from '@/components/FacultyStatistics/xlsxFileDownloadHelper' import '@/components/FacultyStatistics/faculty.css' import { InfoBox } from '@/components/InfoBox' @@ -34,67 +33,6 @@ const getKey = (programmeKeys, index) => { return 'MH' } -const isBetween = (number, lowerLimit, upperLimit) => { - return (lowerLimit === undefined || number >= lowerLimit) && (upperLimit === undefined || number < upperLimit) -} - -export const calculateStats = ( - creditCounts, - maximumAmountOfCredits, - minimumAmountOfCredits = 0, - numberOfCreditCategories = 7 -) => { - const tableStats = [] - if (creditCounts === undefined) return null - - if (Object.keys(creditCounts).length === 0) return null - - const limits = getCreditCategories( - true, - 'academic-year', - maximumAmountOfCredits, - Object.keys(creditCounts), - numberOfCreditCategories - 1, - minimumAmountOfCredits - ) - const tableTitles = ['', 'All'] - for (let i = 0; i < limits.length; i++) { - if (limits[i][0] === undefined) tableTitles.push(`< ${limits[i][1]} credits`) - else if (limits[i][1] === undefined) tableTitles.push(`≥ ${limits[i][0]} credits`) - else tableTitles.push(`${limits[i][0]}–${limits[i][1]} credits`) - } - - Object.keys(creditCounts).forEach(year => { - const yearCreditCount = creditCounts[year] - const yearCounts = [year, yearCreditCount.length] - tableStats.push(yearCounts) - for (let i = 0; i < limits.length; i++) { - yearCounts.push(yearCreditCount.filter(credits => isBetween(credits, limits[i][0], limits[i][1])).length) - } - }) - - const totalCounts = ['Total'] - for (let i = 1; i < tableStats[0].length; i++) { - let columnSum = 0 - for (let j = 0; j < tableStats.length; j++) { - columnSum += tableStats[j][i] - } - totalCounts.push(columnSum) - } - tableStats.push(totalCounts) - - // Calculate statistics for the bar chart (i.e., transpose the tableStats as rows are now columns and vice versa) - const chartStats = [] - for (let i = 2; i < tableStats[0].length; i++) { - const column = [] - for (let j = tableStats.length - 1; j >= 0; j--) { - column.push(tableStats[j][i]) - } - chartStats.push({ name: tableTitles[i].replace('<', 'Less than').replace('≥', 'At least'), data: column }) - } - return { tableStats, chartStats, tableTitles } -} - export const FacultyProgrammeOverview = ({ faculty, graduatedGroup, diff --git a/services/frontend/src/components/FacultyStatistics/facultyHelpers.js b/services/frontend/src/components/FacultyStatistics/facultyHelpers.js index e0f5bf4872..0f77084a19 100644 --- a/services/frontend/src/components/FacultyStatistics/facultyHelpers.js +++ b/services/frontend/src/components/FacultyStatistics/facultyHelpers.js @@ -1,3 +1,5 @@ +import { getCreditCategories } from '@/common' + /* Order of the programme keys (such as TKT, PSYK) is chosen by "old" code: KH -> MH -> T -> FI -> K- -> Numbers containing letters at end -> Y- -> Number @@ -45,3 +47,64 @@ export const sortProgrammeKeys = (programmeKeys, faculty) => { return programmeKeys } } + +const isBetween = (number, lowerLimit, upperLimit) => { + return (lowerLimit === undefined || number >= lowerLimit) && (upperLimit === undefined || number < upperLimit) +} + +export const calculateStats = ( + creditCounts, + maximumAmountOfCredits, + minimumAmountOfCredits = 0, + numberOfCreditCategories = 7 +) => { + const tableStats = [] + if (creditCounts === undefined) return null + + if (Object.keys(creditCounts).length === 0) return null + + const limits = getCreditCategories( + true, + 'academic-year', + maximumAmountOfCredits, + Object.keys(creditCounts), + numberOfCreditCategories - 1, + minimumAmountOfCredits + ) + const tableTitles = ['', 'All'] + for (let i = 0; i < limits.length; i++) { + if (limits[i][0] === undefined) tableTitles.push(`< ${limits[i][1]} credits`) + else if (limits[i][1] === undefined) tableTitles.push(`≥ ${limits[i][0]} credits`) + else tableTitles.push(`${limits[i][0]}–${limits[i][1]} credits`) + } + + Object.keys(creditCounts).forEach(year => { + const yearCreditCount = creditCounts[year] + const yearCounts = [year, yearCreditCount.length] + tableStats.push(yearCounts) + for (let i = 0; i < limits.length; i++) { + yearCounts.push(yearCreditCount.filter(credits => isBetween(credits, limits[i][0], limits[i][1])).length) + } + }) + + const totalCounts = ['Total'] + for (let i = 1; i < tableStats[0].length; i++) { + let columnSum = 0 + for (let j = 0; j < tableStats.length; j++) { + columnSum += tableStats[j][i] + } + totalCounts.push(columnSum) + } + tableStats.push(totalCounts) + + // Calculate statistics for the bar chart (i.e., transpose the tableStats as rows are now columns and vice versa) + const chartStats = [] + for (let i = 2; i < tableStats[0].length; i++) { + const column = [] + for (let j = tableStats.length - 1; j >= 0; j--) { + column.push(tableStats[j][i]) + } + chartStats.push({ name: tableTitles[i].replace('<', 'Less than').replace('≥', 'At least'), data: column }) + } + return { tableStats, chartStats, tableTitles } +} diff --git a/services/frontend/src/components/StudyProgramme/StudyTrackOverview/index.jsx b/services/frontend/src/components/StudyProgramme/StudyTrackOverview/index.jsx index 70a9f86735..2005fedad0 100644 --- a/services/frontend/src/components/StudyProgramme/StudyTrackOverview/index.jsx +++ b/services/frontend/src/components/StudyProgramme/StudyTrackOverview/index.jsx @@ -3,7 +3,7 @@ import { Divider, Loader, Message } from 'semantic-ui-react' import { getGraduationGraphTitle, getTargetCreditsForProgramme } from '@/common' import { studyProgrammeToolTips } from '@/common/InfoToolTips' -import { calculateStats } from '@/components/FacultyStatistics/FacultyProgrammeOverview' +import { calculateStats } from '@/components/FacultyStatistics/facultyHelpers' import { GraduationTimes } from '@/components/FacultyStatistics/TimesAndPaths/GraduationTimes' import { InfoBox } from '@/components/InfoBox' import { BreakdownBarChart } from '@/components/StudyProgramme/BreakdownBarChart'