-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a286aed
commit 9cafb02
Showing
4 changed files
with
187 additions
and
156 deletions.
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
centrifuge-app/src/components/PodIndexerReports/index.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,184 @@ | ||
import { PoolMetadata } from '@centrifuge/centrifuge-js' | ||
import { useCentrifuge } from '@centrifuge/centrifuge-react' | ||
import { Shelf, Text } from '@centrifuge/fabric' | ||
import Chart from 'chart.js/auto' | ||
import * as React from 'react' | ||
import { useQuery } from 'react-query' | ||
import { usePool, usePoolMetadata } from '../../utils/usePools' | ||
|
||
type Props = { | ||
page: keyof Exclude<PoolMetadata['reports'], undefined> | ||
poolId: string | ||
} | ||
|
||
// const POOL_CONFIG_REPORTS: { reports: { [name: string]: { sections: ReportSection[] } } } = { | ||
// reports: { | ||
// poolOverview: { | ||
// sections: [ | ||
// { | ||
// name: 'Exposure by US state', | ||
// aggregate: 'sumOfNormalizedDebtPerState', | ||
// view: 'chart', | ||
// viewData: { type: 'bar' }, | ||
// }, | ||
// { | ||
// name: 'Average FICO score (weighted by outstanding debt) over time', | ||
// aggregate: 'ficoScoreWeightedByNormalizedDebtOverTime', | ||
// view: 'chart', | ||
// viewData: { | ||
// type: 'line', | ||
// options: { | ||
// plugins: { | ||
// legend: { | ||
// labels: { | ||
// font: { | ||
// family: 'Inter', | ||
// size: 20, | ||
// }, | ||
// }, | ||
// }, | ||
// }, | ||
// scales: { | ||
// y: [ | ||
// { | ||
// min: 500, | ||
// max: 700, | ||
// }, | ||
// ], | ||
// }, | ||
// }, | ||
// }, | ||
// }, | ||
// ], | ||
// }, | ||
// }, | ||
// } | ||
|
||
const MOCK_DATA = { | ||
sumOfNormalizedDebtPerState: [ | ||
{ | ||
key1: 'CA', | ||
value1: 200314, | ||
}, | ||
{ | ||
key1: 'TX', | ||
value1: 120123, | ||
}, | ||
{ | ||
key1: 'NY', | ||
value1: 581202, | ||
}, | ||
], | ||
ficoWeightedByNormalizedDebt: [ | ||
{ | ||
key1: '2023-05-20', | ||
value1: 608, | ||
}, | ||
{ | ||
key1: '2023-05-21', | ||
value1: 605, | ||
}, | ||
{ | ||
key1: '2023-05-22', | ||
value1: 610, | ||
}, | ||
{ | ||
key1: '2023-05-23', | ||
value1: 610, | ||
}, | ||
{ | ||
key1: '2023-05-24', | ||
value1: 610, | ||
}, | ||
{ | ||
key1: '2023-05-25', | ||
value1: 605, | ||
}, | ||
{ | ||
key1: '2023-05-26', | ||
value1: 601, | ||
}, | ||
], | ||
} | ||
|
||
export function PodIndexerReports({ page, poolId }: Props) { | ||
const pool = usePool(poolId) | ||
const { data: metadata } = usePoolMetadata(pool) | ||
const centrifuge = useCentrifuge() | ||
const { data } = useQuery( | ||
['podIndexerReports', page], | ||
() => { | ||
const realData = centrifuge.pod.getReports([metadata!.pod!.indexer!, metadata as any, page]) | ||
console.log('realData', realData) | ||
return MOCK_DATA | ||
}, | ||
{ | ||
enabled: !!metadata?.reports, | ||
} | ||
) | ||
|
||
const sectionsWithData = | ||
data && metadata?.reports?.[page].sections.map((s) => ({ ...s, data: (data as any)[s.aggregate] })) | ||
|
||
return sectionsWithData ? <ReportSections sections={sectionsWithData} /> : null | ||
} | ||
|
||
type ReportSectionWithData = Required<PoolMetadata>['reports']['poolOverview']['sections'][0] & { data: any } | ||
|
||
type ChartData = { key1: any; value1: any }[] | ||
|
||
function displayChart(reportSection: ReportSectionWithData, data: ChartData, node: HTMLCanvasElement) { | ||
return new Chart(node, { | ||
...reportSection.viewData, | ||
data: { | ||
labels: data.map((row) => row.key1), | ||
datasets: [ | ||
{ | ||
data: data.map((row) => row.value1), | ||
backgroundColor: '#2762ff', | ||
}, | ||
], | ||
}, | ||
options: { | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
function ReportSections({ sections }: { sections: ReportSectionWithData[] }) { | ||
return ( | ||
<Shelf gap="50px"> | ||
{sections.map((section) => ( | ||
<ReportSection section={section} /> | ||
))} | ||
</Shelf> | ||
) | ||
} | ||
|
||
Chart.defaults.borderColor = '#eee' | ||
Chart.defaults.color = 'rgb(97, 97, 97)' | ||
|
||
function ReportSection({ section }: { section: ReportSectionWithData }) { | ||
const ref = React.useRef<HTMLCanvasElement>(null) | ||
|
||
React.useEffect(() => { | ||
const chart = displayChart(section, section.data, ref.current!) | ||
return () => { | ||
chart.destroy() | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
return ( | ||
<div style={{ width: '50%' }}> | ||
<Text variant="heading5" style={{ marginBottom: '24px' }}> | ||
{section.name} | ||
</Text> | ||
<canvas ref={ref} /> | ||
</div> | ||
) | ||
} |
This file was deleted.
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