forked from nervosnetwork/ckb-explorer-frontend
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show Nodes distribution by Country charts (#279)
Co-authored-by: Chen Yu <keithwhisper@gmail.com>
- Loading branch information
1 parent
9d0e75b
commit 623fd45
Showing
8 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
src/pages/StatisticsChart/mining/NodeCountryDistribution.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { useTranslation } from 'react-i18next' | ||
import { EChartOption } from 'echarts' | ||
import { tooltipColor, tooltipWidth, SmartChartPage } from '../common' | ||
import { getPeers, RawPeer } from '../../../services/NodeProbService' | ||
import { useCurrentLanguage } from '../../../utils/i18n' | ||
import { ChartColorConfig } from '../../../constants/common' | ||
|
||
const Colors = [ | ||
'#069ECD', | ||
'#69C7D4', | ||
'#AACFE9', | ||
'#29B97A', | ||
'#66CC99', | ||
'#228159', | ||
'#525860', | ||
'#74808E', | ||
'#9DA6B0', | ||
'#FBB04C', | ||
] | ||
|
||
interface CountryRecord { | ||
country: string | ||
percent: number | ||
} | ||
|
||
const useOption = ( | ||
list: CountryRecord[], | ||
chartColor: ChartColorConfig, | ||
isMobile: boolean, | ||
isThumbnail = false, | ||
): echarts.EChartOption => { | ||
const { t } = useTranslation() | ||
const currentLanguage = useCurrentLanguage() | ||
|
||
const gridThumbnail = { | ||
left: '4%', | ||
right: '10%', | ||
top: '8%', | ||
bottom: '6%', | ||
containLabel: true, | ||
} | ||
const grid = { | ||
left: '3%', | ||
right: '3%', | ||
top: '5%', | ||
bottom: '5%', | ||
containLabel: true, | ||
} | ||
|
||
const tooltip: EChartOption.Tooltip | undefined = !isThumbnail | ||
? { | ||
formatter: data => { | ||
const item = Array.isArray(data) ? data[0] : data | ||
const widthSpan = (value: string) => tooltipWidth(value, currentLanguage === 'en' ? 100 : 120) | ||
let result = `<div>${tooltipColor('#333333')}${widthSpan(t('statistic.country'))} ${item.data.title}</div>` | ||
result += `<div>${tooltipColor(chartColor.colors[0])}${widthSpan(t('statistic.percent'))} ${ | ||
item.data.value | ||
}%</div>` | ||
return result | ||
}, | ||
} | ||
: { | ||
show: false, | ||
} | ||
|
||
return { | ||
color: [chartColor.colors[0], ...Colors], | ||
tooltip, | ||
grid: isThumbnail ? gridThumbnail : grid, | ||
legend: { | ||
show: !isThumbnail, | ||
right: 40, | ||
bottom: 40, | ||
orient: 'vertical', | ||
icon: 'circle', | ||
}, | ||
series: [ | ||
{ | ||
name: t('statistic.node_country_distribution'), | ||
type: 'pie', | ||
radius: isMobile || isThumbnail ? '50%' : '75%', | ||
center: ['50%', '50%'], | ||
itemStyle: { | ||
emphasis: { | ||
shadowBlur: 10, | ||
shadowOffsetX: 0, | ||
shadowColor: 'rgba(0, 0, 0, 0.5)', | ||
}, | ||
}, | ||
data: list.map(data => { | ||
const country = data.country === 'others' ? t(`statistic.others`) : data.country | ||
return { | ||
name: `${country} (${data.percent}%)`, | ||
title: country, | ||
value: data.percent, | ||
} | ||
}), | ||
}, | ||
], | ||
} | ||
} | ||
|
||
const fetchData = async (): Promise<CountryRecord[]> => { | ||
const list: RawPeer[] = await getPeers() | ||
const result: { key: string; value: number } = list.reduce((acc: any, cur: any) => { | ||
acc[cur.country] = (acc[cur.country] || 0) + 1 | ||
return acc | ||
}, {}) | ||
return Object.entries(result).map(v => ({ | ||
country: v[0], | ||
percent: +(((v[1] as number) * 100) / list.length).toFixed(2), | ||
})) | ||
} | ||
|
||
const toCSV = (countryList: CountryRecord[]) => countryList?.map(r => [r.country, `${r.percent}%`]) ?? [] | ||
|
||
export const NodeCountryDistributionChart = ({ isThumbnail = false }: { isThumbnail?: boolean }) => { | ||
const [t] = useTranslation() | ||
|
||
return ( | ||
<SmartChartPage | ||
title={t('statistic.node_country_distribution')} | ||
isThumbnail={isThumbnail} | ||
fetchData={fetchData} | ||
getEChartOption={useOption} | ||
toCSV={toCSV} | ||
queryKey="fetchStatisticNodeCountryDistribution" | ||
/> | ||
) | ||
} | ||
|
||
export default NodeCountryDistributionChart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import axios from 'axios' | ||
import config from '../../config' | ||
import { NETWORK } from '../../constants/common' | ||
|
||
const { PROB_NODE: node } = config | ||
|
||
if (!node) { | ||
throw new Error('NodeProbService not implemented') | ||
} | ||
|
||
export const getPeers = (): Promise<RawPeer[]> => { | ||
return axios | ||
.get(`${node}/peer`, { | ||
params: { | ||
network: NETWORK, | ||
offline_timeout: 10080, | ||
unknown_offline_timeout: 10080, | ||
}, | ||
}) | ||
.then(res => res.data) | ||
} | ||
|
||
interface LastSeen { | ||
secs_since_epoch: number | ||
nanos_since_epoch: number | ||
} | ||
|
||
export interface RawPeer { | ||
id: number | ||
version: string | ||
version_short: string | ||
last_seen: LastSeen[] | ||
country: string | ||
city: string | ||
latitude: number | ||
longitude: number | ||
node_type: number | ||
} |