fields.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import invert from 'lodash/invert';
  2. import {
  3. SelectValue,
  4. SessionAggregationColumn,
  5. SessionField,
  6. SessionsMeta,
  7. SessionsOperation,
  8. SessionStatus,
  9. } from 'sentry/types';
  10. import {defined} from 'sentry/utils';
  11. import {FieldValue, FieldValueKind} from 'sentry/views/discover/table/types';
  12. enum SessionMetric {
  13. SESSION = 'sentry.sessions.session',
  14. SESSION_DURATION = 'sentry.sessions.session.duration',
  15. SESSION_ERROR = 'sentry.sessions.session.error',
  16. SESSION_CRASH_FREE_RATE = 'session.crash_free_rate',
  17. USER_CRASH_FREE_RATE = 'session.crash_free_user_rate',
  18. SESSION_CRASH_RATE = 'session.crash_rate',
  19. USER_CRASH_RATE = 'session.crash_user_rate',
  20. USER = 'sentry.sessions.user',
  21. SESSION_HEALTHY = 'session.healthy',
  22. USER_HEALTHY = 'session.healthy_user',
  23. SESSION_ABNORMAL = 'session.abnormal',
  24. USER_ABNORMAL = 'session.abnormal_user',
  25. SESSION_CRASHED = 'session.crashed',
  26. USER_CRASHED = 'session.crashed_user',
  27. SESSION_ERRORED = 'session.errored',
  28. USER_ERRORED = 'session.errored_user',
  29. }
  30. export const DERIVED_STATUS_METRICS_PATTERN =
  31. /count_(abnormal|errored|crashed|healthy)\((user|session)\)/;
  32. export enum DerivedStatusFields {
  33. HEALTHY_SESSIONS = 'count_healthy(session)',
  34. HEALTHY_USERS = 'count_healthy(user)',
  35. ABNORMAL_SESSIONS = 'count_abnormal(session)',
  36. ABNORMAL_USERS = 'count_abnormal(user)',
  37. CRASHED_SESSIONS = 'count_crashed(session)',
  38. CRASHED_USERS = 'count_crashed(user)',
  39. ERRORED_SESSIONS = 'count_errored(session)',
  40. ERRORED_USERS = 'count_errored(user)',
  41. }
  42. export const FIELD_TO_METRICS_EXPRESSION = {
  43. 'count_healthy(session)': SessionMetric.SESSION_HEALTHY,
  44. 'count_healthy(user)': SessionMetric.USER_HEALTHY,
  45. 'count_abnormal(session)': SessionMetric.SESSION_ABNORMAL,
  46. 'count_abnormal(user)': SessionMetric.USER_ABNORMAL,
  47. 'count_crashed(session)': SessionMetric.SESSION_CRASHED,
  48. 'count_crashed(user)': SessionMetric.USER_CRASHED,
  49. 'count_errored(session)': SessionMetric.SESSION_ERRORED,
  50. 'count_errored(user)': SessionMetric.USER_ERRORED,
  51. 'count_unique(user)': `count_unique(${SessionMetric.USER})`,
  52. 'sum(session)': `sum(${SessionMetric.SESSION})`,
  53. 'crash_free_rate(session)': SessionMetric.SESSION_CRASH_FREE_RATE,
  54. 'crash_free_rate(user)': SessionMetric.USER_CRASH_FREE_RATE,
  55. 'crash_rate(session)': SessionMetric.SESSION_CRASH_RATE,
  56. 'crash_rate(user)': SessionMetric.USER_CRASH_RATE,
  57. project: 'project_id',
  58. };
  59. export const METRICS_EXPRESSION_TO_FIELD = invert(FIELD_TO_METRICS_EXPRESSION);
  60. export const DISABLED_SORT = [
  61. 'count_errored(session)',
  62. 'count_errored(user)',
  63. 'count_healthy(session)',
  64. 'count_healthy(user)',
  65. 'session.status',
  66. ];
  67. export const TAG_SORT_DENY_LIST = ['project', 'environment'];
  68. export const SESSIONS_FIELDS: Readonly<Partial<Record<SessionField, SessionsMeta>>> = {
  69. [SessionField.SESSION]: {
  70. name: 'session',
  71. operations: [
  72. 'sum',
  73. 'crash_rate',
  74. 'crash_free_rate',
  75. 'count_healthy',
  76. 'count_abnormal',
  77. 'count_crashed',
  78. 'count_errored',
  79. ],
  80. type: 'integer',
  81. },
  82. [SessionField.USER]: {
  83. name: 'user',
  84. operations: [
  85. 'count_unique',
  86. 'crash_rate',
  87. 'crash_free_rate',
  88. 'count_healthy',
  89. 'count_abnormal',
  90. 'count_crashed',
  91. 'count_errored',
  92. ],
  93. type: 'string',
  94. },
  95. [SessionField.SESSION_DURATION]: {
  96. name: 'session.duration',
  97. operations: [],
  98. type: 'duration',
  99. },
  100. };
  101. export const SESSIONS_OPERATIONS: Readonly<
  102. Record<SessionsOperation, SessionAggregationColumn>
  103. > = {
  104. sum: {
  105. outputType: 'integer',
  106. parameters: [
  107. {
  108. kind: 'column',
  109. columnTypes: ['integer'],
  110. defaultValue: SessionField.SESSION,
  111. required: true,
  112. },
  113. ],
  114. },
  115. count_unique: {
  116. outputType: 'integer',
  117. parameters: [
  118. {
  119. kind: 'column',
  120. columnTypes: ['string'],
  121. defaultValue: SessionField.USER,
  122. required: true,
  123. },
  124. ],
  125. },
  126. count_healthy: {
  127. outputType: 'integer',
  128. parameters: [
  129. {
  130. kind: 'column',
  131. columnTypes: ['integer', 'string'],
  132. defaultValue: SessionField.SESSION,
  133. required: true,
  134. },
  135. ],
  136. },
  137. count_abnormal: {
  138. outputType: 'integer',
  139. parameters: [
  140. {
  141. kind: 'column',
  142. columnTypes: ['integer', 'string'],
  143. defaultValue: SessionField.SESSION,
  144. required: true,
  145. },
  146. ],
  147. },
  148. count_crashed: {
  149. outputType: 'integer',
  150. parameters: [
  151. {
  152. kind: 'column',
  153. columnTypes: ['integer', 'string'],
  154. defaultValue: SessionField.SESSION,
  155. required: true,
  156. },
  157. ],
  158. },
  159. count_errored: {
  160. outputType: 'integer',
  161. parameters: [
  162. {
  163. kind: 'column',
  164. columnTypes: ['integer', 'string'],
  165. defaultValue: SessionField.SESSION,
  166. required: true,
  167. },
  168. ],
  169. },
  170. crash_rate: {
  171. outputType: 'percentage',
  172. parameters: [
  173. {
  174. kind: 'column',
  175. columnTypes: ['integer', 'string'],
  176. defaultValue: SessionField.SESSION,
  177. required: true,
  178. },
  179. ],
  180. },
  181. crash_free_rate: {
  182. outputType: 'percentage',
  183. parameters: [
  184. {
  185. kind: 'column',
  186. columnTypes: ['integer', 'string'],
  187. defaultValue: SessionField.SESSION,
  188. required: true,
  189. },
  190. ],
  191. },
  192. };
  193. export const SESSIONS_TAGS = ['environment', 'project', 'release', 'session.status'];
  194. export const SESSIONS_FILTER_TAGS = ['environment', 'project', 'release'];
  195. export const SESSION_STATUSES = Object.values(SessionStatus);
  196. export function generateReleaseWidgetFieldOptions(
  197. fields: SessionsMeta[] = Object.values(SESSIONS_FIELDS),
  198. tagKeys?: string[]
  199. ) {
  200. const fieldOptions: Record<string, SelectValue<FieldValue>> = {};
  201. const operations = new Set<SessionsOperation>();
  202. const knownOperations = Object.keys(SESSIONS_OPERATIONS);
  203. fields
  204. .sort((a, b) => a.name.localeCompare(b.name))
  205. .forEach(field => {
  206. field.operations.forEach(operation => operations.add(operation));
  207. fieldOptions[`field:${field.name}`] = {
  208. label: field.name,
  209. value: {
  210. kind: FieldValueKind.METRICS,
  211. meta: {
  212. name: field.name,
  213. dataType: field.type,
  214. },
  215. },
  216. };
  217. });
  218. Array.from(operations)
  219. .filter(operation => knownOperations.includes(operation))
  220. .sort((a, b) => a.localeCompare(b))
  221. .forEach(operation => {
  222. fieldOptions[`function:${operation}`] = {
  223. label: `${operation}(${'\u2026'})`,
  224. value: {
  225. kind: FieldValueKind.FUNCTION,
  226. meta: {
  227. name: operation,
  228. parameters: SESSIONS_OPERATIONS[operation].parameters.map(param => param),
  229. },
  230. },
  231. };
  232. });
  233. if (defined(tagKeys)) {
  234. // Expose environment. session.status, project etc. as fields.
  235. tagKeys
  236. .sort((a, b) => a.localeCompare(b))
  237. .forEach(tag => {
  238. fieldOptions[`field:${tag}`] = {
  239. label: tag,
  240. value: {
  241. kind: FieldValueKind.FIELD,
  242. meta: {name: tag, dataType: 'string'},
  243. },
  244. };
  245. });
  246. }
  247. return fieldOptions;
  248. }