mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge pull request #14975 from grafana/default-datasource-query-editor
Fix for default datasource query editor
This commit is contained in:
commit
9c8dea062a
@ -197,7 +197,7 @@ export class QueriesTab extends PureComponent<Props, State> {
|
|||||||
<div className="query-editor-rows">
|
<div className="query-editor-rows">
|
||||||
{panel.targets.map((query, index) => (
|
{panel.targets.map((query, index) => (
|
||||||
<QueryEditorRow
|
<QueryEditorRow
|
||||||
datasourceName={query.datasource || panel.datasource}
|
dataSourceValue={query.datasource || panel.datasource}
|
||||||
key={query.refId}
|
key={query.refId}
|
||||||
panel={panel}
|
panel={panel}
|
||||||
query={query}
|
query={query}
|
||||||
|
@ -18,11 +18,12 @@ interface Props {
|
|||||||
onAddQuery: (query?: DataQuery) => void;
|
onAddQuery: (query?: DataQuery) => void;
|
||||||
onRemoveQuery: (query: DataQuery) => void;
|
onRemoveQuery: (query: DataQuery) => void;
|
||||||
onMoveQuery: (query: DataQuery, direction: number) => void;
|
onMoveQuery: (query: DataQuery, direction: number) => void;
|
||||||
datasourceName: string | null;
|
dataSourceValue: string | null;
|
||||||
inMixedMode: boolean;
|
inMixedMode: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
|
loadedDataSourceValue: string | null | undefined;
|
||||||
datasource: DataSourceApi | null;
|
datasource: DataSourceApi | null;
|
||||||
isCollapsed: boolean;
|
isCollapsed: boolean;
|
||||||
angularScope: AngularQueryComponentScope | null;
|
angularScope: AngularQueryComponentScope | null;
|
||||||
@ -36,6 +37,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
|
|||||||
datasource: null,
|
datasource: null,
|
||||||
isCollapsed: false,
|
isCollapsed: false,
|
||||||
angularScope: null,
|
angularScope: null,
|
||||||
|
loadedDataSourceValue: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
@ -61,14 +63,14 @@ export class QueryEditorRow extends PureComponent<Props, State> {
|
|||||||
const dataSourceSrv = getDatasourceSrv();
|
const dataSourceSrv = getDatasourceSrv();
|
||||||
const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
|
const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
|
||||||
|
|
||||||
this.setState({ datasource });
|
this.setState({ datasource, loadedDataSourceValue: this.props.dataSourceValue });
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate() {
|
componentDidUpdate() {
|
||||||
const { datasource } = this.state;
|
const { loadedDataSourceValue } = this.state;
|
||||||
|
|
||||||
// check if we need to load another datasource
|
// check if we need to load another datasource
|
||||||
if (datasource && datasource.name !== this.props.datasourceName) {
|
if (loadedDataSourceValue !== this.props.dataSourceValue) {
|
||||||
if (this.angularQueryEditor) {
|
if (this.angularQueryEditor) {
|
||||||
this.angularQueryEditor.destroy();
|
this.angularQueryEditor.destroy();
|
||||||
this.angularQueryEditor = null;
|
this.angularQueryEditor = null;
|
||||||
@ -178,7 +180,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { query, datasourceName, inMixedMode } = this.props;
|
const { query, inMixedMode } = this.props;
|
||||||
const { datasource, isCollapsed } = this.state;
|
const { datasource, isCollapsed } = this.state;
|
||||||
const isDisabled = query.hide;
|
const isDisabled = query.hide;
|
||||||
|
|
||||||
@ -202,7 +204,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
|
|||||||
{isCollapsed && <i className="fa fa-caret-right" />}
|
{isCollapsed && <i className="fa fa-caret-right" />}
|
||||||
{!isCollapsed && <i className="fa fa-caret-down" />}
|
{!isCollapsed && <i className="fa fa-caret-down" />}
|
||||||
<span>{query.refId}</span>
|
<span>{query.refId}</span>
|
||||||
{inMixedMode && <em className="query-editor-row__context-info"> ({datasourceName})</em>}
|
{inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
|
||||||
{isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
|
{isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
|
||||||
</div>
|
</div>
|
||||||
<div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
|
<div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
|
||||||
|
@ -62,7 +62,7 @@ const mustKeepProps: { [str: string]: boolean } = {
|
|||||||
const defaults: any = {
|
const defaults: any = {
|
||||||
gridPos: { x: 0, y: 0, h: 3, w: 6 },
|
gridPos: { x: 0, y: 0, h: 3, w: 6 },
|
||||||
datasource: null,
|
datasource: null,
|
||||||
targets: [{}],
|
targets: [{ refId: 'A' }],
|
||||||
cachedPluginOptions: {},
|
cachedPluginOptions: {},
|
||||||
transparent: false,
|
transparent: false,
|
||||||
};
|
};
|
||||||
@ -83,7 +83,7 @@ export class PanelModel {
|
|||||||
collapsed?: boolean;
|
collapsed?: boolean;
|
||||||
panels?: any;
|
panels?: any;
|
||||||
soloMode?: boolean;
|
soloMode?: boolean;
|
||||||
targets: any[];
|
targets: DataQuery[];
|
||||||
datasource: string;
|
datasource: string;
|
||||||
thresholds?: any;
|
thresholds?: any;
|
||||||
|
|
||||||
@ -118,6 +118,18 @@ export class PanelModel {
|
|||||||
|
|
||||||
// defaults
|
// defaults
|
||||||
_.defaultsDeep(this, _.cloneDeep(defaults));
|
_.defaultsDeep(this, _.cloneDeep(defaults));
|
||||||
|
// queries must have refId
|
||||||
|
this.ensureQueryIds();
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureQueryIds() {
|
||||||
|
if (this.targets) {
|
||||||
|
for (const query of this.targets) {
|
||||||
|
if (!query.refId) {
|
||||||
|
query.refId = this.getNextQueryLetter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getOptions(panelDefaults) {
|
getOptions(panelDefaults) {
|
||||||
@ -243,7 +255,7 @@ export class PanelModel {
|
|||||||
addQuery(query?: Partial<DataQuery>) {
|
addQuery(query?: Partial<DataQuery>) {
|
||||||
query = query || { refId: 'A' };
|
query = query || { refId: 'A' };
|
||||||
query.refId = this.getNextQueryLetter();
|
query.refId = this.getNextQueryLetter();
|
||||||
this.targets.push(query);
|
this.targets.push(query as DataQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
getNextQueryLetter(): string {
|
getNextQueryLetter(): string {
|
||||||
|
@ -9,6 +9,10 @@ describe('PanelModel', () => {
|
|||||||
model = new PanelModel({
|
model = new PanelModel({
|
||||||
type: 'table',
|
type: 'table',
|
||||||
showColumns: true,
|
showColumns: true,
|
||||||
|
targets: [
|
||||||
|
{refId: 'A'},
|
||||||
|
{noRefId: true}
|
||||||
|
]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -20,6 +24,10 @@ describe('PanelModel', () => {
|
|||||||
expect(model.showColumns).toBe(true);
|
expect(model.showColumns).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should add missing refIds', () => {
|
||||||
|
expect(model.targets[1].refId).toBe('B');
|
||||||
|
});
|
||||||
|
|
||||||
it('getSaveModel should remove defaults', () => {
|
it('getSaveModel should remove defaults', () => {
|
||||||
const saveModel = model.getSaveModel();
|
const saveModel = model.getSaveModel();
|
||||||
expect(saveModel.gridPos).toBe(undefined);
|
expect(saveModel.gridPos).toBe(undefined);
|
||||||
|
Loading…
Reference in New Issue
Block a user