Skip to content

Commit ae93ecb

Browse files
added dowload xlsx file
1 parent 76aa555 commit ae93ecb

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

package-lock.json

Lines changed: 104 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"winston": "^3.11.0",
114114
"winston-gelf-transporter": "^1.0.2",
115115
"winston-loki": "^6.1.3",
116+
"xlsx": "^0.18.5",
116117
"zod": "3.25.61"
117118
}
118119
}

src/client/components/Statistics.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ import {
1515
Tooltip,
1616
Link,
1717
Container,
18+
IconButton,
19+
Stack,
1820
} from '@mui/material'
1921
import { Link as RouterLink } from 'react-router-dom'
2022
import useStatistics from '../hooks/useStatistics'
2123
import { Statistic } from '../types'
2224
import programme from '../locales/programme.json'
2325
import faculties from '../locales/faculties.json'
2426
import useCurrentUser from '../hooks/useCurrentUser'
27+
import * as xlsx from 'xlsx'
28+
import { BlueButton, GrayButton } from './ChatV2/generics/Buttons'
29+
import CloudDownloadIcon from '@mui/icons-material/CloudDownload';
2530

2631
const Statistics = () => {
2732
const [from, setFrom] = useState(1)
@@ -57,10 +62,34 @@ const Statistics = () => {
5762

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

65+
const exportToExcel = (jsonData: any) => {
66+
const book = xlsx.utils.book_new()
67+
const sheet = xlsx.utils.json_to_sheet(jsonData)
68+
xlsx.utils.book_append_sheet(book, sheet, 'Tilastot')
69+
xlsx.writeFile(book, 'statistics.xlsx')
70+
}
71+
72+
const handleXLSX = () => {
73+
const mangledStatistics = statsToShow.map((chat) => {
74+
return (
75+
{
76+
Codes: chat.codes.join(', '),
77+
Course: chat.name[language],
78+
Terms: chat.terms.map(trm => trm.label[language]).join(', '),
79+
Programmes: namesOf(chat.programmes),
80+
Students: chat.students,
81+
UsedTokens: chat.usedTokens,
82+
PromptCount: chat.promptCount
83+
84+
})})
85+
exportToExcel(mangledStatistics)
86+
}
87+
6088
return (
6189
<Container sx={{ mt: '4rem', mb: '10rem' }} maxWidth="xl">
6290
<Box my={2}>
63-
<div>
91+
<Stack direction='row'>
92+
<div>
6493
<span style={{ marginRight: 10 }}>{t('stats:timePeriodStart')}</span>
6594

6695
<Select value={from} onChange={(e) => setFrom(parseInt(e.target.value as string, 10))}>
@@ -91,7 +120,9 @@ const Statistics = () => {
91120
</MenuItem>
92121
))}
93122
</Select>
94-
</div>
123+
</div>
124+
<IconButton onClick={() => {handleXLSX()}} sx={{marginLeft: 'auto'}}><CloudDownloadIcon fontSize="large"/></IconButton>
125+
</Stack>
95126

96127
<TableContainer component={Paper}>
97128
<Table>

0 commit comments

Comments
 (0)