utils.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import moment from 'moment';
  2. import BaseChart from 'sentry/components/charts/baseChart';
  3. import {DateTimeObject} from 'sentry/components/charts/utils';
  4. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  5. import type {SeriesDataUnit} from 'sentry/types/echarts';
  6. /**
  7. * Buckets a week of sequential days into one data unit
  8. */
  9. export function sortSeriesByDay(data: SeriesDataUnit[]): SeriesDataUnit[] {
  10. return data.sort((a, b) => new Date(a.name).getTime() - new Date(b.name).getTime());
  11. }
  12. /**
  13. * Convert an object with date as the key to a series
  14. */
  15. export function convertDayValueObjectToSeries(
  16. data: Record<string, number>
  17. ): SeriesDataUnit[] {
  18. return Object.entries(data).map(([bucket, count]) => ({
  19. value: count,
  20. name: new Date(bucket).getTime(),
  21. }));
  22. }
  23. /**
  24. * Takes a sorted array of trend items and groups them by worst/better/no chagne
  25. */
  26. export function groupByTrend<T extends {trend: number}>(data: T[]): T[] {
  27. const worseItems = data.filter(x => Math.round(x.trend) < 0);
  28. const betterItems = data.filter(x => Math.round(x.trend) > 0);
  29. const zeroItems = data.filter(x => Math.round(x.trend) === 0);
  30. return [...worseItems, ...betterItems, ...zeroItems];
  31. }
  32. export const barAxisLabel = (
  33. dataEntries: number
  34. ): React.ComponentProps<typeof BaseChart>['xAxis'] => {
  35. return {
  36. splitNumber: Math.max(Math.round(dataEntries / 7), 4),
  37. type: 'category',
  38. axisTick: {
  39. alignWithLabel: true,
  40. },
  41. axisLabel: {
  42. formatter: (date: string) => {
  43. return moment(new Date(Number(date))).format('MMM D');
  44. },
  45. },
  46. };
  47. };
  48. const INSIGHTS_DEFAULT_STATS_PERIOD = '8w';
  49. export function dataDatetime(
  50. query: Parameters<typeof normalizeDateTimeParams>[0]
  51. ): DateTimeObject {
  52. const {
  53. start,
  54. end,
  55. statsPeriod,
  56. utc: utcString,
  57. } = normalizeDateTimeParams(query, {
  58. allowEmptyPeriod: true,
  59. allowAbsoluteDatetime: true,
  60. allowAbsolutePageDatetime: true,
  61. });
  62. if (!statsPeriod && !start && !end) {
  63. return {period: INSIGHTS_DEFAULT_STATS_PERIOD};
  64. }
  65. // Following getParams, statsPeriod will take priority over start/end
  66. if (statsPeriod) {
  67. return {period: statsPeriod};
  68. }
  69. const utc = utcString === 'true';
  70. if (start && end) {
  71. return utc
  72. ? {
  73. start: moment.utc(start).format(),
  74. end: moment.utc(end).format(),
  75. utc,
  76. }
  77. : {
  78. start: moment(start).utc().format(),
  79. end: moment(end).utc().format(),
  80. utc,
  81. };
  82. }
  83. return {period: INSIGHTS_DEFAULT_STATS_PERIOD};
  84. }