Modifications of stats card

This commit is contained in:
bourquecharles
2021-05-31 19:24:19 -04:00
parent 3156d1e671
commit 3d86bf3eff
4 changed files with 95 additions and 35 deletions

View File

@@ -3,7 +3,7 @@ import { CFooter } from '@coreui/react';
const TheFooter = () => ( const TheFooter = () => (
<CFooter fixed={false}> <CFooter fixed={false}>
<div>Version 0.0.5</div> <div>Version 0.0.6</div>
<div className="mfs-auto"> <div className="mfs-auto">
<span className="mr-1">Powered by</span> <span className="mr-1">Powered by</span>
<a href="https://coreui.io/react" target="_blank" rel="noopener noreferrer"> <a href="https://coreui.io/react" target="_blank" rel="noopener noreferrer">

View File

@@ -1,20 +1,50 @@
import React from 'react'; import React, {useState} from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { import {
CCard, CCard,
CCardHeader, CCardHeader,
CCardBody CCardBody,
CPopover,
CRow,
CCol
} from '@coreui/react'; } from '@coreui/react';
import { cilSync } from '@coreui/icons';
import CIcon from '@coreui/icons-react';
import StatisticsChartList from './StatisticsChartList'; import StatisticsChartList from './StatisticsChartList';
const DeviceStatisticsCard = ({ selectedDeviceId }) => ( const DeviceStatisticsCard = ({ selectedDeviceId }) => {
const [lastRefresh, setLastRefresh] = useState('');
const refresh = () => {
setLastRefresh(new Date().toString());
}
return (
<CCard> <CCard>
<CCardHeader>Device Statistics</CCardHeader> <CCardHeader>
<CRow>
<CCol>
Statistics
</CCol>
<CCol style={{textAlign: 'right'}}>
<CPopover content="Refresh">
<CIcon
onClick={() => refresh()}
name="cil-sync"
content={cilSync}
size="lg"
color='primary'
/>
</CPopover>
</CCol>
</CRow>
</CCardHeader>
<CCardBody style={{padding: '5%'}}> <CCardBody style={{padding: '5%'}}>
<StatisticsChartList selectedDeviceId={selectedDeviceId}/> <StatisticsChartList selectedDeviceId={selectedDeviceId} lastRefresh={lastRefresh}/>
</CCardBody> </CCardBody>
</CCard> </CCard>
); );
};
DeviceStatisticsCard.propTypes = { DeviceStatisticsCard.propTypes = {
selectedDeviceId: PropTypes.string.isRequired, selectedDeviceId: PropTypes.string.isRequired,

View File

@@ -4,9 +4,9 @@ import { v4 as createUuid } from 'uuid';
import DeviceStatisticsChart from './DeviceStatisticsChart'; import DeviceStatisticsChart from './DeviceStatisticsChart';
import axiosInstance from '../../../utils/axiosInstance'; import axiosInstance from '../../../utils/axiosInstance';
import { getToken } from '../../../utils/authHelper'; import { getToken } from '../../../utils/authHelper';
import { unixToTime } from '../../../utils/helper'; import { unixToTime, capitalizeFirstLetter } from '../../../utils/helper';
const StatisticsChartList = ({ selectedDeviceId }) => { const StatisticsChartList = ({ selectedDeviceId, lastRefresh }) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [deviceStats, setStats] = useState([]); const [deviceStats, setStats] = useState([]);
const [statOptions, setStatOptions] = useState({}); const [statOptions, setStatOptions] = useState({});
@@ -31,12 +31,14 @@ const StatisticsChartList = ({ selectedDeviceId }) => {
if(interfaceTypes[logInterface.name] === undefined) { if(interfaceTypes[logInterface.name] === undefined) {
interfaceTypes[logInterface.name] = i; interfaceTypes[logInterface.name] = i;
interfaceList.push([{ interfaceList.push([{
titleName: logInterface.name,
name: 'Tx', name: 'Tx',
backgroundColor: 'rgb(228,102,81,0.9)', backgroundColor: 'rgb(228,102,81,0.9)',
data: [], data: [],
fill: false fill: false
}, },
{ {
titleName: logInterface.name,
name: 'Rx', name: 'Rx',
backgroundColor: 'rgb(0,216,255,0.9)', backgroundColor: 'rgb(0,216,255,0.9)',
data: [], data: [],
@@ -51,17 +53,8 @@ const StatisticsChartList = ({ selectedDeviceId }) => {
for (const log of sortedData){ for (const log of sortedData){
// Looping through the interfaces of the log // Looping through the interfaces of the log
for (const inter of log.data.interfaces){ for (const inter of log.data.interfaces){
if(inter.name === 'up'){ interfaceList[interfaceTypes[inter.name]][0].data.push(inter.counters.tx_bytes);
interfaceList[0][0].data.push(inter.counters.tx_bytes); interfaceList[interfaceTypes[inter.name]][1].data.push(inter.counters.rx_bytes);
interfaceList[0][1].data.push(inter.counters.rx_bytes);
}
if(inter.name === 'down'){
interfaceList[1][0].data.push(inter.counters.tx_bytes);
interfaceList[1][1].data.push(inter.counters.rx_bytes);
}
/* interfaceList[interfaceTypes[inter.name]][0].data.push(inter.counters.tx_bytes);
interfaceList[interfaceTypes[inter.name]][1].data.push(inter.counters.rx_bytes); */
} }
} }
@@ -72,25 +65,33 @@ const StatisticsChartList = ({ selectedDeviceId }) => {
}, },
xaxis: { xaxis: {
title: { title: {
text: 'Time' text: 'Time',
style: {
fontSize: '15px'
}, },
categories },
categories,
tickAmount:20
}, },
yaxis: { yaxis: {
labels: {
minWidth: 40
},
title: { title: {
text: 'Data (KB)' text: 'Data (KB)',
} style: {
fontSize: '15px'
},
},
}, },
legend: { legend: {
position: 'top', position: 'top',
horizontalAlign: 'right', horizontalAlign: 'right',
floating: true, float: true
offsetY: -20, },
offsetX: -5
}
}; };
console.log(interfaceList);
setStatOptions(options); setStatOptions(options);
setStats(interfaceList); setStats(interfaceList);
} }
@@ -126,12 +127,33 @@ const StatisticsChartList = ({ selectedDeviceId }) => {
} }
}, [selectedDeviceId]); }, [selectedDeviceId]);
useEffect(() => {
if (lastRefresh !== '' && selectedDeviceId){
getStatistics();
}
}, [lastRefresh])
return ( return (
<div> <div>
{ {
deviceStats.map( deviceStats.map(
(data) => <div key={createUuid()}><DeviceStatisticsChart key={createUuid()} data={data} options={statOptions} /></div> (data) =>
<div key={createUuid()}>
<DeviceStatisticsChart
key={createUuid()}
data={data}
options={{
...statOptions,
title: {
text: capitalizeFirstLetter(data[0].titleName),
align: 'left',
style: {
fontSize: '25px'
},
}
}} />
</div>
) )
} }
</div> </div>
@@ -139,7 +161,12 @@ const StatisticsChartList = ({ selectedDeviceId }) => {
} }
StatisticsChartList.propTypes = { StatisticsChartList.propTypes = {
lastRefresh: PropTypes.string,
selectedDeviceId: PropTypes.string.isRequired, selectedDeviceId: PropTypes.string.isRequired,
}; };
StatisticsChartList.defaultProps = {
lastRefresh: ''
}
export default StatisticsChartList; export default StatisticsChartList;

View File

@@ -63,3 +63,6 @@ export const unixToTime = (dateString) => {
} }
export const dateToUnix = (date) => Math.floor(new Date(date).getTime() / 1000); export const dateToUnix = (date) => Math.floor(new Date(date).getTime() / 1000);
export const capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);