utils.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import type {DataCategoryInfo} from 'sentry/types/core';
  2. import type {Spike, SpikeDetails} from 'getsentry/views/spikeProtection/types';
  3. export function getDateFromString(date: string) {
  4. const newDate = new Date(date);
  5. newDate.setMilliseconds(0);
  6. return newDate;
  7. }
  8. // Rounds down the start time to the nearest value in the list of intervals
  9. function findStartIntervalBinarySearch(intervals: string[], target: Date) {
  10. let start = 0;
  11. let end = intervals.length - 1;
  12. if (target <= new Date(intervals[start]!)) {
  13. return intervals[start];
  14. }
  15. while (start <= end) {
  16. const mid = Math.ceil((start + end) / 2);
  17. const interval = new Date(intervals[mid]!);
  18. if (interval >= target) {
  19. end = mid - 1;
  20. } else {
  21. start = mid + 1;
  22. }
  23. }
  24. return intervals[end];
  25. }
  26. // Rounds up the end time to the nearest value in the list of intervals
  27. function findEndIntervalBinarySearch(intervals: string[], target: Date) {
  28. let start = 0;
  29. let end = intervals.length - 1;
  30. if (target >= new Date(intervals[end]!)) {
  31. return intervals[end];
  32. }
  33. while (start <= end) {
  34. const mid = Math.floor((start + end) / 2);
  35. const interval = new Date(intervals[mid]!);
  36. if (interval <= target) {
  37. start = mid + 1;
  38. } else {
  39. end = mid - 1;
  40. }
  41. }
  42. return intervals[start];
  43. }
  44. export function getSpikeInterval(intervals: string[], startDate: Date, endDate: Date) {
  45. return {
  46. startDate: findStartIntervalBinarySearch(intervals, startDate),
  47. endDate: findEndIntervalBinarySearch(intervals, endDate),
  48. };
  49. }
  50. export function getOngoingSpikeInterval(intervals: string[], startDate: Date) {
  51. return {
  52. startDate: findStartIntervalBinarySearch(intervals, startDate),
  53. endDate: intervals[intervals.length - 1],
  54. };
  55. }
  56. export function getSpikeDuration(spike: Spike) {
  57. // in seconds
  58. return spike.endDate
  59. ? Math.round(
  60. (new Date(spike.endDate).valueOf() - new Date(spike.startDate).valueOf()) / 1000
  61. )
  62. : null;
  63. }
  64. export function getSpikeDetailsFromSeries({
  65. storedSpikes,
  66. dataCategory,
  67. }: {
  68. dataCategory: DataCategoryInfo['name'];
  69. storedSpikes: Spike[];
  70. }) {
  71. const actualSpikes: SpikeDetails[] = [];
  72. if (storedSpikes.length === 0) {
  73. return actualSpikes;
  74. }
  75. storedSpikes.forEach(spike => {
  76. const duration = getSpikeDuration(spike);
  77. // only show spikes if they are ongoing
  78. actualSpikes.push({
  79. start: spike.startDate,
  80. end: spike.endDate,
  81. duration,
  82. dropped: spike.eventsDropped,
  83. threshold: spike.initialThreshold,
  84. dataCategory,
  85. });
  86. });
  87. return actualSpikes;
  88. }