Skip to content

Commit

Permalink
fix: work even when bandwidth metrics are disabled (#1024)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias authored and olizilla committed Apr 29, 2019
1 parent 1bdb3eb commit d99403c
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 57 deletions.
4 changes: 3 additions & 1 deletion public/locales/en/status.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@
"yesLabel": "OK",
"noLabel": "No thanks",
"detailsLabel": "More info"
}
},
"bandwidthStats": "Bandwidth Stats",
"bandwidthStatsDisabled": "You have the bandwidth metrics disabled. You can enable them by typing the command bellow or changing the key <1>Swarm.DisableBandwidthMetrics</1> to <3>false</3> on <5>Settings</5>. Then, you need to restart the IPFS daemon to apply the changes."
}
4 changes: 2 additions & 2 deletions src/bundles/connected.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const connected = {

selectIpfsConnected: createSelector(
'selectIpfsReady',
'selectStatsLastSuccess',
'selectStatsLastError',
'selectNodeBandwidthLastSuccess',
'selectNodeBandwidthLastError',
(ipfsReady, lastSuccess, lastError) => ipfsReady && lastSuccess && lastSuccess > lastError
)
}
Expand Down
14 changes: 10 additions & 4 deletions src/bundles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import filesBundle from './files'
import configBundle from './config'
import configSaveBundle from './config-save'
import navbarBundle from './navbar'
import statsBundle from './stats'
import notifyBundle from './notify'
import connectedBundle from './connected'
import retryInitBundle from './retry-init'
Expand All @@ -27,17 +26,24 @@ export default composeBundles(
appIdle({ idleTimeout: 5000 }),
ipfsBundle({
tryWindow: false,
ipfsConnectionTest: (ipfs) => {
ipfsConnectionTest: async (ipfs) => {
// ipfs connection is working if can we fetch the bw stats.
// See: https://github.com/ipfs-shipyard/ipfs-webui/issues/835#issuecomment-466966884
return ipfs.stats.bw()
try {
await ipfs.stats.bw()
} catch (err) {
if (!/bandwidth reporter disabled in config/.test(err)) {
throw err
}
}

return true
}
}),
identityBundle,
navbarBundle,
routesBundle,
redirectsBundle,
statsBundle,
filesBundle(),
exploreBundle(async () => {
const ipldDeps = await Promise.all([
Expand Down
12 changes: 7 additions & 5 deletions src/bundles/node-bandwidth-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ export default function (opts) {
},

reactUpdateNodeBandwidthChartData: createSelector(
'selectNodeBandwidthRaw',
'selectNodeBandwidth',
'selectNodeBandwidthLastSuccess',
'selectNodeBandwidthEnabled',
'selectNodeBandwidthChartData',
(bwRaw, chartData) => {
if (!bwRaw.data) return
(bw, lastSuccess, enabled, chartData) => {
if (!bw || !enabled) return

// Only tests for .in because it has the same timestamps as .out
if (!chartData.in.length || bwRaw.lastSuccess > chartData.in[chartData.in.length - 1].x) {
if (!chartData.in.length || lastSuccess > chartData.in[chartData.in.length - 1].x) {
return {
actionCreator: 'doUpdateNodeBandwidthChartData',
args: [bwRaw.data, bwRaw.lastSuccess, chartData]
args: [bw, lastSuccess, chartData]
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/bundles/node-bandwidth-chart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const mockNodeBandwidthBundle = {
reducer (state = { data: null }, action) {
return action.type === 'UPDATE_MOCK_NODE_BANDWIDTH' ? action.payload : state
},
selectNodeBandwidth: state => state.nodeBandwidth.data,
selectNodeBandwidthLastSuccess: state => state.nodeBandwidth.lastSuccess,
selectNodeBandwidthEnabled: () => true,
selectNodeBandwidthRaw: state => state.nodeBandwidth
}

Expand Down
20 changes: 18 additions & 2 deletions src/bundles/node-bandwidth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import ms from 'milliseconds'
const bundle = createAsyncResourceBundle({
name: 'nodeBandwidth',
actionBaseType: 'NODE_BANDWIDTH',
getPromise: ({ getIpfs }) => getIpfs().stats.bw(),
staleAfter: ms.seconds(10),
getPromise: async ({ getIpfs }) => {
try {
const stats = await getIpfs().stats.bw()
return stats
} catch (err) {
if (/bandwidth reporter disabled in config/.test(err)) {
return { disabled: true }
}

throw err
}
},
staleAfter: ms.seconds(3),
retryAfter: ms.seconds(3),
persist: false,
checkIfOnline: false
})

bundle.selectNodeBandwidthEnabled = state => state.nodeBandwidth.data ? !state.nodeBandwidth.data.disabled : false

bundle.selectNodeBandwidthLastSuccess = state => state.nodeBandwidth.lastSuccess

// Update the node bandwidth if it is stale (appTime - lastSuccess > staleAfter)
bundle.reactNodeBandwidthFetchWhenIdle = createSelector(
'selectNodeBandwidthShouldUpdate',
Expand Down
28 changes: 0 additions & 28 deletions src/bundles/stats.js

This file was deleted.

27 changes: 27 additions & 0 deletions src/status/BandwidthStatsDisabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import { translate, Trans } from 'react-i18next'
import Shell from '../components/shell/Shell'
import Box from '../components/box/Box'

const StatusNotConnected = ({ t }) => {
return (
<Box className='mt3 pa3' >
<h2 className='ttu yellow tracked f6 fw4 aqua mt0 mb4'>{t('bandwidthStats')}</h2>

<p className='mw6 mr2 lh-copy charcoal f6'>
<Trans i18nKey='bandwidthStatsDisabled'>
You have the bandwidth metrics disabled. You can enable them by typing the command bellow
or changing the key <code>Swarm.DisableBandwidthMetrics</code> to <code>false</code> on
<a className='link blue' href='#/settings'>Settings</a>. Then, you need to restart the IPFS
daemon to apply the changes.
</Trans>
</p>

<Shell>
<code className='db'>$ ipfs config --json Swarm.DisableBandwidthMetrics false</code>
</Shell>
</Box>
)
}

export default translate('status')(StatusNotConnected)
9 changes: 5 additions & 4 deletions src/status/NetworkTraffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class NetworkTraffic extends React.Component {
}

componentDidUpdate (_, prevState) {
const { stats } = this.props
const down = stats ? parseInt(stats.bw.rateIn.toFixed(0), 10) : 0
const up = stats ? parseInt(stats.bw.rateOut.toFixed(0), 10) : 0
const { nodeBandwidth } = this.props

const down = nodeBandwidth ? parseInt(nodeBandwidth.rateIn.toFixed(0), 10) : 0
const up = nodeBandwidth ? parseInt(nodeBandwidth.rateOut.toFixed(0), 10) : 0

if (down !== prevState.downSpeed.filled || up !== prevState.upSpeed.filled) {
this.setState({
Expand Down Expand Up @@ -62,6 +63,6 @@ class NetworkTraffic extends React.Component {
}

export default connect(
'selectStats',
'selectNodeBandwidth',
translate('status')(NetworkTraffic)
)
29 changes: 18 additions & 11 deletions src/status/StatusPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import NodeBandwidthChart from './NodeBandwidthChart'
import NetworkTraffic from './NetworkTraffic'
import Box from '../components/box/Box'
import AskToEnable from '../components/ask/AskToEnable'
import BandwidthStatsDisabled from './BandwidthStatsDisabled'

const StatusPage = ({ t, ipfsConnected, analyticsAskToEnable, doEnableAnalytics, doDisableAnalytics }) => {
const StatusPage = ({ t, nodeBandwidthEnabled, ipfsConnected, analyticsAskToEnable, doEnableAnalytics, doDisableAnalytics }) => {
return (
<div data-id='StatusPage' className='mw9 center'>
<Helmet>
Expand Down Expand Up @@ -47,22 +48,28 @@ const StatusPage = ({ t, ipfsConnected, analyticsAskToEnable, doEnableAnalytics,
/>
: null
}
<Box className='mt3 pa3' style={{ opacity: ipfsConnected ? 1 : 0.4 }}>
<div className='flex flex-column flex-row-l'>
<div className='pr0 pr2-l flex-auto'>
<NodeBandwidthChart />
</div>
<div className='dn db-l pl3 pr5'>
<NetworkTraffic />
</div>
</div>
</Box>
<div style={{ opacity: ipfsConnected ? 1 : 0.4 }}>
{ nodeBandwidthEnabled
? <Box className='mt3 pa3'>
<div className='flex flex-column flex-row-l'>
<div className='pr0 pr2-l flex-auto'>
<NodeBandwidthChart />
</div>
<div className='dn db-l pl3 pr5'>
<NetworkTraffic />
</div>
</div>
</Box>
: <BandwidthStatsDisabled />
}
</div>
</div>
)
}

export default connect(
'selectIpfsConnected',
'selectNodeBandwidthEnabled',
'selectAnalyticsAskToEnable',
'doEnableAnalytics',
'doDisableAnalytics',
Expand Down

0 comments on commit d99403c

Please sign in to comment.