2022-10-20 08:20:48 -05:00
|
|
|
import { css } from '@emotion/css';
|
2023-03-13 05:06:04 -05:00
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
2022-10-07 05:39:14 -05:00
|
|
|
import { useMeasure } from 'react-use';
|
|
|
|
|
2023-03-13 05:06:04 -05:00
|
|
|
import { DataFrame, DataFrameView, CoreApp, getEnumDisplayProcessor, createTheme } from '@grafana/data';
|
2022-10-20 08:20:48 -05:00
|
|
|
import { useStyles2 } from '@grafana/ui';
|
2022-10-07 05:39:14 -05:00
|
|
|
|
2022-10-20 08:20:48 -05:00
|
|
|
import { MIN_WIDTH_TO_SHOW_BOTH_TOPTABLE_AND_FLAMEGRAPH, PIXELS_PER_LEVEL } from '../constants';
|
2022-10-07 05:39:14 -05:00
|
|
|
|
|
|
|
import FlameGraph from './FlameGraph/FlameGraph';
|
|
|
|
import { Item, nestedSetToLevels } from './FlameGraph/dataTransform';
|
|
|
|
import FlameGraphHeader from './FlameGraphHeader';
|
|
|
|
import FlameGraphTopTableContainer from './TopTable/FlameGraphTopTableContainer';
|
|
|
|
import { SelectedView } from './types';
|
|
|
|
|
|
|
|
type Props = {
|
2023-03-13 05:06:04 -05:00
|
|
|
data?: DataFrame;
|
2022-10-20 08:20:48 -05:00
|
|
|
app: CoreApp;
|
|
|
|
// Height for flame graph when not used in explore.
|
|
|
|
// This needs to be different to explore flame graph height as we
|
|
|
|
// use panels with user adjustable heights in dashboards etc.
|
|
|
|
flameGraphHeight?: number;
|
2022-10-07 05:39:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const FlameGraphContainer = (props: Props) => {
|
|
|
|
const [topLevelIndex, setTopLevelIndex] = useState(0);
|
2023-01-30 08:02:26 -06:00
|
|
|
const [selectedBarIndex, setSelectedBarIndex] = useState(0);
|
2022-10-07 05:39:14 -05: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-03-13 05:06:04 -05:00
|
|
|
const labelField = props.data?.fields.find((f) => f.name === 'label');
|
|
|
|
|
|
|
|
// Label can actually be an enum field so depending on that we have to access it through display processor. This is
|
|
|
|
// both a backward compatibility but also to allow using a simple dataFrame without enum config. This would allow
|
|
|
|
// users to use this panel with correct query from data sources that do not return profiles natively.
|
|
|
|
const getLabelValue = useCallback(
|
|
|
|
(label: string | number) => {
|
|
|
|
const enumConfig = labelField?.config?.type?.enum;
|
|
|
|
if (enumConfig) {
|
|
|
|
return getEnumDisplayProcessor(createTheme(), enumConfig)(label).text;
|
|
|
|
} else {
|
|
|
|
return label.toString();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[labelField]
|
|
|
|
);
|
|
|
|
|
2022-10-07 05:39:14 -05:00
|
|
|
// 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.
|
|
|
|
const levels = useMemo(() => {
|
|
|
|
if (!props.data) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const dataView = new DataFrameView<Item>(props.data);
|
|
|
|
return nestedSetToLevels(dataView);
|
|
|
|
}, [props.data]);
|
|
|
|
|
2022-10-20 08:20:48 -05:00
|
|
|
const styles = useStyles2(() => getStyles(props.app, PIXELS_PER_LEVEL * levels.length));
|
|
|
|
|
2022-10-07 05:39:14 -05: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 03:40:38 -06:00
|
|
|
useEffect(() => {
|
|
|
|
setTopLevelIndex(0);
|
2023-01-30 08:02:26 -06:00
|
|
|
setSelectedBarIndex(0);
|
2022-12-13 03:40:38 -06:00
|
|
|
setRangeMin(0);
|
|
|
|
setRangeMax(1);
|
|
|
|
}, [props.data]);
|
|
|
|
|
2022-10-07 05:39:14 -05:00
|
|
|
return (
|
2023-03-01 05:32:39 -06:00
|
|
|
<>
|
|
|
|
{props.data && (
|
|
|
|
<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}
|
|
|
|
/>
|
2022-10-07 05:39:14 -05:00
|
|
|
|
2023-03-01 05:32:39 -06:00
|
|
|
{selectedView !== SelectedView.FlameGraph && (
|
|
|
|
<FlameGraphTopTableContainer
|
|
|
|
data={props.data}
|
|
|
|
app={props.app}
|
|
|
|
totalLevels={levels.length}
|
|
|
|
selectedView={selectedView}
|
|
|
|
search={search}
|
|
|
|
setSearch={setSearch}
|
|
|
|
setTopLevelIndex={setTopLevelIndex}
|
|
|
|
setSelectedBarIndex={setSelectedBarIndex}
|
|
|
|
setRangeMin={setRangeMin}
|
|
|
|
setRangeMax={setRangeMax}
|
2023-03-13 05:06:04 -05:00
|
|
|
getLabelValue={getLabelValue}
|
2023-03-01 05:32:39 -06:00
|
|
|
/>
|
|
|
|
)}
|
2022-10-07 05:39:14 -05:00
|
|
|
|
2023-03-01 05:32:39 -06:00
|
|
|
{selectedView !== SelectedView.TopTable && (
|
|
|
|
<FlameGraph
|
|
|
|
data={props.data}
|
|
|
|
app={props.app}
|
|
|
|
flameGraphHeight={props.flameGraphHeight}
|
|
|
|
levels={levels}
|
|
|
|
topLevelIndex={topLevelIndex}
|
|
|
|
selectedBarIndex={selectedBarIndex}
|
|
|
|
rangeMin={rangeMin}
|
|
|
|
rangeMax={rangeMax}
|
|
|
|
search={search}
|
|
|
|
setTopLevelIndex={setTopLevelIndex}
|
|
|
|
setSelectedBarIndex={setSelectedBarIndex}
|
|
|
|
setRangeMin={setRangeMin}
|
|
|
|
setRangeMax={setRangeMax}
|
|
|
|
selectedView={selectedView}
|
2023-03-13 05:06:04 -05:00
|
|
|
getLabelValue={getLabelValue}
|
2023-03-01 05:32:39 -06:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-10-07 05:39:14 -05:00
|
|
|
)}
|
2023-03-01 05:32:39 -06:00
|
|
|
</>
|
2022-10-07 05:39:14 -05:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-20 08:20:48 -05:00
|
|
|
const getStyles = (app: CoreApp, height: number) => ({
|
|
|
|
container: css`
|
|
|
|
height: ${app === CoreApp.Explore ? height + 'px' : '100%'};
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
2022-10-07 05:39:14 -05:00
|
|
|
export default FlameGraphContainer;
|