Skip to content
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

Wrap API - Parse aggregation #106

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions packages/velo-external-db-core/lib/service/api_wrapper.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
const { isObject } = require('velo-external-db-commons')
const { AdapterOperators, EMPTY_FILTER } = require ('./api_wrapper_utils')

const { isObject, isEmptyFilter } = require('velo-external-db-commons')
const { AdapterOperators, AdapterFunctions, EMPTY_FILTER, projectionFieldFor, projectionFunctionFor } = require ('./api_wrapper_utils')

//FilterService
//ApiFilterService
//ApiFilterWrapper
class ApiWrapper {
constructor() {}

parseAggregation(processingStep, postFilteringStep) {
const projection = []

if (isObject(processingStep._id)) {
projection.push(...Object.values(processingStep._id).map(f => projectionFieldFor(f)) )
} else {
projection.push(projectionFieldFor(processingStep._id))
}

Object.keys(processingStep)
.filter(f => f !== '_id')
.forEach(fieldAlias => {
Object.entries(processingStep[fieldAlias])
.forEach(([func, field]) => {
projection.push(projectionFunctionFor(field, fieldAlias, this.wixFunctionToAdapterFunction(func)))
})
})

const postFilter = this.parseFilter(postFilteringStep)

return {
projection,
postFilter
}
}


parseFilter(filter) {
if (!filter || !isObject(filter)) return EMPTY_FILTER
if (!Object.keys(filter)[0]) return EMPTY_FILTER
if (isEmptyFilter(filter)) return EMPTY_FILTER

if(this.isMultipleFieldOperator(filter)) {
const wixOperator = Object.keys(filter)[0]
Expand Down Expand Up @@ -70,6 +95,22 @@ class ApiWrapper {
return AdapterOperators.not
}
}

wixFunctionToAdapterFunction(func) {
switch (func) {
case '$avg':
return AdapterFunctions.avg
case '$max':
return AdapterFunctions.max
case '$min':
return AdapterFunctions.min
case '$sum':
return AdapterFunctions.sum
//COUNT!
}
}


}


Expand Down
131 changes: 128 additions & 3 deletions packages/velo-external-db-core/lib/service/api_wrapper.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Uninitialized, gen } = require('test-commons')
const ApiWrapper = require('./api_wrapper')
const { AdapterOperators, EMPTY_FILTER } = require ('./api_wrapper_utils')
const { AdapterOperators, AdapterFunctions, EMPTY_FILTER } = require ('./api_wrapper_utils')
const Chance = require('chance')
const chance = Chance()
const each = require('jest-each').default
Expand Down Expand Up @@ -128,25 +128,150 @@ describe('Api Wrapper', () => {
})


describe('correctly transform Wix functions to adapter functions', () => {
each([
'$avg', '$max', '$min', '$sum'
])
.test('correctly transform [%s]', (f) => {
const AdapterFunction = f.substring(1)
expect(env.ApiWrapper.wixFunctionToAdapterFunction(f)).toEqual(AdapterFunctions[AdapterFunction])
})
})

