Skip to content

Commit 823b4ba

Browse files
committed
stats betterment
1 parent f1cf493 commit 823b4ba

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

src/server/routes/admin.ts

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import express from 'express'
22
import { Op } from 'sequelize'
33

4+
import { sequelize } from '../db/connection'
5+
46
import { RequestWithUser } from '../types'
57
import { ChatInstance, UserChatInstanceUsage, User, Prompt } from '../db/models'
68
import { getCourse } from '../util/importer'
79
import { run as runUpdater } from '../updater'
810
import InfoText from '../db/models/infotext'
911
import { statsViewerIams } from '../util/config'
12+
import { generateTerms } from '../util/util'
1013

1114
const adminRouter = express.Router()
1215

@@ -52,51 +55,31 @@ adminRouter.post('/chatinstances', async (req, res) => {
5255
return res.status(201).send(newChatInstance)
5356
})
5457

55-
// this function is mostly garbage code
56-
adminRouter.get('/statistics', async (req, res) => {
57-
const yearNow = new Date().getFullYear()
58-
59-
const terms = []
60-
let id = 1
61-
62-
// this is ugly
63-
for (let y = 2023; y <= yearNow + 1; y += 1) {
64-
terms.push({
65-
label: {
66-
en: `spring ${y}`,
67-
fi: `kevät ${y}`,
68-
sv: `vår ${y}`,
69-
},
70-
id,
71-
startDate: `${y}-01-01`,
72-
endDate: `${y}-07-31`,
73-
})
74-
75-
terms.push({
76-
label: {
77-
en: `fall ${y}`,
78-
fi: `syksy ${y}`,
79-
sv: `höst ${y}`,
80-
},
81-
id: id + 1,
82-
startDate: `${y}-08-01`,
83-
endDate: `${y}-12-31`,
84-
})
58+
const getUsages = async () => {
59+
const [usages] = (await sequelize.query(`
60+
SELECT u.*
61+
FROM user_chat_instance_usages u
62+
LEFT JOIN responsibilities r
63+
ON u.user_id = r.user_id AND u.chat_instance_id = r.chat_instance_id
64+
WHERE r.user_id IS NULL AND usage_count > 0;
65+
`)) as any[]
66+
67+
return usages.map((usage) => ({
68+
id: usage.id,
69+
userId: usage.user_id,
70+
usageCount: usage.usage_count,
71+
chatInstanceId: usage.chat_instance_id,
72+
}))
73+
}
8574

86-
id += 2
87-
}
75+
// this function is mostly garbage
76+
adminRouter.get('/statistics', async (req, res) => {
77+
const terms = generateTerms()
8878

8979
const mangelStats = async () => {
90-
const courses = {}
91-
92-
const usages = await UserChatInstanceUsage.findAll({
93-
where: {
94-
usageCount: {
95-
[Op.gt]: 0,
96-
},
97-
},
98-
})
80+
const usages = await getUsages()
9981

82+
const courses = {}
10083
// eslint-disable-next-line no-restricted-syntax
10184
for (const usage of usages) {
10285
if (!courses[usage.chatInstanceId]) {

src/server/util/util.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,39 @@ export const getModelContextLimit = (modelName: string) => {
4848
export const sleep =
4949
// eslint-disable-next-line no-promise-executor-return
5050
(ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
51+
52+
export const generateTerms = () => {
53+
const yearNow = new Date().getFullYear()
54+
55+
const terms = []
56+
let id = 1
57+
58+
// this is ugly
59+
for (let y = 2023; y <= yearNow + 1; y += 1) {
60+
terms.push({
61+
label: {
62+
en: `spring ${y}`,
63+
fi: `kevät ${y}`,
64+
sv: `vår ${y}`,
65+
},
66+
id,
67+
startDate: `${y}-01-01`,
68+
endDate: `${y}-07-31`,
69+
})
70+
71+
terms.push({
72+
label: {
73+
en: `fall ${y}`,
74+
fi: `syksy ${y}`,
75+
sv: `höst ${y}`,
76+
},
77+
id: id + 1,
78+
startDate: `${y}-08-01`,
79+
endDate: `${y}-12-31`,
80+
})
81+
82+
id += 2
83+
}
84+
85+
return terms
86+
}

0 commit comments

Comments
 (0)