Skip to content

Commit

Permalink
Remove unnecessary export types and reorganize types (apache#99)
Browse files Browse the repository at this point in the history
* fix re-export types in translation

* Fix more types

* reorganize type in chart package

* Fix unit tests

* remove Array<> in favor of []
  • Loading branch information
kristw authored Feb 15, 2019
1 parent 61240e4 commit d1f3b57
Show file tree
Hide file tree
Showing 36 changed files with 234 additions and 209 deletions.
17 changes: 9 additions & 8 deletions packages/superset-ui-chart/src/clients/ChartClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
SupersetClientClass,
} from '@superset-ui/connection';
import getChartBuildQueryRegistry from '../registries/ChartBuildQueryRegistrySingleton';
import { FormData, AnnotationLayerMetadata } from '../query/FormData';
import { AnnotationLayerMetadata } from '../types/Annotation';
import { FormData } from '../types/FormData';

export type SliceIdAndOrFormData =
| {
Expand All @@ -29,14 +30,14 @@ interface ChartData {
queryData: object;
}

export interface ChartClientConfig {
client?: SupersetClientInterface | SupersetClientClass;
}

export class ChartClient {
export default class ChartClient {
readonly client: SupersetClientInterface | SupersetClientClass;

constructor(config: ChartClientConfig = {}) {
constructor(
config: {
client?: SupersetClientInterface | SupersetClientClass;
} = {},
) {
const { client = SupersetClient } = config;
this.client = client;
}
Expand Down Expand Up @@ -102,7 +103,7 @@ export class ChartClient {
return Promise.reject(new Error('This feature is not implemented yet.'));
}

loadAnnotations(annotationLayers?: Array<AnnotationLayerMetadata>): Promise<AnnotationData> {
loadAnnotations(annotationLayers?: AnnotationLayerMetadata[]): Promise<AnnotationData> {
if (Array.isArray(annotationLayers) && annotationLayers.length > 0) {
return Promise.all(annotationLayers.map(layer => this.loadAnnotation(layer))).then(results =>
annotationLayers.reduce((prev, layer, i) => {
Expand Down
23 changes: 11 additions & 12 deletions packages/superset-ui-chart/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
export { ChartClient, ChartClientConfig } from './clients/ChartClient';
export { default as ChartMetadata, ChartMetadataConfig } from './models/ChartMetadata';
export {
default as ChartPlugin,
ChartPluginConfig,
BuildQueryFunction,
TransformPropsFunction,
} from './models/ChartPlugin';
export { default as ChartProps, ChartPropsConfig } from './models/ChartProps';
export { default as ChartClient } from './clients/ChartClient';
export { default as ChartMetadata } from './models/ChartMetadata';
export { default as ChartPlugin } from './models/ChartPlugin';
export { default as ChartProps } from './models/ChartProps';

export { default as createLoadableRenderer } from './components/createLoadableRenderer';
export { default as reactify } from './components/reactify';
Expand All @@ -21,6 +16,10 @@ export {
default as getChartTransformPropsRegistry,
} from './registries/ChartTransformPropsRegistrySingleton';

export { QueryContext, buildQueryContext } from './query/buildQueryContext';
export { DatasourceType, DatasourceKey } from './query/DatasourceKey';
export { FormData } from './query/FormData';
export { default as buildQueryContext } from './query/buildQueryContext';
export { default as DatasourceKey } from './query/DatasourceKey';

export * from './types/Annotation';
export * from './types/Datasource';
export * from './types/FormData';
export * from './types/Query';
26 changes: 12 additions & 14 deletions packages/superset-ui-chart/src/models/ChartMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ interface LookupTable {
[key: string]: boolean;
}

export interface ChartMetadataConfig {
name: string;
canBeAnnotationTypes?: Array<string>;
credits?: Array<string>;
description?: string;
show?: boolean;
supportedAnnotationTypes?: Array<string>;
thumbnail: string;
useLegacyApi?: boolean;
}

export default class ChartMetadata {
name: string;
canBeAnnotationTypesLookup: LookupTable;
credits: Array<string>;
credits: string[];
description: string;
show: boolean;
supportedAnnotationTypes: Array<string>;
supportedAnnotationTypes: string[];
thumbnail: string;
useLegacyApi: boolean;

constructor(config: ChartMetadataConfig) {
constructor(config: {
name: string;
canBeAnnotationTypes?: string[];
credits?: string[];
description?: string;
show?: boolean;
supportedAnnotationTypes?: string[];
thumbnail: string;
useLegacyApi?: boolean;
}) {
const {
name,
canBeAnnotationTypes = [],
Expand Down
6 changes: 3 additions & 3 deletions packages/superset-ui-chart/src/models/ChartPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isRequired, Plugin } from '@superset-ui/core';
import ChartMetadata from './ChartMetadata';
import ChartProps from './ChartProps';
import { FormData } from '../query/FormData';
import { QueryContext } from '../query/buildQueryContext';
import getChartMetadataRegistry from '../registries/ChartMetadataRegistrySingleton';
import getChartBuildQueryRegistry from '../registries/ChartBuildQueryRegistrySingleton';
import getChartComponentRegistry from '../registries/ChartComponentRegistrySingleton';
import getChartTransformPropsRegistry from '../registries/ChartTransformPropsRegistrySingleton';
import { FormData } from '../types/FormData';
import { QueryContext } from '../types/Query';

const IDENTITY = (x: any) => x;

Expand All @@ -21,7 +21,7 @@ export type TransformPropsFunction = (
[key: string]: any;
};

export interface ChartPluginConfig<T extends FormData> {
interface ChartPluginConfig<T extends FormData> {
metadata: ChartMetadata;
// use buildQuery for immediate value
buildQuery?: BuildQueryFunction<T>;
Expand Down
4 changes: 2 additions & 2 deletions packages/superset-ui-chart/src/models/ChartProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type SnakeCaseDatasource = PlainObject;
type CamelCaseFormData = PlainObject;
type SnakeCaseFormData = PlainObject;
type QueryData = PlainObject;
type Filters = Array<any>;
type Filters = any[];
type HandlerFunction = (...args: any[]) => void;
type ChartPropsSelector = (c: ChartPropsConfig) => ChartProps;

export interface ChartPropsConfig {
interface ChartPropsConfig {
annotationData?: AnnotationData;
datasource?: SnakeCaseDatasource;
filters?: Filters;
Expand Down
7 changes: 2 additions & 5 deletions packages/superset-ui-chart/src/query/DatasourceKey.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export enum DatasourceType {
Table = 'table',
Druid = 'druid',
}
import { DatasourceType } from '../types/Datasource';

export class DatasourceKey {
export default class DatasourceKey {
readonly id: number;
readonly type: DatasourceType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,9 @@
import { Column } from './Column';
import { FormData } from './FormData';
import { FormData } from '../types/FormData';
import { MetricKey, Metric, FormDataMetric, AdhocMetric, ExpressionType } from '../types/Metric';

export const LABEL_MAX_LENGTH = 43;

// Note that the values of MetricKeys are lower_snake_case because they're
// used as keys of form data jsons.
export enum MetricKey {
METRIC = 'metric',
METRICS = 'metrics',
PERCENT_METRICS = 'percent_metrics',
RIGHT_AXIS_METRIC = 'metric_2',
SECONDARY_METRIC = 'secondary_metric',
X = 'x',
Y = 'y',
SIZE = 'size',
}

export enum Aggregate {
AVG = 'AVG',
COUNT = 'COUNT ',
COUNT_DISTINCT = 'COUNT_DISTINCT',
MAX = 'MAX',
MIN = 'MIN',
SUM = 'SUM',
}

export enum ExpressionType {
SIMPLE = 'SIMPLE',
SQL = 'SQL',
}

interface AdhocMetricSimple {
expressionType: ExpressionType.SIMPLE;
column: Column;
aggregate: Aggregate;
}

interface AdhocMetricSQL {
expressionType: ExpressionType.SQL;
sqlExpression: string;
}

export type AdhocMetric = {
label?: string;
optionName?: string;
} & (AdhocMetricSimple | AdhocMetricSQL);

// Type of metrics in form data
export type FormDataMetric = string | AdhocMetric;

// Type of Metric the client provides to server after unifying various forms
// of metrics in form data
export type Metric = {
label: string;
} & Partial<AdhocMetric>;

export class Metrics {
export default class Metrics {
// Use Array to maintain insertion order for metrics that are order sensitive
private metrics: Metric[];

Expand Down
17 changes: 5 additions & 12 deletions packages/superset-ui-chart/src/query/buildQueryContext.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { buildQueryObject, QueryObject } from './buildQueryObject';
import { DatasourceKey, DatasourceType } from './DatasourceKey';
import { FormData } from './FormData';
import buildQueryObject from './buildQueryObject';
import DatasourceKey from './DatasourceKey';
import { FormData } from '../types/FormData';
import { QueryContext, QueryObject } from '../types/Query';

const WRAP_IN_ARRAY = (baseQueryObject: QueryObject) => [baseQueryObject];

export interface QueryContext {
datasource: {
id: number;
type: DatasourceType;
};
queries: Array<QueryObject>;
}

export function buildQueryContext(
export default function buildQueryContext(
formData: FormData,
buildQuery: (baseQueryObject: QueryObject) => QueryObject[] = WRAP_IN_ARRAY,
): QueryContext {
Expand Down
14 changes: 6 additions & 8 deletions packages/superset-ui-chart/src/query/buildQueryObject.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { FormData, getGranularity } from './FormData';
import { Metric, Metrics } from './Metric';
import Metrics from './Metrics';
import { QueryObject } from '../types/Query';
import { FormData } from '../types/FormData';

// TODO: fill out the rest of the query object
export interface QueryObject {
granularity: string;
groupby?: string[];
metrics?: Metric[];
function getGranularity(formData: FormData): string {
return 'granularity_sqla' in formData ? formData.granularity_sqla : formData.granularity;
}

// Build the common segments of all query objects (e.g. the granularity field derived from
// either sql alchemy or druid). The segments specific to each viz type is constructed in the
// buildQuery method for each viz type (see `wordcloud/buildQuery.ts` for an example).
// Note the type of the formData argument passed in here is the type of the formData for a
// specific viz, which is a subtype of the generic formData shared among all viz types.
export function buildQueryObject<T extends FormData>(formData: T): QueryObject {
export default function buildQueryObject<T extends FormData>(formData: T): QueryObject {
return {
granularity: getGranularity(formData),
metrics: new Metrics(formData).getMetrics(),
Expand Down
6 changes: 6 additions & 0 deletions packages/superset-ui-chart/src/types/Annotation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable import/prefer-default-export */

export type AnnotationLayerMetadata = {
name: string;
sourceType?: string;
};
6 changes: 6 additions & 0 deletions packages/superset-ui-chart/src/types/Datasource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable import/prefer-default-export */

export enum DatasourceType {
Table = 'table',
Druid = 'druid',
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint camelcase: 0 */
/* eslint-disable import/prefer-default-export */
// FormData uses snake_cased keys.
import { FormDataMetric, MetricKey } from './Metric';
import { AnnotationLayerMetadata } from './Annotation';

// Type signature and utility functions for formData shared by all viz types
// It will be gradually filled out as we build out the query object
Expand All @@ -11,15 +13,10 @@ import { FormDataMetric, MetricKey } from './Metric';
// unified into a proper Metric type during buildQuery (see `/query/Metrics.ts`).
type Metrics = Partial<Record<MetricKey, FormDataMetric | FormDataMetric[]>>;

export type AnnotationLayerMetadata = {
name: string;
sourceType?: string;
};

type BaseFormData = {
datasource: string;
viz_type: string;
annotation_layers?: Array<AnnotationLayerMetadata>;
annotation_layers?: AnnotationLayerMetadata[];
} & Metrics;

// FormData is either sqla-based or druid-based
Expand All @@ -32,7 +29,3 @@ type DruidFormData = {
} & BaseFormData;

export type FormData = SqlaFormData | DruidFormData;

export function getGranularity(formData: FormData): string {
return 'granularity_sqla' in formData ? formData.granularity_sqla : formData.granularity;
}
53 changes: 53 additions & 0 deletions packages/superset-ui-chart/src/types/Metric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Column } from './Column';

// Note that the values of MetricKeys are lower_snake_case because they're
// used as keys of form data jsons.
export enum MetricKey {
METRIC = 'metric',
METRICS = 'metrics',
PERCENT_METRICS = 'percent_metrics',
RIGHT_AXIS_METRIC = 'metric_2',
SECONDARY_METRIC = 'secondary_metric',
X = 'x',
Y = 'y',
SIZE = 'size',
}

export enum Aggregate {
AVG = 'AVG',
COUNT = 'COUNT ',
COUNT_DISTINCT = 'COUNT_DISTINCT',
MAX = 'MAX',
MIN = 'MIN',
SUM = 'SUM',
}

export enum ExpressionType {
SIMPLE = 'SIMPLE',
SQL = 'SQL',
}

interface AdhocMetricSimple {
expressionType: ExpressionType.SIMPLE;
column: Column;
aggregate: Aggregate;
}

interface AdhocMetricSQL {
expressionType: ExpressionType.SQL;
sqlExpression: string;
}

export type AdhocMetric = {
label?: string;
optionName?: string;
} & (AdhocMetricSimple | AdhocMetricSQL);

// Type of metrics in form data
export type FormDataMetric = string | AdhocMetric;

// Type of Metric the client provides to server after unifying various forms
// of metrics in form data
export type Metric = {
label: string;
} & Partial<AdhocMetric>;
Loading

0 comments on commit d1f3b57

Please sign in to comment.