Skip to content

Commit

Permalink
feat(query): Add force to QueryContext (#445)
Browse files Browse the repository at this point in the history
* Add force to QueryContext

* fix tests

* fix test
  • Loading branch information
villebro authored and zhaoyongjie committed Nov 26, 2021
1 parent 5ff9f22 commit 4322d1c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('ChartPlugin', () => {
const buildQuery = () => ({
datasource: { id: 1, type: DatasourceType.Table },
queries: [{ granularity: 'day' }],
force: false,
});

const controlPanel = { abc: 1 };
Expand Down Expand Up @@ -55,7 +56,7 @@ describe('ChartPlugin', () => {
expect(plugin.loadBuildQuery).toBeUndefined();
});
it('uses loadBuildQuery field if specified', () => {
expect.assertions(1);
expect.assertions(2);
const plugin = new ChartPlugin({
metadata,
Chart: FakeChart,
Expand All @@ -64,6 +65,7 @@ describe('ChartPlugin', () => {

const fn = plugin.loadBuildQuery!() as BuildQueryFunction<QueryFormData>;
expect(fn(FORM_DATA).queries[0]).toEqual({ granularity: 'day' });
expect(fn(FORM_DATA).force).toEqual(false);
});
it('uses buildQuery field if specified', () => {
expect.assertions(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function buildQueryContext(
): QueryContext {
return {
datasource: new DatasourceKey(formData.datasource).toObject(),
force: formData.force || false,
queries: buildQuery(buildQueryObject(formData)),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ export type QueryObjectMetric = {

export type QueryObjectExtras = Partial<{
/** HAVING condition for Druid */
having_druid: string;
druid_time_origin: string;
having_druid?: string;
druid_time_origin?: string;
/** HAVING condition for SQLAlchemy */
having: string;
time_grain_sqla: string;
having?: string;
relative_start?: string;
relative_end?: string;
time_grain_sqla?: string;
time_range_endpoints?: string[];
/** WHERE condition */
where: string;
where?: string;
}>;

export type QueryObject = {
Expand Down Expand Up @@ -77,5 +80,7 @@ export interface QueryContext {
id: number;
type: DatasourceType;
};
/** Force refresh of all queries */
force: boolean;
queries: QueryObject[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export type BaseFormData = {
row_limit?: string | number | null;
/** The metric used to order timeseries for limiting */
timeseries_limit_metric?: QueryFormDataMetric;
/** Force refresh */
force?: boolean;
} & TimeRange &
QueryFormDataMetrics;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { buildQueryContext } from '../src';

describe('buildQueryContext', () => {
it('should build datasource for table sources', () => {
it('should build datasource for table sources and default force to false', () => {
const queryContext = buildQueryContext({
datasource: '5__table',
granularity_sqla: 'ds',
viz_type: 'table',
});
expect(queryContext.datasource.id).toBe(5);
expect(queryContext.datasource.type).toBe('table');
expect(queryContext.force).toBe(false);
});

it('should build datasource for druid sources', () => {
it('should build datasource for druid sources and set force to true', () => {
const queryContext = buildQueryContext({
datasource: '5__druid',
granularity: 'ds',
viz_type: 'table',
force: true,
});
expect(queryContext.datasource.id).toBe(5);
expect(queryContext.datasource.type).toBe('druid');
expect(queryContext.force).toBe(true);
});
});

0 comments on commit 4322d1c

Please sign in to comment.