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
$: columns = schema ? Object.keys(schema) : []
$: paginatedData =
data && data.length
? data.slice(

View File

@ -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}`

View File

@ -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,
}))

View File

@ -9,11 +9,20 @@ const TOKEN_MAP = {
OR: "||",
}
const SCHEMA_MAP = {
stats: {
const GROUP_PROPERTY = {
group: {
type: "string",
},
}
const FIELD_PROPERTY = {
field: {
type: "string",
},
}
const SCHEMA_MAP = {
stats: {
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) {

View File

@ -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()