2023-11-15 15:33:45 +05:30
|
|
|
/* eslint-disable no-bitwise */
|
2022-03-14 20:12:42 +05:30
|
|
|
import { Span } from 'types/api/trace/getTraceItem';
|
2022-03-03 19:04:23 +05:30
|
|
|
|
2023-01-13 13:50:11 +05:30
|
|
|
import { themeColors } from '../constants/theme';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2023-01-13 13:50:11 +05:30
|
|
|
export const colors = Object.values(themeColors.chartcolors);
|
2022-03-03 19:04:23 +05:30
|
|
|
|
2021-09-23 15:43:43 +05:30
|
|
|
export function getRandomNumber(min: number, max: number): number {
|
|
|
|
|
return Math.random() * (max - min) + min;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getRandomColor = (): string => {
|
|
|
|
|
const index = parseInt(getRandomNumber(0, colors.length - 1).toString(), 10);
|
|
|
|
|
return colors[index];
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-15 15:33:45 +05:30
|
|
|
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
|
|
|
|
export function hexToRgba(hex: string, alpha: number = 1): string {
|
|
|
|
|
// Create a new local variable to work with
|
|
|
|
|
let hexColor = hex;
|
|
|
|
|
|
|
|
|
|
// Ensure the hex string has a "#" at the start
|
|
|
|
|
if (hexColor.charAt(0) === '#') {
|
|
|
|
|
hexColor = hexColor.slice(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if it's a shorthand hex code (e.g., #FFF)
|
|
|
|
|
if (hexColor.length === 3) {
|
|
|
|
|
const r = hexColor.charAt(0);
|
|
|
|
|
const g = hexColor.charAt(1);
|
|
|
|
|
const b = hexColor.charAt(2);
|
|
|
|
|
hexColor = r + r + g + g + b + b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse the r, g, b values
|
|
|
|
|
const bigint = parseInt(hexColor, 16);
|
|
|
|
|
const r = (bigint >> 16) & 255;
|
|
|
|
|
const g = (bigint >> 8) & 255;
|
|
|
|
|
const b = bigint & 255;
|
|
|
|
|
|
|
|
|
|
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 12:32:12 +05:30
|
|
|
export const SIGNOZ_UI_COLOR_HEX = 'signoz_ui_color_hex';
|
|
|
|
|
|
2022-03-14 20:12:42 +05:30
|
|
|
export const spanServiceNameToColorMapping = (
|
|
|
|
|
spans: Span[],
|
|
|
|
|
): { [key: string]: string } => {
|
2023-04-11 12:32:12 +05:30
|
|
|
const allServiceMap = new Map<string, string | undefined>();
|
|
|
|
|
|
2022-03-03 19:04:23 +05:30
|
|
|
spans.forEach((spanItem) => {
|
2023-04-11 12:32:12 +05:30
|
|
|
const signozUiColorKeyIndex = spanItem[7].findIndex(
|
|
|
|
|
(span) => span === SIGNOZ_UI_COLOR_HEX,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
allServiceMap.set(
|
|
|
|
|
spanItem[3],
|
|
|
|
|
signozUiColorKeyIndex === -1
|
|
|
|
|
? undefined
|
|
|
|
|
: spanItem[8][signozUiColorKeyIndex],
|
|
|
|
|
);
|
2022-03-03 19:04:23 +05:30
|
|
|
});
|
2023-04-11 12:32:12 +05:30
|
|
|
|
2022-03-03 19:04:23 +05:30
|
|
|
const serviceToColorMap: { [key: string]: string } = {};
|
2023-04-11 12:32:12 +05:30
|
|
|
|
|
|
|
|
Array.from(allServiceMap).forEach(([serviceName, signozColor], idx) => {
|
|
|
|
|
serviceToColorMap[`${serviceName}`] =
|
|
|
|
|
signozColor || colors[idx % colors.length];
|
2022-03-03 19:04:23 +05:30
|
|
|
});
|
2023-04-11 12:32:12 +05:30
|
|
|
|
2022-03-03 19:04:23 +05:30
|
|
|
return serviceToColorMap;
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-23 15:43:43 +05:30
|
|
|
export default getRandomColor;
|