forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dashboard Scene: Fix snapshots not displaying variables values (grafa…
…na#88967) * Use new snapshot variables from scenes * Add snapshotVariable implementation * Refactor: Extract variables logic from transforSaveModelToScene file --------- Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
- Loading branch information
1 parent
6f63def
commit cd4b7ef
Showing
15 changed files
with
1,286 additions
and
830 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
public/app/features/dashboard-scene/serialization/custom-variables/SnapshotVariable.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { SnapshotVariable } from './SnapshotVariable'; | ||
|
||
describe('SnapshotVariable', () => { | ||
describe('SnapshotVariable state', () => { | ||
it('should create a new snapshotVariable when custom variable is passed', () => { | ||
const { multiVariable } = setupScene(); | ||
const snapshot = new SnapshotVariable(multiVariable); | ||
//expect snapshot to be defined | ||
expect(snapshot).toBeDefined(); | ||
expect(snapshot.state).toBeDefined(); | ||
expect(snapshot.state.type).toBe('snapshot'); | ||
expect(snapshot.state.isReadOnly).toBe(true); | ||
expect(snapshot.state.value).toBe(multiVariable.value); | ||
expect(snapshot.state.text).toBe(multiVariable.text); | ||
expect(snapshot.state.hide).toBe(multiVariable.hide); | ||
}); | ||
}); | ||
}); | ||
|
||
function setupScene() { | ||
// create custom variable type custom | ||
|
||
const multiVariable = { | ||
name: 'Multi', | ||
description: 'Define variable values manually', | ||
text: 'myMultiText', | ||
value: 'myMultiValue', | ||
multi: true, | ||
hide: 0, | ||
}; | ||
|
||
return { multiVariable }; | ||
} |
81 changes: 81 additions & 0 deletions
81
public/app/features/dashboard-scene/serialization/custom-variables/SnapshotVariable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Observable, map, of } from 'rxjs'; | ||
|
||
import { | ||
MultiValueVariable, | ||
MultiValueVariableState, | ||
SceneComponentProps, | ||
ValidateAndUpdateResult, | ||
VariableDependencyConfig, | ||
VariableValueOption, | ||
renderSelectForVariable, | ||
sceneGraph, | ||
VariableGetOptionsArgs, | ||
} from '@grafana/scenes'; | ||
|
||
export interface SnapshotVariableState extends MultiValueVariableState { | ||
query?: string; | ||
} | ||
|
||
export class SnapshotVariable extends MultiValueVariable<SnapshotVariableState> { | ||
protected _variableDependency = new VariableDependencyConfig(this, { | ||
statePaths: [], | ||
}); | ||
|
||
public constructor(initialState: Partial<SnapshotVariableState>) { | ||
super({ | ||
name: '', | ||
type: 'snapshot', | ||
isReadOnly: true, | ||
query: '', | ||
value: '', | ||
text: '', | ||
options: [], | ||
...initialState, | ||
}); | ||
} | ||
|
||
public getValueOptions(args: VariableGetOptionsArgs): Observable<VariableValueOption[]> { | ||
const interpolated = sceneGraph.interpolate(this, this.state.query); | ||
const match = interpolated.match(/(?:\\,|[^,])+/g) ?? []; | ||
|
||
const options = match.map((text) => { | ||
text = text.replace(/\\,/g, ','); | ||
const textMatch = /^(.+)\s:\s(.+)$/g.exec(text) ?? []; | ||
if (textMatch.length === 3) { | ||
const [, key, value] = textMatch; | ||
return { label: key.trim(), value: value.trim() }; | ||
} else { | ||
return { label: text.trim(), value: text.trim() }; | ||
} | ||
}); | ||
|
||
return of(options); | ||
} | ||
|
||
public validateAndUpdate(): Observable<ValidateAndUpdateResult> { | ||
return this.getValueOptions({}).pipe( | ||
map((options) => { | ||
if (this.state.options !== options) { | ||
this._updateValueGivenNewOptions(options); | ||
} | ||
return {}; | ||
}) | ||
); | ||
} | ||
|
||
public static Component = ({ model }: SceneComponentProps<MultiValueVariable<SnapshotVariableState>>) => { | ||
return renderSelectForVariable(model); | ||
}; | ||
// we will always preserve the current value and text for snapshots | ||
private _updateValueGivenNewOptions(options: VariableValueOption[]) { | ||
const { value: currentValue, text: currentText } = this.state; | ||
const stateUpdate: Partial<MultiValueVariableState> = { | ||
options, | ||
loading: false, | ||
value: currentValue ?? [], | ||
text: currentText ?? [], | ||
}; | ||
|
||
this.setState(stateUpdate); | ||
} | ||
} |
Oops, something went wrong.