Skip to content

Commit

Permalink
fix(data-frame): Boolean and "Value" columns (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronwaterman authored Oct 24, 2023
1 parent 76c66b6 commit 941dce3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
23 changes: 12 additions & 11 deletions src/datasources/data-frame/DataFrameDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ it('should convert columns to Grafana fields', async () => {
{
refId: 'A',
tableId: '_',
columns: ['int', 'float', 'string', 'time', 'bool'],
columns: ['int', 'float', 'string', 'time', 'bool', 'Value'],
},
]);

const response = await ds.query(query);

const fields = response.data[0].fields as Field[];
const actual = fields.map(({ name, type, values }) => ({ name, type, values: values.toArray() }));
const actual = fields.map(({ name, type, values, config }) => ({ name, type, values: values.toArray(), config }));
expect(actual).toEqual([
{ name: 'int', type: FieldType.number, values: [1, 2] },
{ name: 'float', type: FieldType.number, values: [1.1, 2.2] },
{ name: 'string', type: FieldType.string, values: ['first', 'second'] },
{ name: 'time', type: FieldType.time, values: [1663135260000, 1663135320000] },
// TODO: Boolean columns are not being converted correctly (should be true, false)
{ name: 'bool', type: FieldType.boolean, values: [true, true] },
{ name: 'int', type: FieldType.number, values: [1, 2], config: {} },
{ name: 'float', type: FieldType.number, values: [1.1, 2.2], config: {} },
{ name: 'string', type: FieldType.string, values: ['first', 'second'], config: {} },
{ name: 'time', type: FieldType.time, values: [1663135260000, 1663135320000], config: {} },
{ name: 'bool', type: FieldType.boolean, values: [true, false], config: {} },
{ name: 'Value', type: FieldType.string, values: ['test1', 'test2'], config: { displayName: 'Value' } },
]);
});

Expand Down Expand Up @@ -252,6 +252,7 @@ const fakeMetadataResponse: TableMetadata = {
{ name: 'float', dataType: 'FLOAT32', columnType: 'NULLABLE', properties: {} },
{ name: 'string', dataType: 'STRING', columnType: 'NULLABLE', properties: {} },
{ name: 'bool', dataType: 'BOOL', columnType: 'NORMAL', properties: {} },
{ name: 'Value', dataType: 'STRING', columnType: 'NULLABLE', properties: {} },
],
id: '_',
name: 'Test Table',
Expand All @@ -260,10 +261,10 @@ const fakeMetadataResponse: TableMetadata = {

const fakeDataResponse: TableDataRows = {
frame: {
columns: ['int', 'float', 'string', 'time', 'bool'],
columns: ['int', 'float', 'string', 'time', 'bool', 'Value'],
data: [
['1', '1.1', 'first', '2022-09-14T06:01:00.0000000Z', 'True'],
['2', '2.2', 'second', '2022-09-14T06:02:00.0000000Z', 'False'],
['1', '1.1', 'first', '2022-09-14T06:01:00.0000000Z', 'True', 'test1'],
['2', '2.2', 'second', '2022-09-14T06:02:00.0000000Z', 'False', 'test2'],
],
},
continuationToken: '_',
Expand Down
28 changes: 24 additions & 4 deletions src/datasources/data-frame/DataFrameDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ export class DataFrameDataSource extends DataSourceBase<DataFrameQuery> {
const tableMetadata = await this.getTableMetadata(processedQuery.tableId);
const columns = this.getColumnTypes(processedQuery.columns, tableMetadata?.columns ?? []);
const tableData = await this.getDecimatedTableData(processedQuery, columns, range, maxDataPoints);
const frame = toDataFrame({
return this.convertDataFrameFields({
refId: processedQuery.refId,
name: processedQuery.tableId,
columns: processedQuery.columns.map((name) => ({ text: name })),
rows: tableData.frame.data,
} as TableData);
return this.convertDataFrameFields(frame, columns);
} as TableData, columns);
}

shouldRunQuery(query: ValidDataFrameQuery): boolean {
Expand Down Expand Up @@ -152,7 +151,14 @@ export class DataFrameDataSource extends DataSourceBase<DataFrameQuery> {
}
}

private convertDataFrameFields(frame: DataFrame, columns: Column[]): DataFrame {
private convertDataFrameFields(tableData: TableData, columns: Column[]): DataFrame {
this.transformBooleanFields(tableData, columns);
const frame = toDataFrame(tableData);
frame.fields.forEach(field => {
if (field.name.toLowerCase() === 'value') {
field.config.displayName = field.name;
}
})
const transformer = standardTransformers.convertFieldTypeTransformer.transformer;
const conversions = columns.map(({ name, dataType }) => ({
targetField: name,
Expand All @@ -162,6 +168,20 @@ export class DataFrameDataSource extends DataSourceBase<DataFrameQuery> {
return transformer({ conversions }, { interpolate: _.identity })([frame])[0];
}

private transformBooleanFields(tableData: TableData, columns: Column[]): void {
const boolColumnIndices: number[] = [];
columns.forEach((column, i) => {
if (column.dataType === 'BOOL') {
boolColumnIndices.push(i);
}
});
if (!!boolColumnIndices.length) {
tableData.rows.forEach(row => {
boolColumnIndices.forEach(i => row[i] = row[i].toLowerCase() === 'true');
})
}
}

private constructTimeFilters(columns: Column[], timeRange: TimeRange): ColumnFilter[] {
const timeIndex = columns.find((c) => c.dataType === 'TIMESTAMP' && c.columnType === 'INDEX');

Expand Down

0 comments on commit 941dce3

Please sign in to comment.