1
0
Fork 0
mirror of synced 2024-09-29 16:51:33 +13:00

Single Calculation Views When Not Grouped

This commit is contained in:
cmack 2020-09-02 11:52:32 +01:00
parent b8b4f1c7b2
commit 6865fdfb28
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 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) {

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