Skip to content

Commit

Permalink
feat(query): add new fields to QueryContext and QueryObject (#583)
Browse files Browse the repository at this point in the history
* feat: add new fields to QueryContext and QueryObject

* linting

* remove change to package.json
  • Loading branch information
villebro authored and zhaoyongjie committed Nov 26, 2021
1 parent d277421 commit 9955156
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe('ChartPlugin', () => {
datasource: { id: 1, type: DatasourceType.Table },
queries: [{ granularity: 'day' }],
force: false,
result_format: 'json',
result_type: 'full',
});

const controlPanel = { abc: 1 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ export default function buildQueryContext(
datasource: new DatasourceKey(formData.datasource).toObject(),
force: formData.force || false,
queries: buildQuery(buildQueryObject(formData)),
result_format: formData.result_format || 'json',
result_type: formData.result_type || 'full',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ export default function buildQueryObject<T extends QueryFormData>(formData: T):
until,
order_desc,
row_limit,
row_offset,
limit,
timeseries_limit_metric,
queryFields,
...residualFormData
} = formData;

const numericRowLimit = Number(row_limit);
const numericRowOffset = Number(row_offset);
const { metrics, groupby, columns } = extractQueryFields(residualFormData, queryFields);
const groupbySet = new Set([...columns, ...groupby]);

Expand All @@ -46,6 +48,7 @@ export default function buildQueryObject<T extends QueryFormData>(formData: T):
order_desc: typeof order_desc === 'undefined' ? true : order_desc,
orderby: [],
row_limit: row_limit == null || Number.isNaN(numericRowLimit) ? undefined : numericRowLimit,
row_offset: row_offset == null || Number.isNaN(numericRowOffset) ? undefined : numericRowOffset,
since,
time_range,
timeseries_limit: limit ? Number(limit) : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export type QueryObject = {

/** Maximum numbers of rows to return */
row_limit?: number;
/** Number of rows to skip */
row_offset?: number;
/** Maximum number of series */
timeseries_limit?: number;
/** The metric used to sort the returned result. */
Expand All @@ -88,6 +90,10 @@ export interface QueryContext {
};
/** Force refresh of all queries */
force: boolean;
/** Type of result to return for queries */
result_type: string;
/** Response format */
result_format: string;
queries: QueryObject[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export type BaseFormData = {
timeseries_limit_metric?: QueryFormResidualDataValue;
/** Force refresh */
force?: boolean;
result_format?: string;
result_type?: string;
queryFields?: QueryFields;
} & TimeRange &
QueryFormResidualData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildQueryContext } from '../src';

describe('buildQueryContext', () => {
it('should build datasource for table sources and default force to false', () => {
it('should build datasource for table sources and apply defaults', () => {
const queryContext = buildQueryContext({
datasource: '5__table',
granularity_sqla: 'ds',
Expand All @@ -10,6 +10,9 @@ describe('buildQueryContext', () => {
expect(queryContext.datasource.id).toBe(5);
expect(queryContext.datasource.type).toBe('table');
expect(queryContext.force).toBe(false);
expect(queryContext.force).toBe(false);
expect(queryContext.result_format).toBe('json');
expect(queryContext.result_type).toBe('full');
});

it('should build datasource for druid sources and set force to true', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('buildQueryObject', () => {
expect(query.timeseries_limit_metric).toEqual({ label: metric });
});

it('should handle null and non-numeric row_limit', () => {
it('should handle null and non-numeric row_limit and row_offset', () => {
const baseQuery = {
datasource: '5__table',
granularity_sqla: 'ds',
Expand All @@ -99,20 +99,25 @@ describe('buildQueryObject', () => {
// undefined
query = buildQueryObject({ ...baseQuery });
expect(query.row_limit).toBeUndefined();
expect(query.row_offset).toBeUndefined();

// null value
query = buildQueryObject({ ...baseQuery, row_limit: null });
query = buildQueryObject({ ...baseQuery, row_limit: null, row_offset: null });
expect(query.row_limit).toBeUndefined();
expect(query.row_offset).toBeUndefined();

query = buildQueryObject({ ...baseQuery, row_limit: 1000 });
query = buildQueryObject({ ...baseQuery, row_limit: 1000, row_offset: 50 });
expect(query.row_limit).toStrictEqual(1000);
expect(query.row_offset).toStrictEqual(50);

// valid string
query = buildQueryObject({ ...baseQuery, row_limit: '200' });
query = buildQueryObject({ ...baseQuery, row_limit: '200', row_offset: '100' });
expect(query.row_limit).toStrictEqual(200);
expect(query.row_offset).toStrictEqual(100);

// invalid string
query = buildQueryObject({ ...baseQuery, row_limit: 'two hundred' });
query = buildQueryObject({ ...baseQuery, row_limit: 'two hundred', row_offset: 'twenty' });
expect(query.row_limit).toBeUndefined();
expect(query.row_offset).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('processGroupby', () => {
});

it('should exclude non-string values', () => {
// @ts-ignore, change to @ts-expect-error when updated to TypeScript>=3.9
// @ts-expect-error
expect(processGroupby(['bar', 1, undefined, null, 'foo'])).toEqual(['bar', 'foo']);
});
});

0 comments on commit 9955156

Please sign in to comment.