2020-10-28 21:35:06 +01:00
|
|
|
const env = require("../environment")
|
2020-10-07 18:56:47 +02:00
|
|
|
const { apiKeyTable } = require("../db/dynamoClient")
|
|
|
|
|
2020-10-09 12:32:25 +02:00
|
|
|
const DEFAULT_USAGE = {
|
2020-10-09 20:10:28 +02:00
|
|
|
rows: 0,
|
2020-10-09 12:32:25 +02:00
|
|
|
storage: 0,
|
|
|
|
views: 0,
|
|
|
|
automationRuns: 0,
|
|
|
|
users: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_PLAN = {
|
2020-10-09 20:10:28 +02:00
|
|
|
rows: 1000,
|
2020-10-09 12:32:25 +02:00
|
|
|
// 1 GB
|
|
|
|
storage: 8589934592,
|
|
|
|
views: 10,
|
|
|
|
automationRuns: 100,
|
|
|
|
users: 10000,
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:56:47 +02:00
|
|
|
function buildUpdateParams(key, property, usage) {
|
|
|
|
return {
|
|
|
|
primary: key,
|
2020-10-09 12:32:25 +02:00
|
|
|
condition:
|
2020-10-09 22:42:20 +02:00
|
|
|
"attribute_exists(#quota) AND attribute_exists(#limits) AND #quota.#prop < #limits.#prop AND #quotaReset > :now",
|
2020-10-07 18:56:47 +02:00
|
|
|
expression: "ADD #quota.#prop :usage",
|
|
|
|
names: {
|
|
|
|
"#quota": "usageQuota",
|
|
|
|
"#prop": property,
|
2020-10-08 18:34:41 +02:00
|
|
|
"#limits": "usageLimits",
|
2020-10-07 18:56:47 +02:00
|
|
|
"#quotaReset": "quotaReset",
|
|
|
|
},
|
|
|
|
values: {
|
|
|
|
":usage": usage,
|
|
|
|
":now": Date.now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:32:25 +02:00
|
|
|
function getNewQuotaReset() {
|
|
|
|
return Date.now() + 2592000000
|
|
|
|
}
|
2020-10-07 18:56:47 +02:00
|
|
|
|
|
|
|
exports.Properties = {
|
2020-10-09 20:10:28 +02:00
|
|
|
ROW: "rows",
|
2020-10-07 18:56:47 +02:00
|
|
|
UPLOAD: "storage",
|
|
|
|
VIEW: "views",
|
|
|
|
USER: "users",
|
|
|
|
AUTOMATION: "automationRuns",
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.getAPIKey = async appId => {
|
2021-03-24 19:21:23 +01:00
|
|
|
if (!env.USE_QUOTAS) {
|
2021-02-17 14:40:14 +01:00
|
|
|
return { apiKey: null }
|
|
|
|
}
|
2020-10-07 18:56:47 +02:00
|
|
|
return apiKeyTable.get({ primary: appId })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a specified API key this will add to the usage object for the specified property.
|
|
|
|
* @param {string} apiKey The API key which is to be updated.
|
|
|
|
* @param {string} property The property which is to be added to (within the nested usageQuota object).
|
|
|
|
* @param {number} usage The amount (this can be negative) to adjust the number by.
|
|
|
|
* @returns {Promise<void>} When this completes the API key will now be up to date - the quota period may have
|
|
|
|
* also been reset after this call.
|
|
|
|
*/
|
|
|
|
exports.update = async (apiKey, property, usage) => {
|
2021-03-24 19:21:23 +01:00
|
|
|
if (!env.USE_QUOTAS) {
|
2020-10-07 18:56:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await apiKeyTable.update(buildUpdateParams(apiKey, property, usage))
|
|
|
|
} catch (err) {
|
2020-10-09 21:44:35 +02:00
|
|
|
// conditional check means the condition failed, need to check why
|
2020-10-08 18:34:41 +02:00
|
|
|
if (err.code === "ConditionalCheckFailedException") {
|
2020-10-07 18:56:47 +02:00
|
|
|
// get the API key so we can check it
|
2020-10-08 18:34:41 +02:00
|
|
|
const keyObj = await apiKeyTable.get({ primary: apiKey })
|
2020-10-09 12:32:25 +02:00
|
|
|
// the usage quota or usage limits didn't exist
|
|
|
|
if (keyObj && (keyObj.usageQuota == null || keyObj.usageLimits == null)) {
|
2020-10-09 21:44:35 +02:00
|
|
|
keyObj.usageQuota =
|
|
|
|
keyObj.usageQuota == null ? DEFAULT_USAGE : keyObj.usageQuota
|
|
|
|
keyObj.usageLimits =
|
|
|
|
keyObj.usageLimits == null ? DEFAULT_PLAN : keyObj.usageLimits
|
2020-10-09 12:32:25 +02:00
|
|
|
keyObj.quotaReset = getNewQuotaReset()
|
|
|
|
await apiKeyTable.put({ item: keyObj })
|
|
|
|
return
|
|
|
|
}
|
2021-07-09 18:50:01 +02:00
|
|
|
// we have in fact breached the reset period
|
2020-10-09 12:32:25 +02:00
|
|
|
else if (keyObj && keyObj.quotaReset <= Date.now()) {
|
2020-10-07 18:56:47 +02:00
|
|
|
// update the quota reset period and reset the values for all properties
|
2020-10-09 12:32:25 +02:00
|
|
|
keyObj.quotaReset = getNewQuotaReset()
|
2020-10-08 18:34:41 +02:00
|
|
|
for (let prop of Object.keys(keyObj.usageQuota)) {
|
2020-10-07 18:56:47 +02:00
|
|
|
if (prop === property) {
|
2020-10-08 18:34:41 +02:00
|
|
|
keyObj.usageQuota[prop] = usage > 0 ? usage : 0
|
2020-10-07 18:56:47 +02:00
|
|
|
} else {
|
2020-10-08 18:34:41 +02:00
|
|
|
keyObj.usageQuota[prop] = 0
|
2020-10-07 18:56:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-08 18:34:41 +02:00
|
|
|
await apiKeyTable.put({ item: keyObj })
|
|
|
|
return
|
2020-10-07 18:56:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 22:42:20 +02:00
|
|
|
throw err
|
2020-10-07 18:56:47 +02:00
|
|
|
}
|
|
|
|
}
|