Skip to content

Commit 6494736

Browse files
authored
Merge pull request #390 from protofire/performance/384-final
2 parents 75f9459 + 74d96be commit 6494736

32 files changed

+234588
-379
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
REACT_APP_GRAPH_NETWORK='Mainnet'
22
REACT_APP_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/makerdao-governance'
33
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
4-
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
4+
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
5+
REACT_APP_LAST_CACHE_UPDATE='1587733935'
6+
REACT_APP_HOME_DATA_TTL='5' # IN MINUTES

netlify.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@
1212
REACT_APP_GRAPH_WS = "wss://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
1313
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
1414
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
15+
REACT_APP_LAST_CACHE_UPDATE='1587733935'
16+
REACT_APP_HOME_DATA_TTL='5' # IN MINUTES
1517

1618
[context.branch-deploy.environment]
1719
REACT_APP_GRAPH_HTTP = "https://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
1820
REACT_APP_GRAPH_WS = "wss://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
1921
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
2022
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
23+
REACT_APP_LAST_CACHE_UPDATE='1587733935'
24+
REACT_APP_HOME_DATA_TTL='5' # IN MINUTES
2125

2226
[context.deploy-preview.environment]
23-
REACT_APP_GRAPH_HTTP = "https://api.thegraph.com/subgraphs/name/lmcorbalan/makerdao-governance"
24-
REACT_APP_GRAPH_WS = "wss://api.thegraph.com/subgraphs/name/lmcorbalan/makerdao-governance"
27+
REACT_APP_GRAPH_HTTP = "https://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
28+
REACT_APP_GRAPH_WS = "wss://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
2529
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
2630
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
31+
REACT_APP_LAST_CACHE_UPDATE='1587733935'
32+
REACT_APP_HOME_DATA_TTL='5' # IN MINUTES

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"gray-matter": "^4.0.2",
3131
"install": "^0.13.0",
3232
"ipfs-only-hash": "^1.0.2",
33+
"localforage": "^1.7.3",
3334
"lscache": "^1.3.0",
3435
"match-sorter": "^4.0.2",
3536
"npm": "^6.11.3",
@@ -46,6 +47,7 @@
4647
"react-tooltip": "^3.11.1",
4748
"recharts": "^1.7.1",
4849
"sanitize.css": "^11.0.0",
50+
"save-as-file": "^0.2.0",
4951
"styled-components": "^4.3.2",
5052
"subscriptions-transport-ws": "^0.9.16",
5153
"typescript": "3.6.2"

src/components/Home/HomeDetail.tsx

