Single Calculation Views When Not Grouped

This commit is contained in:
cmack 2020-09-02 11:52:32 +01:00
parent 72bb96f19d
commit 8dec9b5869
5 changed files with 36 additions and 9 deletions

View File

@ -25,6 +25,7 @@
let currentPage = 0 let currentPage = 0
$: columns = schema ? Object.keys(schema) : [] $: columns = schema ? Object.keys(schema) : []
$: paginatedData = $: paginatedData =
data && data.length data && data.length
? data.slice( ? data.slice(

View File

@ -32,7 +32,10 @@
async function fetchViewData(name, field, groupBy) { async function fetchViewData(name, field, groupBy) {
const params = new URLSearchParams() 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) if (groupBy) params.set("group", groupBy)
let QUERY_VIEW_URL = `/api/views/${name}?${params}` let QUERY_VIEW_URL = `/api/views/${name}?${params}`

View File

@ -80,7 +80,7 @@ exports.save = async function(ctx) {
exports.fetchView = async function(ctx) { exports.fetchView = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) 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}`, { const response = await db.query(`database/${ctx.params.viewName}`, {
include_docs: !stats, include_docs: !stats,
group, group,
@ -89,6 +89,7 @@ exports.fetchView = async function(ctx) {
if (stats) { if (stats) {
response.rows = response.rows.map(row => ({ response.rows = response.rows.map(row => ({
group: row.key, group: row.key,
field,
...row.value, ...row.value,
avg: row.value.sum / row.value.count, avg: row.value.sum / row.value.count,
})) }))

View File

@ -9,11 +9,20 @@ const TOKEN_MAP = {
OR: "||", OR: "||",
} }
const GROUP_PROPERTY = {
group: {
type: "string",
},
}
const FIELD_PROPERTY = {
field: {
type: "string",
},
}
const SCHEMA_MAP = { const SCHEMA_MAP = {
stats: { stats: {
group: {
type: "string",
},
sum: { sum: {
type: "number", type: "number",
}, },
@ -91,13 +100,21 @@ function viewTemplate({ field, modelId, groupBy, filters = [], calculation }) {
const reduction = field ? { reduce: "_stats" } : {} const reduction = field ? { reduce: "_stats" } : {}
let schema = null
if (calculation) {
schema = groupBy
? { ...GROUP_PROPERTY, ...SCHEMA_MAP[calculation] }
: { ...FIELD_PROPERTY, ...SCHEMA_MAP[calculation] }
}
return { return {
meta: { meta: {
field, field,
modelId, modelId,
groupBy, groupBy,
filters, filters,
schema: SCHEMA_MAP[calculation], schema,
calculation, calculation,
}, },
map: `function (doc) { map: `function (doc) {

View File

@ -19,10 +19,15 @@ export default async function fetchData(datasource) {
const { field, groupBy } = datasource const { field, groupBy } = datasource
const params = new URLSearchParams() 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) 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) const response = await api.get(QUERY_VIEW_URL)
return await response.json() return await response.json()