Skip to content

Commit

Permalink
feat(normalization): add normalization for two dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
quanho committed Jan 8, 2020
1 parent d2e2a3d commit 8b410b9
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 20 deletions.
112 changes: 103 additions & 9 deletions src/expression-modifiers/normalization/_tests_/index.spec.js
Expand Up @@ -73,7 +73,7 @@ describe('normalization', () => {
properties.qHyperCubeDef.qDimensions = [dim1];
});

describe('showExcludedValues = true', () => {
describe.skip('showExcludedValues = true', () => {
beforeEach(() => {
modifier.showExcludedValues = true;
});
Expand All @@ -88,15 +88,15 @@ describe('normalization', () => {
expression, modifier, properties, libraryItemsProps, dimensionAndFieldList,
});

expect(outputExpression).to.equal('If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={">=$(=Min([$(=Replace(GetObjectField(0),\']\',\']]\'))]))<=$(=Max([$(=Replace(GetObjectField(0),\']\',\']]\'))]))"}>}0), 0)/Sum (total Aggr(If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={">=$(=Min([$(=Replace(GetObjectField(0),\']\',\']]\'))]))<=$(=Max([$(=Replace(GetObjectField(0),\']\',\']]\'))]))"}>}0), 0), [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
expect(outputExpression).to.equal('If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={">=$(=Min([$(=Replace(GetObjectField(0),\']\',\']]\'))]))<=$(=Max([$(=Replace(GetObjectField(0),\']\',\']]\'))]))"}>}0), 0)/Sum(Total Aggr(If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={">=$(=Min([$(=Replace(GetObjectField(0),\']\',\']]\'))]))<=$(=Max([$(=Replace(GetObjectField(0),\']\',\']]\'))]))"}>}0), 0), [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
});

it('should generate correct expression when dimension is non-numeric', () => {
outputExpression = normalization.generateExpression({
expression, modifier, properties, libraryItemsProps,
});

expect(outputExpression).to.equal('If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={"=Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])>=\'$(=MinString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\' and Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])<=\'$(=MaxString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\'"}>}0), 0)/Sum (total Aggr(If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={"=Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])>=\'$(=MinString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\' and Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])<=\'$(=MaxString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\'"}>}0), 0), [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
expect(outputExpression).to.equal('If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={"=Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])>=\'$(=MinString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\' and Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])<=\'$(=MaxString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\'"}>}0), 0)/Sum(Total Aggr(If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={"=Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])>=\'$(=MinString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\' and Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])<=\'$(=MaxString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\'"}>}0), 0), [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
});
});

Expand Down Expand Up @@ -132,15 +132,15 @@ describe('normalization', () => {
expression, modifier, properties, libraryItemsProps, dimensionAndFieldList,
});

expect(outputExpression).to.equal('If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={">=$(=Min([$(=Replace(GetObjectField(0),\']\',\']]\'))]))<=$(=Max([$(=Replace(GetObjectField(0),\']\',\']]\'))]))"}>}0), 0)/Sum({1} total Aggr({1}If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={">=$(=Min([$(=Replace(GetObjectField(0),\']\',\']]\'))]))<=$(=Max([$(=Replace(GetObjectField(0),\']\',\']]\'))]))"}>}0), 0), [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
expect(outputExpression).to.equal('If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={">=$(=Min([$(=Replace(GetObjectField(0),\']\',\']]\'))]))<=$(=Max([$(=Replace(GetObjectField(0),\']\',\']]\'))]))"}>}0), 0)/Sum({1} Total Aggr({1}If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={">=$(=Min([$(=Replace(GetObjectField(0),\']\',\']]\'))]))<=$(=Max([$(=Replace(GetObjectField(0),\']\',\']]\'))]))"}>}0), 0), [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
});

it('should generate correct expression when dimension is non-numeric', () => {
outputExpression = normalization.generateExpression({
expression, modifier, properties, libraryItemsProps,
});

expect(outputExpression).to.equal('If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={"=Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])>=\'$(=MinString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\' and Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])<=\'$(=MaxString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\'"}>}0), 0)/Sum({1} total Aggr({1}If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={"=Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])>=\'$(=MinString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\' and Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])<=\'$(=MaxString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\'"}>}0), 0), [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
expect(outputExpression).to.equal('If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={"=Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])>=\'$(=MinString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\' and Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])<=\'$(=MaxString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\'"}>}0), 0)/Sum({1} Total Aggr({1}If(Count([$(=Replace(GetObjectField(0),\']\',\']]\'))]) > 0, ( Sum([Sales]) ) + Sum({1<[$(=Replace(GetObjectField(0),\']\',\']]\'))]={"=Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])>=\'$(=MinString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\' and Only({1}[$(=Replace(GetObjectField(0),\']\',\']]\'))])<=\'$(=MaxString([$(=Replace(GetObjectField(0),\']\',\']]\'))]))\'"}>}0), 0), [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
});
});
});
Expand All @@ -160,7 +160,7 @@ describe('normalization', () => {
expression, modifier, properties, libraryItemsProps,
});

expect(outputExpression).to.equal(' ( Sum([Sales]) ) /Sum (total Aggr( ( Sum([Sales]) ) , [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
expect(outputExpression).to.equal(' ( Sum([Sales]) ) /Sum(Total Aggr( ( Sum([Sales]) ) , [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
});
});

