forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pinpoint-apm#7497] ErrorAnalysis > Summary mini chart
- Loading branch information
1 parent
c3c8982
commit 0db4ef4
Showing
8 changed files
with
216 additions
and
0 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
18 changes: 18 additions & 0 deletions
18
web-frontend/src/main/v3/packages/constants/src/types/common/Chart.ts
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,18 @@ | ||
export interface Chart { | ||
title: string; | ||
timestamp: number[]; | ||
metricValueGroups: MetricValueGroup[]; | ||
} | ||
|
||
export interface MetricValueGroup { | ||
groupName: string; | ||
chartType: string; | ||
unit: string; | ||
metricValues: MetricValue[]; | ||
} | ||
|
||
export interface MetricValue { | ||
fieldName: string; | ||
values: number[]; | ||
tags?: any[]; | ||
} |
1 change: 1 addition & 0 deletions
1
web-frontend/src/main/v3/packages/constants/src/types/common/index.ts
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 @@ | ||
export * from './Chart'; |
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
37 changes: 37 additions & 0 deletions
37
.../main/v3/packages/ui/src/components/ErrorAnalysis/table/ErrorGroupedTableVolumneChart.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,37 @@ | ||
import { ErrorAnalysisGroupedErrorList } from '@pinpoint-fe/constants'; | ||
import { ComposedChart, ReferenceLine } from 'recharts'; | ||
import { useRechart } from '../../../hooks/useRechart'; | ||
|
||
export interface ErrorGroupedTableVolumneChart { | ||
chart: ErrorAnalysisGroupedErrorList.ErrorData['chart']; | ||
} | ||
|
||
export const ErrorGroupedTableVolumneChart = ({ chart }: ErrorGroupedTableVolumneChart) => { | ||
const { data, maxValue, renderChartChildComponents } = useRechart(chart); | ||
|
||
return ( | ||
<ComposedChart | ||
width={128} | ||
height={40} | ||
data={data} | ||
margin={{ | ||
top: 6, | ||
right: 30, | ||
bottom: 2, | ||
left: 2, | ||
}} | ||
> | ||
{data?.length && maxValue > -1 && ( | ||
<ReferenceLine | ||
y={maxValue} | ||
stroke="black" | ||
strokeDasharray="3 3" | ||
position="middle" | ||
label={{ position: 'right', value: maxValue }} | ||
ifOverflow="extendDomain" | ||
/> | ||
)} | ||
{renderChartChildComponents()} | ||
</ComposedChart> | ||
); | ||
}; |
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
131 changes: 131 additions & 0 deletions
131
web-frontend/src/main/v3/packages/ui/src/hooks/useRechart.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,131 @@ | ||
import { Chart } from '@pinpoint-fe/constants'; | ||
import { | ||
BarChart, | ||
Bar, | ||
AreaChart, | ||
Area, | ||
LineChart, | ||
Line, | ||
LineProps, | ||
BarProps, | ||
AreaProps, | ||
} from 'recharts'; | ||
import { colors } from '../constant/theme'; | ||
import { type ChartConfig } from '../components/ui/chart'; | ||
import { isNil } from 'lodash'; | ||
import { getRandomColorInHSL } from '../lib/colors'; | ||
import { useState, useEffect } from 'react'; | ||
|
||
export const COLORS = [ | ||
colors.blue[600], | ||
colors.blue[200], | ||
colors.orange[500], | ||
colors.orange[200], | ||
colors.green[600], | ||
colors.green[300], | ||
colors.red[600], | ||
colors.red[300], | ||
colors.purple[500], | ||
colors.purple[300], | ||
colors.stone[700], | ||
colors.stone[400], | ||
colors.pink[400], | ||
colors.pink[200], | ||
colors.gray[500], | ||
colors.gray[300], | ||
colors.lime[500], | ||
colors.yellow[200], | ||
colors.cyan[500], | ||
colors.cyan[200], | ||
]; | ||
|
||
const CHART_DEFINITION = { | ||
line: { | ||
ChartComponent: LineChart, | ||
ChartChildComponent: Line as React.ComponentType<LineProps>, | ||
chartChildProps: { type: 'monotone', dot: false }, | ||
}, | ||
bar: { | ||
ChartComponent: BarChart, | ||
ChartChildComponent: Bar as React.ComponentType<BarProps>, | ||
chartChildProps: {}, | ||
}, | ||
area: { | ||
ChartComponent: AreaChart, | ||
ChartChildComponent: Area as React.ComponentType<AreaProps>, | ||
chartChildProps: { type: 'natural', fillOpacity: '0.4' }, | ||
}, | ||
} as const; | ||
|
||
export interface ChartData { | ||
timestamp: number; | ||
[key: string]: number; | ||
} | ||
|
||
export type ChartDataConfig = ChartConfig & { | ||
[key: string]: { | ||
chartType: string; | ||
}; | ||
}; | ||
|
||
export function useRechart(chart: Chart) { | ||
const [data, setData] = useState<ChartData[]>([]); | ||
const [chartConfig, setChartConfig] = useState<ChartDataConfig>({}); | ||
const [maxValue, setMaxValue] = useState<number>(-1); | ||
|
||
useEffect(() => { | ||
if (!chart) { | ||
return; | ||
} | ||
|
||
const tempData: ChartData[] = []; | ||
const tempChartConfig: ChartDataConfig = {}; | ||
let tempMaxValue = -1; | ||
|
||
chart.timestamp?.forEach((t, i) => { | ||
const dataObj: ChartData = { timestamp: t }; | ||
|
||
chart.metricValueGroups?.forEach((mvg) => { | ||
mvg?.metricValues?.forEach((mv, mvi) => { | ||
const value = (mv?.values?.[i] === -1 ? null : mv?.values?.[i]) as number; | ||
const fieldName = mv?.fieldName || ''; | ||
|
||
if (!isNil(value) && value > tempMaxValue) { | ||
tempMaxValue = value; | ||
} | ||
dataObj[fieldName] = value; | ||
tempChartConfig[fieldName] = { | ||
chartType: mvg?.chartType, | ||
color: COLORS[mvi] ?? getRandomColorInHSL(), | ||
}; | ||
}); | ||
}); | ||
tempData.push(dataObj); | ||
}); | ||
|
||
setData(tempData); | ||
setChartConfig(tempChartConfig); | ||
setMaxValue(tempMaxValue); | ||
}, [chart]); | ||
|
||
function renderChartChildComponents() { | ||
return Object.keys(chartConfig)?.map((configKey) => { | ||
const config = chartConfig[configKey]; | ||
const { ChartChildComponent, chartChildProps } = | ||
CHART_DEFINITION[config?.chartType as keyof typeof CHART_DEFINITION] || | ||
CHART_DEFINITION['line']; | ||
|
||
return ( | ||
<ChartChildComponent | ||
key={configKey} | ||
dataKey={configKey} | ||
fill={chartConfig?.[configKey]?.color} | ||
stroke={chartConfig?.[configKey]?.color} | ||
{...chartChildProps} | ||
/> | ||
); | ||
}); | ||
} | ||
|
||
return { data, chartConfig, maxValue, renderChartChildComponents }; | ||
} |