Skip to content

Commit

Permalink
Create NetworkSpeedsPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMartinDev committed Nov 23, 2021
1 parent 21de1f1 commit b9c851c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
4 changes: 2 additions & 2 deletions client/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';

import { createTheme, CssBaseline, ThemeProvider, Typography, useMediaQuery } from '@mui/material';
import { Box } from '@mui/system';
import ChartsPage from './pages/Charts';
import PanelsPage from './pages/Panels';
import { getStats } from './api';

export function App() {
Expand Down Expand Up @@ -30,7 +30,7 @@ export function App() {
<React.StrictMode>
<ThemeProvider theme={theme}>
<CssBaseline />
<ChartsPage stats={stats} />
<PanelsPage stats={stats} />
</ThemeProvider>
</React.StrictMode>
);
Expand Down
3 changes: 3 additions & 0 deletions client/modules/panels/Chart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Chart({ children }) {

}
41 changes: 41 additions & 0 deletions client/modules/panels/NetworkSpeedsPanel.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Line } from "react-chartjs-2";
import Panel from "./Panel";

export default function NetworkSpeedsPanel({ stats }) {
const statsByDate = _.orderBy(stats, 'asc');
const metricsNames = ['download', 'upload'];

const dates = _.map(statsByDate, 'date');

const download = {
label: 'Download',
data: _.map(statsByDate, 'metrics.download'),
fill: false,
borderColor: '#4caf50',
tension: 0.1,
};

const upload = {
label: 'Upload',
data: _.map(statsByDate, 'metrics.upload'),
fill: false,
borderColor: '#f50057',
tension: 0.1,
};

const data = {
labels: dates,
datasets: [
download,
upload,
],
};

return (
<Panel title="Speeds">
<Line
data={data}
/>
</Panel>
);
}
32 changes: 32 additions & 0 deletions client/modules/panels/Panel.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { AppBar, Card, CardContent, Divider, Toolbar, Typography } from "@mui/material";
import { Box } from "@mui/system";

export default function Panel({ title, children }) {
return (
<Card variant="outlined">
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
<Box
sx={{
display: 'flex',
borderBottom: (theme) => `1px solid ${theme.palette.divider}`,
bgcolor: 'background.paper',
color: 'text.secondary',
'& svg': {
m: 1.5,
},
'& hr': {
mx: 0.5,
},
paddingX: (theme) => theme.spacing(2),
paddingY: (theme) => theme.spacing(1),
}}
>
<Typography>{title}</Typography>
</Box>
<CardContent sx={{ flex: '1 0 auto' }}>
{children}
</CardContent>
</Box>
</Card>
);
}
28 changes: 0 additions & 28 deletions client/pages/Charts.jsx

This file was deleted.

13 changes: 13 additions & 0 deletions client/pages/Panels.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Line } from 'react-chartjs-2';
import _ from 'lodash';

import { Container } from '@mui/material';
import NetworkSpeedChart from '../modules/panels/NetworkSpeedsPanel';

export default function PanelsPage({ stats }) {
return (
<Container>
<NetworkSpeedChart stats={stats} />
</Container>
);
}
8 changes: 5 additions & 3 deletions src/modules/stats/networkStats.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ const _ = require('lodash');

const formatOneNetworkStat = ({ id, download, upload, ping, created_at }) => ({
id,
download,
upload,
ping,
metrics: {
download,
upload,
ping,
},
date: created_at,
});

Expand Down
20 changes: 10 additions & 10 deletions src/modules/stats/stats.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ const { monitorNetworkStats, getStats } = require('./stats.usecases');
module.exports = async (instance) => {
const networkStatsRepository = getNetworkStatsRepository({ knex: instance.knex });

monitorNetworkStats({
options: {
interval: config.get('monitoring.interval'),
},
onData: (data) => {
instance.log.debug(data, 'New network stats');
networkStatsRepository.insertOne({ data });
},
onWarning: instance.log.warn,
});
// monitorNetworkStats({
// options: {
// interval: config.get('monitoring.interval'),
// },
// onData: (data) => {
// instance.log.debug(data, 'New network stats');
// networkStatsRepository.insertOne({ data });
// },
// onWarning: instance.log.warn,
// });

instance.route(getStatsRoute({ instance, networkStatsRepository }));
};
Expand Down

0 comments on commit b9c851c

Please sign in to comment.