|
1 | 1 | import express from 'express'
|
2 | 2 | import { Op } from 'sequelize'
|
3 | 3 |
|
| 4 | +import { sequelize } from '../db/connection' |
| 5 | + |
4 | 6 | import { RequestWithUser } from '../types'
|
5 | 7 | import { ChatInstance, UserChatInstanceUsage, User, Prompt } from '../db/models'
|
6 | 8 | import { getCourse } from '../util/importer'
|
7 | 9 | import { run as runUpdater } from '../updater'
|
8 | 10 | import InfoText from '../db/models/infotext'
|
9 | 11 | import { statsViewerIams } from '../util/config'
|
| 12 | +import { generateTerms } from '../util/util' |
10 | 13 |
|
11 | 14 | const adminRouter = express.Router()
|
12 | 15 |
|
@@ -52,51 +55,31 @@ adminRouter.post('/chatinstances', async (req, res) => {
|
52 | 55 | return res.status(201).send(newChatInstance)
|
53 | 56 | })
|
54 | 57 |
|
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 | +} |
85 | 74 |
|
86 |
| - id += 2 |
87 |
| - } |
| 75 | +// this function is mostly garbage |
| 76 | +adminRouter.get('/statistics', async (req, res) => { |
| 77 | + const terms = generateTerms() |
88 | 78 |
|
89 | 79 | 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() |
99 | 81 |
|
| 82 | + const courses = {} |
100 | 83 | // eslint-disable-next-line no-restricted-syntax
|
101 | 84 | for (const usage of usages) {
|
102 | 85 | if (!courses[usage.chatInstanceId]) {
|
|
0 commit comments