@@ -15,13 +15,18 @@ import {
15
15
Tooltip ,
16
16
Link ,
17
17
Container ,
18
+ IconButton ,
19
+ Stack ,
18
20
} from '@mui/material'
19
21
import { Link as RouterLink } from 'react-router-dom'
20
22
import useStatistics from '../hooks/useStatistics'
21
23
import { Statistic } from '../types'
22
24
import programme from '../locales/programme.json'
23
25
import faculties from '../locales/faculties.json'
24
26
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' ;
25
30
26
31
const Statistics = ( ) => {
27
32
const [ from , setFrom ] = useState ( 1 )
@@ -57,10 +62,34 @@ const Statistics = () => {
57
62
58
63
const statsToShow = statistics . data . filter ( termWithin ) . filter ( belongsToFaculty ) . sort ( byUsage )
59
64
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
+
60
88
return (
61
89
< Container sx = { { mt : '4rem' , mb : '10rem' } } maxWidth = "xl" >
62
90
< Box my = { 2 } >
63
- < div >
91
+ < Stack direction = 'row' >
92
+ < div >
64
93
< span style = { { marginRight : 10 } } > { t ( 'stats:timePeriodStart' ) } </ span >
65
94
66
95
< Select value = { from } onChange = { ( e ) => setFrom ( parseInt ( e . target . value as string , 10 ) ) } >
@@ -91,7 +120,9 @@ const Statistics = () => {
91
120
</ MenuItem >
92
121
) ) }
93
122
</ Select >
94
- </ div >
123
+ </ div >
124
+ < IconButton onClick = { ( ) => { handleXLSX ( ) } } sx = { { marginLeft : 'auto' } } > < CloudDownloadIcon fontSize = "large" /> </ IconButton >
125
+ </ Stack >
95
126
96
127
< TableContainer component = { Paper } >
97
128
< Table >
0 commit comments