|
| 1 | +import type {Theme} from '@emotion/react'; |
| 2 | +import type {LineSeriesOption, YAXisComponentOption} from 'echarts'; |
| 3 | + |
| 4 | +import type {AreaChartSeries} from 'sentry/components/charts/areaChart'; |
| 5 | +import XAxis from 'sentry/components/charts/components/xAxis'; |
| 6 | +import AreaSeries from 'sentry/components/charts/series/areaSeries'; |
| 7 | +import type {SessionApiResponse} from 'sentry/types/organization'; |
| 8 | +import { |
| 9 | + getMetricDetectorChartOption, |
| 10 | + transformSessionResponseToSeries, |
| 11 | + type MetricDetectorChartData, |
| 12 | +} from 'sentry/views/detectors/components/details/metric/charts/metricDetectorChartOptions'; |
| 13 | + |
| 14 | +import {DEFAULT_FONT_FAMILY, makeSlackChartDefaults, slackChartSize} from './slack'; |
| 15 | +import type {RenderDescriptor} from './types'; |
| 16 | +import {ChartType} from './types'; |
| 17 | + |
| 18 | +function transformAreaSeries(series: AreaChartSeries[]): LineSeriesOption[] { |
| 19 | + return series.map(({seriesName, data, ...otherSeriesProps}) => { |
| 20 | + const areaSeries = AreaSeries({ |
| 21 | + name: seriesName, |
| 22 | + data: data.map(({name, value}) => [name, value]), |
| 23 | + lineStyle: { |
| 24 | + opacity: 1, |
| 25 | + width: 0.4, |
| 26 | + }, |
| 27 | + areaStyle: { |
| 28 | + opacity: 1.0, |
| 29 | + }, |
| 30 | + animation: false, |
| 31 | + animationThreshold: 1, |
| 32 | + animationDuration: 0, |
| 33 | + ...otherSeriesProps, |
| 34 | + }); |
| 35 | + |
| 36 | + // Fix incident label font family, cannot use Rubik |
| 37 | + if (areaSeries.markLine?.label) { |
| 38 | + areaSeries.markLine.label.fontFamily = DEFAULT_FONT_FAMILY; |
| 39 | + } |
| 40 | + |
| 41 | + return areaSeries; |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +export function makeMetricDetectorCharts( |
| 46 | + theme: Theme |
| 47 | +): Array<RenderDescriptor<ChartType>> { |
| 48 | + const slackChartDefaults = makeSlackChartDefaults(theme); |
| 49 | + const metricDetectorCharts: Array<RenderDescriptor<ChartType>> = []; |
| 50 | + |
| 51 | + const metricDetectorXaxis = XAxis({ |
| 52 | + theme, |
| 53 | + splitNumber: 3, |
| 54 | + isGroupedByDate: true, |
| 55 | + axisLabel: {fontSize: 11, fontFamily: DEFAULT_FONT_FAMILY}, |
| 56 | + }); |
| 57 | + const metricDetectorYaxis: YAXisComponentOption = { |
| 58 | + axisLabel: {fontSize: 11, fontFamily: DEFAULT_FONT_FAMILY}, |
| 59 | + splitLine: { |
| 60 | + lineStyle: { |
| 61 | + color: theme.chartLineColor, |
| 62 | + opacity: 0.3, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }; |
| 66 | + |
| 67 | + metricDetectorCharts.push({ |
| 68 | + key: ChartType.SLACK_METRIC_DETECTOR_EVENTS, |
| 69 | + getOption: (data: MetricDetectorChartData) => { |
| 70 | + const {chartOption} = getMetricDetectorChartOption(data, theme); |
| 71 | + |
| 72 | + return { |
| 73 | + ...chartOption, |
| 74 | + backgroundColor: theme.background, |
| 75 | + series: transformAreaSeries(chartOption.series), |
| 76 | + xAxis: metricDetectorXaxis, |
| 77 | + yAxis: { |
| 78 | + ...chartOption.yAxis, |
| 79 | + ...metricDetectorYaxis, |
| 80 | + axisLabel: { |
| 81 | + ...chartOption.yAxis!.axisLabel, |
| 82 | + ...metricDetectorYaxis.axisLabel, |
| 83 | + }, |
| 84 | + }, |
| 85 | + grid: slackChartDefaults.grid, |
| 86 | + }; |
| 87 | + }, |
| 88 | + ...slackChartSize, |
| 89 | + }); |
| 90 | + |
| 91 | + interface MetricDetectorSessionData |
| 92 | + extends Omit<MetricDetectorChartData, 'timeseriesData'> { |
| 93 | + sessionResponse: SessionApiResponse; |
| 94 | + } |
| 95 | + |
| 96 | + metricDetectorCharts.push({ |
| 97 | + key: ChartType.SLACK_METRIC_DETECTOR_SESSIONS, |
| 98 | + getOption: (data: MetricDetectorSessionData) => { |
| 99 | + const {sessionResponse, detector, ...rest} = data; |
| 100 | + const {chartOption} = getMetricDetectorChartOption( |
| 101 | + { |
| 102 | + ...rest, |
| 103 | + detector, |
| 104 | + timeseriesData: transformSessionResponseToSeries(sessionResponse, detector), |
| 105 | + }, |
| 106 | + theme |
| 107 | + ); |
| 108 | + |
| 109 | + return { |
| 110 | + ...chartOption, |
| 111 | + backgroundColor: theme.background, |
| 112 | + series: transformAreaSeries(chartOption.series), |
| 113 | + xAxis: metricDetectorXaxis, |
| 114 | + yAxis: { |
| 115 | + ...chartOption.yAxis, |
| 116 | + ...metricDetectorYaxis, |
| 117 | + axisLabel: { |
| 118 | + ...chartOption.yAxis!.axisLabel, |
| 119 | + ...metricDetectorYaxis.axisLabel, |
| 120 | + }, |
| 121 | + }, |
| 122 | + grid: slackChartDefaults.grid, |
| 123 | + }; |
| 124 | + }, |
| 125 | + ...slackChartSize, |
| 126 | + }); |
| 127 | + |
| 128 | + return metricDetectorCharts; |
| 129 | +} |
0 commit comments