@budibase/server 1.0.45 → 1.0.46-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/builder/assets/{index.39e01193.css → index.0df4f7ab.css} +1 -1
  2. package/builder/assets/{index.04ee727d.js → index.6eae2fa0.js} +229 -227
  3. package/builder/assets/{vendor.f9faf6ab.js → vendor.10e2ad1b.js} +12 -12
  4. package/builder/index.html +3 -3
  5. package/coverage/clover.xml +1470 -1285
  6. package/coverage/coverage-final.json +90 -84
  7. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  8. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/application.js.html +90 -90
  10. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/automation.js.html +21 -21
  12. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/component.js.html +1 -1
  15. package/coverage/lcov-report/api/controllers/datasource.js.html +16 -16
  16. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/hosting.js.html +1 -1
  18. package/coverage/lcov-report/api/controllers/index.html +1 -1
  19. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/layout.js.html +6 -6
  21. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  22. package/coverage/lcov-report/api/controllers/permission.js.html +15 -15
  23. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  24. package/coverage/lcov-report/api/controllers/query/index.js.html +19 -19
  25. package/coverage/lcov-report/api/controllers/query/validation.js.html +6 -6
  26. package/coverage/lcov-report/api/controllers/role.js.html +9 -9
  27. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  28. package/coverage/lcov-report/api/controllers/row/external.js.html +16 -16
  29. package/coverage/lcov-report/api/controllers/row/index.html +15 -15
  30. package/coverage/lcov-report/api/controllers/row/index.js.html +26 -26
  31. package/coverage/lcov-report/api/controllers/row/internal.js.html +44 -44
  32. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +8 -8
  33. package/coverage/lcov-report/api/controllers/row/utils.js.html +53 -38
  34. package/coverage/lcov-report/api/controllers/screen.js.html +7 -7
  35. package/coverage/lcov-report/api/controllers/script.js.html +4 -4
  36. package/coverage/lcov-report/api/controllers/table/external.js.html +12 -12
  37. package/coverage/lcov-report/api/controllers/table/index.html +23 -23
  38. package/coverage/lcov-report/api/controllers/table/index.js.html +57 -39
  39. package/coverage/lcov-report/api/controllers/table/internal.js.html +40 -34
  40. package/coverage/lcov-report/api/controllers/table/utils.js.html +86 -62
  41. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  42. package/coverage/lcov-report/api/controllers/user.js.html +20 -20
  43. package/coverage/lcov-report/api/controllers/view/exporters.js.html +4 -4
  44. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  45. package/coverage/lcov-report/api/controllers/view/index.js.html +14 -14
  46. package/coverage/lcov-report/api/controllers/view/utils.js.html +11 -11
  47. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +7 -7
  48. package/coverage/lcov-report/api/controllers/webhook.js.html +15 -15
  49. package/coverage/lcov-report/api/index.html +1 -1
  50. package/coverage/lcov-report/api/index.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/hosting.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/index.html +1 -1
  64. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/static.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  80. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  81. package/coverage/lcov-report/automations/actions.js.html +24 -24
  82. package/coverage/lcov-report/automations/automationUtils.js.html +5 -5
  83. package/coverage/lcov-report/automations/bullboard.js.html +16 -16
  84. package/coverage/lcov-report/automations/index.html +1 -1
  85. package/coverage/lcov-report/automations/index.js.html +1 -1
  86. package/coverage/lcov-report/automations/steps/bash.js.html +6 -6
  87. package/coverage/lcov-report/automations/steps/createRow.js.html +13 -19
  88. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  89. package/coverage/lcov-report/automations/steps/deleteRow.js.html +11 -20
  90. package/coverage/lcov-report/automations/steps/discord.js.html +7 -7
  91. package/coverage/lcov-report/automations/steps/executeQuery.js.html +6 -6
  92. package/coverage/lcov-report/automations/steps/executeScript.js.html +6 -6
  93. package/coverage/lcov-report/automations/steps/filter.js.html +7 -7
  94. package/coverage/lcov-report/automations/steps/index.html +13 -13
  95. package/coverage/lcov-report/automations/steps/integromat.js.html +5 -5
  96. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +8 -8
  97. package/coverage/lcov-report/automations/steps/queryRows.js.html +10 -10
  98. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +5 -5
  99. package/coverage/lcov-report/automations/steps/serverLog.js.html +3 -3
  100. package/coverage/lcov-report/automations/steps/slack.js.html +5 -5
  101. package/coverage/lcov-report/automations/steps/updateRow.js.html +6 -6
  102. package/coverage/lcov-report/automations/steps/utils.js.html +3 -3
  103. package/coverage/lcov-report/automations/steps/zapier.js.html +5 -5
  104. package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
  105. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
  106. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  107. package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
  108. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
  109. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
  110. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
  111. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
  112. package/coverage/lcov-report/automations/triggers.js.html +18 -18
  113. package/coverage/lcov-report/automations/utils.js.html +32 -32
  114. package/coverage/lcov-report/constants/index.html +1 -1
  115. package/coverage/lcov-report/constants/index.js.html +22 -22
  116. package/coverage/lcov-report/constants/layouts.js.html +5 -5
  117. package/coverage/lcov-report/constants/screens.js.html +4 -4
  118. package/coverage/lcov-report/db/client.js.html +18 -18
  119. package/coverage/lcov-report/db/inMemoryView.js.html +7 -7
  120. package/coverage/lcov-report/db/index.html +15 -15
  121. package/coverage/lcov-report/db/index.js.html +3 -3
  122. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +18 -18
  123. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  124. package/coverage/lcov-report/db/linkedRows/index.js.html +31 -31
  125. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +15 -15
  126. package/coverage/lcov-report/db/newid.js.html +4 -4
  127. package/coverage/lcov-report/db/utils.js.html +83 -83
  128. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  129. package/coverage/lcov-report/events/index.html +1 -1
  130. package/coverage/lcov-report/events/index.js.html +5 -5
  131. package/coverage/lcov-report/events/utils.js.html +3 -3
  132. package/coverage/lcov-report/index.html +74 -44
  133. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  134. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  135. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  136. package/coverage/lcov-report/middleware/authorized.js.html +1 -1
  137. package/coverage/lcov-report/middleware/builder.js.html +1 -1
  138. package/coverage/lcov-report/middleware/currentapp.js.html +33 -6
  139. package/coverage/lcov-report/middleware/index.html +25 -25
  140. package/coverage/lcov-report/middleware/joi-validator.js.html +3 -3
  141. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  142. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  143. package/coverage/lcov-report/middleware/usageQuota.js.html +275 -41
  144. package/coverage/lcov-report/middleware/utils.js.html +1 -1
  145. package/coverage/lcov-report/migrations/usageQuotas/index.html +141 -0
  146. package/coverage/lcov-report/migrations/usageQuotas/index.js.html +152 -0
  147. package/coverage/lcov-report/migrations/usageQuotas/syncApps.js.html +134 -0
  148. package/coverage/lcov-report/migrations/usageQuotas/syncRows.js.html +143 -0
  149. package/coverage/lcov-report/threads/automation.js.html +1 -1
  150. package/coverage/lcov-report/threads/index.html +1 -1
  151. package/coverage/lcov-report/threads/index.js.html +10 -10
  152. package/coverage/lcov-report/threads/query.js.html +1 -1
  153. package/coverage/lcov-report/threads/utils.js.html +14 -14
  154. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  155. package/coverage/lcov-report/utilities/budibaseDir.js.html +7 -7
  156. package/coverage/lcov-report/utilities/centralPath.js.html +4 -4
  157. package/coverage/lcov-report/utilities/csvParser.js.html +6 -6
  158. package/coverage/lcov-report/utilities/global.js.html +14 -14
  159. package/coverage/lcov-report/utilities/index.html +1 -1
  160. package/coverage/lcov-report/utilities/index.js.html +25 -25
  161. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +8 -8
  162. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  163. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  164. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  165. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  166. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +53 -53
  167. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +10 -10
  168. package/coverage/lcov-report/utilities/scriptRunner.js.html +5 -5
  169. package/coverage/lcov-report/utilities/security.js.html +8 -8
  170. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  171. package/coverage/lcov-report/utilities/usageQuota/index.html +141 -0
  172. package/coverage/lcov-report/utilities/usageQuota/index.js.html +356 -0
  173. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +302 -0
  174. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +134 -0
  175. package/coverage/lcov-report/utilities/users.js.html +5 -5
  176. package/coverage/lcov-report/utilities/workerRequests.js.html +31 -31
  177. package/coverage/lcov.info +2010 -1698
  178. package/dist/api/controllers/row/utils.js +12 -6
  179. package/dist/api/controllers/table/index.js +10 -4
  180. package/dist/api/controllers/table/internal.js +2 -0
  181. package/dist/api/controllers/table/utils.js +9 -2
  182. package/dist/automations/steps/createRow.js +2 -4
  183. package/dist/automations/steps/deleteRow.js +1 -4
  184. package/dist/environment.js +1 -0
  185. package/dist/integrations/base/sql.js +13 -6
  186. package/dist/integrations/utils.js +53 -8
  187. package/dist/middleware/currentapp.js +9 -0
  188. package/dist/middleware/usageQuota.js +82 -13
  189. package/dist/migrations/usageQuotas/index.js +24 -0
  190. package/dist/migrations/usageQuotas/syncApps.js +26 -0
  191. package/dist/migrations/usageQuotas/syncRows.js +29 -0
  192. package/dist/package.json +4 -4
  193. package/dist/tsconfig.tsbuildinfo +1 -1
  194. package/dist/utilities/{usageQuota.js → usageQuota/index.js} +44 -31
  195. package/dist/utilities/usageQuota/rows.js +77 -0
  196. package/dist/utilities/usageQuota/usageQuoteReset.js +16 -0
  197. package/package.json +5 -5
  198. package/src/api/controllers/row/utils.js +11 -6
  199. package/src/api/controllers/table/index.js +15 -9
  200. package/src/api/controllers/table/internal.js +2 -0
  201. package/src/api/controllers/table/utils.js +9 -1
  202. package/src/automations/steps/createRow.js +2 -4
  203. package/src/automations/steps/deleteRow.js +1 -4
  204. package/src/environment.js +1 -0
  205. package/src/integrations/base/sql.ts +14 -7
  206. package/src/integrations/tests/sql.spec.js +16 -0
  207. package/src/integrations/utils.ts +57 -10
  208. package/src/middleware/currentapp.js +9 -0
  209. package/src/middleware/tests/usageQuota.spec.js +18 -24
  210. package/src/middleware/usageQuota.js +93 -15
  211. package/src/migrations/tests/usageQuotas/index.spec.js +27 -0
  212. package/src/migrations/tests/usageQuotas/syncApps.spec.js +37 -0
  213. package/src/migrations/tests/usageQuotas/syncRows.spec.js +43 -0
  214. package/src/migrations/usageQuotas/index.js +24 -0
  215. package/src/migrations/usageQuotas/syncApps.js +18 -0
  216. package/src/migrations/usageQuotas/syncRows.js +21 -0
  217. package/src/utilities/tests/usageQuota/usageQuota.spec.js +72 -0
  218. package/src/utilities/usageQuota/index.js +92 -0
  219. package/src/utilities/usageQuota/rows.js +74 -0
  220. package/src/utilities/usageQuota/usageQuoteReset.js +18 -0
  221. package/src/utilities/usageQuota.js +0 -73
