anomaliesTable.tsx 4.5 KB

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