chore(console): use rpc to populate the data

This commit is contained in:
George Lima 2019-06-17 15:58:21 -03:00
parent 228c2b1c5a
commit bf650e19dd
4 changed files with 89 additions and 39 deletions

View File

@ -1,16 +1,18 @@
// @flow
import React, { PureComponent, Fragment } from 'react';
// eslint-disable-next-line import/no-extraneous-dependencies
import { ipcRenderer } from 'electron';
import styled, { withTheme } from 'styled-components';
import uuid from 'uuid/v4';
import eres from 'eres';
import humanizeDuration from 'humanize-duration';
import { TextComponent } from '../components/text';
import ConsoleSymbolDark from '../assets/images/console_zcash_dark.png';
import ConsoleSymbolLight from '../assets/images/console_zcash_light.png';
import { DARK } from '../constants/themes';
import rpc from '../../services/api';
import store from '../../config/electron-store';
const Wrapper = styled.div`
max-height: 100%;
@ -31,30 +33,6 @@ const ConsoleImg = styled.img`
width: auto;
`;
const initialLog = `
Thank you for running a Zcash node!
You're helping to strengthen the network and contributing to a social good :)
In order to ensure you are adequately protecting your privacy when using Zcash, please see <https://z.cash/support/security/>.
`;
const defaultState = `
Thank you for running a Zcash node!
You're helping to strengthen the network and contributing to a social good :)
In order to ensure you are adequately protecting your privacy when using Zcash, please see <https://z.cash/support/security/>.
Block height | 0
Connections | 0
Network solution rate | 0 Sol/s
You are currently not mining.
To enable mining, add 'gen=1' to your zcash.conf and restart.
Since starting this node 0 minutes, 0 seconds ago:
- You have validated 0 transactions!
\n
------------------------------------------
`;
const breakpoints = [1, 4, 7, 10, 13];
type Props = {
@ -62,26 +40,68 @@ type Props = {
};
type State = {
log: string,
blockHeight: number,
connections: number,
networkSolutionsRate: number,
};
class Component extends PureComponent<Props, State> {
interval: ?IntervalID = null;
requestOnTheFly: boolean = false;
state = {
log: defaultState,
blockHeight: 0,
connections: 0,
networkSolutionsRate: 0,
};
componentDidMount() {
ipcRenderer.on('zcashd-log', (event: empty, message: string) => {
this.setState(() => ({ log: initialLog + message }));
});
this.interval = setInterval(() => this.update(), 3000);
}
componentWillUnmount() {
ipcRenderer.removeAllListeners('zcashd-log');
clearInterval(this.interval);
}
update = async () => {
if (this.requestOnTheFly) return;
this.requestOnTheFly = true;
const [err, result] = await eres(Promise.all([rpc.getinfo(), rpc.getmininginfo()]));
if (err) return;
this.setState(
{
blockHeight: result[0].blocks,
connections: result[0].connections,
networkSolutionsRate: result[1].networksolps,
},
() => {
this.requestOnTheFly = false;
},
);
};
getLog = (state: State) => `
Thank you for running a Zcash node!
You're helping to strengthen the network and contributing to a social good :)
In order to ensure you are adequately protecting your privacy when using Zcash, please see <https://z.cash/support/security/>.
Block height | ${state.blockHeight}
Connections | ${state.connections}
Network solution rate | ${state.networkSolutionsRate} Sol/s
Started ${humanizeDuration(new Date() - new Date(store.get('DAEMON_START_TIME')), {
round: true,
})} ago
\n
------------------------------------------
`;
render() {
const { log } = this.state;
const { theme } = this.props;
const ConsoleSymbol = theme.mode === DARK ? ConsoleSymbolDark : ConsoleSymbolLight;
@ -90,12 +110,14 @@ class Component extends PureComponent<Props, State> {
<Wrapper id='console-wrapper'>
<Fragment>
<ConsoleImg src={ConsoleSymbol} alt='Zcashd' />
{log.split('\n').map((item, idx) => (
<Fragment key={uuid()}>
<ConsoleText value={item} />
{breakpoints.includes(idx) ? <br /> : null}
</Fragment>
))}
{this.getLog(this.state)
.split('\n')
.map((item, idx) => (
<Fragment key={uuid()}>
<ConsoleText value={item} />
{breakpoints.includes(idx) ? <br /> : null}
</Fragment>
))}
</Fragment>
</Wrapper>
);

View File

@ -0,0 +1,22 @@
declare module 'humanize-duration' {
declare type options = {
language?: string,
fallbacks?: string[],
delimiter?: string,
spacer?: string,
largest?: number,
units?: string[],
round?: boolean,
decimal?: string,
conjunction?: string,
maxDecimalPoints?: number,
unitMeasures?: {
y?: number,
mo?: number,
w?: number,
d?: number,
},
};
declare module.exports: (milli: number, opt?: options) => string;
}

View File

@ -230,6 +230,7 @@
"find-process": "^1.2.1",
"got": "^9.6.0",
"history": "^4.7.2",
"humanize-duration": "^3.18.0",
"lodash.flow": "^3.5.0",
"lodash.groupby": "^4.6.0",
"lodash.uniqby": "^4.7.0",

View File

@ -7887,6 +7887,11 @@ humanize-duration@^3.15.3:
version "3.16.0"
resolved "https://registry.yarnpkg.com/humanize-duration/-/humanize-duration-3.16.0.tgz#c7ec3b898305f007c63893f891870b2b5a2e0a7d"
humanize-duration@^3.18.0:
version "3.18.0"
resolved "https://registry.yarnpkg.com/humanize-duration/-/humanize-duration-3.18.0.tgz#ba07a945d6d4358b9751fecabe27864bf965afbc"
integrity sha512-reYy4EJMqlhX13TDlgSqLYfVGKOoixoEzsSL6DBlp22dScWN8Q2eMgDF4L0q28mzbgO40rnBy3WyEUQEhfYALw==
humanize-string@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/humanize-string/-/humanize-string-1.0.2.tgz#fef0a8bc9b1b857ca4013bbfaea75071736988f6"