grafana/public/app/features/query/components/QueryEditorRows.tsx
Laura Benz dbbab6e95c
Explore: Add feature tracking events (#54514)
* feat: add tracking in query row button group

* feat: add tracking for split view

* feat: add tracking for query inspector tab open

* feat: add tracking for shortened link button

* feat: add tracking for change of datasource

* feat: add tracking for time range options

* refactor: clean up

* refactor: repair tests

* refactor: clean up

* feat: add details to change of data source

* refactor: remove duplicate tracking

* refactor: make tracking reusable in an easier way

* refactor: add property

* refactor: change data for time picker

* refactor: change tracking label for time picker

Co-authored-by: Ivan Ortega Alba <ivanortegaalba@gmail.com>

* refactor: store tracking in explore component

* refactor: add index signature

* refactor: remove ?

* refactor: split into 3 callbacks

* refactor: apply suggestions from code review

Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>

Co-authored-by: Ivan Ortega Alba <ivanortegaalba@gmail.com>
Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>
2022-09-20 06:50:23 -04:00

209 lines
5.8 KiB
TypeScript

import React, { PureComponent } from 'react';
import { DragDropContext, DragStart, Droppable, DropResult } from 'react-beautiful-dnd';
import {
CoreApp,
DataQuery,
DataSourceInstanceSettings,
DataSourceRef,
EventBusExtended,
HistoryItem,
PanelData,
} from '@grafana/data';
import { getDataSourceSrv, reportInteraction } from '@grafana/runtime';
import { QueryEditorRow } from './QueryEditorRow';
interface Props {
// The query configuration
queries: DataQuery[];
dsSettings: DataSourceInstanceSettings;
// Query editing
onQueriesChange: (queries: DataQuery[]) => void;
onAddQuery: (query: DataQuery) => void;
onRunQueries: () => void;
// Query Response Data
data: PanelData;
// Misc
app?: CoreApp;
history?: Array<HistoryItem<DataQuery>>;
eventBus?: EventBusExtended;
onQueryCopied?: () => void;
onQueryRemoved?: () => void;
onQueryToggled?: (queryStatus?: boolean | undefined) => void;
onDatasourceChange?: (dataSource: DataSourceInstanceSettings, query: DataQuery) => void;
}
export class QueryEditorRows extends PureComponent<Props> {
onRemoveQuery = (query: DataQuery) => {
this.props.onQueriesChange(this.props.queries.filter((item) => item !== query));
};
onChangeQuery(query: DataQuery, index: number) {
const { queries, onQueriesChange } = this.props;
// update query in array
onQueriesChange(
queries.map((item, itemIndex) => {
if (itemIndex === index) {
return query;
}
return item;
})
);
}
onDataSourceChange(dataSource: DataSourceInstanceSettings, index: number) {
const { queries, onQueriesChange } = this.props;
if (this.props.onDatasourceChange) {
this.props.onDatasourceChange(dataSource, queries[index]);
}
onQueriesChange(
queries.map((item, itemIndex) => {
if (itemIndex !== index) {
return item;
}
const dataSourceRef: DataSourceRef = {
type: dataSource.type,
uid: dataSource.uid,
};
if (item.datasource) {
const previous = getDataSourceSrv().getInstanceSettings(item.datasource);
if (previous?.type === dataSource.type) {
return {
...item,
datasource: dataSourceRef,
};
}
}
return {
refId: item.refId,
hide: item.hide,
datasource: dataSourceRef,
};
})
);
}
onDragStart = (result: DragStart) => {
const { queries, dsSettings } = this.props;
reportInteraction('query_row_reorder_started', {
startIndex: result.source.index,
numberOfQueries: queries.length,
datasourceType: dsSettings.type,
});
};
onDragEnd = (result: DropResult) => {
const { queries, onQueriesChange, dsSettings } = this.props;
if (!result || !result.destination) {
return;
}
const startIndex = result.source.index;
const endIndex = result.destination.index;
if (startIndex === endIndex) {
reportInteraction('query_row_reorder_canceled', {
startIndex,
endIndex,
numberOfQueries: queries.length,
datasourceType: dsSettings.type,
});
return;
}
const update = Array.from(queries);
const [removed] = update.splice(startIndex, 1);
update.splice(endIndex, 0, removed);
onQueriesChange(update);
reportInteraction('query_row_reorder_ended', {
startIndex,
endIndex,
numberOfQueries: queries.length,
datasourceType: dsSettings.type,
});
};
render() {
const {
dsSettings,
data,
queries,
app,
history,
eventBus,
onAddQuery,
onRunQueries,
onQueryCopied,
onQueryRemoved,
onQueryToggled,
} = this.props;
return (
<DragDropContext onDragStart={this.onDragStart} onDragEnd={this.onDragEnd}>
<Droppable droppableId="transformations-list" direction="vertical">
{(provided) => {
return (
<div ref={provided.innerRef} {...provided.droppableProps}>
{queries.map((query, index) => {
const dataSourceSettings = getDataSourceSettings(query, dsSettings);
const onChangeDataSourceSettings = dsSettings.meta.mixed
? (settings: DataSourceInstanceSettings) => this.onDataSourceChange(settings, index)
: undefined;
return (
<QueryEditorRow
id={query.refId}
index={index}
key={query.refId}
data={data}
query={query}
dataSource={dataSourceSettings}
onChangeDataSource={onChangeDataSourceSettings}
onChange={(query) => this.onChangeQuery(query, index)}
onRemoveQuery={this.onRemoveQuery}
onAddQuery={onAddQuery}
onRunQuery={onRunQueries}
onQueryCopied={onQueryCopied}
onQueryRemoved={onQueryRemoved}
onQueryToggled={onQueryToggled}
queries={queries}
app={app}
history={history}
eventBus={eventBus}
/>
);
})}
{provided.placeholder}
</div>
);
}}
</Droppable>
</DragDropContext>
);
}
}
const getDataSourceSettings = (
query: DataQuery,
groupSettings: DataSourceInstanceSettings
): DataSourceInstanceSettings => {
if (!query.datasource) {
return groupSettings;
}
const querySettings = getDataSourceSrv().getInstanceSettings(query.datasource);
return querySettings || groupSettings;
};