From 1f56d31ebb604887d6598974c1e37470eebb3461 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Thu, 5 May 2022 08:32:14 +0100 Subject: [PATCH] App backfill (wip) --- package.json | 4 +- packages/backend-core/package.json | 2 +- .../analytics/{analytics.js => Analytics.ts} | 44 +++++++---------- .../src/analytics/PosthogClient.ts | 27 ++++++++++ packages/backend-core/src/analytics/index.js | 4 -- packages/backend-core/src/analytics/index.ts | 4 ++ .../backend-core/src/analytics/posthog.js | 21 -------- .../src/events/{events.js => events.ts} | 9 ++-- .../src/events/handlers/account.ts | 8 +-- .../backend-core/src/events/handlers/app.ts | 24 ++++----- .../backend-core/src/events/handlers/auth.ts | 14 +++--- .../src/events/handlers/automation.ts | 14 +++--- .../src/events/handlers/datasource.ts | 8 +-- .../backend-core/src/events/handlers/email.ts | 6 +-- .../src/events/handlers/layout.ts | 6 +-- .../src/events/handlers/license.ts | 12 ++--- .../backend-core/src/events/handlers/org.ts | 12 ++--- .../backend-core/src/events/handlers/query.ts | 12 ++--- .../backend-core/src/events/handlers/role.ts | 12 ++--- .../backend-core/src/events/handlers/row.ts | 4 +- .../src/events/handlers/screen.ts | 6 +-- .../backend-core/src/events/handlers/serve.ts | 8 +-- .../backend-core/src/events/handlers/table.ts | 14 +++--- .../backend-core/src/events/handlers/user.ts | 28 +++++------ .../backend-core/src/events/handlers/view.ts | 22 ++++----- .../src/events/{index.js => index.ts} | 0 packages/backend-core/src/index.ts | 49 +++++++++++++------ packages/server/src/app.ts | 2 +- .../{event_app_backfill.ts => app.ts} | 4 +- .../migrations/functions/backfill/app/app.ts | 20 ++++---- .../backfill/{event_backfill.ts => global.ts} | 2 - .../migrations/functions/backfill/index.ts | 2 + packages/server/src/migrations/index.ts | 8 +-- packages/types/src/core/hosting.ts | 4 ++ packages/types/src/core/index.ts | 1 + .../types/src/documents/account/account.ts | 7 ++- .../types/src/events/{events.ts => event.ts} | 8 ++- packages/types/src/events/index.ts | 2 +- packages/types/src/index.ts | 1 + 39 files changed, 237 insertions(+), 198 deletions(-) rename packages/backend-core/src/analytics/{analytics.js => Analytics.ts} (57%) create mode 100644 packages/backend-core/src/analytics/PosthogClient.ts delete mode 100644 packages/backend-core/src/analytics/index.js create mode 100644 packages/backend-core/src/analytics/index.ts delete mode 100644 packages/backend-core/src/analytics/posthog.js rename packages/backend-core/src/events/{events.js => events.ts} (51%) rename packages/backend-core/src/events/{index.js => index.ts} (100%) rename packages/server/src/migrations/functions/backfill/{event_app_backfill.ts => app.ts} (59%) rename packages/server/src/migrations/functions/backfill/{event_backfill.ts => global.ts} (62%) create mode 100644 packages/server/src/migrations/functions/backfill/index.ts create mode 100644 packages/types/src/core/hosting.ts create mode 100644 packages/types/src/core/index.ts rename packages/types/src/events/{events.ts => event.ts} (97%) diff --git a/package.json b/package.json index 727104d830..294b15488d 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,8 @@ "kill-server": "kill-port 4001 4002", "kill-all": "yarn run kill-builder && yarn run kill-server", "dev": "yarn run kill-all && lerna link && lerna run --parallel dev:builder --concurrency 1", - "dev:noserver": "yarn run kill-builder && lerna link && lerna run dev:stack:up && lerna run --parallel dev:builder --concurrency 1 --ignore @budibase/server --ignore @budibase/worker", - "dev:server": "yarn run kill-server && lerna run --parallel dev:builder --concurrency 1 --scope @budibase/worker --scope @budibase/server", + "dev:noserver": "yarn run kill-builder && lerna link && lerna run dev:stack:up && lerna run --parallel dev:builder --concurrency 1 --ignore @budibase/backend-core --ignore @budibase/server --ignore @budibase/worker", + "dev:server": "yarn run kill-server && lerna run --parallel dev:builder --concurrency 1 --scope @budibase/backend-core --scope @budibase/worker --scope @budibase/server", "test": "lerna run test", "lint:eslint": "eslint packages", "lint:prettier": "prettier --check \"packages/**/*.{js,ts,svelte}\"", diff --git a/packages/backend-core/package.json b/packages/backend-core/package.json index 5ff88b6664..c9a581c73e 100644 --- a/packages/backend-core/package.json +++ b/packages/backend-core/package.json @@ -8,7 +8,7 @@ "license": "GPL-3.0", "scripts": { "build": "rimraf dist/ && tsc -p tsconfig.build.json", - "dev:builder": "rimraf dist/ && tsc -p tsconfig.build.json --watch", + "dev:builder": "rimraf dist/ && tsc -p tsconfig.build.json --watch --preserveWatchOutput", "test": "jest", "test:watch": "jest --watchAll" }, diff --git a/packages/backend-core/src/analytics/analytics.js b/packages/backend-core/src/analytics/Analytics.ts similarity index 57% rename from packages/backend-core/src/analytics/analytics.js rename to packages/backend-core/src/analytics/Analytics.ts index f7fc1360db..e2468c964b 100644 --- a/packages/backend-core/src/analytics/analytics.js +++ b/packages/backend-core/src/analytics/Analytics.ts @@ -1,32 +1,24 @@ -const PosthogClient = require("./posthog") -const env = require("../environment") -const { getTenantId } = require("../context") - -const IdentityType = { - TENANT: "tenant", - USER: "user", - ACCOUNT: "account", -} - -const Hosting = { - CLOUD: "cloud", - SELF: "self", -} +import PosthogClient from "./PosthogClient" +import env from "../environment" +import { getTenantId } from "../context" +import { Account, Hosting, Event, IdentityType } from "@budibase/types" class Analytics { + isEnabled: boolean + posthog: PosthogClient | undefined + constructor() { // check enabled before init - this.isEnabled = !!(!env.SELF_HOSTED && env.ENABLE_ANALYTICS) + this.isEnabled = !!env.ENABLE_ANALYTICS // TODO: use db flag instead if (!this.isEnabled) return - - this.posthog = new PosthogClient(process.env.POSTHOG_TOKEN) + this.posthog = new PosthogClient(env.POSTHOG_TOKEN) } enabled() { return this.isEnabled } - identify(type, id, hosting) { + identify(type: IdentityType, id: string, hosting?: Hosting) { if (!this.isEnabled) return const tenantId = getTenantId() if (!hosting) { @@ -35,12 +27,12 @@ class Analytics { const properties = { type, hosting, - tenant: tenantId, + tenantId, } - this.posthog.identify(id, properties) + this.posthog!.identify(id, properties) } - identifyUser(userId) { + identifyUser(userId: string) { this.identify(IdentityType.USER, userId) } @@ -54,23 +46,23 @@ class Analytics { this.identify(IdentityType.TENANT, distinctId) } - identifyAccount(account) { + identifyAccount(account: Account) { const distinctId = account.accountId const hosting = account.hosting this.identify(IdentityType.ACCOUNT, distinctId, hosting) } - captureEvent(eventName, properties) { + captureEvent(event: Event, properties: any) { if (!this.isEnabled) return // TODO: get the user id from context const userId = "TESTING_USER_ID" - this.posthog.capture(userId, eventName, properties) + this.posthog!.capture(userId, event, properties) } shutdown() { if (!this.isEnabled) return - this.posthog.shutdown() + this.posthog!.shutdown() } } -module.exports = Analytics +export default Analytics diff --git a/packages/backend-core/src/analytics/PosthogClient.ts b/packages/backend-core/src/analytics/PosthogClient.ts new file mode 100644 index 0000000000..f02ba3e7fb --- /dev/null +++ b/packages/backend-core/src/analytics/PosthogClient.ts @@ -0,0 +1,27 @@ +import PostHog from "posthog-node" +import { Event } from "@budibase/types" + +class PosthogClient { + posthog: PostHog + + constructor(token: string | undefined) { + if (!token) { + throw new Error("Posthog token is not defined") + } + this.posthog = new PostHog(token) + } + + identify(distinctId: string, properties: any) { + this.posthog.identify({ distinctId, properties }) + } + + capture(userId: string, event: Event, properties: any) { + this.posthog.capture({ distinctId: userId, event, properties }) + } + + shutdown() { + this.posthog.shutdown() + } +} + +export default PosthogClient diff --git a/packages/backend-core/src/analytics/index.js b/packages/backend-core/src/analytics/index.js deleted file mode 100644 index 5a097552e9..0000000000 --- a/packages/backend-core/src/analytics/index.js +++ /dev/null @@ -1,4 +0,0 @@ -const Analytics = require("./analytics") - -const analytics = new Analytics() -module.exports = analytics diff --git a/packages/backend-core/src/analytics/index.ts b/packages/backend-core/src/analytics/index.ts new file mode 100644 index 0000000000..9be70a1af4 --- /dev/null +++ b/packages/backend-core/src/analytics/index.ts @@ -0,0 +1,4 @@ +import Analytics from "./Analytics" + +const analytics = new Analytics() +export default analytics diff --git a/packages/backend-core/src/analytics/posthog.js b/packages/backend-core/src/analytics/posthog.js deleted file mode 100644 index afe1fb42af..0000000000 --- a/packages/backend-core/src/analytics/posthog.js +++ /dev/null @@ -1,21 +0,0 @@ -const PostHog = require("posthog-node") - -class PosthogClient { - constructor(token) { - this.posthog = new PostHog(token) - } - - identify(distinctId, properties) { - this.posthog.identify({ distinctId, properties }) - } - - capture(userId, event, properties) { - this.posthog.capture({ distinctId: userId, event, properties }) - } - - shutdown() { - this.posthog.shutdown() - } -} - -module.exports = PosthogClient diff --git a/packages/backend-core/src/events/events.js b/packages/backend-core/src/events/events.ts similarity index 51% rename from packages/backend-core/src/events/events.js rename to packages/backend-core/src/events/events.ts index eb00fc5d47..4ae916db16 100644 --- a/packages/backend-core/src/events/events.js +++ b/packages/backend-core/src/events/events.ts @@ -1,16 +1,17 @@ import { getTenantId } from "../context" -import { captureEvent } from "../analytics" +import analytics from "../analytics" +import { Event } from "@budibase/types" -const logEvent = messsage => { +const logEvent = (messsage: string) => { const tenantId = getTenantId() const userId = getTenantId() // TODO console.log(`[audit] [tenant=${tenantId}] [user=${userId}] ${messsage}`) } -export const processEvent = (event, properties) => { +export const processEvent = (event: Event, properties: any) => { // logging logEvent(event) // analytics - captureEvent(event, properties) + analytics.captureEvent(event, properties) } diff --git a/packages/backend-core/src/events/handlers/account.ts b/packages/backend-core/src/events/handlers/account.ts index 9577edc43b..0d3c9ba0c0 100644 --- a/packages/backend-core/src/events/handlers/account.ts +++ b/packages/backend-core/src/events/handlers/account.ts @@ -1,17 +1,17 @@ import { processEvent } from "../events" -import { Events, Account } from "@budibase/types" +import { Event, Account } from "@budibase/types" export function created(account: Account) { const properties = {} - processEvent(Events.ACCOUNT_CREATED, properties) + processEvent(Event.ACCOUNT_CREATED, properties) } export function deleted(account: Account) { const properties = {} - processEvent(Events.ACCOUNT_DELETED, properties) + processEvent(Event.ACCOUNT_DELETED, properties) } export function verified(account: Account) { const properties = {} - processEvent(Events.ACCOUNT_VERIFIED, properties) + processEvent(Event.ACCOUNT_VERIFIED, properties) } diff --git a/packages/backend-core/src/events/handlers/app.ts b/packages/backend-core/src/events/handlers/app.ts index 904f8efeda..197c778679 100644 --- a/packages/backend-core/src/events/handlers/app.ts +++ b/packages/backend-core/src/events/handlers/app.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, App, AppCreatedEvent, AppUpdatedEvent, @@ -17,57 +17,57 @@ import { export function created(app: App) { const properties: AppCreatedEvent = {} - processEvent(Events.APP_CREATED, properties) + processEvent(Event.APP_CREATED, properties) } export function updated(app: App) { const properties: AppUpdatedEvent = {} - processEvent(Events.APP_UPDATED, properties) + processEvent(Event.APP_UPDATED, properties) } export function deleted(app: App) { const properties: AppDeletedEvent = {} - processEvent(Events.APP_DELETED, properties) + processEvent(Event.APP_DELETED, properties) } export function published(app: App) { const properties: AppPublishedEvent = {} - processEvent(Events.APP_PUBLISHED, properties) + processEvent(Event.APP_PUBLISHED, properties) } export function unpublished(app: App) { const properties: AppUnpublishedEvent = {} - processEvent(Events.APP_UNPUBLISHED, properties) + processEvent(Event.APP_UNPUBLISHED, properties) } export function fileImported(app: App) { const properties: AppFileImportedEvent = {} - processEvent(Events.APP_FILE_IMPORTED, properties) + processEvent(Event.APP_FILE_IMPORTED, properties) } export function templateImported(templateKey: string) { const properties: AppTemplateImportedEvent = { templateKey, } - processEvent(Events.APP_TEMPLATE_IMPORTED, properties) + processEvent(Event.APP_TEMPLATE_IMPORTED, properties) } export function versionUpdated(app: App) { const properties: AppVersionUpdatedEvent = {} - processEvent(Events.APP_VERSION_UPDATED, properties) + processEvent(Event.APP_VERSION_UPDATED, properties) } export function versionReverted(app: App) { const properties: AppVersionRevertedEvent = {} - processEvent(Events.APP_VERSION_REVERTED, properties) + processEvent(Event.APP_VERSION_REVERTED, properties) } export function reverted(app: App) { const properties: AppRevertedEvent = {} - processEvent(Events.APP_REVERTED, properties) + processEvent(Event.APP_REVERTED, properties) } export function exported(app: App) { const properties: AppExportedEvent = {} - processEvent(Events.APP_EXPORTED, properties) + processEvent(Event.APP_EXPORTED, properties) } diff --git a/packages/backend-core/src/events/handlers/auth.ts b/packages/backend-core/src/events/handlers/auth.ts index 8d74a9a103..d925d1a340 100644 --- a/packages/backend-core/src/events/handlers/auth.ts +++ b/packages/backend-core/src/events/handlers/auth.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, LoginEvent, LoginSource, LogoutEvent, @@ -15,38 +15,38 @@ export function login(source: LoginSource) { const properties: LoginEvent = { source, } - processEvent(Events.AUTH_LOGIN, properties) + processEvent(Event.AUTH_LOGIN, properties) } export function logout() { const properties: LogoutEvent = {} - processEvent(Events.AUTH_LOGOUT, properties) + processEvent(Event.AUTH_LOGOUT, properties) } export function SSOCreated(type: SSOType) { const properties: SSOCreatedEvent = { type, } - processEvent(Events.AUTH_SSO_CREATED, properties) + processEvent(Event.AUTH_SSO_CREATED, properties) } export function SSOUpdated(type: SSOType) { const properties: SSOUpdatedEvent = { type, } - processEvent(Events.AUTH_SSO_UPDATED, properties) + processEvent(Event.AUTH_SSO_UPDATED, properties) } export function SSOActivated(type: SSOType) { const properties: SSOActivatedEvent = { type, } - processEvent(Events.AUTH_SSO_ACTIVATED, properties) + processEvent(Event.AUTH_SSO_ACTIVATED, properties) } export function SSODeactivated(type: SSOType) { const properties: SSODeactivatedEvent = { type, } - processEvent(Events.AUTH_SSO_DEACTIVATED, properties) + processEvent(Event.AUTH_SSO_DEACTIVATED, properties) } diff --git a/packages/backend-core/src/events/handlers/automation.ts b/packages/backend-core/src/events/handlers/automation.ts index 22986bf935..fa9b71a8cf 100644 --- a/packages/backend-core/src/events/handlers/automation.ts +++ b/packages/backend-core/src/events/handlers/automation.ts @@ -1,7 +1,7 @@ import { processEvent } from "../events" import { Automation, - Events, + Event, AutomationStep, AutomationTrigger, AutomationCreatedEvent, @@ -14,17 +14,17 @@ import { export function created(automation: Automation) { const properties: AutomationCreatedEvent = {} - processEvent(Events.AUTOMATION_CREATED, properties) + processEvent(Event.AUTOMATION_CREATED, properties) } export function deleted(automation: Automation) { const properties: AutomationDeletedEvent = {} - processEvent(Events.AUTOMATION_DELETED, properties) + processEvent(Event.AUTOMATION_DELETED, properties) } export function tested(automation: Automation) { const properties: AutomationTestedEvent = {} - processEvent(Events.AUTOMATION_TESTED, properties) + processEvent(Event.AUTOMATION_TESTED, properties) } // TODO @@ -35,12 +35,12 @@ export function tested(automation: Automation) { export function stepCreated(automation: Automation, step: AutomationStep) { const properties: AutomationStepCreatedEvent = {} - processEvent(Events.AUTOMATION_STEP_CREATED, properties) + processEvent(Event.AUTOMATION_STEP_CREATED, properties) } export function stepDeleted(automation: Automation, step: AutomationStep) { const properties: AutomationStepDeletedEvent = {} - processEvent(Events.AUTOMATION_STEP_DELETED, properties) + processEvent(Event.AUTOMATION_STEP_DELETED, properties) } export function triggerUpdated( @@ -48,5 +48,5 @@ export function triggerUpdated( trigger: AutomationTrigger ) { const properties: AutomationTriggerUpdatedEvent = {} - processEvent(Events.AUTOMATION_TRIGGER_UPDATED, properties) + processEvent(Event.AUTOMATION_TRIGGER_UPDATED, properties) } diff --git a/packages/backend-core/src/events/handlers/datasource.ts b/packages/backend-core/src/events/handlers/datasource.ts index c32d4292ba..9ebe73928e 100644 --- a/packages/backend-core/src/events/handlers/datasource.ts +++ b/packages/backend-core/src/events/handlers/datasource.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, Datasource, DatasourceCreatedEvent, DatasourceUpdatedEvent, @@ -9,15 +9,15 @@ import { export function created(datasource: Datasource) { const properties: DatasourceCreatedEvent = {} - processEvent(Events.DATASOURCE_CREATED, properties) + processEvent(Event.DATASOURCE_CREATED, properties) } export function updated(datasource: Datasource) { const properties: DatasourceUpdatedEvent = {} - processEvent(Events.DATASOURCE_UPDATED, properties) + processEvent(Event.DATASOURCE_UPDATED, properties) } export function deleted(datasource: Datasource) { const properties: DatasourceDeletedEvent = {} - processEvent(Events.DATASOURCE_DELETED, properties) + processEvent(Event.DATASOURCE_DELETED, properties) } diff --git a/packages/backend-core/src/events/handlers/email.ts b/packages/backend-core/src/events/handlers/email.ts index d9f0320540..ff1783b682 100644 --- a/packages/backend-core/src/events/handlers/email.ts +++ b/packages/backend-core/src/events/handlers/email.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, SMTPConfig, SMTPCreatedEvent, SMTPUpdatedEvent, @@ -8,10 +8,10 @@ import { export function SMTPCreated(config: SMTPConfig) { const properties: SMTPCreatedEvent = {} - processEvent(Events.EMAIL_SMTP_CREATED, properties) + processEvent(Event.EMAIL_SMTP_CREATED, properties) } export function SMTPUpdated(config: SMTPConfig) { const properties: SMTPUpdatedEvent = {} - processEvent(Events.EMAIL_SMTP_UPDATED, properties) + processEvent(Event.EMAIL_SMTP_UPDATED, properties) } diff --git a/packages/backend-core/src/events/handlers/layout.ts b/packages/backend-core/src/events/handlers/layout.ts index 325f4dda50..6d4ff9f4a0 100644 --- a/packages/backend-core/src/events/handlers/layout.ts +++ b/packages/backend-core/src/events/handlers/layout.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, Layout, LayoutCreatedEvent, LayoutDeletedEvent, @@ -8,10 +8,10 @@ import { export function created(layout: Layout) { const properties: LayoutCreatedEvent = {} - processEvent(Events.LAYOUT_CREATED, properties) + processEvent(Event.LAYOUT_CREATED, properties) } export function deleted(layout: Layout) { const properties: LayoutDeletedEvent = {} - processEvent(Events.LAYOUT_DELETED, properties) + processEvent(Event.LAYOUT_DELETED, properties) } diff --git a/packages/backend-core/src/events/handlers/license.ts b/packages/backend-core/src/events/handlers/license.ts index f33deb2018..91b97c00ae 100644 --- a/packages/backend-core/src/events/handlers/license.ts +++ b/packages/backend-core/src/events/handlers/license.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, License, LicenseActivatedEvent, LicenseDowngradedEvent, @@ -12,25 +12,25 @@ import { // TODO export function updgraded(license: License) { const properties: LicenseUpgradedEvent = {} - processEvent(Events.LICENSE_UPGRADED, properties) + processEvent(Event.LICENSE_UPGRADED, properties) } // TODO export function downgraded(license: License) { const properties: LicenseDowngradedEvent = {} - processEvent(Events.LICENSE_DOWNGRADED, properties) + processEvent(Event.LICENSE_DOWNGRADED, properties) } // TODO export function updated(license: License) { const properties: LicenseUpdatedEvent = {} - processEvent(Events.LICENSE_UPDATED, properties) + processEvent(Event.LICENSE_UPDATED, properties) } // TODO export function activated(license: License) { const properties: LicenseActivatedEvent = {} - processEvent(Events.LICENSE_ACTIVATED, properties) + processEvent(Event.LICENSE_ACTIVATED, properties) } // TODO @@ -39,5 +39,5 @@ export function quotaExceeded(quotaName: string, value: number) { name: quotaName, value, } - processEvent(Events.LICENSE_QUOTA_EXCEEDED, properties) + processEvent(Event.LICENSE_QUOTA_EXCEEDED, properties) } diff --git a/packages/backend-core/src/events/handlers/org.ts b/packages/backend-core/src/events/handlers/org.ts index 3f8658f2f6..5e1fc12b80 100644 --- a/packages/backend-core/src/events/handlers/org.ts +++ b/packages/backend-core/src/events/handlers/org.ts @@ -1,30 +1,30 @@ import { processEvent } from "../events" -import { Events, VersionCheckedEvent } from "@budibase/types" +import { Event, VersionCheckedEvent } from "@budibase/types" export function nameUpdated() { const properties = {} - processEvent(Events.ORG_NAME_UPDATED, properties) + processEvent(Event.ORG_NAME_UPDATED, properties) } export function logoUpdated() { const properties = {} - processEvent(Events.ORG_LOGO_UPDATED, properties) + processEvent(Event.ORG_LOGO_UPDATED, properties) } export function platformURLUpdated() { const properties = {} - processEvent(Events.ORG_PLATFORM_URL_UPDATED, properties) + processEvent(Event.ORG_PLATFORM_URL_UPDATED, properties) } export function versionChecked(version: number) { const properties: VersionCheckedEvent = { version, } - processEvent(Events.UPDATE_VERSION_CHECKED, properties) + processEvent(Event.UPDATE_VERSION_CHECKED, properties) } // TODO export function analyticsOptOut() { const properties = {} - processEvent(Events.ANALYTICS_OPT_OUT, properties) + processEvent(Event.ANALYTICS_OPT_OUT, properties) } diff --git a/packages/backend-core/src/events/handlers/query.ts b/packages/backend-core/src/events/handlers/query.ts index bc173008bd..059381ba31 100644 --- a/packages/backend-core/src/events/handlers/query.ts +++ b/packages/backend-core/src/events/handlers/query.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, Datasource, Query, QueryCreatedEvent, @@ -14,17 +14,17 @@ import { export const created = (datasource: Datasource, query: Query) => { const properties: QueryCreatedEvent = {} - processEvent(Events.QUERY_CREATED, properties) + processEvent(Event.QUERY_CREATED, properties) } export const updated = (datasource: Datasource, query: Query) => { const properties: QueryUpdatedEvent = {} - processEvent(Events.QUERY_UPDATED, properties) + processEvent(Event.QUERY_UPDATED, properties) } export const deleted = (datasource: Datasource, query: Query) => { const properties: QueryDeletedEvent = {} - processEvent(Events.QUERY_DELETED, properties) + processEvent(Event.QUERY_DELETED, properties) } export const imported = ( @@ -33,7 +33,7 @@ export const imported = ( count: any ) => { const properties: QueryImportedEvent = {} - processEvent(Events.QUERY_IMPORT, properties) + processEvent(Event.QUERY_IMPORT, properties) } // TODO @@ -44,5 +44,5 @@ export const imported = ( export const previewed = (datasource: Datasource) => { const properties: QueryPreviewedEvent = {} - processEvent(Events.QUERY_PREVIEWED, properties) + processEvent(Event.QUERY_PREVIEWED, properties) } diff --git a/packages/backend-core/src/events/handlers/role.ts b/packages/backend-core/src/events/handlers/role.ts index e41b50389b..58abd4bdca 100644 --- a/packages/backend-core/src/events/handlers/role.ts +++ b/packages/backend-core/src/events/handlers/role.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, Role, RoleAssignedEvent, RoleCreatedEvent, @@ -14,25 +14,25 @@ import { export function created(role: Role) { const properties: RoleCreatedEvent = {} - processEvent(Events.ROLE_CREATED, properties) + processEvent(Event.ROLE_CREATED, properties) } export function updated(role: Role) { const properties: RoleUpdatedEvent = {} - processEvent(Events.ROLE_UPDATED, properties) + processEvent(Event.ROLE_UPDATED, properties) } export function deleted(role: Role) { const properties: RoleDeletedEvent = {} - processEvent(Events.ROLE_DELETED, properties) + processEvent(Event.ROLE_DELETED, properties) } export function assigned(user: User, role: Role) { const properties: RoleAssignedEvent = {} - processEvent(Events.ROLE_ASSIGNED, properties) + processEvent(Event.ROLE_ASSIGNED, properties) } export function unassigned(user: User, role: Role) { const properties: RoleUnassignedEvent = {} - processEvent(Events.ROLE_UNASSIGNED, properties) + processEvent(Event.ROLE_UNASSIGNED, properties) } diff --git a/packages/backend-core/src/events/handlers/row.ts b/packages/backend-core/src/events/handlers/row.ts index c5e34f786d..0f6cf3a00c 100644 --- a/packages/backend-core/src/events/handlers/row.ts +++ b/packages/backend-core/src/events/handlers/row.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, RowImportedEvent, RowImportFormat, Table, @@ -19,5 +19,5 @@ export const imported = ( count: number ) => { const properties: RowImportedEvent = {} - processEvent(Events.ROW_IMPORT, properties) + processEvent(Event.ROW_IMPORT, properties) } diff --git a/packages/backend-core/src/events/handlers/screen.ts b/packages/backend-core/src/events/handlers/screen.ts index eade48bf30..d152c43a9c 100644 --- a/packages/backend-core/src/events/handlers/screen.ts +++ b/packages/backend-core/src/events/handlers/screen.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, Screen, ScreenCreatedEvent, ScreenDeletedEvent, @@ -8,10 +8,10 @@ import { export function created(screen: Screen) { const properties: ScreenCreatedEvent = {} - processEvent(Events.SCREEN_CREATED, properties) + processEvent(Event.SCREEN_CREATED, properties) } export function deleted(screen: Screen) { const properties: ScreenDeletedEvent = {} - processEvent(Events.SCREEN_DELETED, properties) + processEvent(Event.SCREEN_DELETED, properties) } diff --git a/packages/backend-core/src/events/handlers/serve.ts b/packages/backend-core/src/events/handlers/serve.ts index fd7694008c..c1cb513d0b 100644 --- a/packages/backend-core/src/events/handlers/serve.ts +++ b/packages/backend-core/src/events/handlers/serve.ts @@ -2,7 +2,7 @@ import { processEvent } from "../events" import { App, BuilderServedEvent, - Events, + Event, AppPreviewServedEvent, AppServedEvent, } from "@budibase/types" @@ -11,15 +11,15 @@ import { export function servedBuilder(version: number) { const properties: BuilderServedEvent = {} - processEvent(Events.SERVED_BUILDER, properties) + processEvent(Event.SERVED_BUILDER, properties) } export function servedApp(app: App) { const properties: AppServedEvent = {} - processEvent(Events.SERVED_APP, properties) + processEvent(Event.SERVED_APP, properties) } export function servedAppPreview(app: App) { const properties: AppPreviewServedEvent = {} - processEvent(Events.SERVED_APP_PREVIEW, properties) + processEvent(Event.SERVED_APP_PREVIEW, properties) } diff --git a/packages/backend-core/src/events/handlers/table.ts b/packages/backend-core/src/events/handlers/table.ts index 5ebadd14fd..ca0109a8ea 100644 --- a/packages/backend-core/src/events/handlers/table.ts +++ b/packages/backend-core/src/events/handlers/table.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, TableExportFormat, TableImportFormat, Table, @@ -15,31 +15,31 @@ import { export function created(table: Table) { const properties: TableCreatedEvent = {} - processEvent(Events.TABLE_CREATED, properties) + processEvent(Event.TABLE_CREATED, properties) } export function updated(table: Table) { const properties: TableUpdatedEvent = {} - processEvent(Events.TABLE_UPDATED, properties) + processEvent(Event.TABLE_UPDATED, properties) } export function deleted(table: Table) { const properties: TableDeletedEvent = {} - processEvent(Events.TABLE_DELETED, properties) + processEvent(Event.TABLE_DELETED, properties) } export function exported(table: Table, format: TableExportFormat) { const properties: TableExportedEvent = {} - processEvent(Events.TABLE_EXPORTED, properties) + processEvent(Event.TABLE_EXPORTED, properties) } export function imported(table: Table, format: TableImportFormat) { const properties: TableImportedEvent = {} - processEvent(Events.TABLE_IMPORTED, properties) + processEvent(Event.TABLE_IMPORTED, properties) } // TODO export function permissionUpdated() { const properties = {} - processEvent(Events.TABLE_PERMISSION_UPDATED, properties) + processEvent(Event.TABLE_PERMISSION_UPDATED, properties) } diff --git a/packages/backend-core/src/events/handlers/user.ts b/packages/backend-core/src/events/handlers/user.ts index 6328059cb1..48ae7b698a 100644 --- a/packages/backend-core/src/events/handlers/user.ts +++ b/packages/backend-core/src/events/handlers/user.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, User, UserCreatedEvent, UserDeletedEvent, @@ -19,71 +19,71 @@ import { export function created(user: User) { const properties: UserCreatedEvent = {} - processEvent(Events.USER_CREATED, properties) + processEvent(Event.USER_CREATED, properties) } export function updated(user: User) { const properties: UserUpdatedEvent = {} - processEvent(Events.USER_UPDATED, properties) + processEvent(Event.USER_UPDATED, properties) } export function deleted(user: User) { const properties: UserDeletedEvent = {} - processEvent(Events.USER_DELETED, properties) + processEvent(Event.USER_DELETED, properties) } // PERMISSIONS export function permissionAdminAssigned(user: User) { const properties: UserPermissionAssignedEvent = {} - processEvent(Events.USER_PERMISSION_ADMIN_ASSIGNED, properties) + processEvent(Event.USER_PERMISSION_ADMIN_ASSIGNED, properties) } export function permissionAdminRemoved(user: User) { const properties: UserPermissionRemovedEvent = {} - processEvent(Events.USER_PERMISSION_ADMIN_REMOVED, properties) + processEvent(Event.USER_PERMISSION_ADMIN_REMOVED, properties) } export function permissionBuilderAssigned(user: User) { const properties: UserPermissionAssignedEvent = {} - processEvent(Events.USER_PERMISSION_BUILDER_ASSIGNED, properties) + processEvent(Event.USER_PERMISSION_BUILDER_ASSIGNED, properties) } export function permissionBuilderRemoved(user: User) { const properties: UserPermissionRemovedEvent = {} - processEvent(Events.USER_PERMISSION_BUILDER_REMOVED, properties) + processEvent(Event.USER_PERMISSION_BUILDER_REMOVED, properties) } // INVITE export function invited(userInfo: any) { const properties: UserInvitedEvent = {} - processEvent(Events.USER_INVITED, properties) + processEvent(Event.USER_INVITED, properties) } export function inviteAccepted(user: User) { const properties: UserInviteAcceptedEvent = {} - processEvent(Events.USER_INVITED_ACCEPTED, properties) + processEvent(Event.USER_INVITED_ACCEPTED, properties) } // PASSWORD export function passwordForceReset(user: User) { const properties: UserPasswordForceResetEvent = {} - processEvent(Events.USER_PASSWORD_FORCE_RESET, properties) + processEvent(Event.USER_PASSWORD_FORCE_RESET, properties) } export function passwordUpdated(user: User) { const properties: UserPasswordUpdatedEvent = {} - processEvent(Events.USER_PASSWORD_UPDATED, properties) + processEvent(Event.USER_PASSWORD_UPDATED, properties) } export function passwordResetRequested(user: User) { const properties: UserPasswordResetRequestedEvent = {} - processEvent(Events.USER_PASSWORD_RESET_REQUESTED, properties) + processEvent(Event.USER_PASSWORD_RESET_REQUESTED, properties) } export function passwordReset(user: User) { const properties: UserPasswordResetEvent = {} - processEvent(Events.USER_PASSWORD_RESET, properties) + processEvent(Event.USER_PASSWORD_RESET, properties) } diff --git a/packages/backend-core/src/events/handlers/view.ts b/packages/backend-core/src/events/handlers/view.ts index 77b6c573ad..9fd3370f57 100644 --- a/packages/backend-core/src/events/handlers/view.ts +++ b/packages/backend-core/src/events/handlers/view.ts @@ -1,6 +1,6 @@ import { processEvent } from "../events" import { - Events, + Event, ViewCalculationCreatedEvent, ViewCalculationDeletedEvent, ViewCalculationUpdatedEvent, @@ -20,50 +20,50 @@ import { export function created(view: View) { const properties: ViewCreatedEvent = {} - processEvent(Events.VIEW_CREATED, properties) + processEvent(Event.VIEW_CREATED, properties) } export function updated(view: View) { const properties: ViewUpdatedEvent = {} - processEvent(Events.VIEW_UPDATED, properties) + processEvent(Event.VIEW_UPDATED, properties) } export function deleted() { const properties: ViewDeletedEvent = {} - processEvent(Events.VIEW_DELETED, properties) + processEvent(Event.VIEW_DELETED, properties) } export function exported(table: Table, format: TableExportFormat) { const properties: ViewExportedEvent = {} - processEvent(Events.VIEW_EXPORTED, properties) + processEvent(Event.VIEW_EXPORTED, properties) } export function filterCreated() { const properties: ViewFilterCreatedEvent = {} - processEvent(Events.VIEW_FILTER_CREATED, properties) + processEvent(Event.VIEW_FILTER_CREATED, properties) } export function filterUpdated() { const properties: ViewFilterUpdatedEvent = {} - processEvent(Events.VIEW_FILTER_UPDATED, properties) + processEvent(Event.VIEW_FILTER_UPDATED, properties) } export function filterDeleted() { const properties: ViewFilterDeletedEvent = {} - processEvent(Events.VIEW_FILTER_DELETED, properties) + processEvent(Event.VIEW_FILTER_DELETED, properties) } export function calculationCreated() { const properties: ViewCalculationCreatedEvent = {} - processEvent(Events.VIEW_CALCULATION_CREATED, properties) + processEvent(Event.VIEW_CALCULATION_CREATED, properties) } export function calculationUpdated() { const properties: ViewCalculationUpdatedEvent = {} - processEvent(Events.VIEW_CALCULATION_UPDATED, properties) + processEvent(Event.VIEW_CALCULATION_UPDATED, properties) } export function calculationDeleted() { const properties: ViewCalculationDeletedEvent = {} - processEvent(Events.VIEW_CALCULATION_DELETED, properties) + processEvent(Event.VIEW_CALCULATION_DELETED, properties) } diff --git a/packages/backend-core/src/events/index.js b/packages/backend-core/src/events/index.ts similarity index 100% rename from packages/backend-core/src/events/index.js rename to packages/backend-core/src/events/index.ts diff --git a/packages/backend-core/src/index.ts b/packages/backend-core/src/index.ts index dab77d130a..3638c8fd0d 100644 --- a/packages/backend-core/src/index.ts +++ b/packages/backend-core/src/index.ts @@ -2,6 +2,23 @@ import db from "./db" import errors from "./errors" import * as events from "./events" import * as migrations from "./migrations" +import * as users from "./users" +import env from "./environment" +import accounts from "./cloud/accounts" +import tenancy from "./tenancy" +import featureFlags from "./featureFlags" +import analytics from "./analytics" +import sessions from "./security/sessions" +import deprovisioning from "./context/deprovision" + +// outer packages +import dbPkg from "../db" +import redis from "../redis" +import objectStore from "../objectStore" +import utils from "../utils" +import cache from "../cache" +import auth from "../auth" +import constants from "../constants" export = { init(opts: any = {}) { @@ -9,24 +26,24 @@ export = { }, // some default exports from the library, however these ideally shouldn't // be used, instead the syntax require("@budibase/backend-core/db") should be used - StaticDatabases: require("./db/utils").StaticDatabases, - db: require("../db"), - redis: require("../redis"), - objectStore: require("../objectStore"), - utils: require("../utils"), - users: require("./users"), - cache: require("../cache"), - auth: require("../auth"), - constants: require("../constants"), + StaticDatabases: dbPkg.StaticDatabases, + db: dbPkg, + redis, + objectStore, + utils, + users, + cache, + auth, + constants, migrations, errors, ...errors.errors, - env: require("./environment"), - accounts: require("./cloud/accounts"), - tenancy: require("./tenancy"), - featureFlags: require("./featureFlags"), + env, + accounts, + tenancy, + featureFlags, events, - analytics: require("./analytics"), - sessions: require("./security/sessions"), - deprovisioning: require("./context/deprovision"), + analytics, + sessions, + deprovisioning, } diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 4060749629..cdd087d68d 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -102,7 +102,7 @@ process.on("SIGTERM", () => { // run migrations on startup if not done via http // not recommended in a clustered environment -if (!env.HTTP_MIGRATIONS) { +if (!env.HTTP_MIGRATIONS && !env.isTest()) { migrations.migrate().catch(err => { console.error("Error performing migrations. Exiting.\n", err) shutdown() diff --git a/packages/server/src/migrations/functions/backfill/event_app_backfill.ts b/packages/server/src/migrations/functions/backfill/app.ts similarity index 59% rename from packages/server/src/migrations/functions/backfill/event_app_backfill.ts rename to packages/server/src/migrations/functions/backfill/app.ts index 5b46959b5b..6080f75b1f 100644 --- a/packages/server/src/migrations/functions/backfill/event_app_backfill.ts +++ b/packages/server/src/migrations/functions/backfill/app.ts @@ -8,4 +8,6 @@ import * as app from "./app/app" * Backfill app events. */ -export const run = async (db: any) => {} +export const run = async (appDb: any) => { + await app.backfill(appDb) +} diff --git a/packages/server/src/migrations/functions/backfill/app/app.ts b/packages/server/src/migrations/functions/backfill/app/app.ts index 6ebfc48297..ab195636cc 100644 --- a/packages/server/src/migrations/functions/backfill/app/app.ts +++ b/packages/server/src/migrations/functions/backfill/app/app.ts @@ -1,10 +1,12 @@ -export const backfillAppCreated = () => {} +import { events, db } from "@budibase/backend-core" +import { App } from "@budibase/types" -export const backfillAppPublished = () => {} - -// APP_CREATED = "app:created", -// APP_PUBLISHED = "app:published", - -// Maybe -// APP_TEMPLATE_IMPORTED = "app:template:imported", -// APP_FILE_IMPORTED = "app:file:imported", +export const backfill = async (appDb: any) => { + const app: App = await appDb.get(db.DocumentTypes.APP_METADATA) + if (db.isDevAppID(app.appId)) { + events.app.created(app) + } + if (db.isProdAppID(app.appId)) { + events.app.published(app) + } +} diff --git a/packages/server/src/migrations/functions/backfill/event_backfill.ts b/packages/server/src/migrations/functions/backfill/global.ts similarity index 62% rename from packages/server/src/migrations/functions/backfill/event_backfill.ts rename to packages/server/src/migrations/functions/backfill/global.ts index d010c1c149..b28cf48d99 100644 --- a/packages/server/src/migrations/functions/backfill/event_backfill.ts +++ b/packages/server/src/migrations/functions/backfill/global.ts @@ -1,5 +1,3 @@ -import * as syncPublishedApps from "../usageQuotas/syncPublishedApps" - /** * Date: * May 2022 diff --git a/packages/server/src/migrations/functions/backfill/index.ts b/packages/server/src/migrations/functions/backfill/index.ts new file mode 100644 index 0000000000..fb9859a581 --- /dev/null +++ b/packages/server/src/migrations/functions/backfill/index.ts @@ -0,0 +1,2 @@ +export * as app from "./app" +export * as global from "./global" diff --git a/packages/server/src/migrations/index.ts b/packages/server/src/migrations/index.ts index bf2e8f6aca..991cde650d 100644 --- a/packages/server/src/migrations/index.ts +++ b/packages/server/src/migrations/index.ts @@ -6,6 +6,7 @@ import * as quota1 from "./functions/quotas1" import * as appUrls from "./functions/appUrls" import * as developerQuota from "./functions/developerQuota" import * as publishedAppsQuota from "./functions/publishedAppsQuota" +import * as backfill from "./functions/backfill" export interface Migration { type: string @@ -59,13 +60,14 @@ export const MIGRATIONS: Migration[] = [ }, { type: migrations.MIGRATION_TYPES.GLOBAL, - name: "event_backfill", - fn: publishedAppsQuota.run, + name: "event_global_backfill", + fn: backfill.global.run, }, { type: migrations.MIGRATION_TYPES.APP, name: "event_app_backfill", - fn: publishedAppsQuota.run, + opts: { all: true }, + fn: backfill.app.run, }, ] diff --git a/packages/types/src/core/hosting.ts b/packages/types/src/core/hosting.ts new file mode 100644 index 0000000000..44d46caa56 --- /dev/null +++ b/packages/types/src/core/hosting.ts @@ -0,0 +1,4 @@ +export enum Hosting { + CLOUD = "cloud", + SELF = "self", +} diff --git a/packages/types/src/core/index.ts b/packages/types/src/core/index.ts new file mode 100644 index 0000000000..11d05c91f4 --- /dev/null +++ b/packages/types/src/core/index.ts @@ -0,0 +1 @@ +export * from "./hosting" diff --git a/packages/types/src/documents/account/account.ts b/packages/types/src/documents/account/account.ts index bfafd9fd85..3822d63d58 100644 --- a/packages/types/src/documents/account/account.ts +++ b/packages/types/src/documents/account/account.ts @@ -1 +1,6 @@ -export interface Account {} +import { Hosting } from "../../core" + +export interface Account { + accountId: string + hosting: Hosting +} diff --git a/packages/types/src/events/events.ts b/packages/types/src/events/event.ts similarity index 97% rename from packages/types/src/events/events.ts rename to packages/types/src/events/event.ts index 9c6a6031e2..2e1f052297 100644 --- a/packages/types/src/events/events.ts +++ b/packages/types/src/events/event.ts @@ -1,4 +1,4 @@ -export enum Events { +export enum Event { // USER USER_CREATED = "user:created", USER_UPDATED = "user:updated", @@ -140,6 +140,12 @@ export enum Events { ACCOUNT_VERIFIED = "account:verified", } +export enum IdentityType { + TENANT = "tenant", + USER = "user", + ACCOUNT = "account", +} + export type RowImportFormat = "csv" export type TableExportFormat = "json" | "csv" export type TableImportFormat = "csv" diff --git a/packages/types/src/events/index.ts b/packages/types/src/events/index.ts index 22a3f9af8d..a47ab8104e 100644 --- a/packages/types/src/events/index.ts +++ b/packages/types/src/events/index.ts @@ -3,7 +3,7 @@ export * from "./auth" export * from "./automation" export * from "./config" export * from "./datasource" -export * from "./events" +export * from "./event" export * from "./layout" export * from "./license" export * from "./org" diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index b6c01a2fe5..b0c3e10d88 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -1,3 +1,4 @@ export * from "./documents" export * from "./events" export * from "./licensing" +export * from "./core"