Support template and app import in quotas + logging
This commit is contained in:
parent
747db3daa2
commit
94cb39829c
|
@ -1,5 +1,5 @@
|
|||
const { DocumentTypes } = require("../db/constants")
|
||||
const { getGlobalDB } = require("../tenancy")
|
||||
const { getGlobalDB, getTenantId } = require("../tenancy")
|
||||
|
||||
exports.MIGRATION_DBS = {
|
||||
GLOBAL_DB: "GLOBAL_DB",
|
||||
|
@ -29,6 +29,7 @@ exports.getMigrationsDoc = async db => {
|
|||
}
|
||||
|
||||
exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => {
|
||||
const tenantId = getTenantId()
|
||||
try {
|
||||
let db
|
||||
if (migrationDb === exports.MIGRATION_DBS.GLOBAL_DB) {
|
||||
|
@ -49,15 +50,18 @@ exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => {
|
|||
return
|
||||
}
|
||||
|
||||
console.log(`Performing migration: ${migrationName}`)
|
||||
console.log(`[Tenant: ${tenantId}] Performing migration: ${migrationName}`)
|
||||
await migrateFn()
|
||||
console.log(`Migration complete: ${migrationName}`)
|
||||
console.log(`[Tenant: ${tenantId}] Migration complete: ${migrationName}`)
|
||||
|
||||
// mark as complete
|
||||
doc[migrationName] = Date.now()
|
||||
await db.put(doc)
|
||||
} catch (err) {
|
||||
console.error(`Error performing migration: ${migrationName}: `, err)
|
||||
console.error(
|
||||
`[Tenant: ${tenantId}] Error performing migration: ${migrationName}: `,
|
||||
err
|
||||
)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -142,12 +142,14 @@ const appPostDelete = async (ctx, usageContext) => {
|
|||
}
|
||||
}
|
||||
|
||||
// const appPostCreate = async (ctx, usageContext) => {
|
||||
// if (ctx.request) {
|
||||
// const rowCount = await getUniqueRows([ctx.appId]).length
|
||||
// await usageQuota.update(usageQuota.Properties.ROW, -rowCount)
|
||||
// }
|
||||
// }
|
||||
const appPostCreate = async ctx => {
|
||||
// app import & template creation
|
||||
if (ctx.request.body.useTemplate === "true") {
|
||||
const rows = await getUniqueRows([ctx.response.body.appId])
|
||||
const rowCount = rows ? rows.length : 0
|
||||
await usageQuota.update(usageQuota.Properties.ROW, rowCount)
|
||||
}
|
||||
}
|
||||
|
||||
const PRE_DELETE = {
|
||||
[usageQuota.Properties.APPS]: appPreDelete,
|
||||
|
@ -160,5 +162,5 @@ const POST_DELETE = {
|
|||
const PRE_CREATE = {}
|
||||
|
||||
const POST_CREATE = {
|
||||
// [usageQuota.Properties.APPS]: appPostCreate,
|
||||
[usageQuota.Properties.APPS]: appPostCreate,
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
|
||||
const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
|
||||
const { getAllApps } = require("@budibase/backend-core/db")
|
||||
const CouchDB = require("../../db")
|
||||
const { getUsageQuotaDoc } = require("../../utilities/usageQuota")
|
||||
|
@ -10,6 +10,8 @@ exports.run = async () => {
|
|||
const appCount = devApps ? devApps.length : 0
|
||||
|
||||
// sync app count
|
||||
const tenantId = getTenantId()
|
||||
console.log(`[Tenant: ${tenantId}] Syncing app count: ${appCount}`)
|
||||
const usageDoc = await getUsageQuotaDoc(db)
|
||||
usageDoc.usageQuota.apps = appCount
|
||||
await db.put(usageDoc)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
|
||||
const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
|
||||
const { getAllApps } = require("@budibase/backend-core/db")
|
||||
const CouchDB = require("../../db")
|
||||
const { getUsageQuotaDoc } = require("../../utilities/usageQuota")
|
||||
|
@ -10,9 +10,12 @@ exports.run = async () => {
|
|||
const allApps = await getAllApps(CouchDB, { all: true })
|
||||
const appIds = allApps ? allApps.map(app => app.appId) : []
|
||||
const rows = await getUniqueRows(appIds)
|
||||
const rowCount = rows ? rows.length : 0
|
||||
|
||||
// sync row count
|
||||
const tenantId = getTenantId()
|
||||
console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`)
|
||||
const usageDoc = await getUsageQuotaDoc(db)
|
||||
usageDoc.usageQuota.rows = rows.length
|
||||
usageDoc.usageQuota.rows = rowCount
|
||||
await db.put(usageDoc)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ const getTenantId = jest.fn()
|
|||
jest.mock("@budibase/backend-core/tenancy", () => ({
|
||||
getTenantId
|
||||
}))
|
||||
const usageQuota = require("../usageQuota")
|
||||
const usageQuota = require("../../usageQuota")
|
||||
const env = require("../../../environment")
|
||||
|
||||
class TestConfiguration {
|
||||
|
|
|
@ -59,6 +59,7 @@ exports.getUniqueRows = async appIds => {
|
|||
try {
|
||||
appRows.push(await getAppRows(appId))
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
// don't error out if we can't count the app rows, just continue
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue