2022-10-20 14:20:48 +01:00
|
|
|
import { css } from '@emotion/css';
|
2023-03-30 11:32:44 +02:00
|
|
|
import React, { useEffect, useMemo, useState } from 'react';
|
2022-10-07 11:39:14 +01:00
|
|
|
import { useMeasure } from 'react-use';
|
|
|
|
|
|
2023-04-13 15:27:01 +02:00
|
|
|
import { DataFrame, CoreApp, GrafanaTheme2 } from '@grafana/data';
|
2023-03-14 15:41:27 +01:00
|
|
|
import { useStyles2, useTheme2 } from '@grafana/ui';
|
2022-10-07 11:39:14 +01:00
|
|
|
|
2023-04-13 15:27:01 +02:00
|
|
|
import { MIN_WIDTH_TO_SHOW_BOTH_TOPTABLE_AND_FLAMEGRAPH } from '../constants';
|
2022-10-07 11:39:14 +01:00
|
|
|
|
|
|
|
|
import FlameGraph from './FlameGraph/FlameGraph';
|
2023-03-30 11:32:44 +02:00
|
|
|
import { FlameGraphDataContainer, LevelItem, nestedSetToLevels } from './FlameGraph/dataTransform';
|
2022-10-07 11:39:14 +01:00
|
|
|
import FlameGraphHeader from './FlameGraphHeader';
|
|
|
|
|
import FlameGraphTopTableContainer from './TopTable/FlameGraphTopTableContainer';
|
2023-05-25 11:08:03 +02:00
|
|
|
import { SelectedView, TextAlign } from './types';
|
2022-10-07 11:39:14 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
2023-03-13 11:06:04 +01:00
|
|
|
data?: DataFrame;
|
2022-10-20 14:20:48 +01:00
|
|
|
app: CoreApp;
|
2022-10-07 11:39:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const FlameGraphContainer = (props: Props) => {
|
|
|
|
|
const [topLevelIndex, setTopLevelIndex] = useState(0);
|
2023-01-30 14:02:26 +00:00
|
|
|
const [selectedBarIndex, setSelectedBarIndex] = useState(0);
|
2022-10-07 11:39:14 +01:00
|
|
|
const [rangeMin, setRangeMin] = useState(0);
|
|
|
|
|
const [rangeMax, setRangeMax] = useState(1);
|
|
|
|
|
const [search, setSearch] = useState('');
|
|
|
|
|
const [selectedView, setSelectedView] = useState(SelectedView.Both);
|
|
|
|
|
const [sizeRef, { width: containerWidth }] = useMeasure<HTMLDivElement>();
|
2023-05-25 11:08:03 +02:00
|
|
|
const [textAlign, setTextAlign] = useState<TextAlign>('left');
|
|
|
|
|
|
2023-03-14 15:41:27 +01:00
|
|
|
const theme = useTheme2();
|
|
|
|
|
|
2023-03-30 11:32:44 +02:00
|
|
|
const [dataContainer, levels] = useMemo((): [FlameGraphDataContainer, LevelItem[][]] | [undefined, undefined] => {
|
2022-10-07 11:39:14 +01:00
|
|
|
if (!props.data) {
|
2023-03-30 11:32:44 +02:00
|
|
|
return [undefined, undefined];
|
2022-10-07 11:39:14 +01:00
|
|
|
}
|
2023-03-30 11:32:44 +02:00
|
|
|
const container = new FlameGraphDataContainer(props.data, theme);
|
|
|
|
|
|
|
|
|
|
// Transform dataFrame with nested set format to array of levels. Each level contains all the bars for a particular
|
|
|
|
|
// level of the flame graph. We do this temporary as in the end we should be able to render directly by iterating
|
|
|
|
|
// over the dataFrame rows.
|
|
|
|
|
return [container, nestedSetToLevels(container)];
|
|
|
|
|
}, [props.data, theme]);
|
2022-10-07 11:39:14 +01:00
|
|
|
|
2023-04-13 15:27:01 +02:00
|
|
|
const styles = useStyles2(getStyles);
|
2022-10-20 14:20:48 +01:00
|
|
|
|
2022-10-07 11:39:14 +01:00
|
|
|
// If user resizes window with both as the selected view
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
containerWidth > 0 &&
|
|
|
|
|
containerWidth < MIN_WIDTH_TO_SHOW_BOTH_TOPTABLE_AND_FLAMEGRAPH &&
|
|
|
|
|
selectedView === SelectedView.Both
|
|
|
|
|
) {
|
|
|
|
|
setSelectedView(SelectedView.FlameGraph);
|
|
|
|
|
}
|
|
|
|
|
}, [selectedView, setSelectedView, containerWidth]);
|
|
|
|
|
|
2022-12-13 09:40:38 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
setTopLevelIndex(0);
|
2023-01-30 14:02:26 +00:00
|
|
|
setSelectedBarIndex(0);
|
2022-12-13 09:40:38 +00:00
|
|
|
setRangeMin(0);
|
|
|
|
|
setRangeMax(1);
|
|
|
|
|
}, [props.data]);
|
|
|
|
|
|
2022-10-07 11:39:14 +01:00
|
|
|
return (
|
2023-03-01 11:32:39 +00:00
|
|
|
<>
|
2023-03-30 11:32:44 +02:00
|
|
|
{dataContainer && (
|
2023-03-01 11:32:39 +00:00
|
|
|
<div ref={sizeRef} className={styles.container}>
|
|
|
|
|
<FlameGraphHeader
|
|
|
|
|
app={props.app}
|
|
|
|
|
setTopLevelIndex={setTopLevelIndex}
|
|
|
|
|
setSelectedBarIndex={setSelectedBarIndex}
|
|
|
|
|
setRangeMin={setRangeMin}
|
|
|
|
|
setRangeMax={setRangeMax}
|
|
|
|
|
search={search}
|
|
|
|
|
setSearch={setSearch}
|
|
|
|
|
selectedView={selectedView}
|
|
|
|
|
setSelectedView={setSelectedView}
|
|
|
|
|
containerWidth={containerWidth}
|
2023-05-25 11:08:03 +02:00
|
|
|
textAlign={textAlign}
|
|
|
|
|
onTextAlignChange={setTextAlign}
|
2023-03-01 11:32:39 +00:00
|
|
|
/>
|
2022-10-07 11:39:14 +01:00
|
|
|
|
2023-04-13 15:27:01 +02:00
|
|
|
<div className={styles.body}>
|
|
|
|
|
{selectedView !== SelectedView.FlameGraph && (
|
|
|
|
|
<FlameGraphTopTableContainer
|
|
|
|
|
data={dataContainer}
|
|
|
|
|
app={props.app}
|
|
|
|
|
totalLevels={levels.length}
|
|
|
|
|
selectedView={selectedView}
|
|
|
|
|
search={search}
|
|
|
|
|
setSearch={setSearch}
|
|
|
|
|
setTopLevelIndex={setTopLevelIndex}
|
|
|
|
|
setSelectedBarIndex={setSelectedBarIndex}
|
|
|
|
|
setRangeMin={setRangeMin}
|
|
|
|
|
setRangeMax={setRangeMax}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2022-10-07 11:39:14 +01:00
|
|
|
|
2023-04-13 15:27:01 +02:00
|
|
|
{selectedView !== SelectedView.TopTable && (
|
|
|
|
|
<FlameGraph
|
|
|
|
|
data={dataContainer}
|
|
|
|
|
app={props.app}
|
|
|
|
|
levels={levels}
|
|
|
|
|
topLevelIndex={topLevelIndex}
|
|
|
|
|
selectedBarIndex={selectedBarIndex}
|
|
|
|
|
rangeMin={rangeMin}
|
|
|
|
|
rangeMax={rangeMax}
|
|
|
|
|
search={search}
|
|
|
|
|
setTopLevelIndex={setTopLevelIndex}
|
|
|
|
|
setSelectedBarIndex={setSelectedBarIndex}
|
|
|
|
|
setRangeMin={setRangeMin}
|
|
|
|
|
setRangeMax={setRangeMax}
|
|
|
|
|
selectedView={selectedView}
|
2023-05-25 11:08:03 +02:00
|
|
|
textAlign={textAlign}
|
2023-04-13 15:27:01 +02:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2023-03-01 11:32:39 +00:00
|
|
|
</div>
|
2022-10-07 11:39:14 +01:00
|
|
|
)}
|
2023-03-01 11:32:39 +00:00
|
|
|
</>
|
2022-10-07 11:39:14 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-13 15:27:01 +02:00
|
|
|
function getStyles(theme: GrafanaTheme2) {
|
|
|
|
|
return {
|
|
|
|
|
container: css({
|
|
|
|
|
height: '100%',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flex: '1 1 0',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
minHeight: 0,
|
2023-05-25 11:08:03 +02:00
|
|
|
gap: theme.spacing(1),
|
2023-04-13 15:27:01 +02:00
|
|
|
}),
|
|
|
|
|
body: css({
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
minHeight: 0,
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-10-20 14:20:48 +01:00
|
|
|
|
2022-10-07 11:39:14 +01:00
|
|
|
export default FlameGraphContainer;
|