Expand Down Expand Up @@ -188,20 +188,64 @@ describe('normalization', () => {
expression, modifier, properties, libraryItemsProps,
});

expect(outputExpression).to.equal(' ( Sum([Sales]) ) /Sum({1} total Aggr({1} ( Sum([Sales]) ) , [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
expect(outputExpression).to.equal(' ( Sum([Sales]) ) /Sum({1} Total Aggr({1} ( Sum([Sales]) ) , [$(=Replace(GetObjectField(0),\']\',\']]\'))]))');
});
});
});
});

describe('Two dimension', () => {
describe('normalization type = relative to total selection', () => {
beforeEach(() => {
modifier.relativeNumbers = 0;
});

it('should generate correct expression ', () => {
outputExpression = normalization.generateExpression({
expression, modifier, properties, libraryItemsProps,
});

expect(outputExpression).to.equal(' ( Sum([Sales]) ) /Sum(Total Aggr( ( Sum([Sales]) ) , [$(=Replace(GetObjectField(0),\']\',\']]\'))], [$(=Replace(GetObjectField(1),\']\',\']]\'))]))');
});
});

describe('normalization type = relative to dimensional universe', () => {
beforeEach(() => {
modifier.relativeNumbers = 1;
});

it('should generate correct expression ', () => {
outputExpression = normalization.generateExpression({
expression, modifier, properties, libraryItemsProps,
});

expect(outputExpression).to.equal(' ( Sum([Sales]) ) /Sum({1} Aggr({1} ( Sum([Sales]) ) , [$(=Replace(GetObjectField(0),\']\',\']]\'))], [$(=Replace(GetObjectField(1),\']\',\']]\'))]))');
});
});

describe('normalization type = relative to total universe', () => {
beforeEach(() => {
modifier.relativeNumbers = 2;
});

it('should generate correct expression ', () => {
outputExpression = normalization.generateExpression({
expression, modifier, properties, libraryItemsProps,
});

expect(outputExpression).to.equal(' ( Sum([Sales]) ) /Sum({1} Total Aggr({1} ( Sum([Sales]) ) , [$(=Replace(GetObjectField(0),\']\',\']]\'))], [$(=Replace(GetObjectField(1),\']\',\']]\'))]))');
});
});
});
});

