This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update ChartFormData and QueryObject to support filters. (#164)
* feat: update types to include filters * refactor: remove namespace * refactor: split function * feat: add filter processing and refactor * feat: revise metrics processing * test: add unit tests * refactor: move type files back * test: add unit tests * fix: unit test * fix: remove exports * docs: add field info * fix: type check undefined * docs: add more comments * build: speed up storybook build
- Loading branch information
Showing
22 changed files
with
803 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { SimpleAdhocFilter, isBinaryAdhocFilter, isUnaryAdhocFilter } from '../types/Filter'; | ||
import { QueryObjectFilterClause } from '../types/Query'; | ||
|
||
export default function convertFilter(filter: SimpleAdhocFilter): QueryObjectFilterClause { | ||
const { subject } = filter; | ||
if (isUnaryAdhocFilter(filter)) { | ||
const { operator } = filter; | ||
|
||
return { | ||
col: subject, | ||
op: operator, | ||
}; | ||
} else if (isBinaryAdhocFilter(filter)) { | ||
const { operator } = filter; | ||
|
||
return { | ||
col: subject, | ||
op: operator, | ||
val: filter.comparator, | ||
}; | ||
} | ||
|
||
const { operator } = filter; | ||
|
||
return { | ||
col: subject, | ||
op: operator, | ||
val: filter.comparator, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ChartFormDataMetric } from '../types/ChartFormData'; | ||
import { QueryObjectMetric } from '../types/Query'; | ||
import { AdhocMetric } from '../types/Metric'; | ||
|
||
export const LABEL_MAX_LENGTH = 43; | ||
|
||
function getDefaultLabel(metric: AdhocMetric) { | ||
let label: string; | ||
if (metric.expressionType === 'SIMPLE') { | ||
label = `${metric.aggregate}(${metric.column.columnName})`; | ||
} else { | ||
label = metric.sqlExpression; | ||
} | ||
|
||
return label.length <= LABEL_MAX_LENGTH | ||
? label | ||
: `${label.substring(0, LABEL_MAX_LENGTH - 3)}...`; | ||
} | ||
|
||
export default function convertMetric(metric: ChartFormDataMetric): QueryObjectMetric { | ||
let formattedMetric; | ||
if (typeof metric === 'string') { | ||
formattedMetric = { | ||
label: metric, | ||
}; | ||
} else { | ||
// Note we further sanitize the metric label for BigQuery datasources | ||
// TODO: move this logic to the client once client has more info on the | ||
// the datasource | ||
const label = metric.label || getDefaultLabel(metric); | ||
formattedMetric = { | ||
...metric, | ||
label, | ||
}; | ||
} | ||
|
||
return formattedMetric; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable camelcase */ | ||
import { ChartFormData, isDruidFormData } from '../types/ChartFormData'; | ||
import { QueryObjectExtras } from '../types/Query'; | ||
|
||
export default function processExtras(formData: ChartFormData): QueryObjectExtras { | ||
const { where = '' } = formData; | ||
|
||
if (isDruidFormData(formData)) { | ||
const { druid_time_origin, having_druid } = formData; | ||
|
||
return { druid_time_origin, having_druid, where }; | ||
} | ||
|
||
const { time_grain_sqla, having } = formData; | ||
|
||
return { having, time_grain_sqla, where }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { ChartFormData } from '../types/ChartFormData'; | ||
import { QueryObjectFilterClause } from '../types/Query'; | ||
import { isSimpleAdhocFilter } from '../types/Filter'; | ||
import convertFilter from './convertFilter'; | ||
|
||
/** Logic formerly in viz.py's process_query_filters */ | ||
export default function processFilters(formData: ChartFormData) { | ||
// TODO: Implement | ||
// utils.convert_legacy_filters_into_adhoc(self.form_data) | ||
|
||
// TODO: Implement | ||
// merge_extra_filters(self.form_data) | ||
|
||
// Split adhoc_filters into four fields according to | ||
// (1) clause (WHERE or HAVING) | ||
// (2) expressionType | ||
// 2.1 SIMPLE (subject + operator + comparator) | ||
// 2.2 SQL (freeform SQL expression)) | ||
|
||
// eslint-disable-next-line camelcase | ||
const { adhoc_filters } = formData; | ||
if (Array.isArray(adhoc_filters)) { | ||
const simpleWhere: QueryObjectFilterClause[] = []; | ||
const simpleHaving: QueryObjectFilterClause[] = []; | ||
const freeformWhere: string[] = []; | ||
const freeformHaving: string[] = []; | ||
|
||
adhoc_filters.forEach(filter => { | ||
const { clause } = filter; | ||
if (isSimpleAdhocFilter(filter)) { | ||
const filterClause = convertFilter(filter); | ||
if (clause === 'WHERE') { | ||
simpleWhere.push(filterClause); | ||
} else { | ||
simpleHaving.push(filterClause); | ||
} | ||
} else { | ||
const { sqlExpression } = filter; | ||
if (clause === 'WHERE') { | ||
freeformWhere.push(sqlExpression); | ||
} else { | ||
freeformHaving.push(sqlExpression); | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
filters: simpleWhere, | ||
having: freeformHaving.map(exp => `(${exp})`).join(' AND '), | ||
having_filters: simpleHaving, | ||
where: freeformWhere.map(exp => `(${exp})`).join(' AND '), | ||
}; | ||
} | ||
|
||
return {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ChartFormData } from '../types/ChartFormData'; | ||
import { QueryObjectMetric } from '../types/Query'; | ||
import { MetricKey } from '../types/Metric'; | ||
import convertMetric from './convertMetric'; | ||
|
||
export default function processMetrics(formData: ChartFormData) { | ||
// Use Array to maintain insertion order | ||
// for metrics that are order sensitive | ||
const metrics: QueryObjectMetric[] = []; | ||
|
||
Object.keys(MetricKey).forEach(key => { | ||
const metric = formData[MetricKey[key as keyof typeof MetricKey]]; | ||
if (metric) { | ||
if (Array.isArray(metric)) { | ||
metric.forEach(m => { | ||
metrics.push(convertMetric(m)); | ||
}); | ||
} else { | ||
metrics.push(convertMetric(metric)); | ||
} | ||
} | ||
}); | ||
|
||
return metrics; | ||
} |
Oops, something went wrong.