PageStats.jsx 5.55 KB
Newer Older
1
import React, { useCallback, useState } from "react";
2
3
import { compose } from "recompose";
import Typography from "@material-ui/core/Typography";
4
5
import { Line } from "react-chartjs-2";
import alasql from "alasql";
6
7
8
9
10
11
12
13
14
15
16
import { makeStyles } from "@material-ui/styles";
import TextField from "@material-ui/core/TextField";
import Button from "@material-ui/core/Button";
import Paper from "@material-ui/core/Paper";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableContainer from "@material-ui/core/TableContainer";
import TableHead from "@material-ui/core/TableHead";
import TablePagination from "@material-ui/core/TablePagination";
import TableRow from "@material-ui/core/TableRow";
17
18
import { withPaddedPaper } from "./shared";

19
20
21
22
const data = [
  { a: 1, b: 1, c: 1 },
  { a: 1, b: 2, c: 1 },
  { a: 1, b: 3, c: 1 },
23
  { a: 2, b: 1, c: 1 }
24
25
];

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const useStyles = makeStyles({
  root: {
    width: "100%"
  },
  container: {
    maxHeight: 440
  }
});

function TableFromData({ data }) {
  const classes = useStyles();
  const [page, setPage] = React.useState(0);
  const [rowsPerPage, setRowsPerPage] = React.useState(10);

  if (data.length === 0) {
    return <></>;
  }

  const columns = Object.keys(data[0]).map(id => ({ id, label: id }));

  const handleChangePage = (event, newPage) => {
    setPage(newPage);
  };

  const handleChangeRowsPerPage = event => {
    setRowsPerPage(+event.target.value);
    setPage(0);
  };

  return (
    <Paper className={classes.root}>
      <TableContainer className={classes.container}>
        <Table stickyHeader aria-label="sticky table">
          <TableHead>
            <TableRow>
              {columns.map(column => (
                <TableCell key={column.id}>{column.label}</TableCell>
              ))}
            </TableRow>
          </TableHead>
          <TableBody>
            {data
              .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
              .map((row, i) => {
                return (
                  <TableRow hover role="checkbox" tabIndex={-1} key={i}>
                    {columns.map(column => {
                      const value = row[column.id];
                      return (
                        <TableCell key={column.id} align={column.align}>
                          {column.format && typeof value === "number"
                            ? column.format(value)
                            : value}
                        </TableCell>
                      );
                    })}
                  </TableRow>
                );
              })}
          </TableBody>
        </Table>
      </TableContainer>
      <TablePagination
        component="div"
        count={data.length}
        rowsPerPage={rowsPerPage}
        page={page}
        onChangePage={handleChangePage}
        onChangeRowsPerPage={handleChangeRowsPerPage}
        labelRowsPerPage="Lignes par page"
        backIconButtonText="Page précédente"
        nextIconButtonText="Page suivante"
      />
    </Paper>
  );
}

function executeRequest(sqlRequest, data) {
  return alasql.promise(sqlRequest, [data]);
}
106

107
108
109
110
/**
 * Component corresponding to the stats page of the site
 */
function PageStats() {
111
112
113
114
115
  const classes = useStyles();
  const [sqlRequest, setSqlRequest] = useState("");
  const [requestError, setRequestError] = useState("");
  const [requestResult, setRequestResult] = useState([]);

116
117
118
  return (
    <>
      <Typography variant="h3">Vive les stats</Typography>
119
120
121
122
123
124
125
126
127
128
      <TextField
        id="standard-multiline-static"
        label="Requête d'exploration"
        multiline
        fullWidth
        margin="normal"
        rows={10}
        placeholder="Entrez la requête SQL"
        value={sqlRequest}
        onChange={event => setSqlRequest(event.target.value)}
129
      />
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
      {requestError !== "" && (
        <>
          <br></br>
          <Typography variant="caption">{requestError.toString()}</Typography>
          <br></br>
        </>
      )}
      <Button
        variant="contained"
        color={"primary"}
        className={classes.button}
        onClick={() => {
          setRequestError("");
          setRequestResult([]);
          executeRequest(sqlRequest, data)
            .then(res => {
              setRequestResult(res);
            })
            .catch(err => {
              setRequestError(err);
            });
          console.log(btoa(sqlRequest));
        }}
      >
        Exécuter
      </Button>
      {requestResult.length !== 0 && (
        <>
          <br></br>
          <TableFromData data={requestResult} />
          <br></br>
          <Button
            variant="outlined"
            color={"secondary"}
            onClick={() => {
              alasql(
                'SELECT * INTO CSV("export_rex_dri.csv", {headers:true}) FROM ?',
                [requestResult]
              );
            }}
          >
            Exporter en CSV
          </Button>
        </>
      )}
175
176
177
178
179
180
    </>
  );
}

PageStats.propTypes = {};

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 * SELECT ts as x, nb_connections as y
 *
 * SELECT ts as x, branche as cat, nb_connections as y
 */

// {/* <Line
//   data={{
//     labels: [
//       "January",
//       "February",
//       "March",
//       "April",
//       "May",
//       "June",
//       "July"
//     ],
//     datasets: [
//       {
//         label: "My First dataset",
//         backgroundColor: "rgb(255, 99, 132)",
//         borderColor: "rgb(255, 99, 132)",
//         data: [0, 10, 5, 2, 20, 30, 45]
//       }
//     ]
//   }}
// /> */}

209
export default compose(withPaddedPaper())(PageStats);