Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Added socker.io for reporting system health stats
Browse files Browse the repository at this point in the history
  • Loading branch information
fechy committed Oct 18, 2016
1 parent 4a22f51 commit 21589cf
Show file tree
Hide file tree
Showing 19 changed files with 356 additions and 207 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"redux-promise": "^0.5.3",
"redux-promise-middleware": "^4.1.0",
"redux-thunk": "^2.1.0",
"socket.io": "^1.5.0",
"socket.io-client": "^1.5.0",
"source-map-support": "^0.4.3",
"style-loader": "^0.13.1",
"universal-router": "1.2.2",
Expand Down
6 changes: 2 additions & 4 deletions src/actions/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import fetch from 'isomorphic-fetch';

import { SYSTEM_STATS } from '../constants';

export function diskUsage() {
export function update(data) {
return {
type: SYSTEM_STATS,
payload: new Promise(resolve => {
fetch('/api/stats').then(response => {
resolve(response.json());
});
resolve(data);
})
}
}
2 changes: 1 addition & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async function onLocationChange(location) {
}

ReactDOM.render(
<App context={context}>{route.component}</App>,
<App context={context} hostname={window.location.hostname}>{route.component}</App>,
container,
() => onRenderComplete(route, location)
);
Expand Down
10 changes: 8 additions & 2 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';

import injectTapEventPlugin from 'react-tap-event-plugin';
import { port } from '../config';
import SystemHealth from './SystemHealth';

import injectTapEventPlugin from 'react-tap-event-plugin';
injectTapEventPlugin();

const ContextType = {
Expand All @@ -30,6 +32,7 @@ class App extends React.Component {
static propTypes = {
context: PropTypes.shape(ContextType).isRequired,
children: PropTypes.element.isRequired,
hostname: PropTypes.string.isRequired,
};

static childContextTypes = ContextType;
Expand All @@ -41,7 +44,10 @@ class App extends React.Component {
render() {
return (
<MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
{React.Children.only(this.props.children)}
<div>
<SystemHealth port={port} ip={this.props.hostname} />
{React.Children.only(this.props.children)}
</div>
</MuiThemeProvider>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import classnames from 'classnames';
import withStyles from 'isomorphic-style-loader/lib/withStyles';

import Navigation from '../Navigation';
import MiniStats from '../MiniStats';
import logo from './RetroPie_Logo-60x60.png';
import s from './Header.css';

Expand All @@ -25,6 +26,7 @@ function Header() {
<img src={logo} />
</div>
</Navigation>
<MiniStats />
</div>
</div>
</div>
Expand Down
14 changes: 14 additions & 0 deletions src/components/MiniStats/MiniStats.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.container {
position: absolute;
top:5px;
right: 10px;
}

.stat {
padding:0 10px;
}

.stat i {
width:15px;
margin-right: 5px;
}
46 changes: 46 additions & 0 deletions src/components/MiniStats/MiniStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import mathjs from 'mathjs';

import * as statsAction from '../../actions/stats';

import s from './MiniStats.css';

const mapStateToProps = (state) => {
return {
isChecking: state.stats.get('isChecking'),
disk: state.stats.get('disk'),
cpu: state.stats.get('cpu'),
memory: state.stats.get('memory'),
}
};

class MiniStats extends Component
{
render() {
const { isChecking, disk, memory, cpu } = this.props;
if (isChecking) {
return (<span />);
}

return (
<div className={s.container}>
<div className={s.stat}>
<i className="fa fa-database" />
<span>{ mathjs.round(disk.get('availablePercentage'), 2) }%</span>
</div>
<div className={s.stat}>
<i className="fa fa-tachometer" />
<span>{ mathjs.round(memory.get('percentage'), 2) }%</span>
</div>
<div className={s.stat}>
<i className="fa fa-fire" />
<span>{ mathjs.round(0, 2) }%</span>
</div>
</div>
)
}
}

export default connect(mapStateToProps)(withStyles(s)(MiniStats));
6 changes: 6 additions & 0 deletions src/components/MiniStats/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "MiniStats",
"version": "0.0.0",
"private": true,
"main": "./MiniStats.js"
}
50 changes: 50 additions & 0 deletions src/components/SystemHealth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { Component } from 'react';
import { connect } from 'react-redux'
import history from '../core/history';

import io from 'socket.io-client';

import * as actions from '../actions/stats';

const mapStateToProps = (state) => {
return {

}
};

const mapDispatchToProps = (dispatch) => {
return {
update: data => dispatch(actions.update(data))
}
};

const statEvent = 'system_health';

class SystemHealth extends Component
{
socket;

componentDidMount() {
const { port, ip } = this.props;
this.socket = io(`http://${ip}:${port}`);
this.socket.on('init', () => {
console.log('init!');
});

this.socket.on(statEvent, ::this._receiveData);
}

componentWillUnmount() {
this.socket.removeAllListeners(statEvent);
}

_receiveData(time, data) {
this.props.update(data);
}

render() {
return(<s />);
}
}

export default connect(mapStateToProps, mapDispatchToProps)(SystemHealth);
23 changes: 1 addition & 22 deletions src/routes/stats-board/StatsBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,14 @@ const mapStateToProps = (state) => {
}
};

const mapDispatchToProps = (dispatch) => {
return {
checkDiskSpace: () => dispatch(statsAction.diskUsage())
}
};

class StatsBoard extends Component
{
interval = null;

constructor(...props) {
super(...props);

this.state = {};
}

componentDidMount() {
// ...
this.props.checkDiskSpace();
this.interval = setInterval(this.props.checkDiskSpace, 5000);
}

componentWillUnmount() {
clearInterval(this.interval);
}

_renderContent() {
const { disk, cpu, memory } = this.props;

Expand Down Expand Up @@ -120,7 +102,4 @@ class StatsBoard extends Component
}
}

export default connect(
mapStateToProps,
mapDispatchToProps
)(withStyles(s)(StatsBoard));
export default connect(mapStateToProps)(withStyles(s)(StatsBoard));
15 changes: 9 additions & 6 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ import configureStore from './store/configureStore';
import buildWorker from './worker';
import { port } from './config';

const DEBUG = !process.argv.includes('--release');

const app = express();

const DEBUG = !process.argv.includes('--release');
const http = app.listen(port, () => {
// Sockets
console.log(`The server is running at http://localhost:${port}/`);
});

const io = require('socket.io').listen(http);

//
// Tell any CSS tooling (such as Material UI) to use all vendor prefixes if the
Expand All @@ -46,7 +53,7 @@ app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

buildWorker(app, DEBUG ? '.dev' : '');
buildWorker(app, DEBUG ? '.dev' : '', io);

//
// Register server-side rendering middleware
Expand Down Expand Up @@ -116,7 +123,3 @@ app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
res.status(err.status || 500);
res.send(`<!doctype html>${html}`);
});

app.listen(port, () => {
console.log(`The server is running at http://localhost:${port}/`);
});
Loading

0 comments on commit 21589cf

Please sign in to comment.