Skip to content

Commit

Permalink
feat: add share ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Jun 6, 2022
1 parent f3a8865 commit 520a47b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions public/locales/en/status.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 45 additions & 0 deletions src/bundles/bitswap-stats.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/bundles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -51,6 +52,7 @@ export default composeBundles(
experimentsBundle,
ipfsDesktop,
repoStats,
bitswapStats,
cliTutorModeBundle,
createAnalyticsBundle({})
)
11 changes: 10 additions & 1 deletion src/status/StatusConnected.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ 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)
const shareRatio = Math.round(sharedSize / downloadedSize * 10) / 10
return (
<header>
<h1 className='montserrat fw2 f3 charcoal ma0 pt0 pb2'>
Expand All @@ -17,6 +20,10 @@ export const StatusConnected = ({ t, peersCount, repoSize }) => {
</a>
</span>
<span className='dn di-ns gray'></span>
<span className='db mt1 mt0-ns dib-ns'>
{t('StatusConnected.shareSize', { downloadedSize: humanDownloadSize, sharedSize: humanSharedSize, ratio: shareRatio })}
</span>
<span className='dn di-ns gray'></span>
<span className='db mt1 mt0-ns dib-ns'>
<a className='link blue' href='#/peers'>
{t('StatusConnected.peersCount', { peersCount: peersCount.toString() })}
Expand All @@ -32,5 +39,7 @@ export const TranslatedStatusConnected = withTranslation('status')(StatusConnect
export default connect(
'selectPeersCount',
'selectRepoSize',
'selectDownloadedSize',
'selectSharedSize',
TranslatedStatusConnected
)
2 changes: 1 addition & 1 deletion src/status/StatusConnected.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ storiesOf('StatusConnected', module)
.addDecorator(i18n)
.addDecorator(checkA11y)
.add('Default', () => (
<StatusConnected peersCount={1001} repoSize={123123912321312} />
<StatusConnected peersCount={1001} repoSize={123123912321312} downloadSize={123123912321312/2} sharedSize={123123912321312*2}/>
))

0 comments on commit 520a47b

Please sign in to comment.