Commit 5f4b5dfe by Ryan McKinley Committed by GitHub

Refactor: use data rather than series in stream callback(#18126)

parent 85da4a16
......@@ -309,9 +309,9 @@ export interface DataStreamState {
request: DataQueryRequest;
/**
* Series data may not be known yet
* Data may not be known yet
*/
series?: DataFrame[];
data?: DataFrame[];
/**
* Error in stream (but may still be running)
......
......@@ -11,7 +11,7 @@ import ErrorBoundary from 'app/core/components/ErrorBoundary/ErrorBoundary';
import { getTimeSrv, TimeSrv } from '../services/TimeSrv';
import { applyPanelTimeOverrides, calculateInnerPanelHeight } from 'app/features/dashboard/utils/panel';
import { profiler } from 'app/core/profiler';
import { getProcessedDataFrame } from '../state/PanelQueryState';
import { getProcessedDataFrames } from '../state/PanelQueryState';
import templateSrv from 'app/features/templating/template_srv';
import config from 'app/core/config';
......@@ -71,7 +71,7 @@ export class PanelChrome extends PureComponent<Props, State> {
this.setState({
data: {
state: LoadingState.Done,
series: getProcessedDataFrame(panel.snapshotData),
series: getProcessedDataFrames(panel.snapshotData),
},
isFirstLoad: false,
});
......
......@@ -168,7 +168,7 @@ describe('PanelQueryRunner', () => {
streamState = {
state: LoadingState.Streaming,
key: 'test-stream-1',
series: [
data: [
{
rows: [],
fields: [],
......
......@@ -34,7 +34,7 @@ export interface QueryRunnerOptions<
}
export enum PanelQueryRunnerFormat {
series = 'series',
frames = 'frames',
legacy = 'legacy',
both = 'both',
}
......@@ -57,7 +57,7 @@ export class PanelQueryRunner {
* Listen for updates to the PanelData. If a query has already run for this panel,
* the results will be immediatly passed to the observer
*/
subscribe(observer: PartialObserver<PanelData>, format = PanelQueryRunnerFormat.series): Unsubscribable {
subscribe(observer: PartialObserver<PanelData>, format = PanelQueryRunnerFormat.frames): Unsubscribable {
if (!this.subject) {
this.subject = new Subject(); // Delay creating a subject until someone is listening
}
......@@ -65,10 +65,10 @@ export class PanelQueryRunner {
if (format === PanelQueryRunnerFormat.legacy) {
this.state.sendLegacy = true;
} else if (format === PanelQueryRunnerFormat.both) {
this.state.sendSeries = true;
this.state.sendFrames = true;
this.state.sendLegacy = true;
} else {
this.state.sendSeries = true;
this.state.sendFrames = true;
}
// Send the last result
......
import { toDataQueryError, PanelQueryState, getProcessedDataFrame } from './PanelQueryState';
import { toDataQueryError, PanelQueryState, getProcessedDataFrames } from './PanelQueryState';
import { MockDataSourceApi } from 'test/mocks/datasource_srv';
import { LoadingState } from '@grafana/data';
import { DataQueryResponse } from '@grafana/ui';
......@@ -65,7 +65,7 @@ describe('getProcessedDataFrame', () => {
target: '',
datapoints: [[100, 1], [200, 2]],
};
const data = getProcessedDataFrame([null, input1, input2, null, null]);
const data = getProcessedDataFrames([null, input1, input2, null, null]);
expect(data.length).toBe(2);
expect(data[0].fields[0].name).toBe(input1.target);
expect(data[0].rows).toBe(input1.datapoints);
......@@ -83,10 +83,10 @@ describe('getProcessedDataFrame', () => {
});
it('supports null values from query OK', () => {
expect(getProcessedDataFrame([null, null, null, null])).toEqual([]);
expect(getProcessedDataFrame(undefined)).toEqual([]);
expect(getProcessedDataFrame((null as unknown) as any[])).toEqual([]);
expect(getProcessedDataFrame([])).toEqual([]);
expect(getProcessedDataFrames([null, null, null, null])).toEqual([]);
expect(getProcessedDataFrames(undefined)).toEqual([]);
expect(getProcessedDataFrames((null as unknown) as any[])).toEqual([]);
expect(getProcessedDataFrames([])).toEqual([]);
});
});
......@@ -129,7 +129,7 @@ describe('stream handling', () => {
state: LoadingState.Loading,
key: 'C',
request: state.request, // From the same request
series: [makeSeriesStub('C')],
data: [makeSeriesStub('C')],
unsubscribe: () => {},
});
expect(state.streams.length).toBe(1);
......@@ -146,7 +146,7 @@ describe('stream handling', () => {
state: LoadingState.Loading,
key: 'D',
request: state.request, // From the same request
series: [makeSeriesStub('D')],
data: [makeSeriesStub('D')],
unsubscribe: () => {},
});
expect(state.streams.length).toBe(2);
......@@ -163,7 +163,7 @@ describe('stream handling', () => {
state: LoadingState.Loading,
key: 'C', // The key to replace previous index 2
request: state.request, // From the same request
series: [makeSeriesStub('X')],
data: [makeSeriesStub('X')],
unsubscribe: () => {},
});
expect(state.streams.length).toBe(2);
......@@ -181,7 +181,7 @@ describe('stream handling', () => {
...state.request,
requestId: 'XXX', // Different request and id
} as any,
series: [makeSeriesStub('C')],
data: [makeSeriesStub('C')],
unsubscribe: () => {},
});
......
......@@ -41,7 +41,7 @@ export class PanelQueryState {
// Active stream results
streams: DataStreamState[] = [];
sendSeries = false;
sendFrames = false;
sendLegacy = false;
// A promise for the running query
......@@ -127,15 +127,15 @@ export class PanelQueryState {
this.executor = null;
// Make sure we send something back -- called run() w/o subscribe!
if (!(this.sendSeries || this.sendLegacy)) {
this.sendSeries = true;
if (!(this.sendFrames || this.sendLegacy)) {
this.sendFrames = true;
}
// Save the result state
this.response = {
state: LoadingState.Done,
request: this.request,
series: this.sendSeries ? getProcessedDataFrame(resp.data) : [],
series: this.sendFrames ? getProcessedDataFrames(resp.data) : [],
legacy: this.sendLegacy ? translateToLegacyData(resp.data) : undefined,
};
resolve(this.validateStreamsAndGetPanelData());
......@@ -156,7 +156,7 @@ export class PanelQueryState {
// it will then delegate real changes to the PanelQueryRunner
dataStreamObserver: DataStreamObserver = (stream: DataStreamState) => {
// Streams only work with the 'series' format
this.sendSeries = true;
this.sendFrames = true;
// Add the stream to our list
let found = false;
......@@ -189,8 +189,8 @@ export class PanelQueryState {
const series: DataFrame[] = [];
for (const stream of this.streams) {
if (stream.series) {
series.push.apply(series, stream.series);
if (stream.data) {
series.push.apply(series, stream.data);
}
try {
......@@ -243,7 +243,7 @@ export class PanelQueryState {
}
active.push(stream);
series.push.apply(series, stream.series);
series.push.apply(series, stream.data);
if (!this.isFinished(stream.state)) {
done = false;
......@@ -277,11 +277,11 @@ export class PanelQueryState {
* Make sure all requested formats exist on the data
*/
getDataAfterCheckingFormats(): PanelData {
const { response, sendLegacy, sendSeries } = this;
const { response, sendLegacy, sendFrames } = this;
if (sendLegacy && (!response.legacy || !response.legacy.length)) {
response.legacy = response.series.map(v => toLegacyResponseData(v));
}
if (sendSeries && !response.series.length && response.legacy) {
if (sendFrames && !response.series.length && response.legacy) {
response.series = response.legacy.map(v => toDataFrame(v));
}
return this.validateStreamsAndGetPanelData();
......@@ -349,7 +349,7 @@ function translateToLegacyData(data: DataQueryResponseData) {
*
* This is also used by PanelChrome for snapshot support
*/
export function getProcessedDataFrame(results?: any[]): DataFrame[] {
export function getProcessedDataFrames(results?: any[]): DataFrame[] {
if (!results) {
return [];
}
......
......@@ -189,7 +189,7 @@ describe('runQueriesBatchEpic', () => {
)
.whenQueryObserverReceivesEvent({
state: LoadingState.Done,
series: null,
data: null,
delta,
key: 'some key',
request: {} as DataQueryRequest,
......
......@@ -104,8 +104,8 @@ export const runQueriesBatchEpic: Epic<ActionOf<any>, ActionOf<any>, StoreState>
// observer subscription, handles datasourceInstance.query observer events and pushes that forward
const streamSubscription = streamHandler.subscribe({
next: event => {
const { state, error, series, delta } = event;
if (!series && !delta && !error) {
const { state, error, data, delta } = event;
if (!data && !delta && !error) {
return;
}
......
......@@ -3,7 +3,7 @@ import { DataQueryResponse, DataQueryResponseData } from '@grafana/ui';
import { TableData, isTableData, LogsModel, toDataFrame, guessFieldTypes, TimeSeries } from '@grafana/data';
import { ExploreItemState, ExploreMode } from 'app/types/explore';
import { getProcessedDataFrame } from 'app/features/dashboard/state/PanelQueryState';
import { getProcessedDataFrames } from 'app/features/dashboard/state/PanelQueryState';
import TableModel, { mergeTablesIntoModel } from 'app/core/table_model';
import { sortLogsResult } from 'app/core/utils/explore';
import { dataFrameToLogsModel } from 'app/core/logs_model';
......@@ -101,7 +101,7 @@ export class ResultProcessor {
};
private makeTimeSeriesList = (rawData: any[]) => {
const dataList = getProcessedDataFrame(rawData);
const dataList = getProcessedDataFrames(rawData);
const dataProcessor = new DataProcessor({ xaxis: {}, aliasColors: [] }); // Hack before we use GraphSeriesXY instead
const timeSeries = dataProcessor.getSeriesList({ dataList });
......
......@@ -158,7 +158,7 @@ class MetricsPanelCtrl extends PanelCtrl {
data: data.legacy,
});
} else {
this.handleDataFrame(data.series);
this.handleDataFrames(data.series);
}
},
};
......@@ -217,7 +217,7 @@ class MetricsPanelCtrl extends PanelCtrl {
});
}
handleDataFrame(data: DataFrame[]) {
handleDataFrames(data: DataFrame[]) {
if (this.dashboard && this.dashboard.snapshot) {
this.panel.snapshotData = data;
}
......
......@@ -113,7 +113,7 @@ export class StreamWorker {
const maxRows = query.buffer ? query.buffer : stream.request.maxDataPoints;
// Edit the first series
const series = stream.series[0];
const series = stream.data[0];
let rows = series.rows.concat(append);
const extra = maxRows - rows.length;
if (extra < 0) {
......@@ -143,7 +143,7 @@ export class SignalWorker extends StreamWorker {
constructor(key: string, query: TestDataQuery, request: DataQueryRequest, observer: DataStreamObserver) {
super(key, query, request, observer);
setTimeout(() => {
this.stream.series = [this.initBuffer(query.refId)];
this.stream.data = [this.initBuffer(query.refId)];
this.looper();
}, 10);
......@@ -253,7 +253,7 @@ export class FetchWorker extends StreamWorker {
onHeader = (series: DataFrame) => {
series.refId = this.refId;
this.stream.series = [series];
this.stream.data = [series];
};
onRow = (row: any[]) => {
......@@ -269,7 +269,7 @@ export class LogsWorker extends StreamWorker {
super(key, query, request, observer);
window.setTimeout(() => {
this.stream.series = [this.initBuffer(query.refId)];
this.stream.data = [this.initBuffer(query.refId)];
this.looper();
}, 10);
}
......
......@@ -13,7 +13,7 @@ import config from 'app/core/config';
import TimeSeries from 'app/core/time_series2';
import { DataFrame, DataLink } from '@grafana/data';
import { getColorFromHexRgbOrName, LegacyResponseData, VariableSuggestion } from '@grafana/ui';
import { getProcessedDataFrame } from 'app/features/dashboard/state/PanelQueryState';
import { getProcessedDataFrames } from 'app/features/dashboard/state/PanelQueryState';
import { PanelQueryRunnerFormat } from 'app/features/dashboard/state/PanelQueryRunner';
import { GraphContextMenuCtrl } from './GraphContextMenuCtrl';
import { getDataLinksVariableSuggestions } from 'app/features/panel/panellinks/link_srv';
......@@ -143,7 +143,7 @@ class GraphCtrl extends MetricsPanelCtrl {
_.defaults(this.panel.xaxis, this.panelDefaults.xaxis);
_.defaults(this.panel.options, this.panelDefaults.options);
this.dataFormat = PanelQueryRunnerFormat.series;
this.dataFormat = PanelQueryRunnerFormat.frames;
this.processor = new DataProcessor(this.panel);
this.contextMenuCtrl = new GraphContextMenuCtrl($scope);
......@@ -210,12 +210,12 @@ class GraphCtrl extends MetricsPanelCtrl {
// This should only be called from the snapshot callback
onDataReceived(dataList: LegacyResponseData[]) {
this.handleDataFrame(getProcessedDataFrame(dataList));
this.handleDataFrames(getProcessedDataFrames(dataList));
}
// Directly support DataFrame skipping event callbacks
handleDataFrame(data: DataFrame[]) {
super.handleDataFrame(data);
handleDataFrames(data: DataFrame[]) {
super.handleDataFrames(data);
this.dataList = data;
this.seriesList = this.processor.getSeriesList({
......
import { DataProcessor } from '../data_processor';
import { getProcessedDataFrame } from 'app/features/dashboard/state/PanelQueryState';
import { getProcessedDataFrames } from 'app/features/dashboard/state/PanelQueryState';
describe('Graph DataProcessor', () => {
const panel: any = {
......@@ -11,7 +11,7 @@ describe('Graph DataProcessor', () => {
describe('getTimeSeries from LegacyResponseData', () => {
// Try each type of data
const dataList = getProcessedDataFrame([
const dataList = getProcessedDataFrames([
{
alias: 'First (time_series)',
datapoints: [[1, 1001], [2, 1002], [3, 1003]],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment