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

fix: fetch local pins stats #1911

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"eslint-plugin-standard": "^4.0.1",
"fake-indexeddb": "^3.1.2",
"get-port": "^5.1.1",
"go-ipfs": "0.9.1",
"go-ipfs": "0.12.0",
"http-proxy": "^1.18.1",
"http-server": "^0.12.3",
"ipfs": "0.58.3",
Expand Down
4 changes: 0 additions & 4 deletions src/bundles/files/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export const ACTIONS = {
PIN_LIST: ('FILES_PIN_LIST'),
/** @type {'FILES_SIZE_GET'} */
SIZE_GET: ('FILES_SIZE_GET'),
/** @type {'FILES_PINS_SIZE_GET'} */
PINS_SIZE_GET: ('FILES_PINS_SIZE_GET'),
/** @type {'FILES_DISMISS_ERRORS'} */
DISMISS_ERRORS: ('FILES_DISMISS_ERRORS'),
/** @type {'FILES_CLEAR_ALL'} */
Expand Down Expand Up @@ -60,8 +58,6 @@ export const IGNORED_FILES = [
export const DEFAULT_STATE = {
pageContent: null,
mfsSize: -1,
pinsSize: 0,
numberOfPins: 0,
pins: [],
sorting: { // TODO: cache this
by: SORTING.BY_NAME,
Expand Down
16 changes: 0 additions & 16 deletions src/bundles/files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,6 @@ const createFilesBundle = () => {
return state
}
}
case ACTIONS.PINS_SIZE_GET: {
const { task, type } = action
const pinsSize = task.status === 'Exit' && task.result.ok
? task.result.value.pinsSize
: 0

const numberOfPins = task.status === 'Exit' && task.result.ok
? task.result.value.numberOfPins
: 0

return {
...updateJob(state, task, type),
pinsSize,
numberOfPins
}
}
case ACTIONS.SIZE_GET: {
const { task, type } = action
const mfsSize = task.status === 'Exit' && task.result.ok
Expand Down
2 changes: 0 additions & 2 deletions src/bundles/files/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export type Model = {
pins: string[]
sorting: Sorting
mfsSize: number
pinsSize: number
numberOfPins: number

pending: PendingJob<any, any>[]
finished: FinishedJob<any>[]
Expand Down
10 changes: 0 additions & 10 deletions src/bundles/files/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ const selectors = () => ({
*/
selectFilesSize: (state) => state.files.mfsSize,

/**
* @param {Model} state
*/
selectPinsSize: (state) => state.files.pinsSize,

/**
* @param {Model} state
*/
selectNumberOfPins: (state) => state.files.numberOfPins,

/**
* @param {Model} state
*/
Expand Down
22 changes: 22 additions & 0 deletions src/bundles/pinning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { pinningServiceTemplates } from '../constants/pinning'
import memoize from 'p-memoize'
import CID from 'cids'
import all from 'it-all'

// This bundle leverages createCacheBundle and persistActions for
// the persistence layer that keeps pins in IndexDB store
Expand Down Expand Up @@ -61,6 +62,8 @@ const pinningBundle = {
pinningServices: [],
remotePins: [],
notRemotePins: [],
localPinsSize: 0,
localNumberOfPins: 0,
arePinningServicesSupported: false
}, action) => {
if (action.type === 'UPDATE_REMOTE_PINS') {
Expand All @@ -70,6 +73,10 @@ const pinningBundle = {
const notRemotePins = uniq([...state.notRemotePins, ...removals].filter(p => !adds.some(a => a === p)))
return { ...state, remotePins, notRemotePins }
}
if (action.type === 'SET_LOCAL_PINS_STATS') {
const { localPinsSize, localNumberOfPins } = action.payload
return { ...state, localNumberOfPins, localPinsSize }
}
if (action.type === 'SET_REMOTE_PINNING_SERVICES') {
const oldServices = state.pinningServices
const newServices = action.payload
Expand Down Expand Up @@ -148,13 +155,28 @@ const pinningBundle = {
selectRemotePins: (state) => state.pinning.remotePins || [],
selectNotRemotePins: (state) => state.pinning.notRemotePins || [],

selectLocalPinsSize: (state) => state.pinning.localPinsSize,
selectLocalNumberOfPins: (state) => state.pinning.localNumberOfPins,

doSelectRemotePinsForFile: (file) => ({ store }) => {
const pinningServicesNames = store.selectPinningServices().map(remote => remote.name)
const remotePinForFile = store.selectRemotePins().filter(pin => cacheId2Cid(pin) === file.cid.toString())
const servicesBeingUsed = remotePinForFile.map(pin => cacheId2ServiceName(pin)).filter(name => pinningServicesNames.includes(name))
return servicesBeingUsed
},

// gets the amount of local pins
doFetchLocalPinsStats: () => async ({ getIpfs, dispatch }) => {
const ipfs = getIpfs()
if (!ipfs) return null

const localPins = await all(ipfs.pin.ls({ type: 'recursive' }))
const localPinsSize = -1 // TODO: right now calculating size of all pins is too expensive (requires ipfs.files.stat per CID)
const localNumberOfPins = localPins.length

dispatch({ type: 'SET_LOCAL_PINS_STATS', payload: { localPinsSize, localNumberOfPins } })
},

// list of services without online check (reads list from config, should be instant)
doFetchPinningServices: () => async ({ getIpfs, store, dispatch }) => {
const ipfs = getIpfs()
Expand Down
15 changes: 8 additions & 7 deletions src/components/pinning-manager/PinningManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import './PinningManager.css'
const ROW_HEIGHT = 50
const HEADER_HEIGHT = 32

export const PinningManager = ({ pinningServices, ipfsReady, arePinningServicesSupported, doFetchPinningServices, doFetchPinningServicesStats, doRemovePinningService, pinsSize, numberOfPins, t }) => {
export const PinningManager = ({ pinningServices, ipfsReady, arePinningServicesSupported, doFetchPinningServices, doFetchPinningServicesStats, doFetchLocalPinsStats, doRemovePinningService, localPinsSize, localNumberOfPins, t }) => {
const [isModalOpen, setModalOpen] = useState(false)
const [isToggleModalOpen, setToggleModalOpen] = useState(false)
const onModalOpen = () => setModalOpen(true)
Expand All @@ -35,12 +35,12 @@ export const PinningManager = ({ pinningServices, ipfsReady, arePinningServicesS
})

useEffect(() => {
ipfsReady && doFetchPinningServices() && doFetchPinningServicesStats()
}, [ipfsReady, doFetchPinningServices, doFetchPinningServicesStats])
ipfsReady && doFetchPinningServices() && doFetchPinningServicesStats() && doFetchLocalPinsStats()
}, [ipfsReady, doFetchPinningServices, doFetchPinningServicesStats, doFetchLocalPinsStats])

const localPinning = useMemo(() =>
({ name: t('localPinning'), type: 'LOCAL', totalSize: pinsSize, numberOfPins }),
[numberOfPins, pinsSize, t])
({ name: t('localPinning'), type: 'LOCAL', totalSize: localPinsSize, numberOfPins: localNumberOfPins }),
[localNumberOfPins, localPinsSize, t])

const sortedServices = useMemo(() =>
(pinningServices || []).sort(sortByProperty(sortSettings.sortBy, sortSettings.sortDirection === SortDirection.ASC ? 1 : -1)),
Expand Down Expand Up @@ -187,12 +187,13 @@ const OptionsCell = ({ doRemovePinningService, name, visitServiceUrl, autoUpload

export default connect(
'selectIpfsReady',
'selectPinsSize',
'selectNumberOfPins',
'selectLocalPinsSize',
'selectLocalNumberOfPins',
'selectPinningServices',
'selectArePinningServicesSupported',
'doFetchPinningServices',
'doFetchPinningServicesStats',
'doFetchLocalPinsStats',
'doRemovePinningService',
PinningManager
)