budibase/packages/server/src/middleware/usageQuota.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

const CouchDB = require("../db")
const usageQuota = require("../utilities/usageQuota")
2021-09-24 00:25:25 +02:00
// const env = require("../environment")
// currently only counting new writes and deletes
const METHOD_MAP = {
POST: 1,
DELETE: -1,
}
const DOMAIN_MAP = {
rows: usageQuota.Properties.ROW,
upload: usageQuota.Properties.UPLOAD,
views: usageQuota.Properties.VIEW,
users: usageQuota.Properties.USER,
2021-09-23 23:40:14 +02:00
applications: usageQuota.Properties.APPS,
// this will not be updated by endpoint calls
// instead it will be updated by triggerInfo
automationRuns: usageQuota.Properties.AUTOMATION,
}
function getProperty(url) {
for (let domain of Object.keys(DOMAIN_MAP)) {
if (url.indexOf(domain) !== -1) {
return DOMAIN_MAP[domain]
}
}
}
module.exports = async (ctx, next) => {
// if in development or a self hosted cloud usage quotas should not be executed
2021-09-23 23:40:14 +02:00
// if (env.isDev() || env.SELF_HOSTED) {
// return next()
// }
const db = new CouchDB(ctx.appId)
let usage = METHOD_MAP[ctx.req.method]
const property = getProperty(ctx.req.url)
if (usage == null || property == null) {
return next()
}
// post request could be a save of a pre-existing entry
if (ctx.request.body && ctx.request.body._id && ctx.request.body._rev) {
try {
await db.get(ctx.request.body._id)
return next()
} catch (err) {
ctx.throw(404, `${ctx.request.body._id} does not exist`)
}
}
2021-03-09 16:13:14 +01:00
// update usage for uploads to be the total size
2021-09-23 23:40:14 +02:00
// if (property === usageQuota.Properties.UPLOAD) {
// const files =
// ctx.request.files.file.length > 1
// ? Array.from(ctx.request.files.file)
// : [ctx.request.files.file]
// usage = files.map(file => file.size).reduce((total, size) => total + size)
// }
try {
2021-09-23 23:40:14 +02:00
await usageQuota.update(ctx.user.tenantId, property, usage)
return next()
} catch (err) {
2021-09-23 23:40:14 +02:00
ctx.throw(400, err)
}
}