|
| 1 | +import type {Theme} from '@emotion/react'; |
| 2 | + |
| 3 | +import {XAxis} from 'sentry/components/charts/components/xAxis'; |
| 4 | +import {LineSeries} from 'sentry/components/charts/series/lineSeries'; |
| 5 | +import {timeSeriesItemToEChartsDataPoint} from 'sentry/utils/timeSeries/timeSeriesItemToEChartsDataPoint'; |
| 6 | +import type {TimeSeries} from 'sentry/views/dashboards/widgets/common/types'; |
| 7 | +import {formatTimeSeriesName} from 'sentry/views/dashboards/widgets/timeSeriesWidget/formatters/formatTimeSeriesName'; |
| 8 | + |
| 9 | +import {DEFAULT_FONT_FAMILY, makeSlackChartDefaults, slackChartSize} from './slack'; |
| 10 | +import type {RenderDescriptor} from './types'; |
| 11 | +import {ChartType} from './types'; |
| 12 | + |
| 13 | +type ExploreChartData = { |
| 14 | + seriesName: string; |
| 15 | + timeSeries: TimeSeries[]; |
| 16 | +}; |
| 17 | + |
| 18 | +export const makeExploreCharts = (theme: Theme): Array<RenderDescriptor<ChartType>> => { |
| 19 | + const exploreXAxis = XAxis({ |
| 20 | + theme, |
| 21 | + splitNumber: 3, |
| 22 | + isGroupedByDate: true, |
| 23 | + axisLabel: {fontSize: 11, fontFamily: DEFAULT_FONT_FAMILY}, |
| 24 | + }); |
| 25 | + |
| 26 | + const slackChartDefaults = makeSlackChartDefaults(theme); |
| 27 | + const exploreCharts: Array<RenderDescriptor<ChartType>> = []; |
| 28 | + |
| 29 | + exploreCharts.push({ |
| 30 | + key: ChartType.SLACK_EXPLORE_LINE, |
| 31 | + getOption: (data: ExploreChartData) => { |
| 32 | + const matching = data.timeSeries.filter(ts => ts.yAxis === data.seriesName); |
| 33 | + |
| 34 | + if (matching.length === 0) { |
| 35 | + return { |
| 36 | + ...slackChartDefaults, |
| 37 | + xAxis: exploreXAxis, |
| 38 | + useUTC: true, |
| 39 | + series: [], |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + const hasGroups = matching.some(ts => ts.groupBy && ts.groupBy.length > 0); |
| 44 | + |
| 45 | + if (!hasGroups) { |
| 46 | + const ts = matching[0]!; |
| 47 | + const color = theme.chart.getColorPalette(0); |
| 48 | + const lineSeries = LineSeries({ |
| 49 | + name: data.seriesName, |
| 50 | + data: ts.values.map(timeSeriesItemToEChartsDataPoint), |
| 51 | + lineStyle: {color: color?.[0], opacity: 1}, |
| 52 | + itemStyle: {color: color?.[0]}, |
| 53 | + }); |
| 54 | + |
| 55 | + return { |
| 56 | + ...slackChartDefaults, |
| 57 | + xAxis: exploreXAxis, |
| 58 | + useUTC: true, |
| 59 | + color, |
| 60 | + series: [lineSeries], |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + const sorted = matching |
| 65 | + .slice() |
| 66 | + .sort((a, b) => (a.meta?.order ?? 0) - (b.meta?.order ?? 0)); |
| 67 | + const hasOther = sorted.some(ts => ts.meta?.isOther); |
| 68 | + const color = theme.chart |
| 69 | + .getColorPalette(sorted.length - 1 - (hasOther ? 1 : 0)) |
| 70 | + ?.slice() as string[]; |
| 71 | + if (hasOther) { |
| 72 | + color.push(theme.tokens.content.secondary); |
| 73 | + } |
| 74 | + |
| 75 | + const series = sorted.map((ts, i) => { |
| 76 | + return LineSeries({ |
| 77 | + name: formatTimeSeriesName(ts), |
| 78 | + data: ts.values.map(timeSeriesItemToEChartsDataPoint), |
| 79 | + lineStyle: {color: color?.[i], opacity: 1}, |
| 80 | + itemStyle: {color: color?.[i]}, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + return { |
| 85 | + ...slackChartDefaults, |
| 86 | + xAxis: exploreXAxis, |
| 87 | + useUTC: true, |
| 88 | + color, |
| 89 | + series, |
| 90 | + }; |
| 91 | + }, |
| 92 | + ...slackChartSize, |
| 93 | + }); |
| 94 | + |
| 95 | + return exploreCharts; |
| 96 | +}; |
0 commit comments