Updating view builder to handle if stats has a filter as well, don't need a conjuction this way.

This commit is contained in:
mike12345567 2022-05-16 13:37:00 +01:00
parent 08f53591e6
commit 7b07cff0c6
4 changed files with 81 additions and 15 deletions

View File

@ -1,14 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`viewBuilder Calculate creates a view with the calculation statistics schema 1`] = `
exports[`viewBuilder Calculate and filter creates a view with the calculation statistics and filter schema 1`] = `
Object {
"map": "function (doc) {
if (doc.tableId === \\"14f1c4e94d6a47b682ce89d35d4c78b0\\" && (!(
if ((doc.tableId === \\"14f1c4e94d6a47b682ce89d35d4c78b0\\" && !(
doc[\\"myField\\"] === undefined ||
doc[\\"myField\\"] === null ||
doc[\\"myField\\"] === \\"\\" ||
(Array.isArray(doc[\\"myField\\"]) && doc[\\"myField\\"].length === 0)
))) {
)) && (doc[\\"age\\"] > 17)) {
emit(doc[\\"_id\\"], doc[\\"myField\\"]);
}
}",
@ -17,8 +17,9 @@ Object {
"field": "myField",
"filters": Array [
Object {
"condition": "NOT_EMPTY",
"key": "myField",
"condition": "MT",
"key": "age",
"value": 17,
},
],
"groupBy": undefined,
@ -51,6 +52,52 @@ Object {
}
`;
exports[`viewBuilder Calculate creates a view with the calculation statistics schema 1`] = `
Object {
"map": "function (doc) {
if ((doc.tableId === \\"14f1c4e94d6a47b682ce89d35d4c78b0\\" && !(
doc[\\"myField\\"] === undefined ||
doc[\\"myField\\"] === null ||
doc[\\"myField\\"] === \\"\\" ||
(Array.isArray(doc[\\"myField\\"]) && doc[\\"myField\\"].length === 0)
)) ) {
emit(doc[\\"_id\\"], doc[\\"myField\\"]);
}
}",
"meta": Object {
"calculation": "stats",
"field": "myField",
"filters": Array [],
"groupBy": undefined,
"schema": Object {
"avg": Object {
"type": "number",
},
"count": Object {
"type": "number",
},
"field": Object {
"type": "string",
},
"max": Object {
"type": "number",
},
"min": Object {
"type": "number",
},
"sum": Object {
"type": "number",
},
"sumsqr": Object {
"type": "number",
},
},
"tableId": "14f1c4e94d6a47b682ce89d35d4c78b0",
},
"reduce": "_stats",
}
`;
exports[`viewBuilder Filter creates a view with multiple filters and conjunctions 1`] = `
Object {
"map": "function (doc) {

View File

@ -44,4 +44,22 @@ describe("viewBuilder", () => {
})).toMatchSnapshot()
})
})
describe("Calculate and filter", () => {
it("creates a view with the calculation statistics and filter schema", () => {
expect(viewTemplate({
"name": "Calculate View",
"field": "myField",
"calculation": "stats",
"tableId": "14f1c4e94d6a47b682ce89d35d4c78b0",
"filters": [
{
"value": 17,
"condition": "MT",
"key": "age",
}
]
})).toMatchSnapshot()
})
})
});

View File

@ -132,7 +132,8 @@ function viewTemplate({ field, tableId, groupBy, filters = [], calculation }) {
delete filters[0].conjunction
}
let schema = null
let schema = null,
statFilter = null
if (calculation) {
schema = {
@ -145,7 +146,9 @@ function viewTemplate({ field, tableId, groupBy, filters = [], calculation }) {
filter.key === field && filter.condition === CONDITIONS.NOT_EMPTY
)
) {
filters.push({ key: field, condition: CONDITIONS.NOT_EMPTY })
statFilter = parseFilterExpression([
{ key: field, condition: CONDITIONS.NOT_EMPTY },
])
}
}
@ -153,7 +156,10 @@ function viewTemplate({ field, tableId, groupBy, filters = [], calculation }) {
const filterExpression = parsedFilters ? `&& (${parsedFilters})` : ""
const emitExpression = parseEmitExpression(field, groupBy)
const tableExpression = `doc.tableId === "${tableId}"`
const coreExpression = statFilter
? `(${tableExpression} && ${statFilter})`
: tableExpression
const reduction = field && calculation ? { reduce: `_${calculation}` } : {}
return {
@ -166,7 +172,7 @@ function viewTemplate({ field, tableId, groupBy, filters = [], calculation }) {
calculation,
},
map: `function (doc) {
if (doc.tableId === "${tableId}" ${filterExpression}) {
if (${coreExpression} ${filterExpression}) {
${emitExpression}
}
}`,

View File

@ -72,12 +72,7 @@ describe("/views", () => {
field: "Price",
calculation: "stats",
tableId: table._id,
filters: [
{
condition: "NOT_EMPTY",
key: "Price",
}
],
filters: [],
schema: {
sum: {
type: "number",