Lines changed: 50 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
PageTitle,
2828
PageSubTitle,
2929
} from '../common'
30-
import { getPollsData, getMKRSupply } from '../../utils/makerdao'
30+
import { getPollsMetaData } from '../../utils/makerdao'
3131
import { getModalContainer, getPollData, getPollsBalances } from '../../utils'
3232
import {
3333
getStakedMkrData,
@@ -50,6 +50,7 @@ import {
5050
//getActivenessBreakdown,
5151
getMostVotedPolls,
5252
getRecentPolls,
53+
getMKRResponsiveness,
5354
} from './helpers'
5455
import styled from 'styled-components'
5556
import LinkableComponent from '../common/LinkableComponent'
@@ -72,6 +73,7 @@ const Loading = () => (
7273

7374
const getParticipation = (data, mkrSupply) => {
7475
const totalMkr: BigNumber = data.reduce((acc, value) => acc.plus(new BigNumber(value.mkr)), new BigNumber('0'))
76+
7577
return totalMkr
7678
.times(100)
7779
.div(mkrSupply)
@@ -89,71 +91,75 @@ type Props = {
8991
}
9092

9193
function HomeDetail(props: Props) {
92-
const { data, gData, history, executivesResponsiveness } = props
94+
const { data, gData, history } = props
9395
const { governanceInfo } = gData
9496
const [isModalOpen, setModalOpen] = useState(false)
95-
const cachedDataPoll = lscache.get('home-polls') || []
96-
const cachedMkrSupply = lscache.get('mkr-supply') || undefined
97-
98-
const cachedDataTopVoters = lscache.get('home-topVoters') || []
99-
const cachedDataPollsResponsiveness = lscache.get('polls-responsiveness') || []
10097

10198
const [isModalChart, setModalChart] = useState(false)
10299
const [chartFilters, setChartFilters] = useState(defaultFilters)
103-
const [mkrSupply, setMkrSupply] = useState<BigNumber | undefined>(cachedMkrSupply)
104-
const [pollsBalances, setBalances] = useState<any>({})
105100

106101
const [modalData, setModalData] = useState({ type: '', component: '' })
107-
const [topVoters, setTopVoters] = useState<any[]>(cachedDataTopVoters)
108-
const [pollsResponsiveness, setPollsResponsiveness] = useState<any[]>(cachedDataPollsResponsiveness)
109-
//const [activenessBreakdown, setActivenessBreakdown] = useState<any>([])
110-
// const [mkrActiveness, setMkrActiveness] = useState<any>([])
102+
103+
const [pollsResponsiveness, setPollsResponsiveness] = useState<any[]>([])
104+
const [topVoters, setTopVoters] = useState<any[]>([])
105+
const [polls, setPolls] = useState<any[]>([])
111106
const [mostVotedPolls, setMostVotedPolls] = useState<any>([])
112-
const [stakedMkr, setStakedMkr] = useState<any>([])
113107
const [recentPolls, setRecentPolls] = useState<any>([])
114108

115-
const [polls, setPolls] = useState<any[]>(cachedDataPoll.length === 0 ? data.polls : cachedDataPoll)
116-
117109
const pollcolumns = expanded => Pollcolumns(expanded)
118-
const votedPollcolumns = () => VotedPollcolumns()
119-
120-
useEffect(() => {
121-
if (cachedDataPoll.length === 0) getPollsBalances(polls).then(balances => setBalances(balances))
122-
if (cachedDataPollsResponsiveness.length === 0)
123-
getPollsMKRResponsiveness(polls).then(responsiveness => setPollsResponsiveness(responsiveness))
124-
}, [polls, cachedDataPoll.length, cachedDataPollsResponsiveness.length])
125-
126-
useEffect(() => {
127-
if (!mkrSupply) {
128-
getMKRSupply().then(supply => setMkrSupply(supply))
129-
}
130-
}, [mkrSupply])
131-
132110
const executiveColumns = expanded => Executivecolumns(expanded)
133111
const topVotersColumns = () => TopVotersColumns()
112+
const votedPollcolumns = () => VotedPollcolumns()
134113
const uncastedExecutiveColumns = () => UncastedExecutivecolumns()
135-
//const activenessBreakdownColumns = () => ActivenessBreakdownColumns()
136114

137-
const executives = data.executives
115+
useEffect(() => {
116+
if (data && data.polls.length && data.executives.length && data.mkrSupply) {
117+
getPollsBalances(data.polls).then(votersSnapshots => {
118+
// TODO - improve function naming (snapshots of acctual voting addresses)
119+
getPollsMetaData(data.polls).then(polls => {
120+
Promise.all(
121+
polls.map(poll => {
122+
return getPollData(poll, votersSnapshots).then(pollData => {
123+
return { ...poll, participation: getParticipation(pollData, data.mkrSupply) }
124+
})
125+
}),
126+
).then(pollsWithPluralityAndParticipation => {
127+
getPollsMKRResponsiveness(polls).then(responsiveness => {
128+
// TODO - are all this state needed?
129+
setPollsResponsiveness(responsiveness)
130+
setPolls(pollsWithPluralityAndParticipation)
131+
setTopVoters(getTopVoters(data.executives, pollsWithPluralityAndParticipation))
132+
setMostVotedPolls(getMostVotedPolls(pollsWithPluralityAndParticipation))
133+
setRecentPolls(getRecentPolls(pollsWithPluralityAndParticipation))
134+
})
135+
})
136+
})
137+
})
138+
}
139+
}, [data])
138140

141+
//////////////////
142+
const [stakedMkr, setStakedMkr] = useState<any>([])
139143
useEffect(() => {
140144
setStakedMkr(getStakedMkrData(data, chartFilters.stakedMkr))
141145
}, [data, chartFilters.stakedMkr])
142146

147+
const giniData = useMemo(() => getGiniData([...data.free, ...data.lock], chartFilters.gini), [
148+
data,
149+
chartFilters.gini,
150+
])
151+
152+
const cachedDataExecutivesResponsiveness = lscache.get('executives-responsiveness') || []
153+
const [executivesResponsiveness, setExecutivesResponsiveness] = useState<any>(cachedDataExecutivesResponsiveness)
143154
useEffect(() => {
144-
if (cachedDataTopVoters.length === 0) {
145-
setTopVoters(getTopVoters(executives, polls))
155+
if (data && data.executives && cachedDataExecutivesResponsiveness.length === 0) {
156+
const responsiveness = getMKRResponsiveness(data.executives)
157+
lscache.set('executives-responsiveness', responsiveness, DEFAULT_CACHE_TTL)
158+
setExecutivesResponsiveness(responsiveness)
146159
}
147-
}, [executives, polls, cachedDataTopVoters.length])
160+
}, [data, cachedDataExecutivesResponsiveness.length])
148161

149-
useEffect(() => {
150-
setMostVotedPolls(getMostVotedPolls(polls))
151-
setRecentPolls(getRecentPolls(polls))
152-
}, [polls])
153-
useEffect(() => {
154-
//setActivenessBreakdown(getActivenessBreakdown(executives))
155-
//setMkrActiveness(getMKRActiveness(executives))
156-
}, [executives])
162+
/////////////////
157163

158164
const getPoll = row => {
159165
if (row.id) history.push(`/poll/${row.id}`)
@@ -163,8 +169,6 @@ function HomeDetail(props: Props) {
163169
if (row.id) history.push(`/executive/${row.id}`)
164170
}
165171

166-
// Data map for building this page
167-
const giniData = getGiniData([...data.free, ...data.lock], chartFilters.gini)
168172
const homeMap = {
169173
table: {
170174
polls: {
@@ -315,7 +319,7 @@ function HomeDetail(props: Props) {
315319
),
316320
},
317321
mkrDistributionPerExecutive: {
318-
data: getMkrDistributionPerExecutive(executives, governanceInfo ? governanceInfo.hat : null),
322+
data: getMkrDistributionPerExecutive(data.executives, governanceInfo ? governanceInfo.hat : null),
319323
component: props => (
320324
<MkrDistributionPerExecutive
321325
expanded
@@ -519,31 +523,6 @@ function HomeDetail(props: Props) {
519523
setModalData(data)
520524
}
521525

522-
useEffect(() => {
523-
if (mkrSupply) {
524-
getPollsData(data.polls).then(result => {
525-
const polls = result.filter(Boolean)
526-
setPolls([...polls])
527-
Promise.all(
528-
polls.map(poll => {
529-
return getPollData(poll, pollsBalances).then(data => {
530-
return { ...poll, participation: getParticipation(data, mkrSupply) }
531-
})
532-
}),
533-
).then(pollsWithPluralityAndParticipation => {
534-
setPolls(pollsWithPluralityAndParticipation)
535-
})
536-
})
537-
}
538-
}, [data.polls, mkrSupply, pollsBalances])
539-
540-
useEffect(() => {
541-
lscache.set('mkr-supply', mkrSupply, DEFAULT_CACHE_TTL)
542-
lscache.set('home-polls', polls, DEFAULT_CACHE_TTL)
543-
lscache.set('home-topVoters', topVoters, DEFAULT_CACHE_TTL)
544-
lscache.set('polls-responsiveness', pollsResponsiveness, DEFAULT_CACHE_TTL)
545-
}, [mkrSupply, polls, topVoters, pollsResponsiveness])
546-
547526
return (
548527
<>
549528
<PageTitle>System Statistics</PageTitle>

src/components/Home/helpers.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
timeLeft,
1616
getPollsBalances,
1717
getVotersBalance,
18+
msToSeconds,
1819
} from '../../utils'
1920
import {
2021
LAST_YEAR,
@@ -91,7 +92,6 @@ export const getStakedMkrData = (data: any, time: string) => {
9192
mkrEvents.map(events => events.filter(({ timestamp }) => timestamp > period.from && timestamp <= period.to)),
9293
totalSupply,
9394
)
94-
//console.log(period, totalSupply)
9595

9696
// Calculate total MKR staked/voting in the period
9797
;({ totalStaked, totalVoting, voters } = calculateVotingMkr(
@@ -513,8 +513,8 @@ export const getPollsMKRResponsiveness = async polls => {
513513
const voteEvents = {}
514514
const days = Math.max(
515515
...polls.map(poll => {
516-
const start = poll.startDate >= 1e12 ? (poll.startDate / 1e3).toFixed(0) : poll.startDate
517-
const end = poll.endDate >= 1e12 ? (poll.endDate / 1e3).toFixed(0) : poll.endDate
516+
const start = msToSeconds(poll.startDate)
517+
const end = msToSeconds(poll.endDate)
518518
const diffDays = differenceInDays(fromUnixTime(end), fromUnixTime(start))
519519

520520
return diffDays

src/components/List/helpers.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const Pollcolumns = () => {
1818
Filter: SelectColumnFilter,
1919
filter: 'includes',
2020
accessor: row => (timeLeft(row.endDate) === 'Ended' ? 'Ended' : 'Active'),
21-
Cell: ({ row }) => (row.original.fetched ? timeLeft(row.original.endDate) : <Loading />),
21+
Cell: ({ row }) => timeLeft(row.original.endDate),
2222
width: 100,
2323
},
2424
{
@@ -80,8 +80,7 @@ export const Pollcolumns = () => {
8080
disableFilters: true,
8181
id: 'date',
8282
sortType: 'datetime',
83-
Cell: ({ row }) =>
84-
row.original.fetched ? format(fromUnixTime(row.original.startDate), 'dd MMM yy') : <Loading />,
83+
Cell: ({ row }) => format(fromUnixTime(row.original.startDate), 'dd MMM yy'),
8584
width: 100,
8685
},
8786
{
@@ -90,7 +89,7 @@ export const Pollcolumns = () => {
9089
disableFilters: true,
9190
separator: true,
9291
sortType: 'datetime',
93-
Cell: ({ row }) => (row.original.fetched ? format(fromUnixTime(row.original.endDate), 'dd MMM yy') : <Loading />),
92+
Cell: ({ row }) => format(fromUnixTime(row.original.endDate), 'dd MMM yy'),
9493
width: 100,
9594
},
9695
]

0 commit comments

Comments
 (0)