2017-12-21 15:54:38 +00:00
|
|
|
// @flow
|
|
|
|
|
2017-11-14 17:34:00 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import React, {Component} from 'react';
|
2019-03-13 12:53:52 +00:00
|
|
|
import {hot} from 'react-hot-loader';
|
2017-11-14 17:34:00 +00:00
|
|
|
|
2019-03-13 12:53:52 +00:00
|
|
|
import withStyles from '@material-ui/core/styles/withStyles';
|
2017-12-21 15:54:38 +00:00
|
|
|
|
2019-03-13 12:53:52 +00:00
|
|
|
import Header from 'Header';
|
|
|
|
import Body from 'Body';
|
|
|
|
import {inserter as logInserter, SAME} from 'Logs';
|
|
|
|
import {inserter as peerInserter} from 'Network';
|
2018-01-23 20:51:04 +00:00
|
|
|
import {MENU} from '../common';
|
2017-12-21 15:54:38 +00:00
|
|
|
import type {Content} from '../types/content';
|
2017-11-14 17:34:00 +00:00
|
|
|
|
2018-01-15 09:20:00 +00:00
|
|
|
// deepUpdate updates an object corresponding to the given update data, which has
|
|
|
|
// the shape of the same structure as the original object. updater also has the same
|
|
|
|
// structure, except that it contains functions where the original data needs to be
|
|
|
|
// updated. These functions are used to handle the update.
|
2017-12-21 15:54:38 +00:00
|
|
|
//
|
2018-01-15 09:20:00 +00:00
|
|
|
// Since the messages have the same shape as the state content, this approach allows
|
|
|
|
// the generalization of the message handling. The only necessary thing is to set a
|
|
|
|
// handler function for every path of the state in order to maximize the flexibility
|
|
|
|
// of the update.
|
2018-01-23 20:51:04 +00:00
|
|
|
const deepUpdate = (updater: Object, update: Object, prev: Object): $Shape<Content> => {
|
2018-01-15 09:20:00 +00:00
|
|
|
if (typeof update === 'undefined') {
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
if (typeof updater === 'function') {
|
2018-01-23 20:51:04 +00:00
|
|
|
return updater(update, prev);
|
2018-01-15 09:20:00 +00:00
|
|
|
}
|
|
|
|
const updated = {};
|
|
|
|
Object.keys(prev).forEach((key) => {
|
2018-01-23 20:51:04 +00:00
|
|
|
updated[key] = deepUpdate(updater[key], update[key], prev[key]);
|
2018-01-15 09:20:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
};
|
|
|
|
|
|
|
|
// shouldUpdate returns the structure of a message. It is used to prevent unnecessary render
|
|
|
|
// method triggerings. In the affected component's shouldComponentUpdate method it can be checked
|
|
|
|
// whether the involved data was changed or not by checking the message structure.
|
|
|
|
//
|
|
|
|
// We could return the message itself too, but it's safer not to give access to it.
|
2018-01-23 20:51:04 +00:00
|
|
|
const shouldUpdate = (updater: Object, msg: Object) => {
|
2018-01-15 09:20:00 +00:00
|
|
|
const su = {};
|
|
|
|
Object.keys(msg).forEach((key) => {
|
2018-01-23 20:51:04 +00:00
|
|
|
su[key] = typeof updater[key] !== 'function' ? shouldUpdate(updater[key], msg[key]) : true;
|
2018-01-15 09:20:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return su;
|
2017-12-21 15:54:38 +00:00
|
|
|
};
|
2018-01-15 09:20:00 +00:00
|
|
|
|
2018-01-23 20:51:04 +00:00
|
|
|
// replacer is a state updater function, which replaces the original data.
|
|
|
|
const replacer = <T>(update: T) => update;
|
2018-01-15 09:20:00 +00:00
|
|
|
|
2018-01-23 20:51:04 +00:00
|
|
|
// appender is a state updater function, which appends the update data to the
|
|
|
|
// existing data. limit defines the maximum allowed size of the created array,
|
|
|
|
// mapper maps the update data.
|
|
|
|
const appender = <T>(limit: number, mapper = replacer) => (update: Array<T>, prev: Array<T>) => [
|
|
|
|
...prev,
|
|
|
|
...update.map(sample => mapper(sample)),
|
|
|
|
].slice(-limit);
|
2018-01-15 09:20:00 +00:00
|
|
|
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
// defaultContent returns the initial value of the state content. Needs to be a function in order to
|
|
|
|
// instantiate the object again, because it is used by the state, and isn't automatically cleaned
|
|
|
|
// when a new connection is established. The state is mutated during the update in order to avoid
|
|
|
|
// the execution of unnecessary operations (e.g. copy of the log array).
|
|
|
|
const defaultContent: () => Content = () => ({
|
2018-01-15 09:20:00 +00:00
|
|
|
general: {
|
|
|
|
version: null,
|
|
|
|
commit: null,
|
|
|
|
},
|
2018-03-08 08:22:21 +00:00
|
|
|
home: {},
|
|
|
|
chain: {},
|
|
|
|
txpool: {},
|
2019-03-13 12:53:52 +00:00
|
|
|
network: {
|
|
|
|
peers: {
|
|
|
|
bundles: {},
|
|
|
|
},
|
|
|
|
diff: [],
|
|
|
|
},
|
|
|
|
system: {
|
2018-01-23 20:51:04 +00:00
|
|
|
activeMemory: [],
|
|
|
|
virtualMemory: [],
|
|
|
|
networkIngress: [],
|
|
|
|
networkEgress: [],
|
|
|
|
processCPU: [],
|
|
|
|
systemCPU: [],
|
|
|
|
diskRead: [],
|
|
|
|
diskWrite: [],
|
2018-01-15 09:20:00 +00:00
|
|
|
},
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
logs: {
|
|
|
|
chunks: [],
|
|
|
|
endTop: false,
|
|
|
|
endBottom: true,
|
2019-03-13 12:53:52 +00:00
|
|
|
topChanged: SAME,
|
|
|
|
bottomChanged: SAME,
|
2018-01-15 09:20:00 +00:00
|
|
|
},
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
});
|
2018-01-15 09:20:00 +00:00
|
|
|
|
2018-01-23 20:51:04 +00:00
|
|
|
// updaters contains the state updater functions for each path of the state.
|
|
|
|
//
|
|
|
|
// TODO (kurkomisi): Define a tricky type which embraces the content and the updaters.
|
2018-01-15 09:20:00 +00:00
|
|
|
const updaters = {
|
|
|
|
general: {
|
|
|
|
version: replacer,
|
|
|
|
commit: replacer,
|
|
|
|
},
|
2018-03-08 08:22:21 +00:00
|
|
|
home: null,
|
|
|
|
chain: null,
|
|
|
|
txpool: null,
|
2019-03-13 12:53:52 +00:00
|
|
|
network: peerInserter(200),
|
2018-03-08 08:22:21 +00:00
|
|
|
system: {
|
2018-01-23 20:51:04 +00:00
|
|
|
activeMemory: appender(200),
|
|
|
|
virtualMemory: appender(200),
|
|
|
|
networkIngress: appender(200),
|
|
|
|
networkEgress: appender(200),
|
|
|
|
processCPU: appender(200),
|
|
|
|
systemCPU: appender(200),
|
|
|
|
diskRead: appender(200),
|
|
|
|
diskWrite: appender(200),
|
2018-01-15 09:20:00 +00:00
|
|
|
},
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
logs: logInserter(5),
|
2018-01-15 09:20:00 +00:00
|
|
|
};
|
|
|
|
|
2018-01-23 20:51:04 +00:00
|
|
|
// styles contains the constant styles of the component.
|
|
|
|
const styles = {
|
|
|
|
dashboard: {
|
|
|
|
display: 'flex',
|
|
|
|
flexFlow: 'column',
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
zIndex: 1,
|
|
|
|
overflow: 'hidden',
|
2018-03-08 08:22:21 +00:00
|
|
|
},
|
2018-01-23 20:51:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// themeStyles returns the styles generated from the theme for the component.
|
|
|
|
const themeStyles: Object = (theme: Object) => ({
|
2017-12-21 15:54:38 +00:00
|
|
|
dashboard: {
|
|
|
|
background: theme.palette.background.default,
|
|
|
|
},
|
2017-11-14 17:34:00 +00:00
|
|
|
});
|
2018-01-15 09:20:00 +00:00
|
|
|
|
2017-12-21 15:54:38 +00:00
|
|
|
export type Props = {
|
2018-01-23 20:51:04 +00:00
|
|
|
classes: Object, // injected by withStyles()
|
2017-11-14 17:34:00 +00:00
|
|
|
};
|
2018-01-15 09:20:00 +00:00
|
|
|
|
2017-12-21 15:54:38 +00:00
|
|
|
type State = {
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
active: string, // active menu
|
|
|
|
sideBar: boolean, // true if the sidebar is opened
|
|
|
|
content: Content, // the visualized data
|
|
|
|
shouldUpdate: Object, // labels for the components, which need to re-render based on the incoming message
|
|
|
|
server: ?WebSocket,
|
2017-12-21 15:54:38 +00:00
|
|
|
};
|
2018-01-15 09:20:00 +00:00
|
|
|
|
2017-12-21 15:54:38 +00:00
|
|
|
// Dashboard is the main component, which renders the whole page, makes connection with the server and
|
|
|
|
// listens for messages. When there is an incoming message, updates the page's content correspondingly.
|
|
|
|
class Dashboard extends Component<Props, State> {
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
active: MENU.get('home').id,
|
|
|
|
sideBar: true,
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
content: defaultContent(),
|
2018-01-15 09:20:00 +00:00
|
|
|
shouldUpdate: {},
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
server: null,
|
2017-12-21 15:54:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// componentDidMount initiates the establishment of the first websocket connection after the component is rendered.
|
|
|
|
componentDidMount() {
|
|
|
|
this.reconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconnect establishes a websocket connection with the server, listens for incoming messages
|
|
|
|
// and tries to reconnect on connection loss.
|
|
|
|
reconnect = () => {
|
2019-03-13 12:53:52 +00:00
|
|
|
const host = process.env.NODE_ENV === 'production' ? window.location.host : 'localhost:8080';
|
|
|
|
const server = new WebSocket(`${((window.location.protocol === 'https:') ? 'wss://' : 'ws://')}${host}/api`);
|
2018-01-15 09:20:00 +00:00
|
|
|
server.onopen = () => {
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
this.setState({content: defaultContent(), shouldUpdate: {}, server});
|
2018-01-15 09:20:00 +00:00
|
|
|
};
|
2017-12-21 15:54:38 +00:00
|
|
|
server.onmessage = (event) => {
|
2018-01-15 09:20:00 +00:00
|
|
|
const msg: $Shape<Content> = JSON.parse(event.data);
|
2017-12-21 15:54:38 +00:00
|
|
|
if (!msg) {
|
2018-01-15 09:20:00 +00:00
|
|
|
console.error(`Incoming message is ${msg}`);
|
2017-12-21 15:54:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.update(msg);
|
|
|
|
};
|
|
|
|
server.onclose = () => {
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
this.setState({server: null});
|
2017-12-21 15:54:38 +00:00
|
|
|
setTimeout(this.reconnect, 3000);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
// send sends a message to the server, which can be accessed only through this function for safety reasons.
|
|
|
|
send = (msg: string) => {
|
|
|
|
if (this.state.server != null) {
|
|
|
|
this.state.server.send(msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-15 09:20:00 +00:00
|
|
|
// update updates the content corresponding to the incoming message.
|
|
|
|
update = (msg: $Shape<Content>) => {
|
|
|
|
this.setState(prevState => ({
|
2018-01-23 20:51:04 +00:00
|
|
|
content: deepUpdate(updaters, msg, prevState.content),
|
|
|
|
shouldUpdate: shouldUpdate(updaters, msg),
|
2018-01-15 09:20:00 +00:00
|
|
|
}));
|
2017-12-21 15:54:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// changeContent sets the active label, which is used at the content rendering.
|
|
|
|
changeContent = (newActive: string) => {
|
|
|
|
this.setState(prevState => (prevState.active !== newActive ? {active: newActive} : {}));
|
|
|
|
};
|
|
|
|
|
2018-01-23 20:51:04 +00:00
|
|
|
// switchSideBar opens or closes the sidebar's state.
|
|
|
|
switchSideBar = () => {
|
|
|
|
this.setState(prevState => ({sideBar: !prevState.sideBar}));
|
2017-12-21 15:54:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-01-23 20:51:04 +00:00
|
|
|
<div className={this.props.classes.dashboard} style={styles.dashboard}>
|
2017-12-21 15:54:38 +00:00
|
|
|
<Header
|
2018-01-23 20:51:04 +00:00
|
|
|
switchSideBar={this.switchSideBar}
|
2017-12-21 15:54:38 +00:00
|
|
|
/>
|
|
|
|
<Body
|
|
|
|
opened={this.state.sideBar}
|
|
|
|
changeContent={this.changeContent}
|
|
|
|
active={this.state.active}
|
|
|
|
content={this.state.content}
|
|
|
|
shouldUpdate={this.state.shouldUpdate}
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 07:59:04 +00:00
|
|
|
send={this.send}
|
2017-12-21 15:54:38 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 17:34:00 +00:00
|
|
|
|
2019-03-13 12:53:52 +00:00
|
|
|
export default hot(module)(withStyles(themeStyles)(Dashboard));
|