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 bcf7e1782a
commit db0b8a6194
4 changed files with 81 additions and 15 deletions

View File

@ -1,14 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // 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 { Object {
"map": "function (doc) { "map": "function (doc) {
if (doc.tableId === \\"14f1c4e94d6a47b682ce89d35d4c78b0\\" && (!( if ((doc.tableId === \\"14f1c4e94d6a47b682ce89d35d4c78b0\\" && !(
doc[\\"myField\\"] === undefined || doc[\\"myField\\"] === undefined ||
doc[\\"myField\\"] === null || doc[\\"myField\\"] === null ||
doc[\\"myField\\"] === \\"\\" || doc[\\"myField\\"] === \\"\\" ||
(Array.isArray(doc[\\"myField\\"]) && doc[\\"myField\\"].length === 0) (Array.isArray(doc[\\"myField\\"]) && doc[\\"myField\\"].length === 0)
))) { )) && (doc[\\"age\\"] > 17)) {
emit(doc[\\"_id\\"], doc[\\"myField\\"]); emit(doc[\\"_id\\"], doc[\\"myField\\"]);
} }
}", }",
@ -17,8 +17,9 @@ Object {
"field": "myField", "field": "myField",
"filters": Array [ "filters": Array [
Object { Object {
"condition": "NOT_EMPTY", "condition": "MT",
"key": "myField", "key": "age",
"value": 17,
}, },
], ],
"groupBy": undefined, "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`] = ` exports[`viewBuilder Filter creates a view with multiple filters and conjunctions 1`] = `
Object { Object {
"map": "function (doc) { "map": "function (doc) {

View File

@ -44,4 +44,22 @@ describe("viewBuilder", () => {
})).toMatchSnapshot() })).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 delete filters[0].conjunction
} }
let schema = null let schema = null,
statFilter = null
if (calculation) { if (calculation) {
schema = { schema = {
@ -145,7 +146,9 @@ function viewTemplate({ field, tableId, groupBy, filters = [], calculation }) {
filter.key === field && filter.condition === CONDITIONS.NOT_EMPTY 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 filterExpression = parsedFilters ? `&& (${parsedFilters})` : ""
const emitExpression = parseEmitExpression(field, groupBy) const emitExpression = parseEmitExpression(field, groupBy)
const tableExpression = `doc.tableId === "${tableId}"`
const coreExpression = statFilter
? `(${tableExpression} && ${statFilter})`
: tableExpression
const reduction = field && calculation ? { reduce: `_${calculation}` } : {} const reduction = field && calculation ? { reduce: `_${calculation}` } : {}
return { return {
@ -166,7 +172,7 @@ function viewTemplate({ field, tableId, groupBy, filters = [], calculation }) {
calculation, calculation,
}, },
map: `function (doc) { map: `function (doc) {
if (doc.tableId === "${tableId}" ${filterExpression}) { if (${coreExpression} ${filterExpression}) {
${emitExpression} ${emitExpression}
} }
}`, }`,

View File

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