123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- import type {SeriesOption} from 'echarts';
- import isArray from 'lodash/isArray';
- import max from 'lodash/max';
- import XAxis from 'sentry/components/charts/components/xAxis';
- import AreaSeries from 'sentry/components/charts/series/areaSeries';
- import BarSeries from 'sentry/components/charts/series/barSeries';
- import LineSeries from 'sentry/components/charts/series/lineSeries';
- import MapSeries from 'sentry/components/charts/series/mapSeries';
- import {lightenHexToRgb} from 'sentry/components/charts/utils';
- import * as countryCodesMap from 'sentry/data/countryCodesMap';
- import {t} from 'sentry/locale';
- import {EventsGeoData, EventsStats} from 'sentry/types';
- import {lightTheme as theme} from 'sentry/utils/theme';
- import {
- DEFAULT_FONT_FAMILY,
- slackChartDefaults,
- slackChartSize,
- slackGeoChartSize,
- } from './slack';
- import {ChartType, RenderDescriptor} from './types';
- const discoverxAxis = XAxis({
- theme,
- splitNumber: 3,
- isGroupedByDate: true,
- axisLabel: {fontSize: 11, fontFamily: DEFAULT_FONT_FAMILY},
- });
- export const discoverCharts: RenderDescriptor<ChartType>[] = [];
- discoverCharts.push({
- key: ChartType.SLACK_DISCOVER_TOTAL_PERIOD,
- getOption: (
- data:
- | {seriesName: string; stats: EventsStats}
- | {stats: Record<string, EventsStats>; seriesName?: string}
- ) => {
- if (isArray(data.stats.data)) {
- const color = theme.charts.getColorPalette(data.stats.data.length - 2);
- const areaSeries = AreaSeries({
- name: data.seriesName,
- data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
- areaStyle: {color: color?.[0], opacity: 1},
- });
- return {
- ...slackChartDefaults,
- useUTC: true,
- color,
- series: [areaSeries],
- };
- }
- const stats = Object.keys(data.stats).map(key =>
- Object.assign({}, {key}, data.stats[key])
- );
- const color = theme.charts.getColorPalette(stats.length - 2);
- const series = stats
- .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
- .map((s, i) =>
- AreaSeries({
- name: s.key,
- stack: 'area',
- data: s.data.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
- areaStyle: {color: color?.[i], opacity: 1},
- })
- );
- return {
- ...slackChartDefaults,
- xAxis: discoverxAxis,
- useUTC: true,
- color,
- series,
- };
- },
- ...slackChartSize,
- });
- discoverCharts.push({
- key: ChartType.SLACK_DISCOVER_TOTAL_DAILY,
- getOption: (
- data:
- | {seriesName: string; stats: EventsStats}
- | {stats: Record<string, EventsStats>; seriesName?: string}
- ) => {
- if (isArray(data.stats.data)) {
- const color = theme.charts.getColorPalette(data.stats.data.length - 2);
- const barSeries = BarSeries({
- name: data.seriesName,
- data: data.stats.data.map(([timestamp, countsForTimestamp]) => ({
- value: [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ],
- })),
- itemStyle: {color: color?.[0], opacity: 1},
- });
- return {
- ...slackChartDefaults,
- xAxis: discoverxAxis,
- useUTC: true,
- color,
- series: [barSeries],
- };
- }
- const stats = Object.keys(data.stats).map(key =>
- Object.assign({}, {key}, data.stats[key])
- );
- const color = theme.charts.getColorPalette(stats.length - 2);
- const series = stats
- .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
- .map((s, i) =>
- BarSeries({
- name: s.key,
- stack: 'area',
- data: s.data.map(([timestamp, countsForTimestamp]) => ({
- value: [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ],
- })),
- itemStyle: {color: color?.[i], opacity: 1},
- })
- );
- return {
- ...slackChartDefaults,
- xAxis: discoverxAxis,
- useUTC: true,
- color,
- series,
- };
- },
- ...slackChartSize,
- });
- discoverCharts.push({
- key: ChartType.SLACK_DISCOVER_TOP5_PERIOD,
- getOption: (
- data: {stats: Record<string, EventsStats>} | {stats: EventsStats; seriesName?: string}
- ) => {
- if (isArray(data.stats.data)) {
- const color = theme.charts.getColorPalette(data.stats.data.length - 2);
- const areaSeries = AreaSeries({
- data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
- areaStyle: {color: color?.[0], opacity: 1},
- });
- return {
- ...slackChartDefaults,
- useUTC: true,
- color,
- series: [areaSeries],
- };
- }
- const stats = Object.values(data.stats);
- const hasOther = Object.keys(data.stats).includes('Other');
- const color = theme.charts.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
- if (hasOther) {
- color.push(theme.chartOther);
- }
- const series = stats
- .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
- .map((topSeries, i) =>
- AreaSeries({
- stack: 'area',
- data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
- areaStyle: {color: color?.[i], opacity: 1},
- })
- );
- return {
- ...slackChartDefaults,
- xAxis: discoverxAxis,
- useUTC: true,
- color,
- series,
- };
- },
- ...slackChartSize,
- });
- discoverCharts.push({
- key: ChartType.SLACK_DISCOVER_TOP5_PERIOD_LINE,
- getOption: (
- data: {stats: Record<string, EventsStats>} | {stats: EventsStats; seriesName?: string}
- ) => {
- if (isArray(data.stats.data)) {
- const color = theme.charts.getColorPalette(data.stats.data.length - 2);
- const lineSeries = LineSeries({
- data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[0], opacity: 1},
- itemStyle: {color: color?.[0]},
- });
- return {
- ...slackChartDefaults,
- useUTC: true,
- color,
- series: [lineSeries],
- };
- }
- const stats = Object.values(data.stats);
- const hasOther = Object.keys(data.stats).includes('Other');
- const color = theme.charts.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
- if (hasOther) {
- color.push(theme.chartOther);
- }
- const series = stats
- .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
- .map((topSeries, i) =>
- LineSeries({
- data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[i], opacity: 1},
- itemStyle: {color: color?.[i]},
- })
- );
- return {
- ...slackChartDefaults,
- xAxis: discoverxAxis,
- useUTC: true,
- color,
- series,
- };
- },
- ...slackChartSize,
- });
- discoverCharts.push({
- key: ChartType.SLACK_DISCOVER_TOP5_DAILY,
- getOption: (
- data: {stats: Record<string, EventsStats>} | {stats: EventsStats; seriesName?: string}
- ) => {
- if (isArray(data.stats.data)) {
- const color = theme.charts.getColorPalette(data.stats.data.length - 2);
- const areaSeries = AreaSeries({
- data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
- areaStyle: {color: color?.[0], opacity: 1},
- });
- return {
- ...slackChartDefaults,
- useUTC: true,
- color,
- series: [areaSeries],
- };
- }
- const stats = Object.values(data.stats);
- const hasOther = Object.keys(data.stats).includes('Other');
- const color = theme.charts.getColorPalette(stats.length - 2 - (hasOther ? 1 : 0));
- if (hasOther) {
- color.push(theme.chartOther);
- }
- const series = stats
- .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
- .map((topSeries, i) =>
- BarSeries({
- stack: 'area',
- data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- itemStyle: {color: color?.[i], opacity: 1},
- })
- );
- return {
- ...slackChartDefaults,
- xAxis: discoverxAxis,
- useUTC: true,
- color,
- series,
- };
- },
- ...slackChartSize,
- });
- discoverCharts.push({
- key: ChartType.SLACK_DISCOVER_PREVIOUS_PERIOD,
- getOption: (
- data:
- | {seriesName: string; stats: EventsStats}
- | {stats: Record<string, EventsStats>; seriesName?: string}
- ) => {
- if (isArray(data.stats.data)) {
- const dataMiddleIndex = Math.floor(data.stats.data.length / 2);
- const current = data.stats.data.slice(dataMiddleIndex);
- const previous = data.stats.data.slice(0, dataMiddleIndex);
- const color = theme.charts.getColorPalette(data.stats.data.length - 2);
- const areaSeries = AreaSeries({
- name: data.seriesName,
- data: current.map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
- areaStyle: {color: color?.[0], opacity: 1},
- });
- const previousPeriod = LineSeries({
- name: t('previous %s', data.seriesName),
- data: previous.map(([_, countsForTimestamp], i) => [
- current[i][0] * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: theme.gray200, type: 'dotted'},
- itemStyle: {color: theme.gray200},
- });
- return {
- ...slackChartDefaults,
- useUTC: true,
- color,
- series: [areaSeries, previousPeriod],
- };
- }
- const stats = Object.keys(data.stats).map(key =>
- Object.assign({}, {key}, data.stats[key])
- );
- const color = theme.charts.getColorPalette(stats.length - 2);
- const previousPeriodColor = lightenHexToRgb(color);
- const areaSeries: SeriesOption[] = [];
- const lineSeries: SeriesOption[] = [];
- stats
- .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
- .forEach((s, i) => {
- const dataMiddleIndex = Math.floor(s.data.length / 2);
- const current = s.data.slice(dataMiddleIndex);
- const previous = s.data.slice(0, dataMiddleIndex);
- areaSeries.push(
- AreaSeries({
- name: s.key,
- stack: 'area',
- data: s.data
- .slice(dataMiddleIndex)
- .map(([timestamp, countsForTimestamp]) => [
- timestamp * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
- areaStyle: {color: color?.[i], opacity: 1},
- })
- );
- lineSeries.push(
- LineSeries({
- name: t('previous %s', s.key),
- stack: 'previous',
- data: previous.map(([_, countsForTimestamp], index) => [
- current[index][0] * 1000,
- countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- ]),
- lineStyle: {color: previousPeriodColor?.[i], type: 'dotted'},
- itemStyle: {color: previousPeriodColor?.[i]},
- })
- );
- });
- return {
- ...slackChartDefaults,
- xAxis: discoverxAxis,
- useUTC: true,
- color,
- series: [...areaSeries, ...lineSeries],
- };
- },
- ...slackChartSize,
- });
- discoverCharts.push({
- key: ChartType.SLACK_DISCOVER_WORLDMAP,
- getOption: (data: {seriesName: string; stats: {data: EventsGeoData}}) => {
- const mapSeries = MapSeries({
- map: 'sentryWorld',
- name: data.seriesName,
- data: data.stats.data.map(country => ({
- name: country['geo.country_code'],
- value: country.count,
- })),
- nameMap: countryCodesMap.default,
- aspectScale: 0.85,
- zoom: 1.1,
- center: [10.97, 9.71],
- itemStyle: {
- areaColor: theme.gray200,
- borderColor: theme.backgroundSecondary,
- },
- });
- // For absolute values, we want min/max to based on min/max of series
- // Otherwise it should be 0-100
- const maxValue = max(data.stats.data.map(value => value.count)) || 1;
- return {
- backgroundColor: theme.background,
- visualMap: [
- {
- left: 'right',
- min: 0,
- max: maxValue,
- inRange: {
- color: [theme.purple200, theme.purple300],
- },
- text: ['High', 'Low'],
- textStyle: {
- color: theme.textColor,
- },
- // Whether show handles, which can be dragged to adjust "selected range".
- // False because the handles are pretty ugly
- calculable: false,
- },
- ],
- series: [mapSeries],
- };
- },
- ...slackGeoChartSize,
- });
|