metricsChartPalette.tsx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {useCallback, useRef} from 'react';
  2. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  3. import theme from 'sentry/utils/theme';
  4. const CACHE_SIZE = 20; // number of palettes to cache
  5. export function createChartPalette(seriesNames: string[]): Record<string, string> {
  6. const uniqueSeriesNames = Array.from(new Set(seriesNames));
  7. // We do length - 2 to be aligned with the colors in other parts of the app (copy-pasta)
  8. // We use Math.max to avoid numbers < -1 as then `getColorPalette` returns undefined (not typesafe because of array access and casting)
  9. const chartColors =
  10. theme.charts.getColorPalette(Math.max(uniqueSeriesNames.length - 2, -1)) ??
  11. CHART_PALETTE[CHART_PALETTE.length - 1];
  12. return uniqueSeriesNames.reduce(
  13. (palette, seriesName, i) => {
  14. palette[seriesName] = chartColors[i % chartColors.length];
  15. return palette;
  16. },
  17. {} as Record<string, string>
  18. );
  19. }
  20. /**
  21. * **NOTE: Not yet optimized for performance, it should only be used for the metrics page with a limited amount of series**
  22. *
  23. * Runtime complexity is O(n * m) where n is the number of palettes in the cache and m is the number of seriesNames
  24. *
  25. * Creates chart palettes for the given seriesNames and caches them in a LRU cache
  26. * If a palette for the given seriesNames already exists in the cache, it will be returned
  27. * @param cache object that will contain the cached palettes
  28. * @param seriesNames names of the series for which to get the palette for
  29. * @returns an object mapping seriesNames to colors
  30. */
  31. export function getCachedChartPalette(
  32. cache: Readonly<Record<string, string>>[],
  33. seriesNames: string[]
  34. ): Readonly<Record<string, string>> {
  35. // Check if we already have a palette that includes all of the given seriesNames
  36. // We search in reverse to get the most recent palettes first
  37. let cacheIndex = -1;
  38. for (let i = cache.length - 1; i >= 0; i--) {
  39. const palette = cache[i];
  40. if (seriesNames.every(seriesName => seriesName in palette)) {
  41. cacheIndex = i;
  42. break;
  43. }
  44. }
  45. if (cacheIndex > -1) {
  46. const cachedPalette = cache[cacheIndex];
  47. if (cacheIndex !== cache.length - 1) {
  48. // Move the cached palette to the end of the cache, so it is the most recent one
  49. cache.splice(cacheIndex, 1);
  50. cache.push(cachedPalette);
  51. }
  52. return cachedPalette;
  53. }
  54. // If we do not have a palette for the given seriesNames, create one
  55. const newPalette = createChartPalette(seriesNames);
  56. // Single series palettes will always be the same, so we do not need to cache them
  57. if (seriesNames.length > 1) {
  58. cache.push(newPalette);
  59. }
  60. // Don't cache more than CACHE_SIZE palettes, so we do not create a memory leak
  61. if (cache.length > CACHE_SIZE) {
  62. const overflow = cache.length - CACHE_SIZE;
  63. cache.splice(0, overflow);
  64. }
  65. return newPalette;
  66. }
  67. /**
  68. * **NOTE: Not yet optimized for performance, it should only be used for the metrics page with a limited amount of series**
  69. */
  70. export const useGetCachedChartPalette = () => {
  71. const cacheRef = useRef<Readonly<Record<string, string>>[]>([]);
  72. return useCallback((seriesNames: string[]) => {
  73. // copy the cache to avoid mutating it
  74. return {...getCachedChartPalette(cacheRef.current, seriesNames)};
  75. }, []);
  76. };