From 8dec9b5869deb4cdefc2e992b93d724ce319bbdf Mon Sep 17 00:00:00 2001 From: cmack Date: Wed, 2 Sep 2020 11:52:32 +0100 Subject: [PATCH] Single Calculation Views When Not Grouped --- .../database/DataTable/Table.svelte | 1 + .../database/DataTable/ViewDataTable.svelte | 5 +++- packages/server/src/api/controllers/record.js | 3 ++- .../src/api/controllers/view/viewBuilder.js | 25 ++++++++++++++++--- packages/standard-components/src/fetchData.js | 11 +++++--- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/builder/src/components/database/DataTable/Table.svelte b/packages/builder/src/components/database/DataTable/Table.svelte index 8bfdebc2e1..a7857a3fef 100644 --- a/packages/builder/src/components/database/DataTable/Table.svelte +++ b/packages/builder/src/components/database/DataTable/Table.svelte @@ -25,6 +25,7 @@ let currentPage = 0 $: columns = schema ? Object.keys(schema) : [] + $: paginatedData = data && data.length ? data.slice( diff --git a/packages/builder/src/components/database/DataTable/ViewDataTable.svelte b/packages/builder/src/components/database/DataTable/ViewDataTable.svelte index 11e0f85601..2426e3ea41 100644 --- a/packages/builder/src/components/database/DataTable/ViewDataTable.svelte +++ b/packages/builder/src/components/database/DataTable/ViewDataTable.svelte @@ -32,7 +32,10 @@ async function fetchViewData(name, field, groupBy) { const params = new URLSearchParams() - if (field) params.set("stats", true) + if (field) { + params.set("field", field) + params.set("stats", true) + } if (groupBy) params.set("group", groupBy) let QUERY_VIEW_URL = `/api/views/${name}?${params}` diff --git a/packages/server/src/api/controllers/record.js b/packages/server/src/api/controllers/record.js index c7c17857da..4685079f68 100644 --- a/packages/server/src/api/controllers/record.js +++ b/packages/server/src/api/controllers/record.js @@ -80,7 +80,7 @@ exports.save = async function(ctx) { exports.fetchView = async function(ctx) { const db = new CouchDB(ctx.user.instanceId) - const { stats, group } = ctx.query + const { stats, group, field } = ctx.query const response = await db.query(`database/${ctx.params.viewName}`, { include_docs: !stats, group, @@ -89,6 +89,7 @@ exports.fetchView = async function(ctx) { if (stats) { response.rows = response.rows.map(row => ({ group: row.key, + field, ...row.value, avg: row.value.sum / row.value.count, })) diff --git a/packages/server/src/api/controllers/view/viewBuilder.js b/packages/server/src/api/controllers/view/viewBuilder.js index 9ea22bfce0..b5f9638115 100644 --- a/packages/server/src/api/controllers/view/viewBuilder.js +++ b/packages/server/src/api/controllers/view/viewBuilder.js @@ -9,11 +9,20 @@ const TOKEN_MAP = { OR: "||", } +const GROUP_PROPERTY = { + group: { + type: "string", + }, +} + +const FIELD_PROPERTY = { + field: { + type: "string", + }, +} + const SCHEMA_MAP = { stats: { - group: { - type: "string", - }, sum: { type: "number", }, @@ -91,13 +100,21 @@ function viewTemplate({ field, modelId, groupBy, filters = [], calculation }) { const reduction = field ? { reduce: "_stats" } : {} + let schema = null + + if (calculation) { + schema = groupBy + ? { ...GROUP_PROPERTY, ...SCHEMA_MAP[calculation] } + : { ...FIELD_PROPERTY, ...SCHEMA_MAP[calculation] } + } + return { meta: { field, modelId, groupBy, filters, - schema: SCHEMA_MAP[calculation], + schema, calculation, }, map: `function (doc) { diff --git a/packages/standard-components/src/fetchData.js b/packages/standard-components/src/fetchData.js index 51e1b9235b..7a1c4b5f7c 100644 --- a/packages/standard-components/src/fetchData.js +++ b/packages/standard-components/src/fetchData.js @@ -19,10 +19,15 @@ export default async function fetchData(datasource) { const { field, groupBy } = datasource const params = new URLSearchParams() - if (field) params.set("stats", true) + if (field) { + params.set("field", field) + params.set("stats", true) + } if (groupBy) params.set("group", groupBy) - let QUERY_VIEW_URL = - field && groupBy ? `/api/views/${name}?${params}` : `/api/views/${name}` + + let QUERY_VIEW_URL = field + ? `/api/views/${name}?${params}` + : `/api/views/${name}` const response = await api.get(QUERY_VIEW_URL) return await response.json()