Skip to content
This repository was archived by the owner on Apr 27, 2023. It is now read-only.

Commit d571d3c

Browse files
committed
fix first review bugs
1 parent dba0765 commit d571d3c

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

src/SimplePanel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import FlameGraphRenderer from 'components/FlameGraphRenderer';
88

99
interface Props extends PanelProps<SimpleOptions> {}
1010

11-
export const SimplePanel: React.FC<Props> = ({ options, data }) => {
11+
export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) => {
1212
const theme = useTheme();
1313
const styles = getStyles();
1414
return (
1515
<>
1616
<div className={styles.app}>
1717
<div className={`${styles.appContainer} flamegraph-wrapper`}>
18-
<FlameGraphRenderer options={options} data={data} />
18+
<FlameGraphRenderer options={options} data={data} width={width} height={height} />
1919
</div>
2020
</div>
2121
</>

src/components/FlameGraphRenderer.tsx

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ const getStyles = stylesFactory(() => {
3232
margin: 0 6px;
3333
width: 100%;
3434
`,
35+
flamegraphTitle: css`
36+
width: 100%;
37+
display: block;
38+
text-align: center;
39+
`,
3540
};
3641
});
3742

@@ -64,9 +69,9 @@ class FlameGraphRenderer extends React.Component {
6469
this.rangeMin = 0;
6570
this.rangeMax = 1;
6671
this.query = "";
67-
const panelContainer = document.querySelector('.flamegraph-wrapper')?.closest('.panel-wrapper');
68-
const panelContanerResizeObserver = new ResizeObserver(this.resizeHandler);
69-
panelContanerResizeObserver.observe(panelContainer);
72+
this.panelContainer = document.querySelector('.flamegraph-wrapper')?.closest('.panel-wrapper');
73+
// const panelContanerResizeObserver = new ResizeObserver(this.resizeHandler);
74+
// panelContanerResizeObserver.observe(panelContainer);
7075
window.addEventListener("focus", this.focusHandler);
7176

7277
if (this.props.shortcut) {
@@ -81,14 +86,17 @@ class FlameGraphRenderer extends React.Component {
8186
}
8287

8388
componentDidUpdate(prevProps, prevState) {
89+
const name = this.props.data.series[this.props.data.series.length - 1].name;
8490
const from = this.props.data?.timeRange?.raw.from.valueOf();
8591
const until = this.props.data?.timeRange?.raw.to.valueOf();
92+
const prevName = prevProps.data.series[prevProps.data.series.length - 1].name;
8693
const prevFrom = prevProps.data?.timeRange?.raw.from.valueOf();
8794
const prevUntil = prevProps.data?.timeRange?.raw.to.valueOf();
88-
if (from !== prevFrom || until !== prevUntil) {
95+
if (from !== prevFrom || until !== prevUntil || name !== prevName) {
8996
this.updateFlameBearerData()
9097
}
9198

99+
this.resizeHandler();
92100
if (
93101
this.state.flamebearer &&
94102
prevState.flamebearer != this.state.flamebearer
@@ -227,7 +235,12 @@ class FlameGraphRenderer extends React.Component {
227235
// this is here to debounce resize events (see: https://css-tricks.com/debouncing-throttling-explained-examples/)
228236
// because rendering is expensive
229237
clearTimeout(this.resizeFinish);
230-
const responsiveHeight = (el[0].contentRect.height - 60) / this.state.flamebearer.levels.length;
238+
let responsiveHeight = (this.panelContainer.getClientRects()[0].height - 60) / this.state.flamebearer.levels.length;
239+
this.levelsToShow = this.state.flamebearer.levels.length;
240+
while(responsiveHeight < 14) {
241+
this.levelsToShow -=1;
242+
responsiveHeight = (this.panelContainer.getClientRects()[0].height - 60) / this.levelsToShow;
243+
}
231244
this.pxPerLevel = responsiveHeight > 20 ? 20 : responsiveHeight;
232245
this.resizeFinish = setTimeout(this.renderCanvas, 50);
233246
};
@@ -265,8 +278,8 @@ class FlameGraphRenderer extends React.Component {
265278
this.canvas.height = this.pxPerLevel * (levels.length - this.topLevel);
266279
this.canvas.style.width='100%';
267280
this.canvas.style.height='100%';
268-
this.canvas.width = this.canvas.offsetWidth;
269-
this.canvas.height = this.canvas.offsetHeight;
281+
this.canvas.width = this.props.width;
282+
this.canvas.height = this.props.height;
270283
if (devicePixelRatio > 1) {
271284
this.canvas.width *= 2;
272285
this.canvas.height *= 2;
@@ -279,7 +292,7 @@ class FlameGraphRenderer extends React.Component {
279292

280293
const formatter = this.createFormatter();
281294
// i = level
282-
for (let i = 0; i < levels.length - this.topLevel; i++) {
295+
for (let i = 0; i < this.levelsToShow - this.topLevel; i++) {
283296
const level = levels[this.topLevel + i];
284297
for (let j = 0; j < level.length; j += 4) {
285298
// j = 0: x start of bar
@@ -460,8 +473,7 @@ class FlameGraphRenderer extends React.Component {
460473
className={this.styles.flamegraphPane}
461474
>
462475
<div className='flamegraph-header'>
463-
<span></span>
464-
<span>Frame width represents {unitsToFlamegraphTitle[this.state.units]}</span>
476+
<span className={this.styles.flamegraphTitle}>Frame width represents {unitsToFlamegraphTitle[this.state.units]}</span>
465477
</div>
466478
<canvas
467479
height="0"

src/img/logo.svg

Lines changed: 32 additions & 1 deletion
Loading

src/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"links": [
2121
{
2222
"name": "Website",
23-
"url": "https://github.com/grafana/grafana-starter-panel"
23+
"url": "https://pyroscope.io"
2424
},
2525
{
2626
"name": "License",

0 commit comments

Comments
 (0)