anomaliesTable.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import {ReactNode} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import Count from 'sentry/components/count';
  5. import GridEditable, {
  6. COL_WIDTH_UNDEFINED,
  7. GridColumnOrder,
  8. } from 'sentry/components/gridEditable';
  9. import SortLink from 'sentry/components/gridEditable/sortLink';
  10. import {IconArrow} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Organization} from 'sentry/types';
  14. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  15. import {ColumnType, fieldAlignment} from 'sentry/utils/discover/fields';
  16. import {AnomalyInfo} from 'sentry/utils/performance/anomalies/anomaliesQuery';
  17. type Props = {
  18. isLoading: boolean;
  19. location: Location;
  20. organization: Organization;
  21. anomalies?: AnomalyInfo[];
  22. };
  23. const transformRow = (anom: AnomalyInfo): TableDataRowWithExtras => {
  24. return {
  25. anomaly: `#${anom.id}`,
  26. confidence: anom.confidence,
  27. timestamp: new Date(anom.start),
  28. timeInterval: anom.end - anom.start,
  29. expected: anom.expected,
  30. received: anom.received,
  31. };
  32. };
  33. export default function AnomaliesTable(props: Props) {
  34. const {location, organization, isLoading, anomalies} = props;
  35. const data: TableDataRowWithExtras[] = anomalies?.map(transformRow) || [];
  36. return (
  37. <GridEditable
  38. isLoading={isLoading}
  39. data={data}
  40. columnOrder={Object.values(COLUMNS)}
  41. columnSortBy={[]}
  42. grid={{
  43. renderHeadCell,
  44. renderBodyCell: renderBodyCellWithMeta(location, organization),
  45. }}
  46. location={location}
  47. />
  48. );
  49. }
  50. function renderHeadCell(column: TableColumn, _index: number): ReactNode {
  51. const align = fieldAlignment(column.key, COLUMN_TYPE[column.key]);
  52. return (
  53. <SortLink
  54. title={column.name}
  55. align={align}
  56. direction={undefined}
  57. canSort={false}
  58. generateSortLink={() => undefined}
  59. />
  60. );
  61. }
  62. function renderBodyCellWithMeta(location: Location, organization: Organization) {
  63. return (column: TableColumn, dataRow: TableDataRowWithExtras): React.ReactNode => {
  64. const fieldRenderer = getFieldRenderer(column.key, COLUMN_TYPE);
  65. if (column.key === 'confidence') {
  66. return (
  67. <ConfidenceCell>
  68. {dataRow.confidence === 'low' ? (
  69. <LowConfidence>{t('Low Confidence')}</LowConfidence>
  70. ) : (
  71. <HighConfidence>{t('High Confidence')}</HighConfidence>
  72. )}
  73. </ConfidenceCell>
  74. );
  75. }
  76. if (column.key === 'expected') {
  77. return (
  78. <NumberCell>
  79. <Count value={dataRow.expected} />
  80. </NumberCell>
  81. );
  82. }
  83. if (column.key === 'received') {
  84. return (
  85. <NumberCell>
  86. <Count value={dataRow.received} />
  87. <IconArrow
  88. size="sm"
  89. direction={dataRow.received > dataRow.expected ? 'up' : 'down'}
  90. />
  91. </NumberCell>
  92. );
  93. }
  94. return fieldRenderer(dataRow, {location, organization});
  95. };
  96. }
  97. const NumberCell = styled('div')`
  98. display: flex;
  99. justify-content: flex-end;
  100. align-items: center;
  101. gap: ${space(0.5)};
  102. `;
  103. const LowConfidence = styled('div')`
  104. color: ${p => p.theme.yellow300};
  105. `;
  106. const HighConfidence = styled('div')`
  107. color: ${p => p.theme.red300};
  108. `;
  109. const ConfidenceCell = styled('div')`
  110. text-align: left;
  111. justify-self: flex-end;
  112. flex-grow: 1;
  113. `;
  114. type TableColumnKey =
  115. | 'anomaly'
  116. | 'confidence'
  117. | 'timeInterval'
  118. | 'timestamp'
  119. | 'expected'
  120. | 'received';
  121. type TableColumn = GridColumnOrder<TableColumnKey>;
  122. type TableDataRow = Record<TableColumnKey, any>;
  123. type TableDataRowWithExtras = TableDataRow & {};
  124. const COLUMNS: Record<TableColumnKey, TableColumn> = {
  125. anomaly: {
  126. key: 'anomaly',
  127. name: t('Anomaly'),
  128. width: COL_WIDTH_UNDEFINED,
  129. },
  130. confidence: {
  131. key: 'confidence',
  132. name: t('Confidence'),
  133. width: COL_WIDTH_UNDEFINED,
  134. },
  135. timeInterval: {
  136. key: 'timeInterval',
  137. name: t('Time Interval'),
  138. width: COL_WIDTH_UNDEFINED,
  139. },
  140. timestamp: {
  141. key: 'timestamp',
  142. name: t('Timestamp'),
  143. width: COL_WIDTH_UNDEFINED,
  144. },
  145. expected: {
  146. key: 'expected',
  147. name: t('Expected'),
  148. width: COL_WIDTH_UNDEFINED,
  149. },
  150. received: {
  151. key: 'received',
  152. name: t('Received'),
  153. width: COL_WIDTH_UNDEFINED,
  154. },
  155. };
  156. const COLUMN_TYPE: Record<TableColumnKey, ColumnType> = {
  157. anomaly: 'string',
  158. confidence: 'string',
  159. timeInterval: 'duration',
  160. timestamp: 'date',
  161. expected: 'number',
  162. received: 'number',
  163. };