Skip to content

Commit

Permalink
including auto generated avg metrics in druid (apache#4718)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Lyons authored and michellethomas committed May 23, 2018
1 parent 5ddb312 commit aebc56f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@ function getDefaultAggregateForColumn(column) {
return null;
}

const autoGeneratedMetricRegex = /^(LONG|DOUBLE|FLOAT)?(SUM|AVG|MAX|MIN|COUNT)\([A-Z_][A-Z0-9_]*\)$/i;
function isAutoGeneratedMetric(savedMetric) {
return (
autoGeneratedMetricRegex.test(savedMetric.expression) ||
autoGeneratedMetricRegex.test(savedMetric.verbose_name)
);
}
const sqlaAutoGeneratedMetricRegex = /^(LONG|DOUBLE|FLOAT)?(SUM|AVG|MAX|MIN|COUNT)\([A-Z_][A-Z0-9_]*\)$/i;
const druidAutoGeneratedMetricRegex = /^(LONG|DOUBLE|FLOAT)?(SUM|MAX|MIN|COUNT)\([A-Z_][A-Z0-9_]*\)$/i;

export default class MetricsControl extends React.PureComponent {
constructor(props) {
Expand All @@ -90,6 +85,7 @@ export default class MetricsControl extends React.PureComponent {
this.checkIfAggregateInInput = this.checkIfAggregateInInput.bind(this);
this.optionsForSelect = this.optionsForSelect.bind(this);
this.selectFilterOption = this.selectFilterOption.bind(this);
this.isAutoGeneratedMetric = this.isAutoGeneratedMetric.bind(this);
this.optionRenderer = VirtualizedRendererWrap(option => (
<MetricDefinitionOption option={option} />
), { ignoreAutogeneratedMetrics: true });
Expand Down Expand Up @@ -209,6 +205,13 @@ export default class MetricsControl extends React.PureComponent {
});
}

isAutoGeneratedMetric(savedMetric) {
if (this.props.datasourceType === 'druid') {
return druidAutoGeneratedMetricRegex.test(savedMetric.verbose_name);
}
return sqlaAutoGeneratedMetricRegex.test(savedMetric.expression);
}

selectFilterOption(option, filterValue) {
if (this.state.aggregateInInput) {
let endIndex = filterValue.length;
Expand All @@ -220,7 +223,7 @@ export default class MetricsControl extends React.PureComponent {
(option.column_name.toLowerCase().indexOf(valueAfterAggregate.toLowerCase()) >= 0);
}
return option.optionName &&
(!option.metric_name || !isAutoGeneratedMetric(option)) &&
(!option.metric_name || !this.isAutoGeneratedMetric(option)) &&
(option.optionName.toLowerCase().indexOf(filterValue.toLowerCase()) >= 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const defaultProps = {
{ metric_name: 'sum__value', expression: 'SUM(energy_usage.value)' },
{ metric_name: 'avg__value', expression: 'AVG(energy_usage.value)' },
],
datasourceType: 'sqla',
};

function setup(overrides) {
Expand Down Expand Up @@ -188,7 +189,7 @@ describe('MetricsControl', () => {

describe('option filter', () => {
it('includes user defined metrics', () => {
const { wrapper } = setup();
const { wrapper } = setup({ datasourceType: 'druid' });

expect(!!wrapper.instance().selectFilterOption(
{
Expand All @@ -200,6 +201,19 @@ describe('MetricsControl', () => {
)).to.be.true;
});

it('includes auto generated avg metrics for druid', () => {
const { wrapper } = setup({ datasourceType: 'druid' });

expect(!!wrapper.instance().selectFilterOption(
{
metric_name: 'a_metric',
optionName: 'a_metric',
expression: 'AVG(metric)',
},
'a',
)).to.be.true;
});

it('includes columns and aggregates', () => {
const { wrapper } = setup();

Expand All @@ -214,6 +228,19 @@ describe('MetricsControl', () => {
)).to.be.true;
});

it('excludes auto generated avg metrics for sqla', () => {
const { wrapper } = setup();

expect(!!wrapper.instance().selectFilterOption(
{
metric_name: 'a_metric',
optionName: 'a_metric',
expression: 'AVG(metric)',
},
'a',
)).to.be.false;
});

it('excludes auto generated metrics', () => {
const { wrapper } = setup();

Expand Down

0 comments on commit aebc56f

Please sign in to comment.