mirror of
https://github.com/grafana/grafana.git
synced 2025-01-26 16:27:02 -06:00
InputDataSource: better empty value support (#17075)
This commit is contained in:
parent
238a929262
commit
d8280b895d
@ -1,4 +1,4 @@
|
||||
import InputDatasource from './InputDatasource';
|
||||
import InputDatasource, { describeSeriesData } from './InputDatasource';
|
||||
import { InputQuery, InputOptions } from './types';
|
||||
import { readCSV, DataSourceInstanceSettings, PluginMeta } from '@grafana/ui';
|
||||
import { getQueryOptions } from 'test/helpers/getQueryOptions';
|
||||
@ -31,4 +31,18 @@ describe('InputDatasource', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('SeriesData descriptions', () => {
|
||||
expect(describeSeriesData([])).toEqual('');
|
||||
expect(describeSeriesData(null)).toEqual('');
|
||||
expect(
|
||||
describeSeriesData([
|
||||
{
|
||||
name: 'x',
|
||||
fields: [{ name: 'a' }],
|
||||
rows: [],
|
||||
},
|
||||
])
|
||||
).toEqual('1 Fields, 0 Rows');
|
||||
});
|
||||
});
|
||||
|
@ -17,28 +17,14 @@ export class InputDatasource extends DataSourceApi<InputQuery, InputOptions> {
|
||||
this.data = instanceSettings.jsonData.data ? instanceSettings.jsonData.data : [];
|
||||
}
|
||||
|
||||
getDescription(data: SeriesData[]): string {
|
||||
if (!data) {
|
||||
return '';
|
||||
}
|
||||
if (data.length > 1) {
|
||||
const count = data.reduce((acc, series) => {
|
||||
return acc + series.rows.length;
|
||||
}, 0);
|
||||
return `${data.length} Series, ${count} Rows`;
|
||||
}
|
||||
const series = data[0];
|
||||
return `${series.fields.length} Fields, ${series.rows.length} Rows`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a query to a simple text string
|
||||
*/
|
||||
getQueryDisplayText(query: InputQuery): string {
|
||||
if (query.data) {
|
||||
return 'Panel Data: ' + this.getDescription(query.data);
|
||||
return 'Panel Data: ' + describeSeriesData(query.data);
|
||||
}
|
||||
return `Shared Data From: ${this.name} (${this.getDescription(this.data)})`;
|
||||
return `Shared Data From: ${this.name} (${describeSeriesData(this.data)})`;
|
||||
}
|
||||
|
||||
metricFindQuery(query: string, options?: any) {
|
||||
@ -96,4 +82,21 @@ export class InputDatasource extends DataSourceApi<InputQuery, InputOptions> {
|
||||
}
|
||||
}
|
||||
|
||||
export function describeSeriesData(data: SeriesData[]): string {
|
||||
if (!data || !data.length) {
|
||||
return '';
|
||||
}
|
||||
if (data.length > 1) {
|
||||
const count = data.reduce((acc, series) => {
|
||||
return acc + series.rows.length;
|
||||
}, 0);
|
||||
return `${data.length} Series, ${count} Rows`;
|
||||
}
|
||||
const series = data[0];
|
||||
if (!series.fields) {
|
||||
return 'Missing Fields';
|
||||
}
|
||||
return `${series.fields.length} Fields, ${series.rows.length} Rows`;
|
||||
}
|
||||
|
||||
export default InputDatasource;
|
||||
|
@ -2,7 +2,7 @@
|
||||
import React, { PureComponent } from 'react';
|
||||
|
||||
// Types
|
||||
import { InputDatasource } from './InputDatasource';
|
||||
import { InputDatasource, describeSeriesData } from './InputDatasource';
|
||||
import { InputQuery, InputOptions } from './types';
|
||||
|
||||
import { FormLabel, Select, QueryEditorProps, SelectOptionItem, SeriesData, TableInputCSV, toCSV } from '@grafana/ui';
|
||||
@ -80,10 +80,10 @@ export class InputQueryEditor extends PureComponent<Props, State> {
|
||||
|
||||
<div className="btn btn-link">
|
||||
{query.data ? (
|
||||
datasource.getDescription(query.data)
|
||||
describeSeriesData(query.data)
|
||||
) : (
|
||||
<a href={`datasources/edit/${id}/`}>
|
||||
{name}: {datasource.getDescription(datasource.data)}
|
||||
{name}: {describeSeriesData(datasource.data)}
|
||||
<i className="fa fa-pencil-square-o" />
|
||||
</a>
|
||||
)}
|
||||
|
Loading…
Reference in New Issue
Block a user