Skip to content

Commit

Permalink
[Faculties] Move calculateStats into facultyHelpers to enable hot…
Browse files Browse the repository at this point in the history
… reloading
  • Loading branch information
valtterikantanen committed Nov 5, 2024
1 parent 0e66802 commit 388a605
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 388a605

Please sign in to comment.