This repository has been archived by the owner on Feb 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from etclabscore/feat/stats-miners
feat: miner stats
- Loading branch information
Showing
13 changed files
with
352 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from "react"; | ||
import { VictoryTooltip } from "victory"; | ||
|
||
class CustomPieChartLabel extends React.Component { | ||
public static defaultEvents = (VictoryTooltip as any).defaultEvents; | ||
public render() { | ||
return ( | ||
<> | ||
{(this.props as any).defaultActive && | ||
<VictoryTooltip | ||
{...(this.props as any)} | ||
active={(this.props as any).defaultActive && | ||
(this.props as any).defaultActive.x === (this.props as any).datum.x} | ||
text={`${(this.props as any).datum.x}\n${(this.props as any).datum.y}`} | ||
cornerRadius={5} | ||
height={40} | ||
flyoutStyle={{ | ||
stroke: "none", | ||
}} | ||
/> | ||
} | ||
<VictoryTooltip | ||
{...(this.props as any)} | ||
// active={(this.props as any).defaultActive && | ||
// (this.props as any).defaultActive.x === (this.props as any).datum.x} | ||
width={100} | ||
text={`${(this.props as any).datum.x}\n${(this.props as any).datum.y}`} | ||
// x={(this.props as any).width / 2} | ||
// y={(this.props as any).y + 15} | ||
// orientation="bottom" | ||
// pointerLength={0} | ||
cornerRadius={5} | ||
height={40} | ||
flyoutStyle={{ | ||
stroke: "none", | ||
}} | ||
/> | ||
</> | ||
); | ||
} | ||
} | ||
|
||
export default CustomPieChartLabel; |
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,97 @@ | ||
import React, { useState } from "react"; | ||
import { Grid } from "@material-ui/core"; | ||
import ChartCard from "./ChartCard"; | ||
import { VictoryPie } from "victory"; | ||
import { hexToString } from "@etclabscore/eserialize"; | ||
import CustomPieChartLabel from "./CustomPieChartLabel"; | ||
import { useTranslation } from "react-i18next"; | ||
import _ from "lodash"; | ||
|
||
const blockTopMinerCount = (blocks: any[]) => { | ||
const result = _(blocks).chain() | ||
.countBy((b: any) => hexToString(b.extraData)) | ||
.map((key: string, val: number) => ({ | ||
x: val, | ||
y: key, | ||
label: val, | ||
})) | ||
.sortBy((item: any) => item.y) | ||
.reverse() | ||
.value(); | ||
return result; | ||
}; | ||
|
||
const blockTopMinerCountByAddress = (blocks: any[]) => { | ||
const result = _(blocks).chain() | ||
.countBy((b: any) => b.miner) | ||
.map((key: string, val: number) => ({ | ||
x: val, | ||
y: key, | ||
label: val, | ||
})) | ||
.sortBy((item: any) => item.y) | ||
.reverse() | ||
.value(); | ||
return result; | ||
}; | ||
|
||
interface IProps { | ||
blocks: any[]; | ||
config: any; | ||
} | ||
|
||
const config = { | ||
blockTime: 15, // seconds | ||
blockHistoryLength: 100, | ||
chartHeight: 200, | ||
chartWidth: 400, | ||
}; | ||
|
||
const MinerStats: React.FC<IProps> = ({blocks}) => { | ||
const [showDefaultPieHover, setShowDefaultPieHover] = useState(true); | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<Grid container justify="space-evenly"> | ||
<Grid key="uncles" item xs={12} md={4} lg={4}> | ||
<ChartCard title={t("Top Miners last blocks by address", { count: config.blockHistoryLength })}> | ||
<VictoryPie | ||
cornerRadius={1} | ||
// innerRadius={50} | ||
colorScale="cool" | ||
data={blockTopMinerCountByAddress(blocks)} | ||
events={[{ | ||
target: "data", | ||
eventHandlers: { | ||
onMouseOver: () => { | ||
return [{ | ||
target: "labels", | ||
mutation: () => { | ||
setShowDefaultPieHover(false); | ||
return { active: true }; | ||
}, | ||
}]; | ||
}, | ||
}, | ||
}]} | ||
labelComponent={<CustomPieChartLabel {...{ | ||
defaultActive: showDefaultPieHover ? blockTopMinerCountByAddress(blocks)[0] : undefined, | ||
}} />} | ||
> | ||
</VictoryPie> | ||
</ChartCard> | ||
</Grid> | ||
<Grid key="uncles" item xs={12} md={3} lg={3}> | ||
<ChartCard title={t("Top Miners last blocks by extraData", { count: config.blockHistoryLength })}> | ||
<VictoryPie | ||
colorScale="cool" | ||
labelComponent={<CustomPieChartLabel />} | ||
data={blockTopMinerCount(blocks)} | ||
/> | ||
</ChartCard> | ||
</Grid> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default MinerStats; |
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,97 @@ | ||
import React from "react"; | ||
import { Table, TableRow, TableCell, TableHead, TableBody, Typography, Button } from "@material-ui/core"; | ||
import { hexToString, hexToNumber } from "@etclabscore/eserialize"; | ||
import { useHistory } from "react-router-dom"; | ||
import _ from "lodash"; | ||
import greenColor from "@material-ui/core/colors/green"; | ||
|
||
const blockTopMiners = (blocks: any[]) => { | ||
const result = _(blocks).chain() | ||
.countBy((b: any) => b.miner) | ||
.map((key: string, val: number) => ({ | ||
address: val, | ||
blocksMined: key, | ||
})) | ||
.sortBy((item: any) => item.blocksMined) | ||
.reverse() | ||
.value(); | ||
return result; | ||
}; | ||
|
||
const groupByMiner = (blocks: any[]) => { | ||
const result = _.chain(blocks) | ||
.groupBy((b: any) => b.miner) | ||
.map((value, key) => { | ||
return { | ||
[key]: _.groupBy(value, (item) => { | ||
return hexToString(item.extraData); | ||
}), | ||
}; | ||
}) | ||
.value(); | ||
return result; | ||
}; | ||
|
||
interface IProps { | ||
blocks: any[]; | ||
} | ||
|
||
const MinerStatsTable: React.FC<IProps> = ({ blocks }) => { | ||
const history = useHistory(); | ||
const topMiners = blockTopMiners(blocks); | ||
const groupedMiners = Object.assign({}, ...groupByMiner(blocks)); | ||
return ( | ||
<Table aria-label="simple table"> | ||
<TableHead > | ||
<TableRow> | ||
<TableCell>Blocks Mined</TableCell> | ||
<TableCell>Address</TableCell> | ||
<TableCell>ExtraData</TableCell> | ||
<TableCell>Blocks</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{topMiners.map((minerData: any) => ( | ||
<TableRow key={minerData.miner}> | ||
<TableCell component="th" scope="row"> | ||
{minerData.blocksMined} | ||
</TableCell> | ||
<TableCell>{minerData.address}</TableCell> | ||
<TableCell colSpan={2}> | ||
<Table> | ||
<TableBody> | ||
{_.map(groupedMiners[minerData.address], (bs: any[], key: string) => ( | ||
<TableRow> | ||
<TableCell>{key}</TableCell> | ||
<TableCell colSpan={1}> | ||
{bs.map((block) => { | ||
const percentFull = (hexToNumber(block.gasUsed) / hexToNumber(block.gasLimit)) * 100; | ||
return ( | ||
<Button | ||
variant="outlined" | ||
style={{ | ||
margin: "3px", | ||
background: `linear-gradient(to right, ${greenColor[600]} 0% ${percentFull}%, transparent ${percentFull}% 100%)`, | ||
}} | ||
onClick={() => history.push(`/block/${block.hash}`)} | ||
> | ||
<Typography> | ||
{hexToNumber(block.number)} | ||
</Typography> | ||
</Button> | ||
); | ||
})} | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table > | ||
); | ||
}; | ||
|
||
export default MinerStatsTable; |
Oops, something went wrong.