diff --git a/public/locales/en/status.json b/public/locales/en/status.json index 981fd984a..8c1c4651e 100644 --- a/public/locales/en/status.json +++ b/public/locales/en/status.json @@ -10,6 +10,7 @@ "countMore": "...and {count} more", "StatusConnected": { "repoSize": "Hosting {repoSize} of data", + "shareSize": "Downloaded {downloadedSize} and Shared {sharedSize} ({ratio})", "peersCount": "{peersCount, plural, one {Discovered 1 peer} other {Discovered {peersCount} peers}}" }, "customApiConfig": "Custom JSON configuration", diff --git a/src/bundles/bitswap-stats.js b/src/bundles/bitswap-stats.js new file mode 100644 index 000000000..f19e27650 --- /dev/null +++ b/src/bundles/bitswap-stats.js @@ -0,0 +1,45 @@ +import { createAsyncResourceBundle, createSelector } from 'redux-bundler' + +const bundle = createAsyncResourceBundle({ + name: 'bitswapStats', + getPromise: async ({ getIpfs }) => { + const rawData = await getIpfs().stats.bitswap() + // early gc, dont keep arround the peer list + return { downloadedSize: rawData.dataReceived, sharedSize: rawData.dataSent } + }, + staleAfter: 60000, + persist: false, + checkIfOnline: false +}) + +bundle.selectDownloadedSize = createSelector( + 'selectBitswapStats', + (bitswapStats) => { + if (bitswapStats && bitswapStats.downloadedSize) { + return bitswapStats.downloadedSize.toString() + } + } +) + +bundle.selectSharedSize = createSelector( + 'selectBitswapStats', + (bitswapStats) => { + console.log(bitswapStats) + if (bitswapStats && bitswapStats.sharedSize) { + return bitswapStats.sharedSize.toString() + } + } +) + +// Fetch the config if we don't have it or it's more than `staleAfter` ms old +bundle.reactBitswapStatsFetch = createSelector( + 'selectBitswapStatsShouldUpdate', + 'selectIpfsReady', + (shouldUpdate, ipfsReady) => { + if (shouldUpdate && ipfsReady) { + return { actionCreator: 'doFetchBitswapStats' } + } + } +) + +export default bundle diff --git a/src/bundles/index.js b/src/bundles/index.js index ca71ac7ea..97943eec1 100644 --- a/src/bundles/index.js +++ b/src/bundles/index.js @@ -20,6 +20,7 @@ import identityBundle from './identity' import bundleCache from '../lib/bundle-cache' import ipfsDesktop from './ipfs-desktop' import repoStats from './repo-stats' +import bitswapStats from './bitswap-stats' import createAnalyticsBundle from './analytics' import experimentsBundle from './experiments' import cliTutorModeBundle from './cli-tutor-mode' @@ -51,6 +52,7 @@ export default composeBundles( experimentsBundle, ipfsDesktop, repoStats, + bitswapStats, cliTutorModeBundle, createAnalyticsBundle({}) ) diff --git a/src/status/StatusConnected.js b/src/status/StatusConnected.js index 350fa18a7..c26249436 100644 --- a/src/status/StatusConnected.js +++ b/src/status/StatusConnected.js @@ -3,8 +3,15 @@ import { withTranslation, Trans } from 'react-i18next' import { connect } from 'redux-bundler-react' import { humanSize } from '../lib/files' -export const StatusConnected = ({ t, peersCount, repoSize }) => { +export const StatusConnected = ({ t, peersCount, repoSize, downloadedSize, sharedSize }) => { const humanRepoSize = humanSize(repoSize || 0) + const humanDownloadSize = humanSize(downloadedSize || 0) + const humanSharedSize = humanSize(sharedSize || 0) + let shareRatio = sharedSize / downloadedSize + if (isNaN(shareRatio)) { + shareRatio = Infinity + } + shareRatio = Math.round(shareRatio * 10) / 10 return (

@@ -17,6 +24,10 @@ export const StatusConnected = ({ t, peersCount, repoSize }) => { + + {t('StatusConnected.shareSize', { downloadedSize: humanDownloadSize, sharedSize: humanSharedSize, ratio: shareRatio })} + + {t('StatusConnected.peersCount', { peersCount: peersCount.toString() })} @@ -32,5 +43,7 @@ export const TranslatedStatusConnected = withTranslation('status')(StatusConnect export default connect( 'selectPeersCount', 'selectRepoSize', + 'selectDownloadedSize', + 'selectSharedSize', TranslatedStatusConnected ) diff --git a/src/status/StatusConnected.stories.js b/src/status/StatusConnected.stories.js index b39b2687e..af7fae016 100644 --- a/src/status/StatusConnected.stories.js +++ b/src/status/StatusConnected.stories.js @@ -9,5 +9,5 @@ storiesOf('StatusConnected', module) .addDecorator(i18n) .addDecorator(checkA11y) .add('Default', () => ( - + ))