Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
chore: remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbranham committed Nov 11, 2021
1 parent 32d61df commit 32e3d41
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 186 deletions.
14 changes: 1 addition & 13 deletions plugins/plugin-chart-handlebars/src/Handlebars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ const Styles = styled.div<HandlebarsStylesProps>`
height: ${({ height }) => height};
width: ${({ width }) => width};
overflow-y: scroll;
h3 {
/* You can use your props to control CSS! */
font-size: ${({ theme, headerFontSize }) => theme.typography.sizes[headerFontSize]};
font-weight: ${({ theme, boldText }) => theme.typography.weights[boldText ? 'bold' : 'normal']};
}
`;

/**
Expand Down Expand Up @@ -74,13 +68,7 @@ export default function Handlebars(props: HandlebarsProps) {
console.log('Plugin props', props);

return (
<Styles
ref={rootElem}
boldText={props.boldText}
headerFontSize={props.headerFontSize}
height={height}
width={width}
>
<Styles ref={rootElem} height={height} width={width}>
<h3>{props.headerText}</h3>
<HandlebarsViewer data={{ data }} templateSource={templateSource} />
</Styles>
Expand Down
180 changes: 8 additions & 172 deletions plugins/plugin-chart-handlebars/src/plugin/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,179 +16,15 @@
* specific language governing permissions and limitations
* under the License.
*/
import {
buildQueryContext,
ensureIsArray,
getMetricLabel,
PostProcessingRule,
QueryFormData,
QueryMode,
QueryObject,
removeDuplicates,
} from '@superset-ui/core';
import { BuildQuery } from '@superset-ui/core/lib/chart/registries/ChartBuildQueryRegistrySingleton';
import { HandlebarsQueryFormData } from '../types';
import { buildQueryContext, QueryFormData } from '@superset-ui/core';

/**
* The buildQuery function is used to create an instance of QueryContext that's
* sent to the chart data endpoint. In addition to containing information of which
* datasource to use, it specifies the type (e.g. full payload, samples, query) and
* format (e.g. CSV or JSON) of the result and whether or not to force refresh the data from
* the datasource as opposed to using a cached copy of the data, if available.
*
* More importantly though, QueryContext contains a property `queries`, which is an array of
* QueryObjects specifying individual data requests to be made. A QueryObject specifies which
* columns, metrics and filters, among others, to use during the query. Usually it will be enough
* to specify just one query based on the baseQueryObject, but for some more advanced use cases
* it is possible to define post processing operations in the QueryObject, or multiple queries
* if a viz needs multiple different result sets.
*/

/**
* Infer query mode from form data. If `all_columns` is set, then raw records mode,
* otherwise defaults to aggregation mode.
*
* The same logic is used in `controlPanel` with control values as well.
*/
export function getQueryMode(formData: QueryFormData) {
const { query_mode: mode } = formData;
if (mode === QueryMode.aggregate || mode === QueryMode.raw) {
return mode;
}
const rawColumns = formData?.all_columns;
const hasRawColumns = rawColumns && rawColumns.length > 0;
return hasRawColumns ? QueryMode.raw : QueryMode.aggregate;
}

