-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature/set default viz #3
Changes from all commits
d2c9abe
616a5f2
8e792c7
995de9b
bdac73d
9daa543
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,10 +28,11 @@ import { | |
ControlPanelConfig, | ||
ControlPanelsContainerProps, | ||
sections, | ||
ControlState, | ||
} from '@superset-ui/chart-controls'; | ||
|
||
import cidrRegex from 'cidr-regex'; | ||
|
||
import cidrRegex from 'cidr-regex'; | ||
|
||
|
||
|
||
|
@@ -61,15 +62,6 @@ function isQueryMode(mode: QueryMode) { | |
const isAggMode = isQueryMode(QueryMode.aggregate); | ||
const isRawMode = isQueryMode(QueryMode.raw); | ||
|
||
function getDataSourceSql(controls: ControlStateMapping): QueryMode { | ||
return controls?.datasource?.datasource?.sql; | ||
} | ||
|
||
function datasourceAcceptsParam(paramType: string) { | ||
return ({ controls }: ControlPanelsContainerProps) => getDataSourceSql(controls)?.includes(paramType); | ||
} | ||
|
||
const datasourceAcceptsIpParam = datasourceAcceptsParam('_ipv4_parameter_'); | ||
|
||
|
||
const queryMode: ControlConfig<'RadioButtonControl'> = { | ||
|
@@ -94,7 +86,6 @@ const queryMode: ControlConfig<'RadioButtonControl'> = { | |
|
||
|
||
|
||
|
||
function isIP(v: unknown) { | ||
if (typeof v === 'string' && v.trim().length > 0) { | ||
//console.log(v.trim()); | ||
|
@@ -122,10 +113,55 @@ function validateIP(v: unknown) { | |
} | ||
} | ||
|
||
return ('is expected to be an ip address or cidr'); | ||
return (' is expected to be an IP address in dotted decimal or CIDR notation'); | ||
} | ||
|
||
|
||
/** | ||
* Validates the adhoc filter control. Each filter has a subject (the column name for example SRC_PORT) and a comparator (the value being tested), | ||
* it can be a single value for operators like !=, >, <= etc | ||
* or it can be an array of values for example when the IN or NOT IN operator is used. | ||
* | ||
* @param filters an array of adhoc filter with the following attributes | ||
* @param state the current state of the adhoc filter control it includes a copy of the columns as defined in the dataset model | ||
* @returns a string explaining the reason why the control is in an invalid state or false if there is no errors | ||
*/ | ||
function adhocFilterValidator(filters: unknown, state: ControlState) { | ||
cccs-jc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (Array.isArray(filters)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if it's not an array? Any error handling needed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as far as I know it's always an array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can live with this |
||
for (let i = 0; i < filters.length; i++) { | ||
const filter = filters[i]; | ||
// Find the corresponding column in the model | ||
const column = state.columns.find((c: any) => c.column_name == filter.subject); | ||
if (typeof column !== 'undefined' && typeof column.type !== 'undefined') { | ||
// Currently supporting 2 types of columns | ||
// IPV4 | ||
// IPV4 FILTER | ||
if (column.type.includes('IPV4')) { | ||
const v = filter.comparator; | ||
// check single value | ||
if (typeof v === 'string' && v.trim().length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any error handling needed if it's none of the types in the if-else? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it's not a type we validate it just reports no errors, we will of course add more of these at a later date There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhhhh gotcha. Thought this might've been IP specific. |
||
const error = validateIP(v.trim()); | ||
if (error) { | ||
return filter.subject + error; | ||
} | ||
} | ||
// check array of values | ||
else if (Array.isArray(v)) { | ||
for (let index = 0; index < v.length; index++) { | ||
const element = v[index]; | ||
const error = validateIP(element.trim()); | ||
if (error) { | ||
return filter.subject + error; | ||
} | ||
} | ||
} | ||
} | ||
// else we assume the value is okay | ||
// more type validators can be added here | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
const config: ControlPanelConfig = { | ||
|
||
|
@@ -137,28 +173,6 @@ const config: ControlPanelConfig = { | |
label: t('Query'), | ||
expanded: true, | ||
controlSetRows: [ | ||
[ | ||
{ | ||
name: '_ipv4_parameter_', | ||
config: { | ||
type: 'SelectControl', | ||
label: t('IP Params'), | ||
default: [], | ||
multi: true, | ||
allowClear: true, | ||
freeForm: true, | ||
allowAll: true, | ||
tokenSeparators: [' ', ',', '\n', '\t', ';'], | ||
validators: [validateIP], | ||
renderTrigger: false, | ||
description: t('The IPs or CIDRs to filter'), | ||
visibility: datasourceAcceptsIpParam, | ||
} | ||
|
||
}, | ||
|
||
], | ||
|
||
[ | ||
{ | ||
name: 'query_mode', | ||
|
@@ -211,7 +225,14 @@ const config: ControlPanelConfig = { | |
}, | ||
], | ||
|
||
['adhoc_filters'], | ||
[ | ||
{ | ||
name: 'adhoc_filters', | ||
override: { | ||
validators: [adhocFilterValidator], | ||
} | ||
} | ||
], | ||
[ | ||
{ | ||
name: 'row_limit', | ||
|
@@ -279,13 +300,17 @@ const config: ControlPanelConfig = { | |
}, | ||
], | ||
|
||
// override controls that are inherited by the default configuration | ||
controlOverrides: { | ||
series: { | ||
validators: [validateNonEmpty], | ||
clearable: false, | ||
}, | ||
row_limit: { | ||
default: 100, | ||
viz_type: { | ||
default: 'hello_world' | ||
}, | ||
time_range: { | ||
default: t('Last day'), | ||
}, | ||
}, | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this just extra whitespace that got added? It could probably go.