134 lines
3.3 KiB
TypeScript
Raw Normal View History

/*eslint-disable*/
//@ts-nocheck
import { cloneDeep, find, maxBy, uniq, uniqBy, groupBy, sumBy } from 'lodash-es';
import { graphDataType } from './ServiceMap';
2021-05-09 14:44:14 +05:30
const MIN_WIDTH = 10;
2021-05-09 22:51:08 +05:30
const MAX_WIDTH = 20;
const DEFAULT_FONT_SIZE = 6;
export const getDimensions = (num, highest) => {
const percentage = (num / highest) * 100;
const width = (percentage * (MAX_WIDTH - MIN_WIDTH)) / 100 + MIN_WIDTH;
2021-05-09 20:47:56 +05:30
const fontSize = DEFAULT_FONT_SIZE;
return {
fontSize,
width,
};
};
2021-05-09 14:44:14 +05:30
export const getGraphData = (serviceMap, isDarkMode): graphDataType => {
const { items } = serviceMap;
const services = Object.values(groupBy(items, 'child')).map((e) => {
return {
serviceName: e[0].child,
errorRate: sumBy(e, 'errorRate'),
callRate: sumBy(e, 'callRate'),
}
});
2022-05-22 20:59:35 +05:30
const highestCallCount = maxBy(items, (e) => e?.callCount)?.callCount;
const highestCallRate = maxBy(services, (e) => e?.callRate)?.callRate;
const divNum = Number(
String(1).padEnd(highestCallCount.toString().length, '0'),
);
const links = cloneDeep(items).map((node) => {
const { parent, child, callCount, callRate, errorRate, p99 } = node;
2021-05-09 14:44:14 +05:30
return {
source: parent,
target: child,
value: (100 - callCount / divNum) * 0.03,
callRate,
errorRate,
p99,
2021-05-09 14:44:14 +05:30
};
});
const uniqParent = uniqBy(cloneDeep(items), 'parent').map((e) => e.parent);
const uniqChild = uniqBy(cloneDeep(items), 'child').map((e) => e.child);
2021-05-09 14:44:14 +05:30
const uniqNodes = uniq([...uniqParent, ...uniqChild]);
const nodes = uniqNodes.map((node, i) => {
const service = find(services, (service) => service.serviceName === node);
let color = isDarkMode ? '#7CA568' : '#D5F2BB';
2021-05-09 19:12:54 +05:30
if (!service) {
return {
id: node,
group: i + 1,
2021-05-09 20:47:56 +05:30
fontSize: DEFAULT_FONT_SIZE,
2021-05-09 19:12:54 +05:30
width: MIN_WIDTH,
color,
nodeVal: MIN_WIDTH,
};
}
if (service.errorRate > 0) {
color = isDarkMode ? '#DB836E' : '#F98989';
}
2021-05-09 22:51:08 +05:30
const { fontSize, width } = getDimensions(service.callRate, highestCallRate);
return {
id: node,
group: i + 1,
fontSize,
width,
color,
nodeVal: width,
};
});
2021-05-09 14:44:14 +05:30
return {
nodes,
links,
};
};
2021-05-16 15:08:31 +05:30
2021-05-15 18:23:29 +05:30
export const getZoomPx = (): number => {
const { width } = window.screen;
2021-05-15 18:23:29 +05:30
if (width < 1400) {
return 190;
}
if (width > 1400 && width < 1700) {
2021-06-07 20:58:02 +05:30
return 380;
}
if (width > 1700) {
2021-06-07 20:58:02 +05:30
return 470;
2021-05-15 18:23:29 +05:30
}
return 190;
2021-05-16 18:44:26 +05:30
};
const getRound2DigitsAfterDecimal = (num: number) => {
if (num === 0) {
return 0;
}
return num.toFixed(20).match(/^-?\d*\.?0*\d{0,2}/)[0];
}
export const getTooltip = (link: {
2021-05-16 15:08:31 +05:30
p99: number;
errorRate: number;
callRate: number;
id: string;
}) => {
return `<div style="color:#333333;padding:12px;background: white;border-radius: 2px;">
<div class="keyval">
<div class="key">P99 latency:</div>
<div class="val">${getRound2DigitsAfterDecimal(link.p99/ 1000000)}ms</div>
2021-05-16 15:08:31 +05:30
</div>
<div class="keyval">
<div class="key">Request:</div>
<div class="val">${getRound2DigitsAfterDecimal(link.callRate)}/sec</div>
2021-05-16 15:08:31 +05:30
</div>
<div class="keyval">
<div class="key">Error Rate:</div>
<div class="val">${getRound2DigitsAfterDecimal(link.errorRate)}%</div>
2021-05-16 15:08:31 +05:30
</div>
</div>`;
};
export const transformLabel = (label: string) => {
2021-05-17 14:29:35 +05:30
const MAX_LENGTH = 13;
const MAX_SHOW = 10;
if (label.length > MAX_LENGTH) {
2021-05-17 14:29:35 +05:30
return `${label.slice(0, MAX_SHOW)}...`;
}
return label;
};