Skip to content

Commit

Permalink
Use url sync
Browse files Browse the repository at this point in the history
  • Loading branch information
joey-grafana committed Jan 31, 2025
1 parent b32791d commit ed8ecf9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/components/Explore/GroupBySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ export function GroupBySelector({ options, radioAttributes, value, onChange, sho
options={getModifiedSelectOptions(otherAttrOptions)}
onChange={(selected) => {
const newSelected = selected?.value ?? defaultOnChangeValue;
locationService.partial({ 'var-groupBy': newSelected });
onChange(newSelected);
}}
className={styles.select}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface TraceSceneState extends SceneObjectState {
}

export class TracesByServiceScene extends SceneObjectBase<TraceSceneState> {
protected _urlSync = new SceneObjectUrlSyncConfig(this, { keys: ['actionView', 'selection'] });
public _urlSync = new SceneObjectUrlSyncConfig(this, { keys: ['actionView', 'selection', 'metric'] });

public constructor(state: MakeOptional<TraceSceneState, 'body'>) {
super({
Expand All @@ -76,6 +76,7 @@ export class TracesByServiceScene extends SceneObjectBase<TraceSceneState> {
this._subs.add(
metricVariable.subscribeToState((newState, prevState) => {
if (newState.value !== prevState.value) {
this.setState({ metric: newState.value as MetricFunction });
const selection = getDefaultSelectionForMetric(newState.value as MetricFunction);
if (selection) {
this.setState({ selection });
Expand Down Expand Up @@ -155,6 +156,7 @@ export class TracesByServiceScene extends SceneObjectBase<TraceSceneState> {
getUrlState() {
return {
actionView: this.state.actionView,
metric: this.state.metric,
selection: this.state.selection ? JSON.stringify(this.state.selection) : undefined,
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Explore/panels/histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { getTraceByServiceScene, shouldShowSelection } from '../../../utils/util
import { ComparisonSelection } from '../../../utils/shared';
import { reportAppInteraction, USER_EVENTS_ACTIONS, USER_EVENTS_PAGES } from '../../../utils/analytics';
import { PanelBuilders, SceneFlexItem, SceneFlexLayout, SceneObject } from '@grafana/scenes';
import { locationService } from '@grafana/runtime';

export function getHistogramVizPanel(scene: SceneObject, yBuckets: number[]) {
const parent = getTraceByServiceScene(scene);
Expand All @@ -23,7 +22,6 @@ export function getHistogramVizPanel(scene: SceneObject, yBuckets: number[]) {
const rawSelection = args[0];
// @ts-ignore
const newSelection: ComparisonSelection = { type: 'manual', raw: rawSelection };
locationService.partial({ 'selection': newSelection });

newSelection.timeRange = {
from: Math.round((rawSelection.x?.from || 0) / 1000),
Expand All @@ -39,7 +37,9 @@ export function getHistogramVizPanel(scene: SceneObject, yBuckets: number[]) {
const yTo = yBucketToDuration(args[0].y?.to || 0, yBuckets);
newSelection.duration = { from: yFrom, to: yTo };

parent.setState({ selection: newSelection });
parent._urlSync.performBrowserHistoryAction(() => {
parent.setState({ selection: newSelection });
});
if (!shouldShowSelection(parent.state.actionView)) {
parent.setActionView('comparison');
}
Expand Down
13 changes: 7 additions & 6 deletions src/pages/Explore/TraceExploration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
import {
LocationService,
config,
locationService,
// @ts-ignore
sidecarServiceSingleton_EXPERIMENTAL,
} from '@grafana/runtime';
Expand All @@ -40,7 +39,7 @@ import {
VAR_LATENCY_THRESHOLD,
VAR_METRIC,
} from '../../utils/shared';
import { getTraceExplorationScene, getFilterSignature, getFiltersVariable } from '../../utils/utils';
import { getTraceExplorationScene, getFilterSignature, getFiltersVariable, getTraceByServiceSceneAsDescendent } from '../../utils/utils';
import { DetailsScene } from '../../components/Explore/TracesByService/DetailsScene';
import { FilterByVariable } from 'components/Explore/filters/FilterByVariable';
import { getSignalForKey, primarySignalOptions } from './primary-signals';
Expand Down Expand Up @@ -191,8 +190,9 @@ export class TraceExploration extends SceneObjectBase<TraceExplorationState> {
return;
}

locationService.partial({ 'primarySignal': signal });
this.setState({ primarySignal: signal });
this._urlSync.performBrowserHistoryAction(() => {
this.setState({ primarySignal: signal });
});
};

public onChangeMetricFunction = (metric: string) => {
Expand All @@ -201,8 +201,9 @@ export class TraceExploration extends SceneObjectBase<TraceExplorationState> {
return;
}

locationService.partial({ 'var-metric': metric });
variable.changeValueTo(metric);
getTraceByServiceSceneAsDescendent(this)._urlSync.performBrowserHistoryAction(() => {
variable.changeValueTo(metric);
});
};

public getMetricFunction() {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Explore/TraceExplorationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function TraceExplorationView({ exploration }: { exploration: TraceExplor
}

return (
<UrlSyncContextProvider scene={exploration} updateUrlOnInit={true} createBrowserHistorySteps={false}>
<UrlSyncContextProvider scene={exploration} updateUrlOnInit={true} createBrowserHistorySteps={true}>
<exploration.Component model={exploration} />
</UrlSyncContextProvider>
);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export function getTraceByServiceScene(model: SceneObject): TracesByServiceScene
return sceneGraph.getAncestor(model, TracesByServiceScene);
}

export function getTraceByServiceSceneAsDescendent(model: SceneObject): TracesByServiceScene {
const scenes = sceneGraph.findDescendents(model, TracesByServiceScene);

if (!scenes || scenes.length < 1 || !(scenes[0] instanceof TracesByServiceScene)) {
throw new Error('TracesByServiceScene not found');
}

return scenes[0];
}

export function newTracesExploration(
locationService: LocationService,
initialDS?: string,
Expand Down

0 comments on commit ed8ecf9

Please sign in to comment.