describe('extractExpression', () => {
describe('one dimension', () => {
describe('One dimension', () => {
beforeEach(() => {
properties.qHyperCubeDef.qDimensions = [dim1];
});

describe('showExcludedValues = true', () => {
describe.skip('showExcludedValues = true', () => {
beforeEach(() => {
modifier.showExcludedValues = true;
});
Expand Down Expand Up @@ -342,5 +386,55 @@ describe('normalization', () => {
});
});
});

describe('Two dimensions', () => {
describe('normalization type = relative to total selection', () => {
beforeEach(() => {
modifier.relativeNumbers = 0;
});
it('should extract correct expression', () => {
outputExpression = normalization.generateExpression({
expression, modifier, properties, libraryItemsProps,
});
inputExp = normalization.extractInputExpression({
outputExpression, modifier, properties, libraryItemsProps,
});

expect(inputExp).to.equal(expression);
});
});

describe('normalization type = relative to dimensional universe', () => {
beforeEach(() => {
modifier.relativeNumbers = 1;
});
it('should extract correct expression', () => {
outputExpression = normalization.generateExpression({
expression, modifier, properties, libraryItemsProps,
});
inputExp = normalization.extractInputExpression({
outputExpression, modifier, properties, libraryItemsProps,
});

expect(inputExp).to.equal(expression);
});
});

describe('normalization type = relative to total universe', () => {
beforeEach(() => {
modifier.relativeNumbers = 2;
});
it('should extract correct expression', () => {
outputExpression = normalization.generateExpression({
expression, modifier, properties, libraryItemsProps,
});
inputExp = normalization.extractInputExpression({
outputExpression, modifier, properties, libraryItemsProps,
});

expect(inputExp).to.equal(expression);
});
});
});
});
});
31 changes: 20 additions & 11 deletions src/expression-modifiers/normalization/index.js
Expand Up @@ -12,19 +12,30 @@ const DEFAULT_OPTIONS = {

const maxNumDimensionsSupported = 2;

function getAggrOneDim(expression, dim1) {
return `Aggr(${expression}, ${dim1})`;
function getDimsComp(numberOfDims) {
const dims = [];
for (let i = 0; i < numberOfDims; i++) {
dims[i] = helper.getDimDefWithWrapper(i);
}
const s = dims.join(', ');
return s;
}

function getAggrDisregardSelect(expression, dim1) {
return `Aggr({1}${expression}, ${dim1})`;
function getAggr(expression, numberOfDims) {
const dimsComp = getDimsComp(numberOfDims);
return `Aggr(${expression}, ${dimsComp})`;
}

function getAggrDisregardSelect(expression, numberOfDims) {
const dimsComp = getDimsComp(numberOfDims);
return `Aggr({1}${expression}, ${dimsComp})`;
}

function getTotal(expression) {
return `total ${expression}`;
return `Total ${expression}`;
}
function getSum(expression) {
return `Sum (${expression})`;
return `Sum(${expression})`;
}

function getDivide(measureExp, expression) {
Expand Down Expand Up @@ -71,8 +82,6 @@ export default {
'qHyperCubeDef.qDimensions',
[],
);
const dim = helper.getDimDefWithWrapper(modifier.primaryDimension);

const expWithExcludedComp = helper.getExpressionWithExcludedComp({
expression,
modifier,
Expand All @@ -84,13 +93,13 @@ export default {

switch (modifier.relativeNumbers) {
case 0:
generatedExpression = getSum(getTotal(getAggrOneDim(generatedExpression, dim)));
generatedExpression = getSum(getTotal(getAggr(generatedExpression, numberOfDims)));
break;
case 1:
generatedExpression = getSumDisregardSelect(getAggrDisregardSelect(generatedExpression, dim));
generatedExpression = getSumDisregardSelect(getAggrDisregardSelect(generatedExpression, numberOfDims));
break;
case 2:
generatedExpression = getSumDisregardSelect(getTotal(getAggrDisregardSelect(generatedExpression, dim)));
generatedExpression = getSumDisregardSelect(getTotal(getAggrDisregardSelect(generatedExpression, numberOfDims)));
break;
default:
generatedExpression = expWithExcludedComp;
Expand Down

0 comments on commit 8b410b9

Please sign in to comment.