Support template and app import in quotas + logging

This commit is contained in:
Rory Powell 2022-01-18 15:37:16 +00:00
parent 747db3daa2
commit 94cb39829c
6 changed files with 27 additions and 15 deletions

View File

@ -1,5 +1,5 @@
const { DocumentTypes } = require("../db/constants") const { DocumentTypes } = require("../db/constants")
const { getGlobalDB } = require("../tenancy") const { getGlobalDB, getTenantId } = require("../tenancy")
exports.MIGRATION_DBS = { exports.MIGRATION_DBS = {
GLOBAL_DB: "GLOBAL_DB", GLOBAL_DB: "GLOBAL_DB",
@ -29,6 +29,7 @@ exports.getMigrationsDoc = async db => {
} }
exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => { exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => {
const tenantId = getTenantId()
try { try {
let db let db
if (migrationDb === exports.MIGRATION_DBS.GLOBAL_DB) { if (migrationDb === exports.MIGRATION_DBS.GLOBAL_DB) {
@ -49,15 +50,18 @@ exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => {
return return
} }
console.log(`Performing migration: ${migrationName}`) console.log(`[Tenant: ${tenantId}] Performing migration: ${migrationName}`)
await migrateFn() await migrateFn()
console.log(`Migration complete: ${migrationName}`) console.log(`[Tenant: ${tenantId}] Migration complete: ${migrationName}`)
// mark as complete // mark as complete
doc[migrationName] = Date.now() doc[migrationName] = Date.now()
await db.put(doc) await db.put(doc)
} catch (err) { } catch (err) {
console.error(`Error performing migration: ${migrationName}: `, err) console.error(
`[Tenant: ${tenantId}] Error performing migration: ${migrationName}: `,
err
)
throw err throw err
} }
} }

View File

@ -142,12 +142,14 @@ const appPostDelete = async (ctx, usageContext) => {
} }
} }
// const appPostCreate = async (ctx, usageContext) => { const appPostCreate = async ctx => {
// if (ctx.request) { // app import & template creation
// const rowCount = await getUniqueRows([ctx.appId]).length if (ctx.request.body.useTemplate === "true") {
// await usageQuota.update(usageQuota.Properties.ROW, -rowCount) const rows = await getUniqueRows([ctx.response.body.appId])
// } const rowCount = rows ? rows.length : 0
// } await usageQuota.update(usageQuota.Properties.ROW, rowCount)
}
}
const PRE_DELETE = { const PRE_DELETE = {
[usageQuota.Properties.APPS]: appPreDelete, [usageQuota.Properties.APPS]: appPreDelete,
@ -160,5 +162,5 @@ const POST_DELETE = {
const PRE_CREATE = {} const PRE_CREATE = {}
const POST_CREATE = { const POST_CREATE = {
// [usageQuota.Properties.APPS]: appPostCreate, [usageQuota.Properties.APPS]: appPostCreate,
} }

View File

@ -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 { getAllApps } = require("@budibase/backend-core/db")
const CouchDB = require("../../db") const CouchDB = require("../../db")
const { getUsageQuotaDoc } = require("../../utilities/usageQuota") const { getUsageQuotaDoc } = require("../../utilities/usageQuota")
@ -10,6 +10,8 @@ exports.run = async () => {
const appCount = devApps ? devApps.length : 0 const appCount = devApps ? devApps.length : 0
// sync app count // sync app count
const tenantId = getTenantId()
console.log(`[Tenant: ${tenantId}] Syncing app count: ${appCount}`)
const usageDoc = await getUsageQuotaDoc(db) const usageDoc = await getUsageQuotaDoc(db)
usageDoc.usageQuota.apps = appCount usageDoc.usageQuota.apps = appCount
await db.put(usageDoc) await db.put(usageDoc)

View File

@ -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 { getAllApps } = require("@budibase/backend-core/db")
const CouchDB = require("../../db") const CouchDB = require("../../db")
const { getUsageQuotaDoc } = require("../../utilities/usageQuota") const { getUsageQuotaDoc } = require("../../utilities/usageQuota")
@ -10,9 +10,12 @@ exports.run = async () => {
const allApps = await getAllApps(CouchDB, { all: true }) const allApps = await getAllApps(CouchDB, { all: true })
const appIds = allApps ? allApps.map(app => app.appId) : [] const appIds = allApps ? allApps.map(app => app.appId) : []
const rows = await getUniqueRows(appIds) const rows = await getUniqueRows(appIds)
const rowCount = rows ? rows.length : 0
// sync row count // sync row count
const tenantId = getTenantId()
console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`)
const usageDoc = await getUsageQuotaDoc(db) const usageDoc = await getUsageQuotaDoc(db)
usageDoc.usageQuota.rows = rows.length usageDoc.usageQuota.rows = rowCount
await db.put(usageDoc) await db.put(usageDoc)
} }

View File

@ -2,7 +2,7 @@ const getTenantId = jest.fn()
jest.mock("@budibase/backend-core/tenancy", () => ({ jest.mock("@budibase/backend-core/tenancy", () => ({
getTenantId getTenantId
})) }))
const usageQuota = require("../usageQuota") const usageQuota = require("../../usageQuota")
const env = require("../../../environment") const env = require("../../../environment")
class TestConfiguration { class TestConfiguration {

View File

@ -59,6 +59,7 @@ exports.getUniqueRows = async appIds => {
try { try {
appRows.push(await getAppRows(appId)) appRows.push(await getAppRows(appId))
} catch (e) { } catch (e) {
console.error(e)
// don't error out if we can't count the app rows, just continue // don't error out if we can't count the app rows, just continue
} }
} }