describe('Parse aggregation', () => {
test('single id field without function or postFilter', () => {
const processingStep = { _id: `$${ctx.fieldName}` }
const postFilteringStep = null

expect(env.ApiWrapper.parseAggregation(processingStep, postFilteringStep)).toEqual({
projection: [{ name: ctx.fieldName, alias: ctx.fieldName }],
postFilter: EMPTY_FILTER
})
})

test('multiple id fields without function or postFilter', () => {
const processingStep = {
_id: {
field1: `$${ctx.fieldName}`,
field2: `$${ctx.anotherFieldName}`
}
}
const postFilteringStep = null

expect(env.ApiWrapper.parseAggregation(processingStep, postFilteringStep)).toEqual({
projection: [
{ name: ctx.fieldName, alias: ctx.fieldName },
{ name: ctx.anotherFieldName, alias: ctx.anotherFieldName }
],
postFilter: EMPTY_FILTER
})
})

test('single id field with function field and without postFilter', () => {
const processingStep = {
_id: `$${ctx.fieldName}`,
[ctx.fieldAlias]: {
$avg: `$${ctx.anotherFieldName}`
}
}
const postFilteringStep = null

expect(env.ApiWrapper.parseAggregation(processingStep, postFilteringStep)).toEqual({
projection: [
{ name: ctx.fieldName, alias: ctx.fieldName },
{ name: ctx.anotherFieldName, alias: ctx.fieldAlias, function: AdapterFunctions.avg }
],
postFilter: EMPTY_FILTER
})
})

test('single id field with count function and without postFilter', () => {
const processingStep = {
_id: `$${ctx.fieldName}`,
[ctx.fieldAlias]: {
$sum: 1
}
}
const postFilteringStep = null

expect(env.ApiWrapper.parseAggregation(processingStep, postFilteringStep)).toEqual({
projection: [
{ name: ctx.fieldName, alias: ctx.fieldName },
{ alias: ctx.fieldAlias, function: AdapterFunctions.count }
],
postFilter: EMPTY_FILTER
})
})

test('multiple function fields and without postFilter', () => {
const processingStep = {
_id: `$${ctx.fieldName}`,
[ctx.fieldAlias]: {
$avg: `$${ctx.anotherFieldName}`
},
[ctx.anotherFieldAlias]: {
$sum: `$${ctx.moreFieldName}`
}
}
const postFilteringStep = null

expect(env.ApiWrapper.parseAggregation(processingStep, postFilteringStep)).toEqual({
projection: [
{ name: ctx.fieldName, alias: ctx.fieldName },
{ name: ctx.anotherFieldName, alias: ctx.fieldAlias, function: AdapterFunctions.avg },
{ name: ctx.moreFieldName, alias: ctx.anotherFieldAlias, function: AdapterFunctions.sum }
],
postFilter: EMPTY_FILTER
})
})

test('function and postFilter', () => {
const processingStep = {
_id: `$${ctx.fieldName}`,
[ctx.fieldAlias]: {
$avg: `$${ctx.anotherFieldName}`
}
}

const postFilteringStep = ctx.filter

expect(env.ApiWrapper.parseAggregation(processingStep, postFilteringStep)).toEqual({
projection: [
{ name: ctx.fieldName, alias: ctx.fieldName },
{ name: ctx.anotherFieldName, alias: ctx.fieldAlias, function: AdapterFunctions.avg }
],
postFilter: env.ApiWrapper.parseFilter(ctx.filter)
})
})
})


const env = {
ApiWrapper: Uninitialized
}

const ctx = {
filter: Uninitialized,
fieldName: Uninitialized,
anotherFieldName: Uninitialized,
moreFieldName: Uninitialized,
fieldAlias: Uninitialized,
anotherFieldAlias: Uninitialized,
fieldValue: Uninitialized,
operator: Uninitialized,
fieldListValue: Uninitialized
fieldListValue: Uninitialized,
}

beforeEach(() => {
ctx.filter = gen.randomFilter()
ctx.anotherFilter = gen.randomFilter()
ctx.fieldName = chance.word()
ctx.anotherFieldName = chance.word()
ctx.moreFieldName = chance.word()
ctx.fieldAlias = chance.word()
ctx.anotherFieldAlias = chance.word()
ctx.fieldValue = chance.word()
ctx.operator = gen.randomOperator()
ctx.fieldListValue = [chance.word(), chance.word(), chance.word(), chance.word(), chance.word()]

})
})
22 changes: 21 additions & 1 deletion packages/velo-external-db-core/lib/service/api_wrapper_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ const AdapterOperators = { //should not be here - in data_common or something
not: 'not'
}

const AdapterFunctions = { //should not be here - in data_common or something
avg: 'avg',
max: 'max',
min: 'min',
sum: 'sum',
count: 'count'
}

const EMPTY_FILTER = {}

module.exports = { AdapterOperators, EMPTY_FILTER }
const projectionFieldFor = (fieldName, fieldAlias) => (
{ name: fieldName.substring(1), alias: fieldAlias || fieldName.substring(1) }
)

const projectionFunctionFor = (fieldName, fieldAlias, func) => {
if (isCountFunc(func, fieldName))
return { alias: fieldAlias, function: AdapterFunctions.count }
return { name: fieldName.substring(1), alias: fieldAlias || fieldName.substring(1), function: func }
}

const isCountFunc = (func, value) => (func === AdapterFunctions.sum && value === 1)

module.exports = { AdapterOperators, AdapterFunctions, EMPTY_FILTER, projectionFieldFor, projectionFunctionFor }