fields.spec.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import {
  2. aggregateMultiPlotType,
  3. aggregateOutputType,
  4. explodeField,
  5. fieldAlignment,
  6. generateAggregateFields,
  7. getAggregateAlias,
  8. isAggregateEquation,
  9. isAggregateField,
  10. isMeasurement,
  11. measurementType,
  12. parseFunction,
  13. } from 'sentry/utils/discover/fields';
  14. describe('parseFunction', function () {
  15. it('returns null on non aggregate fields', function () {
  16. expect(parseFunction('field')).toEqual(null);
  17. expect(parseFunction('under_field')).toEqual(null);
  18. expect(parseFunction('foo.bar.is-Enterprise_42')).toEqual(null);
  19. });
  20. it('handles 0 arg functions', function () {
  21. expect(parseFunction('count()')).toEqual({
  22. name: 'count',
  23. arguments: [],
  24. });
  25. expect(parseFunction('count_unique()')).toEqual({
  26. name: 'count_unique',
  27. arguments: [],
  28. });
  29. });
  30. it('handles 1 arg functions', function () {
  31. expect(parseFunction('count(id)')).toEqual({
  32. name: 'count',
  33. arguments: ['id'],
  34. });
  35. expect(parseFunction('count_unique(user)')).toEqual({
  36. name: 'count_unique',
  37. arguments: ['user'],
  38. });
  39. expect(parseFunction('count_unique(issue.id)')).toEqual({
  40. name: 'count_unique',
  41. arguments: ['issue.id'],
  42. });
  43. expect(parseFunction('count(foo.bar.is-Enterprise_42)')).toEqual({
  44. name: 'count',
  45. arguments: ['foo.bar.is-Enterprise_42'],
  46. });
  47. });
  48. it('handles 2 arg functions', function () {
  49. expect(parseFunction('percentile(transaction.duration,0.81)')).toEqual({
  50. name: 'percentile',
  51. arguments: ['transaction.duration', '0.81'],
  52. });
  53. expect(parseFunction('percentile(transaction.duration, 0.11)')).toEqual({
  54. name: 'percentile',
  55. arguments: ['transaction.duration', '0.11'],
  56. });
  57. });
  58. it('handles 3 arg functions', function () {
  59. expect(parseFunction('count_if(transaction.duration,greater,0.81)')).toEqual({
  60. name: 'count_if',
  61. arguments: ['transaction.duration', 'greater', '0.81'],
  62. });
  63. expect(parseFunction('count_if(some_tag,greater,"0.81,123,152,()")')).toEqual({
  64. name: 'count_if',
  65. arguments: ['some_tag', 'greater', '"0.81,123,152,()"'],
  66. });
  67. expect(parseFunction('function(foo, bar, baz)')).toEqual({
  68. name: 'function',
  69. arguments: ['foo', 'bar', 'baz'],
  70. });
  71. });
  72. it('handles 4 arg functions', function () {
  73. expect(parseFunction('to_other(release,"0.81,123,152,()",others,current)')).toEqual({
  74. name: 'to_other',
  75. arguments: ['release', '"0.81,123,152,()"', 'others', 'current'],
  76. });
  77. });
  78. });
  79. describe('getAggregateAlias', function () {
  80. it('no-ops simple fields', function () {
  81. expect(getAggregateAlias('field')).toEqual('field');
  82. expect(getAggregateAlias('under_field')).toEqual('under_field');
  83. expect(getAggregateAlias('foo.bar.is-Enterprise_42')).toEqual(
  84. 'foo.bar.is-Enterprise_42'
  85. );
  86. });
  87. it('handles 0 arg functions', function () {
  88. expect(getAggregateAlias('count()')).toEqual('count');
  89. expect(getAggregateAlias('count_unique()')).toEqual('count_unique');
  90. });
  91. it('handles 1 arg functions', function () {
  92. expect(getAggregateAlias('count(id)')).toEqual('count_id');
  93. expect(getAggregateAlias('count_unique(user)')).toEqual('count_unique_user');
  94. expect(getAggregateAlias('count_unique(issue.id)')).toEqual('count_unique_issue_id');
  95. expect(getAggregateAlias('count(foo.bar.is-Enterprise_42)')).toEqual(
  96. 'count_foo_bar_is_Enterprise_42'
  97. );
  98. });
  99. it('handles 2 arg functions', function () {
  100. expect(getAggregateAlias('percentile(transaction.duration,0.81)')).toEqual(
  101. 'percentile_transaction_duration_0_81'
  102. );
  103. expect(getAggregateAlias('percentile(transaction.duration, 0.11)')).toEqual(
  104. 'percentile_transaction_duration_0_11'
  105. );
  106. });
  107. it('handles to_other with symbols', function () {
  108. expect(
  109. getAggregateAlias('to_other(release,"release:beta@1.1.1 (2)",others,current)')
  110. ).toEqual('to_other_release__release_beta_1_1_1__2___others_current');
  111. });
  112. });
  113. describe('isAggregateField', function () {
  114. it('detects aliases', function () {
  115. expect(isAggregateField('p888')).toBe(false);
  116. expect(isAggregateField('other_field')).toBe(false);
  117. expect(isAggregateField('foo.bar.is-Enterprise_42')).toBe(false);
  118. });
  119. it('detects functions', function () {
  120. expect(isAggregateField('count()')).toBe(true);
  121. expect(isAggregateField('p75()')).toBe(true);
  122. expect(isAggregateField('percentile(transaction.duration, 0.55)')).toBe(true);
  123. expect(isAggregateField('last_seen()')).toBe(true);
  124. expect(isAggregateField('thing(')).toBe(false);
  125. expect(isAggregateField('unique_count(user)')).toBe(true);
  126. expect(isAggregateField('unique_count(foo.bar.is-Enterprise_42)')).toBe(true);
  127. });
  128. });
  129. describe('isAggregateEquation', function () {
  130. it('detects functions', function () {
  131. expect(isAggregateEquation('equation|5 + count()')).toBe(true);
  132. expect(
  133. isAggregateEquation('equation|percentile(transaction.duration, 0.55) / count()')
  134. ).toBe(true);
  135. expect(isAggregateEquation('equation|(5 + 5) + (count() - 2)')).toBe(true);
  136. });
  137. it('detects lack of functions', function () {
  138. expect(isAggregateEquation('equation|5 + 5')).toBe(false);
  139. expect(isAggregateEquation('equation|(5 + 5)')).toBe(false);
  140. expect(isAggregateEquation('equation|5 + (thing - other_thing)')).toBe(false);
  141. expect(isAggregateEquation('equation|5+(thing-other_thing)')).toBe(false);
  142. });
  143. });
  144. describe('measurement', function () {
  145. it('isMeasurement', function () {
  146. expect(isMeasurement('measurements.fp')).toBe(true);
  147. expect(isMeasurement('measurements.fcp')).toBe(true);
  148. expect(isMeasurement('measurements.lcp')).toBe(true);
  149. expect(isMeasurement('measurements.fid')).toBe(true);
  150. expect(isMeasurement('measurements.foo')).toBe(true);
  151. expect(isMeasurement('measurements.bar')).toBe(true);
  152. expect(isMeasurement('timestamp')).toBe(false);
  153. expect(isMeasurement('project.id')).toBe(false);
  154. expect(isMeasurement('transaction')).toBe(false);
  155. expect(isMeasurement('max(timestamp)')).toBe(false);
  156. expect(isMeasurement('percentile(measurements.fcp, 0.5)')).toBe(false);
  157. });
  158. it('measurementType', function () {
  159. expect(measurementType('measurements.fp')).toBe('duration');
  160. expect(measurementType('measurements.fcp')).toBe('duration');
  161. expect(measurementType('measurements.lcp')).toBe('duration');
  162. expect(measurementType('measurements.fid')).toBe('duration');
  163. expect(measurementType('measurements.foo')).toBe('number');
  164. expect(measurementType('measurements.bar')).toBe('number');
  165. });
  166. });
  167. describe('explodeField', function () {
  168. it('explodes fields', function () {
  169. expect(explodeField({field: 'foobar'})).toEqual({
  170. kind: 'field',
  171. field: 'foobar',
  172. });
  173. // has width
  174. expect(explodeField({field: 'foobar', width: 123})).toEqual({
  175. kind: 'field',
  176. field: 'foobar',
  177. });
  178. // has aggregation
  179. expect(explodeField({field: 'count(foobar)', width: 123})).toEqual({
  180. kind: 'function',
  181. function: ['count', 'foobar', undefined, undefined],
  182. });
  183. // custom tag
  184. expect(explodeField({field: 'foo.bar.is-Enterprise_42', width: 123})).toEqual({
  185. kind: 'field',
  186. field: 'foo.bar.is-Enterprise_42',
  187. });
  188. // custom tag with aggregation
  189. expect(explodeField({field: 'count(foo.bar.is-Enterprise_42)', width: 123})).toEqual({
  190. kind: 'function',
  191. function: ['count', 'foo.bar.is-Enterprise_42', undefined, undefined],
  192. });
  193. });
  194. });
  195. describe('aggregateOutputType', function () {
  196. it('handles unknown fields', function () {
  197. expect(aggregateOutputType('')).toEqual('number');
  198. expect(aggregateOutputType('blerg')).toEqual('number');
  199. });
  200. it('handles duration functions', function () {
  201. expect(aggregateOutputType('p50()')).toEqual('duration');
  202. expect(aggregateOutputType('p75()')).toEqual('duration');
  203. expect(aggregateOutputType('p95()')).toEqual('duration');
  204. expect(aggregateOutputType('p99()')).toEqual('duration');
  205. expect(aggregateOutputType('p100()')).toEqual('duration');
  206. expect(aggregateOutputType('p50(transaction.duration)')).toEqual('duration');
  207. expect(aggregateOutputType('p75(transaction.duration)')).toEqual('duration');
  208. expect(aggregateOutputType('p95(transaction.duration)')).toEqual('duration');
  209. expect(aggregateOutputType('p99(transaction.duration)')).toEqual('duration');
  210. expect(aggregateOutputType('p100(transaction.duration)')).toEqual('duration');
  211. expect(aggregateOutputType('percentile(transaction.duration, 0.51)')).toEqual(
  212. 'duration'
  213. );
  214. expect(aggregateOutputType('percentile(transaction.duration,0.99)')).toEqual(
  215. 'duration'
  216. );
  217. });
  218. it('handles percentage functions', function () {
  219. expect(aggregateOutputType('failure_rate()')).toEqual('percentage');
  220. });
  221. it('handles number functions', function () {
  222. expect(aggregateOutputType('apdex()')).toEqual('number');
  223. expect(aggregateOutputType('apdex(500)')).toEqual('number');
  224. expect(aggregateOutputType('count_miserable(user, 500)')).toEqual('number');
  225. expect(aggregateOutputType('user_misery(500)')).toEqual('number');
  226. expect(aggregateOutputType('eps()')).toEqual('number');
  227. expect(aggregateOutputType('epm()')).toEqual('number');
  228. });
  229. it('handles inherit functions', function () {
  230. expect(aggregateOutputType('sum(transaction.duration)')).toEqual('duration');
  231. expect(aggregateOutputType('sum(stack.colno)')).toEqual('number');
  232. expect(aggregateOutputType('min(stack.colno)')).toEqual('number');
  233. expect(aggregateOutputType('min(timestamp)')).toEqual('date');
  234. expect(aggregateOutputType('max(stack.colno)')).toEqual('number');
  235. expect(aggregateOutputType('max(timestamp)')).toEqual('date');
  236. });
  237. it('handles measurements', function () {
  238. expect(aggregateOutputType('sum(measurements.fcp)')).toEqual('duration');
  239. expect(aggregateOutputType('min(measurements.fcp)')).toEqual('duration');
  240. expect(aggregateOutputType('max(measurements.fcp)')).toEqual('duration');
  241. expect(aggregateOutputType('avg(measurements.fcp)')).toEqual('duration');
  242. expect(aggregateOutputType('percentile(measurements.fcp, 0.5)')).toEqual('duration');
  243. expect(aggregateOutputType('sum(measurements.bar)')).toEqual('number');
  244. expect(aggregateOutputType('min(measurements.bar)')).toEqual('number');
  245. expect(aggregateOutputType('max(measurements.bar)')).toEqual('number');
  246. expect(aggregateOutputType('avg(measurements.bar)')).toEqual('number');
  247. expect(aggregateOutputType('percentile(measurements.bar, 0.5)')).toEqual('number');
  248. expect(aggregateOutputType('p50(measurements.bar)')).toEqual('number');
  249. expect(aggregateOutputType('p75(measurements.bar)')).toEqual('number');
  250. expect(aggregateOutputType('p95(measurements.bar)')).toEqual('number');
  251. expect(aggregateOutputType('p99(measurements.bar)')).toEqual('number');
  252. expect(aggregateOutputType('p100(measurements.bar)')).toEqual('number');
  253. });
  254. });
  255. describe('aggregateMultiPlotType', function () {
  256. it('handles unknown functions', function () {
  257. expect(aggregateMultiPlotType('blerg')).toBe('area');
  258. expect(aggregateMultiPlotType('blerg(uhoh)')).toBe('area');
  259. });
  260. it('handles known functions', function () {
  261. expect(aggregateMultiPlotType('sum(transaction.duration)')).toBe('area');
  262. expect(aggregateMultiPlotType('p95()')).toBe('line');
  263. expect(aggregateMultiPlotType('equation|sum(transaction.duration) / 2')).toBe('line');
  264. });
  265. });
  266. describe('generateAggregateFields', function () {
  267. const organization = TestStubs.Organization();
  268. it('gets default aggregates', function () {
  269. expect(generateAggregateFields(organization, [])).toContainEqual({field: 'count()'});
  270. });
  271. it('includes fields from eventFields', function () {
  272. expect(
  273. generateAggregateFields(organization, [{field: 'not_real_aggregate()'}])
  274. ).toContainEqual({field: 'not_real_aggregate()'});
  275. });
  276. it('excludes fields from aggregates', function () {
  277. expect(generateAggregateFields(organization, [], ['count()'])).not.toContainEqual({
  278. field: 'count()',
  279. });
  280. });
  281. });
  282. describe('fieldAlignment()', function () {
  283. it('works with only field name', function () {
  284. expect(fieldAlignment('event.type')).toEqual('left');
  285. // Should be right, but we don't have any type data.
  286. expect(fieldAlignment('transaction.duration')).toEqual('left');
  287. });
  288. it('works with type parameter', function () {
  289. expect(fieldAlignment('transaction.duration', 'duration')).toEqual('right');
  290. expect(fieldAlignment('device.battery_level', 'number')).toEqual('right');
  291. expect(fieldAlignment('min(timestamp)', 'datetime')).toEqual('left');
  292. });
  293. it('can use table metadata', function () {
  294. const meta = {
  295. 'transaction.duration': 'duration',
  296. title: 'string',
  297. };
  298. expect(fieldAlignment('transaction.duration', 'never', meta)).toEqual('right');
  299. expect(fieldAlignment('transaction.duration', undefined, meta)).toEqual('right');
  300. expect(fieldAlignment('title', undefined, meta)).toEqual('left');
  301. });
  302. });