diff --git a/packages/server/src/api/controllers/view/tests/__snapshots__/viewBuilder.spec.js.snap b/packages/server/src/api/controllers/view/tests/__snapshots__/viewBuilder.spec.js.snap index 99f67593fd..4572a8a24f 100644 --- a/packages/server/src/api/controllers/view/tests/__snapshots__/viewBuilder.spec.js.snap +++ b/packages/server/src/api/controllers/view/tests/__snapshots__/viewBuilder.spec.js.snap @@ -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) { diff --git a/packages/server/src/api/controllers/view/tests/viewBuilder.spec.js b/packages/server/src/api/controllers/view/tests/viewBuilder.spec.js index d1674bca08..58fb68cfa7 100644 --- a/packages/server/src/api/controllers/view/tests/viewBuilder.spec.js +++ b/packages/server/src/api/controllers/view/tests/viewBuilder.spec.js @@ -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() + }) + }) }); \ No newline at end of file diff --git a/packages/server/src/api/controllers/view/viewBuilder.js b/packages/server/src/api/controllers/view/viewBuilder.js index 125964a50e..6596e0d9e7 100644 --- a/packages/server/src/api/controllers/view/viewBuilder.js +++ b/packages/server/src/api/controllers/view/viewBuilder.js @@ -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} } }`, diff --git a/packages/server/src/api/routes/tests/view.spec.js b/packages/server/src/api/routes/tests/view.spec.js index 48d7f8c552..b1c5f655c6 100644 --- a/packages/server/src/api/routes/tests/view.spec.js +++ b/packages/server/src/api/routes/tests/view.spec.js @@ -72,12 +72,7 @@ describe("/views", () => { field: "Price", calculation: "stats", tableId: table._id, - filters: [ - { - condition: "NOT_EMPTY", - key: "Price", - } - ], + filters: [], schema: { sum: { type: "number",