Default to historical timestamp

This commit is contained in:
Rory Powell 2022-06-01 14:44:12 +01:00
parent bf9c6cbc42
commit b570563d41
22 changed files with 68 additions and 29 deletions

View File

@ -12,7 +12,10 @@ import {
AutomationsRunEvent, AutomationsRunEvent,
} from "@budibase/types" } from "@budibase/types"
export async function created(automation: Automation, timestamp?: string) { export async function created(
automation: Automation,
timestamp?: string | number
) {
const properties: AutomationCreatedEvent = { const properties: AutomationCreatedEvent = {
appId: automation.appId, appId: automation.appId,
automationId: automation._id as string, automationId: automation._id as string,
@ -62,7 +65,7 @@ export const run = async (count: number, timestamp?: string | number) => {
export async function stepCreated( export async function stepCreated(
automation: Automation, automation: Automation,
step: AutomationStep, step: AutomationStep,
timestamp?: string timestamp?: string | number
) { ) {
const properties: AutomationStepCreatedEvent = { const properties: AutomationStepCreatedEvent = {
appId: automation.appId, appId: automation.appId,

View File

@ -7,7 +7,10 @@ import {
DatasourceDeletedEvent, DatasourceDeletedEvent,
} from "@budibase/types" } from "@budibase/types"
export async function created(datasource: Datasource, timestamp?: string) { export async function created(
datasource: Datasource,
timestamp?: string | number
) {
const properties: DatasourceCreatedEvent = { const properties: DatasourceCreatedEvent = {
datasourceId: datasource._id as string, datasourceId: datasource._id as string,
source: datasource.source, source: datasource.source,

View File

@ -6,7 +6,7 @@ import {
LayoutDeletedEvent, LayoutDeletedEvent,
} from "@budibase/types" } from "@budibase/types"
export async function created(layout: Layout, timestamp?: string) { export async function created(layout: Layout, timestamp?: string | number) {
const properties: LayoutCreatedEvent = { const properties: LayoutCreatedEvent = {
layoutId: layout._id as string, layoutId: layout._id as string,
} }

View File

@ -16,7 +16,7 @@ import {
export const created = async ( export const created = async (
datasource: Datasource, datasource: Datasource,
query: Query, query: Query,
timestamp?: string timestamp?: string | number
) => { ) => {
const properties: QueryCreatedEvent = { const properties: QueryCreatedEvent = {
queryId: query._id as string, queryId: query._id as string,

View File

@ -10,7 +10,7 @@ import {
User, User,
} from "@budibase/types" } from "@budibase/types"
export async function created(role: Role, timestamp?: string) { export async function created(role: Role, timestamp?: string | number) {
const properties: RoleCreatedEvent = { const properties: RoleCreatedEvent = {
roleId: role._id as string, roleId: role._id as string,
permissionId: role.permissionId, permissionId: role.permissionId,

View File

@ -9,7 +9,7 @@ import {
/* eslint-disable */ /* eslint-disable */
export const created = async (count: number, timestamp?: string) => { export const created = async (count: number, timestamp?: string | number) => {
const properties: RowsCreatedEvent = { const properties: RowsCreatedEvent = {
count, count,
} }

View File

@ -6,7 +6,7 @@ import {
ScreenDeletedEvent, ScreenDeletedEvent,
} from "@budibase/types" } from "@budibase/types"
export async function created(screen: Screen, timestamp?: string) { export async function created(screen: Screen, timestamp?: string | number) {
const properties: ScreenCreatedEvent = { const properties: ScreenCreatedEvent = {
layoutId: screen.layoutId, layoutId: screen.layoutId,
screenId: screen._id as string, screenId: screen._id as string,

View File

@ -11,7 +11,7 @@ import {
TableImportedEvent, TableImportedEvent,
} from "@budibase/types" } from "@budibase/types"
export async function created(table: Table, timestamp?: string) { export async function created(table: Table, timestamp?: string | number) {
const properties: TableCreatedEvent = { const properties: TableCreatedEvent = {
tableId: table._id as string, tableId: table._id as string,
} }

View File

@ -19,7 +19,7 @@ import {
/* eslint-disable */ /* eslint-disable */
export async function created(view: View, timestamp?: string) { export async function created(view: View, timestamp?: string | number) {
const properties: ViewCreatedEvent = { const properties: ViewCreatedEvent = {
tableId: view.tableId, tableId: view.tableId,
} }
@ -48,7 +48,7 @@ export async function exported(table: Table, format: TableExportFormat) {
await publishEvent(Event.VIEW_EXPORTED, properties) await publishEvent(Event.VIEW_EXPORTED, properties)
} }
export async function filterCreated(view: View, timestamp?: string) { export async function filterCreated(view: View, timestamp?: string | number) {
const properties: ViewFilterCreatedEvent = { const properties: ViewFilterCreatedEvent = {
tableId: view.tableId, tableId: view.tableId,
} }
@ -69,7 +69,10 @@ export async function filterDeleted(view: View) {
await publishEvent(Event.VIEW_FILTER_DELETED, properties) await publishEvent(Event.VIEW_FILTER_DELETED, properties)
} }
export async function calculationCreated(view: View, timestamp?: string) { export async function calculationCreated(
view: View,
timestamp?: string | number
) {
const properties: ViewCalculationCreatedEvent = { const properties: ViewCalculationCreatedEvent = {
tableId: view.tableId, tableId: view.tableId,
calculation: view.calculation as ViewCalculation, calculation: view.calculation as ViewCalculation,

View File

@ -9,6 +9,7 @@ import * as global from "./global"
import { App, AppBackfillSucceededEvent, Event } from "@budibase/types" import { App, AppBackfillSucceededEvent, Event } from "@budibase/types"
import { db as dbUtils, events } from "@budibase/backend-core" import { db as dbUtils, events } from "@budibase/backend-core"
import env from "../../../environment" import env from "../../../environment"
import { DEFAULT_TIMESTAMP } from "."
const failGraceful = env.SELF_HOSTED && !env.isDev() const failGraceful = env.SELF_HOSTED && !env.isDev()
@ -59,8 +60,11 @@ export const run = async (appDb: any) => {
// events for this tenant // events for this tenant
await events.backfillCache.start(EVENTS) await events.backfillCache.start(EVENTS)
let timestamp: string | number = DEFAULT_TIMESTAMP
const app: App = await appDb.get(dbUtils.DocumentTypes.APP_METADATA) const app: App = await appDb.get(dbUtils.DocumentTypes.APP_METADATA)
const timestamp = app.createdAt as string if (app.createdAt) {
timestamp = app.createdAt as string
}
if (dbUtils.isProdAppID(app.appId)) { if (dbUtils.isProdAppID(app.appId)) {
await events.app.published(app, timestamp) await events.app.published(app, timestamp)

View File

@ -11,7 +11,7 @@ const getAutomations = async (appDb: any): Promise<Automation[]> => {
return response.rows.map((row: any) => row.doc) return response.rows.map((row: any) => row.doc)
} }
export const backfill = async (appDb: any, timestamp: string) => { export const backfill = async (appDb: any, timestamp: string | number) => {
const automations = await getAutomations(appDb) const automations = await getAutomations(appDb)
for (const automation of automations) { for (const automation of automations) {

View File

@ -11,7 +11,7 @@ const getDatasources = async (appDb: any): Promise<Datasource[]> => {
return response.rows.map((row: any) => row.doc) return response.rows.map((row: any) => row.doc)
} }
export const backfill = async (appDb: any, timestamp: string) => { export const backfill = async (appDb: any, timestamp: string | number) => {
const datasources: Datasource[] = await getDatasources(appDb) const datasources: Datasource[] = await getDatasources(appDb)
for (const datasource of datasources) { for (const datasource of datasources) {

View File

@ -11,7 +11,7 @@ const getLayouts = async (appDb: any): Promise<Layout[]> => {
return response.rows.map((row: any) => row.doc) return response.rows.map((row: any) => row.doc)
} }
export const backfill = async (appDb: any, timestamp: string) => { export const backfill = async (appDb: any, timestamp: string | number) => {
const layouts: Layout[] = await getLayouts(appDb) const layouts: Layout[] = await getLayouts(appDb)
for (const layout of layouts) { for (const layout of layouts) {

View File

@ -18,7 +18,7 @@ const getDatasource = async (
return appDb.get(datasourceId) return appDb.get(datasourceId)
} }
export const backfill = async (appDb: any, timestamp: string) => { export const backfill = async (appDb: any, timestamp: string | number) => {
const queries: Query[] = await getQueries(appDb) const queries: Query[] = await getQueries(appDb)
for (const query of queries) { for (const query of queries) {

View File

@ -11,7 +11,7 @@ const getRoles = async (appDb: any): Promise<Role[]> => {
return response.rows.map((row: any) => row.doc) return response.rows.map((row: any) => row.doc)
} }
export const backfill = async (appDb: any, timestamp: string) => { export const backfill = async (appDb: any, timestamp: string | number) => {
const roles = await getRoles(appDb) const roles = await getRoles(appDb)
for (const role of roles) { for (const role of roles) {

View File

@ -11,7 +11,7 @@ const getScreens = async (appDb: any): Promise<Screen[]> => {
return response.rows.map((row: any) => row.doc) return response.rows.map((row: any) => row.doc)
} }
export const backfill = async (appDb: any, timestamp: string) => { export const backfill = async (appDb: any, timestamp: string | number) => {
const screens = await getScreens(appDb) const screens = await getScreens(appDb)
for (const screen of screens) { for (const screen of screens) {

View File

@ -11,7 +11,7 @@ const getTables = async (appDb: any): Promise<Table[]> => {
return response.rows.map((row: any) => row.doc) return response.rows.map((row: any) => row.doc)
} }
export const backfill = async (appDb: any, timestamp: string) => { export const backfill = async (appDb: any, timestamp: string | number) => {
const tables = await getTables(appDb) const tables = await getTables(appDb)
for (const table of tables) { for (const table of tables) {

View File

@ -16,6 +16,7 @@ import {
Event, Event,
} from "@budibase/types" } from "@budibase/types"
import env from "../../../environment" import env from "../../../environment"
import { DEFAULT_TIMESTAMP } from "."
const failGraceful = env.SELF_HOSTED && !env.isDev() const failGraceful = env.SELF_HOSTED && !env.isDev()
@ -88,13 +89,16 @@ const EVENTS = [
export const run = async (db: any) => { export const run = async (db: any) => {
try { try {
const tenantId = tenancy.getTenantId() const tenantId = tenancy.getTenantId()
let installTimestamp let timestamp: string | number = DEFAULT_TIMESTAMP
const totals: any = {} const totals: any = {}
const errors: any = [] const errors: any = []
try { try {
installTimestamp = await getInstallTimestamp(db) const installTimestamp = await getInstallTimestamp(db)
if (installTimestamp) {
timestamp = installTimestamp
}
} catch (e) { } catch (e) {
handleError(e, errors) handleError(e, errors)
} }
@ -108,7 +112,7 @@ export const run = async (db: any) => {
await events.identification.identifyTenantGroup( await events.identification.identifyTenantGroup(
tenantId, tenantId,
account, account,
installTimestamp timestamp
) )
} catch (e) { } catch (e) {
handleError(e, errors) handleError(e, errors)
@ -119,7 +123,7 @@ export const run = async (db: any) => {
await events.backfillCache.start(EVENTS) await events.backfillCache.start(EVENTS)
try { try {
await configs.backfill(db, installTimestamp) await configs.backfill(db, timestamp)
} catch (e) { } catch (e) {
handleError(e, errors) handleError(e, errors)
} }

View File

@ -1,3 +1,4 @@
import { DEFAULT_TIMESTAMP } from "./../index"
import { events } from "@budibase/backend-core" import { events } from "@budibase/backend-core"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { App } from "@budibase/types" import { App } from "@budibase/types"
@ -33,8 +34,14 @@ export const backfill = async (allApps: App[]) => {
const usage = await quotas.getQuotaUsage() const usage = await quotas.getQuotaUsage()
const rows = usage.usageQuota.rows const rows = usage.usageQuota.rows
const rowsTimestamp = getOldestCreatedAt(allApps) let timestamp: string | number = DEFAULT_TIMESTAMP
await events.rows.created(rows, rowsTimestamp)
const oldestAppTimestamp = getOldestCreatedAt(allApps)
if (oldestAppTimestamp) {
timestamp = oldestAppTimestamp
}
await events.rows.created(rows, timestamp)
for (const [monthString, quotas] of Object.entries(usage.monthly)) { for (const [monthString, quotas] of Object.entries(usage.monthly)) {
if (monthString === "current") { if (monthString === "current") {

View File

@ -1,5 +1,6 @@
import { events, db as dbUtils } from "@budibase/backend-core" import { events, db as dbUtils } from "@budibase/backend-core"
import { User, CloudAccount } from "@budibase/types" import { User, CloudAccount } from "@budibase/types"
import { DEFAULT_TIMESTAMP } from ".."
// manually define user doc params - normally server doesn't read users from the db // manually define user doc params - normally server doesn't read users from the db
const getUserParams = (props: any) => { const getUserParams = (props: any) => {
@ -22,7 +23,10 @@ export const backfill = async (
const users = await getUsers(globalDb) const users = await getUsers(globalDb)
for (const user of users) { for (const user of users) {
const timestamp = user.createdAt as number let timestamp: string | number = DEFAULT_TIMESTAMP
if (user.createdAt) {
timestamp = user.createdAt
}
await events.identification.identifyUser(user, account, timestamp) await events.identification.identifyUser(user, account, timestamp)
await events.user.created(user, timestamp) await events.user.created(user, timestamp)

View File

@ -1,3 +1,7 @@
export * as app from "./app" export * as app from "./app"
export * as global from "./global" export * as global from "./global"
export * as installation from "./installation" export * as installation from "./installation"
// historical events are free in posthog - make sure we default to a
// historical time if no other can be found
export const DEFAULT_TIMESTAMP = new Date(2022, 0, 1).getTime()

View File

@ -1,3 +1,4 @@
import { DEFAULT_TIMESTAMP } from "./index"
import { events, tenancy, installation } from "@budibase/backend-core" import { events, tenancy, installation } from "@budibase/backend-core"
import { Installation } from "@budibase/types" import { Installation } from "@budibase/types"
import * as global from "./global" import * as global from "./global"
@ -28,11 +29,17 @@ export const run = async () => {
// need to use the default tenant to try to get the installation time // need to use the default tenant to try to get the installation time
await tenancy.doInTenant(tenancy.DEFAULT_TENANT_ID, async () => { await tenancy.doInTenant(tenancy.DEFAULT_TENANT_ID, async () => {
const db = tenancy.getGlobalDB() const db = tenancy.getGlobalDB()
const installTimestamp = (await global.getInstallTimestamp(db)) as number let timestamp: string | number = DEFAULT_TIMESTAMP
const installTimestamp = await global.getInstallTimestamp(db)
if (installTimestamp) {
timestamp = installTimestamp
}
const install: Installation = await installation.getInstall() const install: Installation = await installation.getInstall()
await events.identification.identifyInstallationGroup( await events.identification.identifyInstallationGroup(
install.installId, install.installId,
installTimestamp timestamp
) )
}) })
await events.backfill.installationSucceeded() await events.backfill.installationSucceeded()