Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sharing statistics on the homepage #1942

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions public/locales/en/status.json
Expand Up @@ -10,6 +10,7 @@
"countMore": "...and {count} more",
"StatusConnected": {
"repoSize": "Hosting {repoSize} of data",
"shareSize": "Downloaded {downloadedSize} and Shared {sharedSize} ({ratio})",
Copy link
Member

@lidel lidel Jun 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jorropo @SgtPooki @hacdias thoughts on presentation / phrasing here?

These are stats for the current session and not the total across history.

Same for "Discovered peers".

Maybe we should prefix them with "Current session" or "Since {startup date}"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add some indication that this is not since the beginning, but just for the current session. Maybe some surrounding box saying current session?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of a "Since {date}" prefix.

Also, this is a great candidate for #1220

"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
@@ -0,0 +1,45 @@
import { createAsyncResourceBundle, createSelector } from 'redux-bundler'

const bundle = createAsyncResourceBundle({
name: 'bitswapStats',
getPromise: async ({ getIpfs }) => {
const rawData = await getIpfs().stats.bitswap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this function throws?

Also, prefer const { dataReceived, dataSent } = await getIpfs().stats.bitswap()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this function throws?

I have no idea, wtf is javascript anyway ?

(I guess the ressources are never initialised which isn't that bad ?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated with try/catch

// 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint should be yelling here about using optional chaining instead, e.g. bitswapStats?.downloadedSize. Also, most of our eslint rules prefer explicit boolean checks, so either

  1. Boolean(bitswapStats?.downloadedSize) or
  2. bitswapStats?.downloadedSize != null

You may want to check your editor settings.

return bitswapStats.downloadedSize.toString()
}
}
)

bundle.selectSharedSize = createSelector(
'selectBitswapStats',
(bitswapStats) => {
console.log(bitswapStats)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need this? If so, prefer logger.debug or similar. If not, remove please :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug I forgot to remove

if (bitswapStats && bitswapStats.sharedSize) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Boolean(bitswapStats?.sharedSize) or
  2. bitswapStats?.sharedSize != null

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' }
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
}
}
)

export default bundle
2 changes: 2 additions & 0 deletions src/bundles/index.js
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({})
)
22 changes: 21 additions & 1 deletion src/status/StatusConnected.js
Expand Up @@ -3,8 +3,22 @@ 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)
downloadedSize = downloadedSize || 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we should prefer the null coalescence operator (??) instead of the OR operator (||) for default values if possible. I assume we also want to make sure downloadedSize is a number? Number(downloadedSize ?? 0)

ditto for other vars here.

const humanDownloadSize = humanSize(downloadedSize)
sharedSize = sharedSize || 0
const humanSharedSize = humanSize(sharedSize)
let shareRatio = sharedSize / downloadedSize
if (isNaN(shareRatio)) {
shareRatio = Infinity
}
if (shareRatio < 10) {
// round to 1 decimal place if that bellow 10
shareRatio = Math.round(shareRatio * 10) / 10
} else {
shareRatio = Math.round(shareRatio)
}
return (
<header>
<h1 className='montserrat fw2 f3 charcoal ma0 pt0 pb2'>
Expand All @@ -17,6 +31,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 +50,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
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.3333}/>
))