@@ -1,11 +1,5 @@
1
1
  jest.mock("../../db")
2
2
  jest.mock("../../utilities/usageQuota")
3
- jest.mock("../../environment", () => ({
4
- isTest: () => true,
5
- isProd: () => false,
6
- isDev: () => true,
7
- _set: () => {},
8
- }))
9
3
  jest.mock("@budibase/backend-core/tenancy", () => ({
10
4
  getTenantId: () => "testing123"
11
5
  }))
@@ -29,9 +23,10 @@ class TestConfiguration {
29
23
  },
30
24
  req: {
31
25
  method: "POST",
32
- url: "/rows"
26
+ url: "/applications"
33
27
  }
34
28
  }
29
+ usageQuota.useQuotas = () => true
35
30
  }
36
31
 
37
32
  executeMiddleware() {
@@ -113,7 +108,6 @@ describe("usageQuota middleware", () => {
113
108
 
114
109
  it("calculates and persists the correct usage quota for the relevant action", async () => {
115
110
  config.setUrl("/rows")
116
- config.setProd(true)
117
111
 
118
112
  await config.executeMiddleware()
119
113
 
@@ -121,20 +115,20 @@ describe("usageQuota middleware", () => {
121
115
  expect(config.next).toHaveBeenCalled()
122
116
  })
123
117
 
124
- it("calculates the correct file size from a file upload call and adds it to quota", async () => {
125
- config.setUrl("/upload")
126
- config.setProd(true)
127
- config.setFiles([
128
- {
129
- size: 100
130
- },
131
- {
132
- size: 10000
133
- },
134
- ])
135
- await config.executeMiddleware()
136
-
137
- expect(usageQuota.update).toHaveBeenCalledWith("storage", 10100)
138
- expect(config.next).toHaveBeenCalled()
139
- })
118
+ // it("calculates the correct file size from a file upload call and adds it to quota", async () => {
119
+ // config.setUrl("/upload")
120
+ // config.setProd(true)
121
+ // config.setFiles([
122
+ // {
123
+ // size: 100
124
+ // },
125
+ // {
126
+ // size: 10000
127
+ // },
128
+ // ])
129
+ // await config.executeMiddleware()
130
+
131
+ // expect(usageQuota.update).toHaveBeenCalledWith("storage", 10100)
132
+ // expect(config.next).toHaveBeenCalled()
133
+ // })
140
134
  })
@@ -1,14 +1,11 @@
1
1
  const CouchDB = require("../db")
2
2
  const usageQuota = require("../utilities/usageQuota")
3
- const env = require("../environment")
4
- const { getTenantId } = require("@budibase/backend-core/tenancy")
3
+ const { getUniqueRows } = require("../utilities/usageQuota/rows")
5
4
  const {
6
5
  isExternalTable,
7
6
  isRowId: isExternalRowId,
8
7
  } = require("../integrations/utils")
9
-
10
- // tenants without limits
11
- const EXCLUDED_TENANTS = ["bb", "default", "bbtest", "bbstaging"]
8
+ const migration = require("../migrations/usageQuotas")
12
9
 
13
10
  // currently only counting new writes and deletes
14
11
  const METHOD_MAP = {
@@ -18,13 +15,13 @@ const METHOD_MAP = {
18
15
 
19
16
  const DOMAIN_MAP = {
20
17
  rows: usageQuota.Properties.ROW,
21
- upload: usageQuota.Properties.UPLOAD,
22
- views: usageQuota.Properties.VIEW,
23
- users: usageQuota.Properties.USER,
18
+ // upload: usageQuota.Properties.UPLOAD, // doesn't work yet
19
+ // views: usageQuota.Properties.VIEW, // doesn't work yet
20
+ // users: usageQuota.Properties.USER, // doesn't work yet
24
21
  applications: usageQuota.Properties.APPS,
25
22
  // this will not be updated by endpoint calls
26
23
  // instead it will be updated by triggerInfo
27
- automationRuns: usageQuota.Properties.AUTOMATION,
24
+ // automationRuns: usageQuota.Properties.AUTOMATION, // doesn't work yet
28
25
  }
29
26
 
30
27
  function getProperty(url) {
@@ -36,10 +33,7 @@ function getProperty(url) {
36
33
  }
37
34
 
38
35
  module.exports = async (ctx, next) => {
39
- const tenantId = getTenantId()
40
-
41
- // if in development or a self hosted cloud usage quotas should not be executed
42
- if (env.isDev() || env.SELF_HOSTED || EXCLUDED_TENANTS.includes(tenantId)) {
36
+ if (!usageQuota.useQuotas()) {
43
37
  return next()
44
38
  }
45
39
 
@@ -80,9 +74,93 @@ module.exports = async (ctx, next) => {
80
74
  usage = files.map(file => file.size).reduce((total, size) => total + size)
81
75
  }
82
76
  try {
83
- await usageQuota.update(property, usage)
84
- return next()
77
+ await migration.run()
78
+ await performRequest(ctx, next, property, usage)
85
79
  } catch (err) {
86
80
  ctx.throw(400, err)
87
81
  }
88
82
  }
83
+
84
+ const performRequest = async (ctx, next, property, usage) => {
85
+ const usageContext = {
86
+ skipNext: false,
87
+ skipUsage: false,
88
+ [usageQuota.Properties.APPS]: {},
89
+ }
90
+
91
+ if (usage === -1) {
92
+ if (PRE_DELETE[property]) {
93
+ await PRE_DELETE[property](ctx, usageContext)
94
+ }
95
+ } else {
96
+ if (PRE_CREATE[property]) {
97
+ await PRE_CREATE[property](ctx, usageContext)
98
+ }
99
+ }
100
+
101
+ // run the request
102
+ if (!usageContext.skipNext) {
103
+ await usageQuota.update(property, usage, { dryRun: true })
104
+ await next()
105
+ }
106
+
107
+ if (usage === -1) {
108
+ if (POST_DELETE[property]) {
109
+ await POST_DELETE[property](ctx, usageContext)
110
+ }
111
+ } else {
112
+ if (POST_CREATE[property]) {
113
+ await POST_CREATE[property](ctx, usageContext)
114
+ }
115
+ }
116
+
117
+ // update the usage
118
+ if (!usageContext.skipUsage) {
119
+ await usageQuota.update(property, usage)
120
+ }
121
+ }
122
+
123
+ const appPreDelete = async (ctx, usageContext) => {
124
+ if (ctx.query.unpublish) {
125
+ // don't run usage decrement for unpublish
126
+ usageContext.skipUsage = true
127
+ return
128
+ }
129
+
130
+ // store the row count to delete
131
+ const rows = await getUniqueRows([ctx.appId])
132
+ if (rows.length) {
133
+ usageContext[usageQuota.Properties.APPS] = { rowCount: rows.length }
134
+ }
135
+ }
136
+
137
+ const appPostDelete = async (ctx, usageContext) => {
138
+ // delete the app rows from usage
139
+ const rowCount = usageContext[usageQuota.Properties.APPS].rowCount
140
+ if (rowCount) {
141
+ await usageQuota.update(usageQuota.Properties.ROW, -rowCount)
142
+ }
143
+ }
144
+
145
+ const appPostCreate = async ctx => {
146
+ // app import & template creation
147
+ if (ctx.request.body.useTemplate === "true") {
148
+ const rows = await getUniqueRows([ctx.response.body.appId])
149
+ const rowCount = rows ? rows.length : 0
150
+ await usageQuota.update(usageQuota.Properties.ROW, rowCount)
151
+ }
152
+ }
153
+
154
+ const PRE_DELETE = {
155
+ [usageQuota.Properties.APPS]: appPreDelete,
156
+ }
157
+
158
+ const POST_DELETE = {
159
+ [usageQuota.Properties.APPS]: appPostDelete,
160
+ }
161
+
162
+ const PRE_CREATE = {}
163
+
164
+ const POST_CREATE = {
165
+ [usageQuota.Properties.APPS]: appPostCreate,
166
+ }
@@ -0,0 +1,27 @@
1
+ const env = require("../../../environment")
2
+ const TestConfig = require("../../../tests/utilities/TestConfiguration")
3
+
4
+ const syncApps = jest.fn()
5
+ const syncRows = jest.fn()
6
+
7
+ jest.mock("../../usageQuotas/syncApps", () => ({ run: syncApps }) )
8
+ jest.mock("../../usageQuotas/syncRows", () => ({ run: syncRows }) )
9
+
10
+ const migrations = require("../../usageQuotas")
11
+
12
+ describe("run", () => {
13
+ let config = new TestConfig(false)
14
+
15
+ beforeEach(async () => {
16
+ await config.init()
17
+ env._set("USE_QUOTAS", 1)
18
+ })
19
+
20
+ afterAll(config.end)
21
+
22
+ it("runs the required migrations", async () => {
23
+ await migrations.run()
24
+ expect(syncApps).toHaveBeenCalledTimes(1)
25
+ expect(syncRows).toHaveBeenCalledTimes(1)
26
+ })
27
+ })
@@ -0,0 +1,37 @@
1
+ const { getGlobalDB } = require("@budibase/backend-core/tenancy")
2
+ const TestConfig = require("../../../tests/utilities/TestConfiguration")
3
+ const { getUsageQuotaDoc, update, Properties } = require("../../../utilities/usageQuota")
4
+ const syncApps = require("../../usageQuotas/syncApps")
5
+ const env = require("../../../environment")
6
+
7
+ describe("syncApps", () => {
8
+ let config = new TestConfig(false)
9
+
10
+ beforeEach(async () => {
11
+ await config.init()
12
+ env._set("USE_QUOTAS", 1)
13
+ })
14
+
15
+ afterAll(config.end)
16
+
17
+ it("runs successfully", async () => {
18
+ // create the usage quota doc and mock usages
19
+ const db = getGlobalDB()
20
+ await getUsageQuotaDoc(db)
21
+ await update(Properties.APPS, 3)
22
+
23
+ let usageDoc = await getUsageQuotaDoc(db)
24
+ expect(usageDoc.usageQuota.apps).toEqual(3)
25
+
26
+ // create an extra app to test the migration
27
+ await config.createApp("quota-test")
28
+
29
+ // migrate
30
+ await syncApps.run()
31
+
32
+ // assert the migration worked
33
+ usageDoc = await getUsageQuotaDoc(db)
34
+ expect(usageDoc.usageQuota.apps).toEqual(2)
35
+ })
36
+ })
37
+
@@ -0,0 +1,43 @@
1
+ const { getGlobalDB } = require("@budibase/backend-core/tenancy")
2
+ const TestConfig = require("../../../tests/utilities/TestConfiguration")
3
+ const { getUsageQuotaDoc, update, Properties } = require("../../../utilities/usageQuota")
4
+ const syncRows = require("../../usageQuotas/syncRows")
5
+ const env = require("../../../environment")
6
+
7
+ describe("syncRows", () => {
8
+ let config = new TestConfig(false)
9
+
10
+ beforeEach(async () => {
11
+ await config.init()
12
+ env._set("USE_QUOTAS", 1)
13
+ })
14
+
15
+ afterAll(config.end)
16
+
17
+ it("runs successfully", async () => {
18
+ // create the usage quota doc and mock usages
19
+ const db = getGlobalDB()
20
+ await getUsageQuotaDoc(db)
21
+ await update(Properties.ROW, 300)
22
+
23
+ let usageDoc = await getUsageQuotaDoc(db)
24
+ expect(usageDoc.usageQuota.rows).toEqual(300)
25
+
26
+ // app 1
27
+ await config.createTable()
28
+ await config.createRow()
29
+ // app 2
30
+ await config.createApp()
31
+ await config.createTable()
32
+ await config.createRow()
33
+ await config.createRow()
34
+
35
+ // migrate
36
+ await syncRows.run()
37
+
38
+ // assert the migration worked
39
+ usageDoc = await getUsageQuotaDoc(db)
40
+ expect(usageDoc.usageQuota.rows).toEqual(3)
41
+ })
42
+ })
43
+
@@ -0,0 +1,24 @@
1
+ const {
2
+ MIGRATIONS,
3
+ MIGRATION_DBS,
4
+ migrateIfRequired,
5
+ } = require("@budibase/backend-core/migrations")
6
+ const { useQuotas } = require("../../utilities/usageQuota")
7
+ const syncApps = require("./syncApps")
8
+ const syncRows = require("./syncRows")
9
+
10
+ exports.run = async () => {
11
+ if (!useQuotas()) {
12
+ return
13
+ }
14
+
15
+ // Jan 2022
16
+ await migrateIfRequired(
17
+ MIGRATION_DBS.GLOBAL_DB,
18
+ MIGRATIONS.QUOTAS_1,
19
+ async () => {
20
+ await syncApps.run()
21
+ await syncRows.run()
22
+ }
23
+ )
24
+ }
@@ -0,0 +1,18 @@
1
+ const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
2
+ const { getAllApps } = require("@budibase/backend-core/db")
3
+ const CouchDB = require("../../db")
4
+ const { getUsageQuotaDoc } = require("../../utilities/usageQuota")
5
+
6
+ exports.run = async () => {
7
+ const db = getGlobalDB()
8
+ // get app count
9
+ const devApps = await getAllApps(CouchDB, { dev: true })
10
+ const appCount = devApps ? devApps.length : 0
11
+
12
+ // sync app count
13
+ const tenantId = getTenantId()
14
+ console.log(`[Tenant: ${tenantId}] Syncing app count: ${appCount}`)
15
+ const usageDoc = await getUsageQuotaDoc(db)
16
+ usageDoc.usageQuota.apps = appCount
17
+ await db.put(usageDoc)
18
+ }
@@ -0,0 +1,21 @@
1
+ const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
2
+ const { getAllApps } = require("@budibase/backend-core/db")
3
+ const CouchDB = require("../../db")
4
+ const { getUsageQuotaDoc } = require("../../utilities/usageQuota")
5
+ const { getUniqueRows } = require("../../utilities/usageQuota/rows")
6
+
7
+ exports.run = async () => {
8
+ const db = getGlobalDB()
9
+ // get all rows in all apps
10
+ const allApps = await getAllApps(CouchDB, { all: true })
11
+ const appIds = allApps ? allApps.map(app => app.appId) : []
12
+ const rows = await getUniqueRows(appIds)
13
+ const rowCount = rows ? rows.length : 0
14
+
15
+ // sync row count
16
+ const tenantId = getTenantId()
17
+ console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`)
18
+ const usageDoc = await getUsageQuotaDoc(db)
19
+ usageDoc.usageQuota.rows = rowCount
20
+ await db.put(usageDoc)
21
+ }
@@ -0,0 +1,72 @@
1
+ const getTenantId = jest.fn()
2
+ jest.mock("@budibase/backend-core/tenancy", () => ({
3
+ getTenantId
4
+ }))
5
+ const usageQuota = require("../../usageQuota")
6
+ const env = require("../../../environment")
7
+
8
+ class TestConfiguration {
9
+ constructor() {
10
+ this.enableQuotas()
11
+ }
12
+
13
+ enableQuotas = () => {
14
+ env.USE_QUOTAS = 1
15
+ }
16
+
17
+ disableQuotas = () => {
18
+ env.USE_QUOTAS = null
19
+ }
20
+
21
+ setTenantId = (tenantId) => {
22
+ getTenantId.mockReturnValue(tenantId)
23
+ }
24
+
25
+ setExcludedTenants = (tenants) => {
26
+ env.EXCLUDE_QUOTAS_TENANTS = tenants
27
+ }
28
+
29
+ reset = () => {
30
+ this.disableQuotas()
31
+ this.setExcludedTenants(null)
32
+ }
33
+ }
34
+
35
+ describe("usageQuota", () => {
36
+ let config
37
+
38
+ beforeEach(() => {
39
+ config = new TestConfiguration()
40
+ })
41
+
42
+ afterEach(() => {
43
+ config.reset()
44
+ })
45
+
46
+ describe("useQuotas", () => {
47
+ it("works when no settings have been provided", () => {
48
+ config.reset()
49
+ expect(usageQuota.useQuotas()).toBe(false)
50
+ })
51
+ it("honours USE_QUOTAS setting", () => {
52
+ config.disableQuotas()
53
+ expect(usageQuota.useQuotas()).toBe(false)
54
+
55
+ config.enableQuotas()
56
+ expect(usageQuota.useQuotas()).toBe(true)
57
+ })
58
+ it("honours EXCLUDE_QUOTAS_TENANTS setting", () => {
59
+ config.setTenantId("test")
60
+
61
+ // tenantId is in the list
62
+ config.setExcludedTenants("test, test2, test2")
63
+ expect(usageQuota.useQuotas()).toBe(false)
64
+ config.setExcludedTenants("test,test2,test2")
65
+ expect(usageQuota.useQuotas()).toBe(false)
66
+
67
+ // tenantId is not in the list
68
+ config.setTenantId("other")
69
+ expect(usageQuota.useQuotas()).toBe(true)
70
+ })
71
+ })
72
+ })
@@ -0,0 +1,92 @@
1
+ const env = require("../../environment")
2
+ const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
3
+ const {
4
+ StaticDatabases,
5
+ generateNewUsageQuotaDoc,
6
+ } = require("@budibase/backend-core/db")
7
+
8
+ exports.useQuotas = () => {
9
+ // check if quotas are enabled
10
+ if (env.USE_QUOTAS) {
11
+ // check if there are any tenants without limits
12
+ if (env.EXCLUDE_QUOTAS_TENANTS) {
13
+ const excludedTenants = env.EXCLUDE_QUOTAS_TENANTS.replace(
14
+ /\s/g,
15
+ ""
16
+ ).split(",")
17
+ const tenantId = getTenantId()
18
+ if (excludedTenants.includes(tenantId)) {
19
+ return false
20
+ }
21
+ }
22
+ return true
23
+ }
24
+ return false
25
+ }
26
+
27
+ exports.Properties = {
28
+ ROW: "rows",
29
+ UPLOAD: "storage", // doesn't work yet
30
+ VIEW: "views", // doesn't work yet
31
+ USER: "users", // doesn't work yet
32
+ AUTOMATION: "automationRuns", // doesn't work yet
33
+ APPS: "apps",
34
+ EMAILS: "emails", // doesn't work yet
35
+ }
36
+
37
+ exports.getUsageQuotaDoc = async db => {
38
+ let quota
39
+ try {
40
+ quota = await db.get(StaticDatabases.GLOBAL.docs.usageQuota)
41
+ } catch (err) {
42
+ // doc doesn't exist. Create it
43
+ quota = generateNewUsageQuotaDoc()
44
+ const response = await db.put(quota)
45
+ quota._rev = response.rev
46
+ }
47
+
48
+ return quota
49
+ }
50
+
51
+ /**
52
+ * Given a specified tenantId this will add to the usage object for the specified property.
53
+ * @param {string} property The property which is to be added to (within the nested usageQuota object).
54
+ * @param {number} usage The amount (this can be negative) to adjust the number by.
55
+ * @returns {Promise<void>} When this completes the API key will now be up to date - the quota period may have
56
+ * also been reset after this call.
57
+ */
58
+ exports.update = async (property, usage, opts = { dryRun: false }) => {
59
+ if (!exports.useQuotas()) {
60
+ return
61
+ }
62
+
63
+ try {
64
+ const db = getGlobalDB()
65
+ const quota = await exports.getUsageQuotaDoc(db)
66
+
67
+ // increment the quota
68
+ quota.usageQuota[property] += usage
69
+
70
+ if (
71
+ quota.usageQuota[property] > quota.usageLimits[property] &&
72
+ usage > 0 // allow for decrementing usage when the quota is already exceeded
73
+ ) {
74
+ throw new Error(
75
+ `You have exceeded your usage quota of ${quota.usageLimits[property]} ${property}.`
76
+ )
77
+ }
78
+
79
+ if (quota.usageQuota[property] < 0) {
80
+ // never go negative if the quota has previously been exceeded
81
+ quota.usageQuota[property] = 0
82
+ }
83
+
84
+ // update the usage quotas
85
+ if (!opts.dryRun) {
86
+ await db.put(quota)
87
+ }
88
+ } catch (err) {
89
+ console.error(`Error updating usage quotas for ${property}`, err)
90
+ throw err
91
+ }
92
+ }
@@ -0,0 +1,74 @@
1
+ const { getRowParams, USER_METDATA_PREFIX } = require("../../db/utils")
2
+ const CouchDB = require("../../db")
3
+ const { isDevAppID, getDevelopmentAppID } = require("@budibase/backend-core/db")
4
+
5
+ const ROW_EXCLUSIONS = [USER_METDATA_PREFIX]
6
+
7
+ const getAppPairs = appIds => {
8
+ // collect the app ids into dev / prod pairs
9
+ // keyed by the dev app id
10
+ const pairs = {}
11
+ for (let appId of appIds) {
12
+ const devId = getDevelopmentAppID(appId)
13
+ if (!pairs[devId]) {
14
+ pairs[devId] = {}
15
+ }
16
+ if (isDevAppID(appId)) {
17
+ pairs[devId].devId = appId
18
+ } else {
19
+ pairs[devId].prodId = appId
20
+ }
21
+ }
22
+ return pairs
23
+ }
24
+
25
+ const getAppRows = async appId => {
26
+ const appDb = new CouchDB(appId)
27
+ const response = await appDb.allDocs(
28
+ getRowParams(null, null, {
29
+ include_docs: false,
30
+ })
31
+ )
32
+ return response.rows
33
+ .map(r => r.id)
34
+ .filter(id => {
35
+ for (let exclusion of ROW_EXCLUSIONS) {
36
+ if (id.startsWith(exclusion)) {
37
+ return false
38
+ }
39
+ }
40
+ return true
41
+ })
42
+ }
43
+
44
+ /**
45
+ * Return a set of all rows in the given app ids.
46
+ * The returned rows will be unique on a per dev/prod app basis.
47
+ * Rows duplicates may exist across apps due to data import so they are not filtered out.
48
+ */
49
+ exports.getUniqueRows = async appIds => {
50
+ let uniqueRows = []
51
+ const pairs = getAppPairs(appIds)
52
+
53
+ for (let pair of Object.values(pairs)) {
54
+ let appRows = []
55
+ for (let appId of [pair.devId, pair.prodId]) {
56
+ if (!appId) {
57
+ continue
58
+ }
59
+ try {
60
+ appRows.push(await getAppRows(appId))
61
+ } catch (e) {
62
+ console.error(e)
63
+ // don't error out if we can't count the app rows, just continue
64
+ }
65
+ }
66
+
67
+ // ensure uniqueness on a per app pair basis
68
+ // this can't be done on all rows because app import results in
69
+ // duplicate row ids across apps
70
+ uniqueRows = uniqueRows.concat(...new Set(appRows))
71
+ }
72
+
73
+ return uniqueRows
74
+ }
@@ -0,0 +1,18 @@
1
+ // UNUSED CODE
2
+ // Preserved for future use
3
+
4
+ /* eslint-disable no-unused-vars */
5
+
6
+ function getNewQuotaReset() {
7
+ return Date.now() + 2592000000
8
+ }
9
+
10
+ function resetQuotasIfRequired(quota) {
11
+ // Check if the quota needs reset
12
+ if (Date.now() >= quota.quotaReset) {
13
+ quota.quotaReset = getNewQuotaReset()
14
+ for (let prop of Object.keys(quota.usageQuota)) {
15
+ quota.usageQuota[prop] = 0
16
+ }
17
+ }
18
+ }