Skip to content

Commit

Permalink
[Study programmes] Make types more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Sep 13, 2024
1 parent 66341a1 commit c4daf60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
15 changes: 8 additions & 7 deletions services/backend/src/routes/studyProgramme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getGraduationStatsForStudytrack } from '../services/studyProgramme/stud
import { updateBasicView, updateStudytrackView } from '../services/studyProgramme/studyProgrammeUpdates'
import { getStudyRightsInProgramme } from '../services/studyProgramme/studyRightFinders'
import { getStudyTrackStatsForStudyProgramme } from '../services/studyProgramme/studyTrackStats'
import { Graduated, SpecialGroups, YearType } from '../types'
import logger from '../util/logger'
import { logInfoForGrafana } from '../util/logInfoForGrafana'

Expand All @@ -43,8 +44,8 @@ router.get('/creditstats', async (req: GetCreditStatsRequest, res: Response) =>

interface GetStatsRequest extends Request {
query: {
year_type: string
special_groups: string
year_type: YearType
special_groups: SpecialGroups
combined_programme: string
}
}
Expand Down Expand Up @@ -127,8 +128,8 @@ router.get('/:id/coursestats', async (req: GetCourseStatsRequest, res: Response)

interface GetStudyTrackStatsRequest extends Request {
query: {
graduated: string
special_groups: string
graduated: Graduated
special_groups: SpecialGroups
combined_programme: string
}
}
Expand Down Expand Up @@ -212,9 +213,9 @@ router.get('/:id/update_studytrackview', async (req: GetUpdateViewRequest, res:

interface GetEvaluationStatsRequest extends Request {
query: {
graduated: string
year_type: string
special_groups: string
graduated: Graduated
year_type: YearType
special_groups: SpecialGroups
}
}

Expand Down
28 changes: 15 additions & 13 deletions services/backend/src/services/analyticsService.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import moment from 'moment'

import { facultyCodes, ignoredFacultyCodes } from '../config/organizationConstants'
import { Graduated, SpecialGroups, YearType } from '../types'
import { redisClient } from './redis'

// Only new bachelor, masters and doctoral programmes get their data updated in redis every night, use redis for them
const isUpdatedNewProgramme = (code: string) => code.includes('KH') || code.includes('MH') || /^(T)[0-9]{6}$/.test(code)
const filteredFacultyCodes = facultyCodes.filter(item => !ignoredFacultyCodes.includes(item))
const isFaculty = (code: string) => filteredFacultyCodes.includes(code)

const createRedisKeyForBasicStats = (id: string, yearType: string, specialGroups: string) => {
const createRedisKeyForBasicStats = (id: string, yearType: YearType, specialGroups: SpecialGroups) => {
return `BASIC_STATS_${id}_${yearType}_${specialGroups}`
}
const createRedisKeyForCreditStats = (id: string, yearType: string, specialGroups: string) => {
const createRedisKeyForCreditStats = (id: string, yearType: YearType, specialGroups: SpecialGroups) => {
return `CREDIT_STATS_${id}_${yearType}_${specialGroups}`
}
const createRedisKeyForGraduationStats = (id: string, yearType: string, specialGroups: string) => {
const createRedisKeyForGraduationStats = (id: string, yearType: YearType, specialGroups: SpecialGroups) => {
return `GRADUATION_STATS_${id}_${yearType}_${specialGroups}`
}
const createRedisKeyForStudytrackStats = (id: string, graduated: string, specialGroups: string) => {
const createRedisKeyForStudytrackStats = (id: string, graduated: Graduated, specialGroups: SpecialGroups) => {
return `STUDYTRACK_STATS_${id}_${graduated}_${specialGroups}`
}

export const getBasicStats = async (
id: string,
combinedProgramme: string | null,
yearType: string,
specialGroups: string
yearType: YearType,
specialGroups: SpecialGroups
) => {
if (!isUpdatedNewProgramme(id)) {
return null
Expand All @@ -38,7 +40,7 @@ export const getBasicStats = async (
return JSON.parse(dataFromRedis)
}

export const setBasicStats = async (data, yearType: string, specialGroups: string) => {
export const setBasicStats = async (data, yearType: YearType, specialGroups: SpecialGroups) => {
const { id } = data
const redisKey = createRedisKeyForBasicStats(id, yearType, specialGroups)
const dataToRedis = {
Expand Down Expand Up @@ -94,8 +96,8 @@ export const setCreditStats = async (data, isAcademicYear: boolean, specialGroup
export const getGraduationStats = async (
id: string,
combinedProgramme: string | null,
yearType: string,
specialGroups: string
yearType: YearType,
specialGroups: SpecialGroups
) => {
if (!isUpdatedNewProgramme(id)) {
return null
Expand All @@ -109,7 +111,7 @@ export const getGraduationStats = async (
return JSON.parse(dataFromRedis)
}

export const setGraduationStats = async (data, yearType: string, specialGroups: string) => {
export const setGraduationStats = async (data, yearType: YearType, specialGroups: SpecialGroups) => {
const { id } = data
const redisKey = createRedisKeyForGraduationStats(id, yearType, specialGroups)
const dataToRedis = {
Expand All @@ -130,8 +132,8 @@ export const setGraduationStats = async (data, yearType: string, specialGroups:
export const getStudytrackStats = async (
id: string,
combinedProgramme: string | null,
graduated: string,
specialGroups: string
graduated: Graduated,
specialGroups: SpecialGroups
) => {
if (!isUpdatedNewProgramme(id)) {
return null
Expand All @@ -145,7 +147,7 @@ export const getStudytrackStats = async (
return JSON.parse(dataFromRedis)
}

export const setStudytrackStats = async (data, graduated: string, specialGroups: string) => {
export const setStudytrackStats = async (data, graduated: Graduated, specialGroups: SpecialGroups) => {
const { id } = data
const redisKey = createRedisKeyForStudytrackStats(id, graduated, specialGroups)
const dataToRedis = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { getStudyRightsInProgramme } from './studyRightFinders'
import { getStudyTrackStatsForStudyProgramme } from './studyTrackStats'

export const updateBasicView = async (code: string, combinedProgramme: string) => {
const yearOptions = ['CALENDAR_YEAR', 'ACADEMIC_YEAR']
const specialGroupOptions = ['SPECIAL_INCLUDED', 'SPECIAL_EXCLUDED']
const yearOptions = ['CALENDAR_YEAR', 'ACADEMIC_YEAR'] as const
const specialGroupOptions = ['SPECIAL_INCLUDED', 'SPECIAL_EXCLUDED'] as const

for (const yearType of yearOptions) {
for (const specialGroup of specialGroupOptions) {
Expand Down Expand Up @@ -37,8 +37,8 @@ export const updateBasicView = async (code: string, combinedProgramme: string) =
}

export const updateStudytrackView = async (code: string, combinedProgramme: string) => {
const graduatedOptions = ['GRADUATED_INCLUDED', 'GRADUATED_EXCLUDED']
const specialGroupOptions = ['SPECIAL_INCLUDED', 'SPECIAL_EXCLUDED']
const graduatedOptions = ['GRADUATED_INCLUDED', 'GRADUATED_EXCLUDED'] as const
const specialGroupOptions = ['SPECIAL_INCLUDED', 'SPECIAL_EXCLUDED'] as const
const studyRightsOfProgramme = await getStudyRightsInProgramme(code, false, true)

for (const graduated of graduatedOptions) {
Expand Down

0 comments on commit c4daf60

Please sign in to comment.