query_hints.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { getQueryHints } from '../query_hints';
  2. describe('getQueryHints()', () => {
  3. it('returns no hints for no series', () => {
  4. expect(getQueryHints('', [])).toEqual(null);
  5. });
  6. it('returns no hints for empty series', () => {
  7. expect(getQueryHints('', [{ datapoints: [] }])).toEqual(null);
  8. });
  9. it('returns no hint for a monotonously decreasing series', () => {
  10. const series = [{ datapoints: [[23, 1000], [22, 1001]] }];
  11. const hints = getQueryHints('metric', series);
  12. expect(hints).toEqual(null);
  13. });
  14. it('returns no hint for a flat series', () => {
  15. const series = [{ datapoints: [[null, 1000], [23, 1001], [null, 1002], [23, 1003]] }];
  16. const hints = getQueryHints('metric', series);
  17. expect(hints).toEqual(null);
  18. });
  19. it('returns a rate hint for a monotonously increasing series', () => {
  20. const series = [{ datapoints: [[23, 1000], [24, 1001]] }];
  21. const hints = getQueryHints('metric', series);
  22. expect(hints.length).toBe(1);
  23. expect(hints[0]).toMatchObject({
  24. label: 'Time series is monotonously increasing.',
  25. fix: {
  26. action: {
  27. type: 'ADD_RATE',
  28. query: 'metric',
  29. },
  30. },
  31. });
  32. });
  33. it('returns no rate hint for a monotonously increasing series that already has a rate', () => {
  34. const series = [{ datapoints: [[23, 1000], [24, 1001]] }];
  35. const hints = getQueryHints('rate(metric[1m])', series);
  36. expect(hints).toEqual(null);
  37. });
  38. it('returns a rate hint w/o action for a complex monotonously increasing series', () => {
  39. const series = [{ datapoints: [[23, 1000], [24, 1001]] }];
  40. const hints = getQueryHints('sum(metric)', series);
  41. expect(hints.length).toBe(1);
  42. expect(hints[0].label).toContain('rate()');
  43. expect(hints[0].fix).toBeUndefined();
  44. });
  45. it('returns a rate hint for a monotonously increasing series with missing data', () => {
  46. const series = [{ datapoints: [[23, 1000], [null, 1001], [24, 1002]] }];
  47. const hints = getQueryHints('metric', series);
  48. expect(hints.length).toBe(1);
  49. expect(hints[0]).toMatchObject({
  50. label: 'Time series is monotonously increasing.',
  51. fix: {
  52. action: {
  53. type: 'ADD_RATE',
  54. query: 'metric',
  55. },
  56. },
  57. });
  58. });
  59. it('returns a histogram hint for a bucket series', () => {
  60. const series = [{ datapoints: [[23, 1000]] }];
  61. const hints = getQueryHints('metric_bucket', series);
  62. expect(hints.length).toBe(1);
  63. expect(hints[0]).toMatchObject({
  64. label: 'Time series has buckets, you probably wanted a histogram.',
  65. fix: {
  66. action: {
  67. type: 'ADD_HISTOGRAM_QUANTILE',
  68. query: 'metric_bucket',
  69. },
  70. },
  71. });
  72. });
  73. });