2021-08-26 11:50:47 +05:30
|
|
|
import Spinner from 'components/Spinner';
|
|
|
|
|
import React, { useEffect, useRef } from 'react';
|
|
|
|
|
import { ForceGraph2D } from 'react-force-graph';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { RouteComponentProps, withRouter } from 'react-router-dom';
|
2021-05-09 14:44:14 +05:30
|
|
|
import {
|
2021-08-26 11:50:47 +05:30
|
|
|
getDetailedServiceMapItems,
|
|
|
|
|
getServiceMapItems,
|
2021-05-09 14:44:14 +05:30
|
|
|
serviceMapStore,
|
2021-08-26 11:50:47 +05:30
|
|
|
} from 'store/actions';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { AppState } from 'store/reducers';
|
2021-08-26 11:50:47 +05:30
|
|
|
import styled from 'styled-components';
|
2021-10-20 09:24:55 +05:30
|
|
|
import { GlobalTime } from 'types/actions/globalTime';
|
2021-05-17 17:43:04 +05:30
|
|
|
|
2021-08-26 11:50:47 +05:30
|
|
|
import SelectService from './SelectService';
|
|
|
|
|
import { getGraphData, getTooltip, getZoomPx, transformLabel } from './utils';
|
2021-05-09 22:51:08 +05:30
|
|
|
|
|
|
|
|
const Container = styled.div`
|
|
|
|
|
.force-graph-container .graph-tooltip {
|
|
|
|
|
background: black;
|
|
|
|
|
padding: 1px;
|
|
|
|
|
.keyval {
|
|
|
|
|
display: flex;
|
|
|
|
|
.key {
|
|
|
|
|
margin-right: 4px;
|
|
|
|
|
}
|
|
|
|
|
.val {
|
|
|
|
|
margin-left: auto;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-05-09 14:44:14 +05:30
|
|
|
interface ServiceMapProps extends RouteComponentProps<any> {
|
|
|
|
|
serviceMap: serviceMapStore;
|
|
|
|
|
globalTime: GlobalTime;
|
2021-11-22 12:26:20 +05:30
|
|
|
getServiceMapItems: (time: GlobalTime) => void;
|
|
|
|
|
getDetailedServiceMapItems: (time: GlobalTime) => void;
|
2021-05-09 14:44:14 +05:30
|
|
|
}
|
|
|
|
|
interface graphNode {
|
|
|
|
|
id: string;
|
|
|
|
|
group: number;
|
|
|
|
|
}
|
|
|
|
|
interface graphLink {
|
|
|
|
|
source: string;
|
|
|
|
|
target: string;
|
|
|
|
|
value: number;
|
|
|
|
|
}
|
|
|
|
|
export interface graphDataType {
|
|
|
|
|
nodes: graphNode[];
|
|
|
|
|
links: graphLink[];
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function ServiceMap(props: ServiceMapProps): JSX.Element {
|
2021-05-09 14:44:14 +05:30
|
|
|
const fgRef = useRef();
|
2021-05-23 14:15:13 +05:30
|
|
|
|
2021-05-09 14:44:14 +05:30
|
|
|
const {
|
|
|
|
|
getDetailedServiceMapItems,
|
|
|
|
|
getServiceMapItems,
|
|
|
|
|
globalTime,
|
|
|
|
|
serviceMap,
|
|
|
|
|
} = props;
|
2021-05-23 14:15:13 +05:30
|
|
|
|
2021-05-09 14:44:14 +05:30
|
|
|
useEffect(() => {
|
2021-05-23 16:06:40 +05:30
|
|
|
/*
|
|
|
|
|
Call the apis only when the route is loaded.
|
|
|
|
|
Check this issue: https://github.com/SigNoz/signoz/issues/110
|
|
|
|
|
*/
|
2021-10-20 09:24:55 +05:30
|
|
|
getServiceMapItems(globalTime);
|
|
|
|
|
getDetailedServiceMapItems(globalTime);
|
2021-11-22 12:26:20 +05:30
|
|
|
}, [globalTime, getServiceMapItems, getDetailedServiceMapItems]);
|
2021-05-09 14:44:14 +05:30
|
|
|
|
2021-05-09 23:45:42 +05:30
|
|
|
useEffect(() => {
|
2021-08-26 11:50:47 +05:30
|
|
|
fgRef.current && fgRef.current.d3Force('charge').strength(-400);
|
2021-05-09 23:45:42 +05:30
|
|
|
});
|
2021-05-09 15:41:57 +05:30
|
|
|
if (!serviceMap.items.length || !serviceMap.services.length) {
|
2021-08-23 11:38:25 +05:30
|
|
|
return <Spinner size="large" tip="Loading..." />;
|
2021-05-09 14:44:14 +05:30
|
|
|
}
|
2021-05-09 15:41:57 +05:30
|
|
|
|
2021-11-22 12:26:20 +05:30
|
|
|
const zoomToService = (value: string): void => {
|
|
|
|
|
fgRef &&
|
|
|
|
|
fgRef.current &&
|
|
|
|
|
fgRef.current.zoomToFit(700, getZoomPx(), (e) => e.id === value);
|
2021-05-09 18:27:37 +05:30
|
|
|
};
|
|
|
|
|
|
2021-05-17 14:42:39 +05:30
|
|
|
const zoomToDefault = () => {
|
2021-11-22 12:26:20 +05:30
|
|
|
fgRef && fgRef.current && fgRef.current.zoomToFit(100, 120);
|
2021-05-09 18:27:37 +05:30
|
|
|
};
|
|
|
|
|
|
2021-05-09 15:41:57 +05:30
|
|
|
const { nodes, links } = getGraphData(serviceMap);
|
2021-05-09 14:44:14 +05:30
|
|
|
const graphData = { nodes, links };
|
2021-01-18 02:18:49 +05:30
|
|
|
return (
|
2021-05-09 22:51:08 +05:30
|
|
|
<Container>
|
2021-05-09 18:27:37 +05:30
|
|
|
<SelectService
|
|
|
|
|
services={serviceMap.services}
|
|
|
|
|
zoomToService={zoomToService}
|
2021-05-17 14:42:39 +05:30
|
|
|
zoomToDefault={zoomToDefault}
|
2021-05-09 18:27:37 +05:30
|
|
|
/>
|
2021-05-09 14:44:14 +05:30
|
|
|
<ForceGraph2D
|
|
|
|
|
ref={fgRef}
|
|
|
|
|
cooldownTicks={100}
|
|
|
|
|
graphData={graphData}
|
2021-05-16 15:08:31 +05:30
|
|
|
nodeLabel={getTooltip}
|
2021-05-09 14:44:14 +05:30
|
|
|
linkAutoColorBy={(d) => d.target}
|
|
|
|
|
linkDirectionalParticles="value"
|
|
|
|
|
linkDirectionalParticleSpeed={(d) => d.value}
|
|
|
|
|
nodeCanvasObject={(node, ctx, globalScale) => {
|
2021-05-16 15:50:32 +05:30
|
|
|
const label = transformLabel(node.id);
|
2022-03-22 12:10:31 +05:30
|
|
|
const { fontSize } = node;
|
2021-05-09 22:51:08 +05:30
|
|
|
ctx.font = `${fontSize}px Roboto`;
|
2022-03-22 12:10:31 +05:30
|
|
|
const { width } = node;
|
2021-05-09 22:51:08 +05:30
|
|
|
|
2021-05-09 14:44:14 +05:30
|
|
|
ctx.fillStyle = node.color;
|
2021-05-09 22:51:08 +05:30
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.arc(node.x, node.y, width, 0, 2 * Math.PI, false);
|
|
|
|
|
ctx.fill();
|
2021-08-26 11:50:47 +05:30
|
|
|
ctx.textAlign = 'center';
|
|
|
|
|
ctx.textBaseline = 'middle';
|
|
|
|
|
ctx.fillStyle = '#646464';
|
2021-05-09 14:44:14 +05:30
|
|
|
ctx.fillText(label, node.x, node.y);
|
|
|
|
|
}}
|
2021-05-09 18:27:37 +05:30
|
|
|
onNodeClick={(node) => {
|
2021-08-26 11:50:47 +05:30
|
|
|
const tooltip = document.querySelector('.graph-tooltip');
|
2021-05-09 18:27:37 +05:30
|
|
|
if (tooltip && node) {
|
2021-05-16 15:08:31 +05:30
|
|
|
tooltip.innerHTML = getTooltip(node);
|
2021-05-09 18:27:37 +05:30
|
|
|
}
|
|
|
|
|
}}
|
2021-05-09 14:44:14 +05:30
|
|
|
nodePointerAreaPaint={(node, color, ctx) => {
|
|
|
|
|
ctx.fillStyle = color;
|
2021-05-09 22:51:08 +05:30
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.arc(node.x, node.y, 5, 0, 2 * Math.PI, false);
|
|
|
|
|
ctx.fill();
|
2021-05-09 14:44:14 +05:30
|
|
|
}}
|
|
|
|
|
/>
|
2021-05-09 22:51:08 +05:30
|
|
|
</Container>
|
2021-01-18 02:18:49 +05:30
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-01-03 18:15:44 +05:30
|
|
|
|
2021-05-09 14:44:14 +05:30
|
|
|
const mapStateToProps = (
|
2021-09-23 15:43:43 +05:30
|
|
|
state: AppState,
|
2021-05-09 14:44:14 +05:30
|
|
|
): {
|
|
|
|
|
serviceMap: serviceMapStore;
|
|
|
|
|
globalTime: GlobalTime;
|
|
|
|
|
} => {
|
|
|
|
|
return {
|
|
|
|
|
serviceMap: state.serviceMap,
|
|
|
|
|
globalTime: state.globalTime,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-23 11:38:25 +05:30
|
|
|
export default withRouter(
|
|
|
|
|
connect(mapStateToProps, {
|
2022-03-22 12:10:31 +05:30
|
|
|
getServiceMapItems,
|
|
|
|
|
getDetailedServiceMapItems,
|
2021-08-23 11:38:25 +05:30
|
|
|
})(ServiceMap),
|
|
|
|
|
);
|