Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1197 pass filter values context to querys process result #1198

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/api",
"version": "10.34.8",
"version": "10.35.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/dashboard",
"version": "10.34.8",
"version": "10.35.0",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand Down
3 changes: 2 additions & 1 deletion dashboard/src/api-caller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DataSourceType, TPayloadForSQL } from '~/model';
import { formatSQL, postProcessSQLQuery, preProcessSQLQuery } from '../utils/sql';
import { APIClient } from './request';
import { IDataSource, PaginationResponse } from './types';
import { payloadToDashboardState } from '~/utils/dashboard-state';

export type QueryFailureError = {
code: 'BAD_REQUEST';
Expand All @@ -25,7 +26,7 @@ export async function queryBySQL({ query, name, payload }: IQueryBySQL, signal:
const formattedSQL = formatSQL(sql, payload);
const finalSQL = preProcessSQLQuery({ sql: formattedSQL, pre_process });
let data = await APIClient.query(signal)({ type, key, query: finalSQL }, { params: { name } });
data = postProcessSQLQuery(post_process, data);
data = postProcessSQLQuery(post_process, data, payloadToDashboardState(payload));
return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export const DEFAULT_HTTP_REQ_PROCESSING = {
" return { method: 'POST', url: '/', params: {}, headers: {}, data: {} }",
'}',
].join('\n'),
post: ['function process_result(res, utils) {', ' // your code goes here', ' return data', '}'].join('\n'),
post: ['function process_result(res, utils, state) {', ' // your code goes here', ' return data', '}'].join(
'\n',
),
};

export const TabPanel_HTTP = observer(({ queryModel }: { queryModel: QueryRenderModelInstance }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const DEFAULT_SQL_REQ_PROCESSING = {
'}',
].join('\n'),
post: [
'function process_result(data, utils) {',
'function process_result(data, utils, state) {',
' // process data and return the result',
' return data',
'}',
Expand Down
4 changes: 4 additions & 0 deletions dashboard/src/model/meta-model/dashboard/content/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ export type TPayloadForSQL = {
global_sql_snippets: AnyObject;
filters: AnyObject;
};
export type TDashboardState = {
filters: AnyObject;
context: AnyObject;
};
export type TPayloadForSQLSnippet = Omit<TPayloadForSQL, 'sql_snippets' | 'global_sql_snippets'>;
export type TPayloadForViz = Omit<TPayloadForSQL, 'sql_snippets' | 'global_sql_snippets'>;
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
postProcessWithQuery,
preProcessWithDataSource,
} from '~/utils/http-query';
import { payloadToDashboardState } from '~/utils/dashboard-state';

export const QueryRenderModel = types
.compose(
Expand All @@ -30,9 +31,14 @@ export const QueryRenderModel = types
get contentModel(): any {
return this.rootModel.content; // dashboard content model
},
get payload() {
return this.contentModel.payloadForSQL;
},
get dashboardState() {
return payloadToDashboardState(this.payload);
},
get formattedSQL() {
const payload = this.contentModel.payloadForSQL;
return explainSQL(self.sql, payload);
return explainSQL(self.sql, this.payload);
},
get typedAsSQL() {
return [DataSourceType.Postgresql, DataSourceType.MySQL].includes(self.type);
Expand All @@ -45,7 +51,7 @@ export const QueryRenderModel = types
return this.rootModel.datasources.find({ type, key });
},
get httpConfigString() {
const { context, filters } = this.contentModel.payloadForSQL;
const { context, filters } = this.payload;
const { name, pre_process } = self.json;

const config = explainHTTPRequest(pre_process, context, filters);
Expand Down Expand Up @@ -102,7 +108,7 @@ export const QueryRenderModel = types
self.controller = new AbortController();
self.state = 'loading';
try {
const payload = self.contentModel.payloadForSQL;
const payload = self.payload;
self.data = yield* toGenerator(
queryBySQL(
{
Expand Down Expand Up @@ -152,7 +158,7 @@ export const QueryRenderModel = types
),
);
let data = postProcessWithDataSource(self.datasource, res);
data = postProcessWithQuery(post_process, data);
data = postProcessWithQuery(post_process, data, self.dashboardState);

self.data = data;
self.state = 'idle';
Expand Down
17 changes: 17 additions & 0 deletions dashboard/src/utils/dashboard-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { TDashboardState, TPayloadForSQL } from '~/model';

function getEmptyState(): TDashboardState {
return {
filters: {},
context: {},
};
}

export function payloadToDashboardState(payload: TPayloadForSQL) {
const empty = getEmptyState();
if (!payload) {
return getEmptyState();
}
const { filters = empty.filters, context = empty.context } = payload;
return { filters, context };
}
6 changes: 3 additions & 3 deletions dashboard/src/utils/http-query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosRequestConfig } from 'axios';
import { IDataSource } from '~/api-caller/types';
import { ContextRecordType, FilterValuesType } from '~/model';
import { ContextRecordType, FilterValuesType, TDashboardState } from '~/model';
import { functionUtils } from './function-utils';

export function buildHTTPRequest(
Expand All @@ -25,9 +25,9 @@ export function preProcessWithDataSource(datasource: IDataSource, config: AxiosR
export function postProcessWithDataSource(datasource: IDataSource, res: any) {
return new Function(`return ${datasource.config.processing.post}`)()(res, functionUtils);
}
export function postProcessWithQuery(post_process: TFunctionString, res: any) {
export function postProcessWithQuery(post_process: TFunctionString, res: any, state: TDashboardState) {
if (!post_process) {
return res;
}
return new Function(`return ${post_process}`)()(res, functionUtils);
return new Function(`return ${post_process}`)()(res, functionUtils, state);
}
6 changes: 3 additions & 3 deletions dashboard/src/utils/sql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TPayloadForSQL, TPayloadForSQLSnippet } from '~/model';
import { TDashboardState, TPayloadForSQL, TPayloadForSQLSnippet } from '~/model';
import { functionUtils } from './function-utils';

export function formatSQL(sql: string, payload: TPayloadForSQL | TPayloadForSQLSnippet) {
Expand Down Expand Up @@ -35,12 +35,12 @@ export function preProcessSQLQuery({ sql, pre_process }: { sql: string; pre_proc
}
}

export function postProcessSQLQuery(post_process: TFunctionString, data: any) {
export function postProcessSQLQuery(post_process: TFunctionString, data: any, state: TDashboardState) {
if (!post_process.trim()) {
return data;
}
try {
return new Function(`return ${post_process}`)()(data, functionUtils);
return new Function(`return ${post_process}`)()(data, functionUtils, state);
} catch (error) {
console.error(error);
return data;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/root",
"version": "10.34.8",
"version": "10.35.0",
"private": true,
"workspaces": [
"api",
Expand Down
2 changes: 1 addition & 1 deletion settings-form/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/settings-form",
"version": "10.34.8",
"version": "10.35.0",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@devtable/website",
"private": true,
"license": "Apache-2.0",
"version": "10.34.8",
"version": "10.35.0",
"scripts": {
"dev": "vite",
"preview": "vite preview"
Expand Down