const buildQuery: BuildQuery<HandlebarsQueryFormData> = (
formData: HandlebarsQueryFormData,
options,
) => {
const { percent_metrics: percentMetrics, order_desc: orderDesc = false } = formData;
const queryMode = getQueryMode(formData);
const sortByMetric = ensureIsArray(formData.timeseries_limit_metric)[0];
let formDataCopy = formData;
// never include time in raw records mode
if (queryMode === QueryMode.raw) {
formDataCopy = {
...formData,
include_time: false,
};
}

return buildQueryContext(formDataCopy, baseQueryObject => {
let { metrics, orderby = [] } = baseQueryObject;
let postProcessing: PostProcessingRule[] = [];
export default function buildQuery(formData: QueryFormData) {
const { metric, sort_by_metric } = formData;

if (queryMode === QueryMode.aggregate) {
metrics = metrics || [];
// orverride orderby with timeseries metric when in aggregation mode
if (sortByMetric) {
orderby = [[sortByMetric, !orderDesc]];
} else if (metrics?.length > 0) {
// default to ordering by first metric in descending order
// when no "sort by" metric is set (regargless if "SORT DESC" is set to true)
orderby = [[metrics[0], false]];
}
// add postprocessing for percent metrics only when in aggregation mode
if (percentMetrics && percentMetrics.length > 0) {
const percentMetricLabels = removeDuplicates(percentMetrics.map(getMetricLabel));
metrics = removeDuplicates(metrics.concat(percentMetrics), getMetricLabel);
postProcessing = [
{
operation: 'contribution',
options: {
columns: percentMetricLabels,
rename_columns: percentMetricLabels.map(x => `%${x}`),
},
},
];
}
}

const moreProps: Partial<QueryObject> = {};
const ownState = options?.ownState ?? {};
if (formDataCopy.server_pagination) {
moreProps.row_limit = ownState.pageSize ?? formDataCopy.server_page_length;
moreProps.row_offset = (ownState.currentPage ?? 0) * (ownState.pageSize ?? 0);
}

let queryObject = {
return buildQueryContext(formData, baseQueryObject => [
{
...baseQueryObject,
orderby,
metrics,
post_processing: postProcessing,
...moreProps,
};

if (
formData.server_pagination &&
options?.extras?.cachedChanges?.[formData.slice_id] &&
JSON.stringify(options?.extras?.cachedChanges?.[formData.slice_id]) !==
JSON.stringify(queryObject.filters)
) {
queryObject = { ...queryObject, row_offset: 0 };
updateExternalFormData(options?.hooks?.setDataMask, 0, queryObject.row_limit ?? 0);
}
// Because we use same buildQuery for all table on the page we need split them by id
options?.hooks?.setCachedChanges({ [formData.slice_id]: queryObject.filters });

const extraQueries: QueryObject[] = [];
if (metrics?.length && formData.show_totals && queryMode === QueryMode.aggregate) {
extraQueries.push({
...queryObject,
columns: [],
row_limit: 0,
row_offset: 0,
post_processing: [],
});
}

const interactiveGroupBy = formData.extra_form_data?.interactive_groupby;
if (interactiveGroupBy && queryObject.columns) {
queryObject.columns = [...new Set([...queryObject.columns, ...interactiveGroupBy])];
}

if (formData.server_pagination) {
return [
{ ...queryObject },
{ ...queryObject, row_limit: 0, row_offset: 0, post_processing: [], is_rowcount: true },
...extraQueries,
];
}

return [queryObject, ...extraQueries];
});
};

// Use this closure to cache changing of external filters, if we have server pagination we need reset page to 0, after
// external filter changed
export const cachedBuildQuery = (): BuildQuery<HandlebarsQueryFormData> => {
let cachedChanges: any = {};
const setCachedChanges = (newChanges: any) => {
cachedChanges = { ...cachedChanges, ...newChanges };
};

return (formData, options) =>
buildQuery(
{ ...formData },
{
extras: { cachedChanges },
ownState: options?.ownState ?? {},
hooks: {
...options?.hooks,
setDataMask: () => {},
setCachedChanges,
},
},
);
};

export default cachedBuildQuery();
function updateExternalFormData(
setDataMask: import('@superset-ui/core').SetDataMaskHook | undefined,
arg1: number,
arg2: number,
) {
throw new Error('Function not implemented.');
...(sort_by_metric && { orderby: [[metric, false]] }),
},
]);
}
6 changes: 5 additions & 1 deletion plugins/plugin-chart-handlebars/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ import {
TimeseriesDataRecord,
} from '@superset-ui/core';

export interface HandlebarsStylesProps {}
export interface HandlebarsStylesProps {
height: number;
width: number;
}

interface HandlebarsCustomizeProps {
headerText: string;
handlebarsTemplate?: string;
styleTemplate?: string;
}
Expand Down

0 comments on commit 32e3d41

Please sign in to comment.