Skip to content

Commit

Permalink
course stats tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mluukkai committed Sep 15, 2024
1 parent 08389e1 commit e8e677c
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 8 deletions.
38 changes: 36 additions & 2 deletions src/client/components/Admin/Statistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import {
import useStatistics from '../../hooks/useStatistics'
import { Statistic } from '../../types'
import programme from '../../locales/programme.json'
import faculties from '../../locales/faculties.json'

const Statistics = () => {
const [from, setFrom] = useState(1)
const [to, setTo] = useState(3)
const [to, setTo] = useState(4)
const [selectedFaculty, setFaculties] = useState('H00')
const { statistics, isLoading } = useStatistics()
const { t, i18n } = useTranslation()
const { language } = i18n
Expand All @@ -42,12 +44,23 @@ const Statistics = () => {
)

const byUsage = (a, b) => b.usedTokens - a.usedTokens

const termWithin = (stat: Statistic) => {
const terms = stat.terms.map((tr) => tr.id)
return selectedTerms.some((term) => terms.includes(term))
}

const statsToShow = statistics.data.filter(termWithin).sort(byUsage)
const belongsToFaculty = (stat: Statistic) => {
if (selectedFaculty === 'H00') return true
return stat.programmes.some((p) =>
p.startsWith(selectedFaculty.substring(1))
)
}

const statsToShow = statistics.data
.filter(termWithin)
.filter(belongsToFaculty)
.sort(byUsage)

return (
<div>
Expand Down Expand Up @@ -79,6 +92,19 @@ const Statistics = () => {
</MenuItem>
))}
</Select>

<span style={{ margin: 10 }}>{t('admin:showing')}</span>

<Select
value={selectedFaculty}
onChange={(e) => setFaculties(e.target.value as string)}
>
{faculties.map((f) => (
<MenuItem key={f.code} value={f.code}>
{f.name[language]}
</MenuItem>
))}
</Select>
</div>

<TableContainer component={Paper}>
Expand Down Expand Up @@ -115,6 +141,11 @@ const Statistics = () => {
<b>{t('admin:usageCount')}</b>
</Typography>
</TableCell>
<TableCell align="left">
<Typography variant="h6">
<b>{t('admin:promptCount')}</b>
</Typography>
</TableCell>
</TableRow>
</TableHead>
<TableBody>
Expand Down Expand Up @@ -142,6 +173,9 @@ const Statistics = () => {
<TableCell align="left">
<Typography>{chat.usedTokens}</Typography>
</TableCell>
<TableCell align="left">
<Typography>{chat.promptCount}</Typography>
</TableCell>
</TableRow>
))}
</TableBody>
Expand Down
3 changes: 2 additions & 1 deletion src/client/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@
"noChats": "No open course chats"
},
"admin": {
"showing": "Showing",
"timePeriodStart": "Starting",
"timePeriodStop": "ending",
"courseStats": "Course stats",
"courseTerms": "Terms",
"courseCodes": "Course codes",
"programCodes": "Programmes",
"studentCounts": "Students",
"studentCount": "Students",
"hideTeachers": "Hide teachers",
"showTeachers": "Show teachers",
"showStudentList": "Show students",
Expand Down
117 changes: 117 additions & 0 deletions src/client/locales/faculties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
[
{
"code": "H00",
"name": {
"en": "All faculties",
"fi": "Kaikki tiedekunnat",
"sv": "Alla fakulteten"
}
},
{
"code": "H57",
"name": {
"en": "Faculty of Biological and Environmental Sciences",
"fi": "Bio- ja ympäristötieteellinen tiedekunta",
"sv": "Bio- och miljövetenskapliga fakulteten"
}
},
{
"code": "H90",
"name": {
"en": "Faculty of Veterinary Medicine",
"fi": "Eläinlääketieteellinen tiedekunta",
"sv": "Veterinärmedicinska fakulteten"
}
},
{
"code": "H55",
"name": {
"en": "Faculty of Pharmacy",
"fi": "Farmasian tiedekunta",
"sv": "Farmaceutiska fakulteten"
}
},
{
"code": "H40",
"name": {
"en": "Faculty of Arts",
"fi": "Humanistinen tiedekunta",
"sv": "Humanistiska fakulteten"
}
},
{
"code": "H60",
"name": {
"en": "Faculty of Educational Sciences",
"fi": "Kasvatustieteellinen tiedekunta",
"sv": "Pedagogiska fakulteten"
}
},
{
"code": "H30",
"name": {
"en": "Faculty of Medicine",
"fi": "Lääketieteellinen tiedekunta",
"sv": "Medicinska fakulteten"
}
},
{
"code": "H80",
"name": {
"en": "Faculty of Agriculture and Forestry",
"fi": "Maatalous-metsätieteellinen tiedekunta",
"sv": "Agrikultur-forstvetenskapliga fakulteten"
}
},
{
"code": "H50",
"name": {
"en": "Faculty of Science",
"fi": "Matemaattis-luonnontieteellinen tiedekunta",
"sv": "Matematisk-naturvetenskapliga fakulteten"
}
},
{
"code": "H20",
"name": {
"en": "Faculty of Law",
"fi": "Oikeustieteellinen tiedekunta",
"sv": "Juridiska fakulteten"
}
},
{
"code": "H74",
"name": {
"en": "Swedish School of Social Science",
"fi": "Svenska social- och kommunalhögskolan",
"sv": "Svenska social- och kommunalhögskolan"
},
"parent_id": "hy-org-2024-03-27-1",
"createdAt": "2021-01-12T10:56:28.451Z",
"updatedAt": "2024-09-14T01:00:00.495Z"
},
{
"id": "hy-org-1000000580",
"code": "H10",
"name": {
"en": "Faculty of Theology",
"fi": "Teologinen tiedekunta",
"sv": "Teologiska fakulteten"
},
"parent_id": "hy-org-2024-03-27-5",
"createdAt": "2021-01-12T10:56:28.430Z",
"updatedAt": "2024-09-14T01:00:00.451Z"
},
{
"id": "hy-org-1000000940",
"code": "H70",
"name": {
"en": "Faculty of Social Sciences",
"fi": "Valtiotieteellinen tiedekunta",
"sv": "Statsvetenskapliga fakulteten"
},
"parent_id": "hy-org-2024-03-27-5",
"createdAt": "2021-01-12T10:56:28.451Z",
"updatedAt": "2024-09-14T01:00:00.399Z"
}
]
5 changes: 3 additions & 2 deletions src/client/locales/fi.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@
"invalidFileType": "Tiedostotyyppiä ei tueta"
},
"admin": {
"showing": "Näytetään",
"timePeriodStart": "Alkaen",
"timePeriodStop": "päättyen",
"courseStats": "Kurssitilastot",
"courseTerms": "Lukukaudet",
"courseCodes": "Kurssikoodit",
"programCodes": "Koulutusohjelmat",
"studentCounts": "Opiskelijoita",
"programCodes": "Ohjelmat",
"studentCount": "Opisk",
"hideTeachers": "Piilota opettajat ja vastuuhenkilöt",
"showTeachers": "Näytä opettajat ja vastuuhenkilöt",
"showStudentList": "Näytä opiskelijat",
Expand Down
1 change: 1 addition & 0 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Statistic {
programmes: string[]
students: number
usedTokens: number
promptCount: number
}

export interface StatisticResponse {
Expand Down
18 changes: 15 additions & 3 deletions src/server/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express'
import { Op } from 'sequelize'

import { RequestWithUser } from '../types'
import { ChatInstance, UserChatInstanceUsage, User } from '../db/models'
import { ChatInstance, UserChatInstanceUsage, User, Prompt } from '../db/models'
import { getCourse } from '../util/importer'
import { run as runUpdater } from '../updater'
import InfoText from '../db/models/infotext'
Expand Down Expand Up @@ -45,12 +45,14 @@ adminRouter.post('/chatinstances', async (req, res) => {
return res.status(201).send(newChatInstance)
})

// this function is mostly garbage code
adminRouter.get('/statistics', async (req, res) => {
const yearNow = new Date().getFullYear()

const terms = []
let id = 1

// this is ugly
for (let y = 2023; y <= yearNow + 1; y += 1) {
terms.push({
label: {
Expand Down Expand Up @@ -119,7 +121,7 @@ adminRouter.get('/statistics', async (req, res) => {
}, [])
}

const extractFields = (chatInstance: ChatInstance) => {
const extractFields = (chatInstance: ChatInstance & { prompts: any[] }) => {
const units = chatInstance.courseUnits

const codes = units.map((u) => u.code)
Expand All @@ -137,6 +139,7 @@ adminRouter.get('/statistics', async (req, res) => {
programmes: getUniqueValues(programmes),
students: courses[chatInstance.id].students,
usedTokens: courses[chatInstance.id].usedTokens,
promptCount: chatInstance.prompts.length,
}
}

Expand All @@ -145,7 +148,16 @@ adminRouter.get('/statistics', async (req, res) => {
// eslint-disable-next-line no-restricted-syntax
for (const courseId of Object.keys(courses)) {
// eslint-disable-next-line no-await-in-loop
const chatInstance = await ChatInstance.findByPk(courseId)
const chatInstance = (await ChatInstance.findByPk(courseId, {
include: [
{
model: Prompt,
as: 'prompts',
attributes: ['id'],
},
],
})) as ChatInstance & { prompts: any[] }

datas.push(extractFields(chatInstance))
}

Expand Down

0 comments on commit e8e677c

Please sign in to comment.