2022-11-09 01:02:24 -06:00
|
|
|
import { Observable, Subject } from 'rxjs';
|
|
|
|
|
|
|
|
import { queryMetricTree } from 'app/plugins/datasource/testdata/metricTree';
|
|
|
|
|
2022-11-16 04:36:30 -06:00
|
|
|
import { sceneGraph } from '../../core/sceneGraph';
|
2022-11-09 01:02:24 -06:00
|
|
|
import { SceneComponentProps } from '../../core/types';
|
2022-11-15 05:54:24 -06:00
|
|
|
import { VariableDependencyConfig } from '../VariableDependencyConfig';
|
2022-12-13 01:20:09 -06:00
|
|
|
import { renderSelectForVariable } from '../components/VariableValueSelect';
|
2022-11-09 01:02:24 -06:00
|
|
|
import { VariableValueOption } from '../types';
|
|
|
|
|
|
|
|
import { MultiValueVariable, MultiValueVariableState, VariableGetOptionsArgs } from './MultiValueVariable';
|
|
|
|
|
|
|
|
export interface TestVariableState extends MultiValueVariableState {
|
|
|
|
query: string;
|
|
|
|
delayMs?: number;
|
|
|
|
issuedQuery?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This variable is only designed for unit tests and potentially e2e tests.
|
|
|
|
*/
|
|
|
|
export class TestVariable extends MultiValueVariable<TestVariableState> {
|
|
|
|
private completeUpdate = new Subject<number>();
|
|
|
|
public isGettingValues = true;
|
2022-11-25 07:20:56 -06:00
|
|
|
public getValueOptionsCount = 0;
|
2022-11-09 01:02:24 -06:00
|
|
|
|
2022-11-15 05:54:24 -06:00
|
|
|
protected _variableDependency = new VariableDependencyConfig(this, {
|
|
|
|
statePaths: ['query'],
|
|
|
|
});
|
|
|
|
|
2022-11-16 04:36:30 -06:00
|
|
|
public constructor(initialState: Partial<TestVariableState>) {
|
|
|
|
super({
|
2022-12-12 06:01:27 -06:00
|
|
|
type: 'custom',
|
2022-11-16 04:36:30 -06:00
|
|
|
name: 'Test',
|
|
|
|
value: 'Value',
|
|
|
|
text: 'Text',
|
|
|
|
query: 'Query',
|
|
|
|
options: [],
|
|
|
|
...initialState,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-09 01:02:24 -06:00
|
|
|
public getValueOptions(args: VariableGetOptionsArgs): Observable<VariableValueOption[]> {
|
|
|
|
const { delayMs } = this.state;
|
|
|
|
|
2022-11-25 07:20:56 -06:00
|
|
|
this.getValueOptionsCount += 1;
|
|
|
|
|
2022-11-09 01:02:24 -06:00
|
|
|
return new Observable<VariableValueOption[]>((observer) => {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
2022-11-25 07:20:56 -06:00
|
|
|
const sub = this.completeUpdate.subscribe({
|
2022-11-09 01:02:24 -06:00
|
|
|
next: () => {
|
|
|
|
observer.next(this.issueQuery());
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let timeout: NodeJS.Timeout | undefined;
|
|
|
|
|
|
|
|
if (delayMs) {
|
|
|
|
timeout = setTimeout(() => this.signalUpdateCompleted(), delayMs);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isGettingValues = true;
|
|
|
|
|
|
|
|
return () => {
|
2022-11-25 07:20:56 -06:00
|
|
|
sub.unsubscribe();
|
2022-11-09 01:02:24 -06:00
|
|
|
clearTimeout(timeout);
|
|
|
|
this.isGettingValues = false;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private issueQuery() {
|
2022-11-16 04:36:30 -06:00
|
|
|
const interpolatedQuery = sceneGraph.interpolate(this, this.state.query);
|
2022-11-09 01:02:24 -06:00
|
|
|
const options = queryMetricTree(interpolatedQuery).map((x) => ({ label: x.name, value: x.name }));
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
issuedQuery: interpolatedQuery,
|
|
|
|
options,
|
|
|
|
});
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Useful from tests */
|
|
|
|
public signalUpdateCompleted() {
|
|
|
|
this.completeUpdate.next(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Component = ({ model }: SceneComponentProps<MultiValueVariable>) => {
|
2022-12-13 01:20:09 -06:00
|
|
|
return renderSelectForVariable(model);
|
2022-11-09 01:02:24 -06:00
|
|
|
};
|
|
|
|
}
|