-
Notifications
You must be signed in to change notification settings - Fork 88
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
Sort the nodes table #987
base: staging
Are you sure you want to change the base?
Sort the nodes table #987
Changes from 10 commits
d06f069
e8b9d40
7f4cb10
2771dc9
a058890
6c047c7
45a808b
357e5b4
5f660c5
71d46b3
e711089
68fa586
be1e4d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { FC } from 'react' | ||
import { FC, useState } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { Loader } from '../shared/components/Loader' | ||
import { durationToHuman } from '../shared/utils' | ||
|
@@ -15,21 +15,28 @@ const renderLastLedger = (ledger) => | |
) : ( | ||
<i>unknown</i> | ||
) | ||
|
||
const renderLedgerHistory = (ledgers, range) => { | ||
const getLedgerHistory = (ledgers, range, MAX_WIDTH = 160) => { | ||
let count = 0 | ||
const MAX_WIDTH = 160 | ||
let boxes = '' | ||
const min = Math.max(range[1] - 10000000, range[0]) | ||
const diff = range[1] - min | ||
|
||
if (ledgers) { | ||
const boxes = ledgers.map((l) => { | ||
boxes = ledgers.map((l) => { | ||
const [low, high] = l | ||
const width = Math.min((high - low + 1) / diff, 1) * MAX_WIDTH | ||
const left = Math.max((low - min) / diff, 0) * MAX_WIDTH | ||
count += high - low | ||
return <div key={low} style={{ left, width }} /> | ||
}) | ||
} | ||
return { boxes, count } | ||
} | ||
const renderLedgerHistory = (ledgers, range) => { | ||
const MAX_WIDTH = 160 | ||
|
||
if (ledgers) { | ||
const { boxes, count } = getLedgerHistory(ledgers, range, MAX_WIDTH) | ||
|
||
if (count < 0) { | ||
return null | ||
|
@@ -99,59 +106,200 @@ export const NodesTable: FC<{ nodes: NodeData[] }> = ({ | |
}) => { | ||
const nodes = unformattedNodes ? formatLedgerHistory(unformattedNodes) : null | ||
const ledgerRange = nodes && getLedgerRange(nodes) | ||
const [sortedField, setSortedField] = useState(null) | ||
const [sortOrder, setSortOrder] = useState(null) | ||
|
||
const requestSort = (key) => { | ||
let direction = 'desc' | ||
if (sortedField === key && sortOrder === 'desc') { | ||
direction = 'asc' | ||
} | ||
setSortOrder(direction) | ||
setSortedField(key) | ||
} | ||
|
||
const { t } = useTranslation() | ||
const renderNode = (node) => ( | ||
<tr key={node.node_public_key}> | ||
<td className="pubkey text-truncate">{node.node_public_key}</td> | ||
<td className="ip text-truncate">{node.ip}</td> | ||
<td className="state center"> | ||
<td className={getClassNamesFor('pubkey text-truncate', 'pubkey')}> | ||
{node.node_public_key} | ||
</td> | ||
<td className={getClassNamesFor('ip text-truncate', 'ip')}>{node.ip}</td> | ||
<td className={getClassNamesFor('state center', 'server_state')}> | ||
<span className={node.server_state}>{node.server_state}</span> | ||
</td> | ||
<td className="version">{getVersion(node.version)}</td> | ||
<td className="last-ledger">{renderLastLedger(node.validated_ledger)}</td> | ||
<td className="uptime">{durationToHuman(node.uptime)}</td> | ||
<td className="peers right"> | ||
<td className={getClassNamesFor('version', 'rippled_version')}> | ||
{getVersion(node.version)} | ||
</td> | ||
<td className={getClassNamesFor('last-ledger', 'last_ledger')}> | ||
{renderLastLedger(node.validated_ledger)} | ||
</td> | ||
<td className={getClassNamesFor('uptime', 'uptime')}> | ||
{durationToHuman(node.uptime)} | ||
</td> | ||
<td className={getClassNamesFor('peers right', 'peers')}> | ||
{node.inbound_count + node.outbound_count} | ||
</td> | ||
<td className="in-out"> | ||
<small> | ||
({node.inbound_count}:{node.outbound_count}) | ||
</small> | ||
</td> | ||
<td className="ledgers"> | ||
<td className={getClassNamesFor('ledgers', 'ledger_history')}> | ||
{renderLedgerHistory(node.ledgers, ledgerRange)} | ||
</td> | ||
<td className="quorum right">{node.quorum}</td> | ||
<td className="load-factor right"> | ||
<td className={getClassNamesFor('quorum right', 'quorum')}> | ||
{node.quorum} | ||
</td> | ||
<td className={getClassNamesFor('load-factor right', 'load')}> | ||
{node.load_factor && node.load_factor > 1 | ||
? node.load_factor.toFixed(2) | ||
: ''} | ||
</td> | ||
<td className="latency right"> | ||
<td className={getClassNamesFor('latency right', 'latency')}> | ||
{node.io_latency_ms && node.io_latency_ms > 1} | ||
</td> | ||
</tr> | ||
) | ||
|
||
const compareSemanticVersions = ( | ||
a: string, | ||
b: string, | ||
returnValue: number, | ||
) => { | ||
const a1 = a.split('.') | ||
const b1 = b.split('.') | ||
|
||
const len = Math.min(a1.length, b1.length) | ||
|
||
for (let i = 0; i < len; i++) { | ||
const a2 = +a1[i] || 0 | ||
const b2 = +b1[i] || 0 | ||
|
||
if (a2 !== b2) { | ||
return a2 > b2 ? returnValue : returnValue * -1 | ||
} | ||
} | ||
return b1.length - a1.length | ||
} | ||
|
||
if (nodes !== null) { | ||
const sort = (key: any, order: string) => { | ||
const returnValue = order === 'desc' ? 1 : -1 | ||
|
||
if (key === 'peers') { | ||
nodes.sort((a, b) => | ||
a.inbound_count + a.outbound_count > | ||
b.inbound_count + b.outbound_count | ||
? returnValue * -1 | ||
: returnValue, | ||
) | ||
} else if (key === 'ledger_history') { | ||
nodes.sort((a, b) => | ||
getLedgerHistory(a.ledgers, ledgerRange).count > | ||
getLedgerHistory(b.ledgers, ledgerRange).count | ||
? returnValue * -1 | ||
: returnValue, | ||
) | ||
} else if (key === 'rippled_version') { | ||
nodes.sort((a, b) => | ||
compareSemanticVersions(a.version, b.version, returnValue), | ||
) | ||
} else { | ||
nodes.sort((a, b) => (a[key] > b[key] ? returnValue * -1 : returnValue)) | ||
} | ||
} | ||
|
||
sort(sortedField, sortOrder) | ||
} | ||
|
||
const getClassNamesFor = (name, order) => | ||
sortedField === order ? `${name} sorted` : name | ||
const getClassArrowFor = (field) => { | ||
if (sortedField === field && sortOrder === 'desc') { | ||
return 'arrow down' | ||
} | ||
if (sortedField === field && sortOrder === 'asc') { | ||
return 'arrow up' | ||
} | ||
return '' | ||
} | ||
|
||
const content = nodes ? ( | ||
<table className="basic"> | ||
<thead> | ||
<tr> | ||
<th className="pubkey">{t('node_pubkey')}</th> | ||
<th className="ip">{t('ip')}</th> | ||
<th className="server-state center">{t('state')}</th> | ||
<th className="version">{t('rippled_version')}</th> | ||
<th className="last-ledger">{t('last_ledger')}</th> | ||
<th className="uptime">{t('uptime')}</th> | ||
<th className="peers right">{t('peers')}</th> | ||
<th className={getClassNamesFor('pubkey', 'pubkey')}> | ||
<a href="#" onClick={() => requestSort('pubkey')}> | ||
<i className={getClassArrowFor('pubkey')} /> | ||
{t('node_pubkey')} | ||
</a> | ||
</th> | ||
<th className={getClassNamesFor('ip', 'ip')}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe can create a reusable component like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im working on this even after the rebase, should have that function pushed up one level and then also use this sorting on the validators page. |
||
<a href="#" onClick={() => requestSort('ip')}> | ||
<i className={getClassArrowFor('ip')} /> | ||
{t('ip')} | ||
</a> | ||
</th> | ||
<th | ||
className={getClassNamesFor('server-state center', 'server_state')} | ||
> | ||
<a href="#" onClick={() => requestSort('server_state')}> | ||
<i className={getClassArrowFor('server_state')} /> | ||
{t('state')} | ||
</a> | ||
</th> | ||
<th className={getClassNamesFor('version', 'rippled_version')}> | ||
<a href="#" onClick={() => requestSort('rippled_version')}> | ||
<i className={getClassArrowFor('rippled_version')} /> | ||
{t('rippled_version')}{' '} | ||
</a> | ||
</th> | ||
<th className={getClassNamesFor('last-ledger', 'last_ledger')}> | ||
<a href="#" onClick={() => requestSort('last_ledger')}> | ||
<i className={getClassArrowFor('last_ledger')} /> | ||
{t('last_ledger')} | ||
</a> | ||
</th> | ||
<th className={getClassNamesFor('uptime', 'uptime')}> | ||
<a href="#" onClick={() => requestSort('uptime')}> | ||
<i className={getClassArrowFor('uptime')} /> | ||
{t('uptime')} | ||
</a> | ||
</th> | ||
<th className={getClassNamesFor('peers right', 'peers')}> | ||
<a href="#" onClick={() => requestSort('peers')}> | ||
<i className={getClassArrowFor('peers')} /> | ||
{t('peers')} | ||
</a> | ||
</th> | ||
<th className="in-out"> | ||
<small>{t('in_out')}</small> | ||
</th> | ||
<th className="ledgers">{t('ledger_history')}</th> | ||
<th className="quorum right">{t('quorum')}</th> | ||
<th className="load-factor right">{t('load')}</th> | ||
<th className="latency right">{t('latency')}</th> | ||
<th className={getClassNamesFor('ledgers', 'ledger_history')}> | ||
<a href="#" onClick={() => requestSort('ledger_history')}> | ||
<i className={getClassArrowFor('ledger_history')} /> | ||
{t('ledger_history')} | ||
</a> | ||
</th> | ||
<th className={getClassNamesFor('quorum right', 'quorum')}> | ||
<a href="#" onClick={() => requestSort('quorum')}> | ||
<i className={getClassArrowFor('quorum')} /> | ||
{t('quorum')} | ||
</a> | ||
</th> | ||
<th className={getClassNamesFor('load-factor right', 'load')}> | ||
<a href="#" onClick={() => requestSort('load')}> | ||
<i className={getClassArrowFor('load')} /> | ||
{t('load')} | ||
</a> | ||
</th> | ||
<th className={getClassNamesFor('latency right', 'latency')}> | ||
<a href="#" onClick={() => requestSort('latency')}> | ||
<i className={getClassArrowFor('latency')} /> | ||
{t('latency')} | ||
</a> | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody>{nodes.map(renderNode)}</tbody> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add tests for this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests have now been added that cover the compareSemanticVersions, via sorting the Version column.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to see this function tested separately and cover all cases. similar to this:
explorer/src/containers/shared/test/utils.test.ts
Lines 13 to 33 in 1e808ad