The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/dashboard-scene/serialization/custom-variables/SnapshotVariable.tsx

81 lines
2.3 KiB

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);
}
}