Skip to content

Commit

Permalink
fix: use getIn to access immutable map
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakia committed Apr 20, 2024
1 parent 86d49a6 commit 0dbfdc2
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 65 deletions.
17 changes: 11 additions & 6 deletions src/core/accounts/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getFilteredRepresentatives(state) {
(r) =>
fuzzySearch(search, r.account || '') ||
fuzzySearch(search, r.alias || '') ||
(r.telemetry.address || '').includes(search)
r.getIn(['telemetry', 'address'], '').includes(search)
)
}

Expand Down Expand Up @@ -83,8 +83,9 @@ export function getRepresentativesTotalWeight(state) {
const accounts = getRepresentatives(state)
let weight = BigNumber(0)
for (const rep of accounts.valueSeq()) {
if (!rep.account_meta.weight) continue
weight = BigNumber(rep.account_meta.weight).plus(weight)
const rep_weight = rep.getIn(['account_meta', 'weight'])
if (!rep_weight) continue
weight = BigNumber(rep_weight).plus(weight)
}

return weight.toNumber()
Expand All @@ -94,8 +95,9 @@ export function getOnlineRepresentativesTotalWeight(state) {
const accounts = getOnlineRepresentatives(state)
let weight = BigNumber(0)
for (const rep of accounts.valueSeq()) {
if (!rep.account_meta.weight) continue
weight = BigNumber(rep.account_meta.weight).plus(weight)
const rep_weight = rep.getIn(['account_meta', 'weight'])
if (!rep_weight) continue
weight = BigNumber(rep_weight).plus(weight)
}

return weight.toNumber()
Expand Down Expand Up @@ -123,5 +125,8 @@ export function getNetworkUnconfirmedBlockCount(state) {

const rep = sorted.first()

return rep.getIn(['telemetry', 'block_count']) - rep.getIn(['telemetry', 'cemented_count'])
return (
rep.getIn(['telemetry', 'block_count']) -
rep.getIn(['telemetry', 'cemented_count'])
)
}
2 changes: 1 addition & 1 deletion src/views/components/representative-alerts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const mapStateToProps = createSelector(
const lowUptime = reps.filter((a) => {
const uptime = a.get('uptime')
const online = uptime.filter((u) => u.online)
const total = uptime.length
const total = uptime.size
const pct = online / total
return pct < 0.75
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,30 @@ export default class RepresentativeAlerts extends React.Component {
)}
</TableCell>
<TableCell className='rep__alert-metric' align='right'>
{BigNumber(row.account.account_meta.weight)
{BigNumber(
row.account.getIn(['account_meta', 'weight'], 0)
)
.shiftedBy(-30)
.toFormat(0)}
</TableCell>
<TableCell className='rep__alert-metric' align='right'>
{row.account.account_meta.weight && onlineWeight
? `${BigNumber(row.account.account_meta.weight)
{row.account.getIn(['account_meta', 'weight'], 0) &&
onlineWeight
? `${BigNumber(
row.account.getIn(['account_meta', 'weight'], 0)
)
.dividedBy(onlineWeight)
.multipliedBy(100)
.toFormat(2)} %`
: '-'}
</TableCell>
<TableCell className='rep__alert-metric' align='right'>
{row.account.telemetry.cemented_behind >= 0
{row.account.getIn(
['telemetry', 'cemented_behind'],
-1
) >= 0
? BigNumber(
row.account.telemetry.cemented_behind
row.account.getIn(['telemetry', 'cemented_behind'])
).toFormat(0)
: '-'}
</TableCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,20 @@ const mapStateToProps = createSelector(
]
const metrics = thresholds.map((p) => ({ ...p, total: 0 }))
for (const rep of accounts.valueSeq()) {
if (!rep.account_meta.weight) continue
const rep_weight = rep.getIn(['account_meta', 'weight'], 0)
if (!rep_weight) continue

// add to unknown
if (rep.telemetry.bandwidth_cap === undefined) {
metrics[0].total = BigNumber(rep.account_meta.weight)
if (rep.getIn(['telemetry', 'bandwidth_cap']) === undefined) {
metrics[0].total = BigNumber(rep_weight)
.plus(metrics[0].total)
.toFixed()
continue
}

// add to unlimited
if (rep.telemetry.bandwidth_cap === 0) {
metrics[1].total = BigNumber(rep.account_meta.weight)
if (rep.getIn(['telemetry', 'bandwidth_cap']) === 0) {
metrics[1].total = BigNumber(rep_weight)
.plus(metrics[1].total)
.toFixed()
continue
Expand All @@ -88,8 +89,8 @@ const mapStateToProps = createSelector(
const lastIdx = Math.max(metrics.length - 1, 0)
let i = 2
for (; i < lastIdx; i++) {
if (rep.telemetry.bandwidth_cap <= metrics[i].threshold) {
metrics[i].total = BigNumber(rep.account_meta.weight)
if (rep.getIn(['telemetry', 'bandwidth_cap']) <= metrics[i].threshold) {
metrics[i].total = BigNumber(rep_weight)
.plus(metrics[i].total)
.toFixed()
break
Expand All @@ -98,7 +99,7 @@ const mapStateToProps = createSelector(

// add to catch all
if (i === lastIdx) {
metrics[i].total = BigNumber(rep.account_meta.weight)
metrics[i].total = BigNumber(rep_weight)
.plus(metrics[i].total)
.toFixed()
}
Expand Down
21 changes: 11 additions & 10 deletions src/views/components/representatives-cemented-by-weight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,32 @@ const mapStateToProps = createSelector(
]
const metrics = thresholds.map((p) => ({ ...p, total: 0 }))
for (const rep of accounts.valueSeq()) {
if (!rep.account_meta.weight) continue
const rep_weight = rep.getIn(['account_meta', 'weight'])
if (!rep_weight) continue

const blocksBehind = rep.telemetry.cemented_behind
if (blocksBehind == null) {
metrics[0].total = BigNumber(rep.account_meta.weight)
const blocks_behind = rep.getIn(['telemetry', 'cemented_behind'])
if (blocks_behind == null) {
metrics[0].total = BigNumber(rep_weight)
.plus(metrics[0].total)
.toFixed()
continue
}

// stop before the last one (catch-all)
const lastIdx = Math.max(metrics.length - 1, 0)
const last_idx = Math.max(metrics.length - 1, 0)
let i = 1
for (; i < lastIdx; i++) {
if (blocksBehind <= metrics[i].threshold) {
metrics[i].total = BigNumber(rep.account_meta.weight)
for (; i < last_idx; i++) {
if (blocks_behind <= metrics[i].threshold) {
metrics[i].total = BigNumber(rep_weight)
.plus(metrics[i].total)
.toFixed()
break
}
}

// add to catch all
if (i === lastIdx) {
metrics[i].total = BigNumber(rep.account_meta.weight)
if (i === last_idx) {
metrics[i].total = BigNumber(rep_weight)
.plus(metrics[i].total)
.toFixed()
}
Expand Down
21 changes: 11 additions & 10 deletions src/views/components/representatives-checked-by-weight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,32 @@ const mapStateToProps = createSelector(
]
const metrics = thresholds.map((p) => ({ ...p, total: 0 }))
for (const rep of accounts.valueSeq()) {
if (!rep.account_meta.weight) continue
const rep_weight = rep.getIn(['account_meta', 'weight'])
if (!rep_weight) continue

const blocksBehind = rep.telemetry.block_behind
if (blocksBehind == null) {
metrics[0].total = BigNumber(rep.account_meta.weight)
const blocks_behind = rep.getIn(['telemetry', 'block_behind'])
if (blocks_behind == null) {
metrics[0].total = BigNumber(rep_weight)
.plus(metrics[0].total)
.toFixed()
continue
}

// stop before the last one (catch-all)
const lastIdx = Math.max(metrics.length - 1, 0)
const last_idx = Math.max(metrics.length - 1, 0)
let i = 1
for (; i < lastIdx; i++) {
if (blocksBehind <= metrics[i].threshold) {
metrics[i].total = BigNumber(rep.account_meta.weight)
for (; i < last_idx; i++) {
if (blocks_behind <= metrics[i].threshold) {
metrics[i].total = BigNumber(rep_weight)
.plus(metrics[i].total)
.toFixed()
break
}
}

// add to catch all
if (i === lastIdx) {
metrics[i].total = BigNumber(rep.account_meta.weight)
if (i === last_idx) {
metrics[i].total = BigNumber(rep_weight)
.plus(metrics[i].total)
.toFixed()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,29 @@ export default class RepresentativesClusterCharts extends React.Component {
const bandwidthData = []
const uncheckedData = []
accounts.forEach((a) => {
if (a.telemetry.cemented_behind > 1000) return
const weight = BigNumber(a.account_meta.weight)
if (a.getIn(['telemetry', 'cemented_behind']) > 1000) return
const weight = BigNumber(a.getIn(['account_meta', 'weight']))
.dividedBy(totalWeight)
.multipliedBy(100)
.toFixed()
const label = a.alias || a.account
confirmationsData.push([a.telemetry.cemented_behind, weight, label])
blocksData.push([a.telemetry.block_behind, weight, label])
peersData.push([a.telemetry.peer_count, weight, label])
uncheckedData.push([a.telemetry.unchecked_count, weight, label])
const label = a.getIn(['alias'], a.getIn(['account']))
confirmationsData.push([
a.getIn(['telemetry', 'cemented_behind']),
weight,
label
])
blocksData.push([a.getIn(['telemetry', 'block_behind']), weight, label])
peersData.push([a.getIn(['telemetry', 'peer_count']), weight, label])
uncheckedData.push([
a.getIn(['telemetry', 'unchecked_count']),
weight,
label
])

// exclude 0 (unlimited)
if (a.telemetry.bandwidth_cap)
if (a.getIn(['telemetry', 'bandwidth_cap']))
bandwidthData.push([
a.telemetry.bandwidth_cap / (1024 * 1024),
a.getIn(['telemetry', 'bandwidth_cap']) / (1024 * 1024),
weight,
label
])
Expand Down
11 changes: 5 additions & 6 deletions src/views/components/representatives-country-by-weight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ const mapStateToProps = createSelector(
}

for (const rep of accounts.valueSeq()) {
if (!rep.account_meta.weight) continue
const weight = rep.getIn(['account_meta', 'weight'])
if (!weight) continue

const country = rep.network.country
const country = rep.getIn(['network', 'country'])
if (!country) {
countries.unknown = BigNumber(rep.account_meta.weight)
.plus(countries.unknown)
.toFixed()
countries.unknown = BigNumber(weight).plus(countries.unknown).toFixed()
continue
}

countries[country] = BigNumber(rep.account_meta.weight)
countries[country] = BigNumber(weight)
.plus(countries[country] || 0)
.toFixed()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class RepresentativesOffline extends React.Component {
account: p.account,
alias: p.alias,
is_online: p.is_online,
weight: p.account_meta.weight || 0,
weight: p.getIn(['account_meta', 'weight'], 0),
last_online: p.last_online,
diff: (p.last_online || 0) - (p.last_offline || 0)
}
Expand Down
11 changes: 5 additions & 6 deletions src/views/components/representatives-provider-by-weight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ const mapStateToProps = createSelector(
}

for (const rep of accounts.valueSeq()) {
if (!rep.account_meta.weight) continue
const weight = rep.getIn(['account_meta', 'weight'])
if (!weight) continue

const provider = rep.network.asname
const provider = rep.getIn(['network', 'asname'])
if (!provider) {
providers.unknown = BigNumber(rep.account_meta.weight)
.plus(providers.unknown)
.toFixed()
providers.unknown = BigNumber(weight).plus(providers.unknown).toFixed()
continue
}

providers[provider] = BigNumber(rep.account_meta.weight)
providers[provider] = BigNumber(weight)
.plus(providers[provider] || 0)
.toFixed()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ const mapStateToProps = createSelector(
const versions = {}

for (const rep of accounts.valueSeq()) {
if (!rep.account_meta.weight) continue
const weight = rep.getIn(['account_meta', 'weight'])
if (!weight) continue

const version = rep.version
versions[version] = BigNumber(rep.account_meta.weight)
versions[version] = BigNumber(weight)
.plus(versions[version] || 0)
.toFixed()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class RepresentativesWeightChart extends React.Component {

const weightData = []
accounts.forEach((a) => {
const bn = BigNumber(a.account_meta.weight)
const bn = BigNumber(a.getIn(['account_meta', 'weight']))
const weight = bn.shiftedBy(-30).toFixed(0)
const pct = bn.dividedBy(denominator).multipliedBy(100).toFixed(1)

Expand Down

0 comments on commit 0dbfdc2

Please sign in to comment.