Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0 metrics #15118

Merged
merged 16 commits into from
Dec 9, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ const EXCLUDED_EVENTS: Event[] = [
Event.ROLE_UPDATED,
Event.DATASOURCE_UPDATED,
Event.QUERY_UPDATED,
Event.TABLE_UPDATED,
Event.VIEW_UPDATED,
Event.VIEW_FILTER_UPDATED,
Event.VIEW_CALCULATION_UPDATED,
Event.AUTOMATION_TRIGGER_UPDATED,
Event.USER_GROUP_UPDATED,
Expand Down
1 change: 1 addition & 0 deletions packages/backend-core/src/events/publishers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export { default as plugin } from "./plugin"
export { default as backup } from "./backup"
export { default as environmentVariable } from "./environmentVariable"
export { default as auditLog } from "./auditLog"
export { default as rowAction } from "./rowAction"
13 changes: 13 additions & 0 deletions packages/backend-core/src/events/publishers/rowAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { publishEvent } from "../events"
import { Event, RowActionCreatedEvent } from "@budibase/types"

async function created(
rowAction: RowActionCreatedEvent,
timestamp?: string | number
) {
await publishEvent(Event.ROW_ACTION_CREATED, rowAction, timestamp)
}

export default {
created,
}
33 changes: 27 additions & 6 deletions packages/backend-core/src/events/publishers/table.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { publishEvent } from "../events"
import {
Event,
TableExportFormat,
FieldType,
Table,
TableCreatedEvent,
TableUpdatedEvent,
TableDeletedEvent,
TableExportedEvent,
TableExportFormat,
TableImportedEvent,
TableUpdatedEvent,
} from "@budibase/types"

async function created(table: Table, timestamp?: string | number) {
Expand All @@ -20,14 +21,34 @@ async function created(table: Table, timestamp?: string | number) {
await publishEvent(Event.TABLE_CREATED, properties, timestamp)
}

async function updated(table: Table) {
async function updated(oldTable: Table, newTable: Table) {
// only publish the event if it has fields we are interested in
let defaultValues, aiColumn

// check that new fields have been added
for (const key in newTable.schema) {
if (!oldTable.schema[key]) {
const newColumn = newTable.schema[key]
if ("default" in newColumn && newColumn.default != null) {
defaultValues = true
}
if (newColumn.type === FieldType.AI) {
aiColumn = newColumn.operation
}
}
}

const properties: TableUpdatedEvent = {
tableId: table._id as string,
tableId: newTable._id as string,
defaultValues,
aiColumn,
audited: {
name: table.name,
name: newTable.name,
},
}
await publishEvent(Event.TABLE_UPDATED, properties)
if (defaultValues || aiColumn) {
await publishEvent(Event.TABLE_UPDATED, properties)
}
}

async function deleted(table: Table) {
Expand Down
56 changes: 43 additions & 13 deletions packages/backend-core/src/events/publishers/view.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { publishEvent } from "../events"
import {
CalculationType,
Event,
Table,
TableExportFormat,
View,
ViewCalculation,
ViewCalculationCreatedEvent,
ViewCalculationDeletedEvent,
ViewCalculationUpdatedEvent,
Expand All @@ -11,22 +16,22 @@ import {
ViewFilterDeletedEvent,
ViewFilterUpdatedEvent,
ViewUpdatedEvent,
View,
ViewCalculation,
Table,
TableExportFormat,
ViewV2,
ViewJoinCreatedEvent,
} from "@budibase/types"

/* eslint-disable */

async function created(view: View, timestamp?: string | number) {
async function created(view: ViewV2, timestamp?: string | number) {
const properties: ViewCreatedEvent = {
name: view.name,
type: view.type,
tableId: view.tableId,
}
await publishEvent(Event.VIEW_CREATED, properties, timestamp)
}

async function updated(view: View) {
async function updated(view: ViewV2) {
const properties: ViewUpdatedEvent = {
tableId: view.tableId,
}
Expand All @@ -48,16 +53,27 @@ async function exported(table: Table, format: TableExportFormat) {
await publishEvent(Event.VIEW_EXPORTED, properties)
}

async function filterCreated(view: View, timestamp?: string | number) {
async function filterCreated(
{ tableId, filterGroups }: { tableId: string; filterGroups: number },
timestamp?: string | number
) {
const properties: ViewFilterCreatedEvent = {
tableId: view.tableId,
tableId,
filterGroups,
}
await publishEvent(Event.VIEW_FILTER_CREATED, properties, timestamp)
}

async function filterUpdated(view: View) {
async function filterUpdated({
tableId,
filterGroups,
}: {
tableId: string
filterGroups: number
}) {
const properties: ViewFilterUpdatedEvent = {
tableId: view.tableId,
tableId: tableId,
filterGroups,
}
await publishEvent(Event.VIEW_FILTER_UPDATED, properties)
}
Expand All @@ -69,10 +85,16 @@ async function filterDeleted(view: View) {
await publishEvent(Event.VIEW_FILTER_DELETED, properties)
}

async function calculationCreated(view: View, timestamp?: string | number) {
async function calculationCreated(
{
tableId,
calculationType,
}: { tableId: string; calculationType: CalculationType },
timestamp?: string | number
) {
const properties: ViewCalculationCreatedEvent = {
tableId: view.tableId,
calculation: view.calculation as ViewCalculation,
tableId,
calculation: calculationType,
}
await publishEvent(Event.VIEW_CALCULATION_CREATED, properties, timestamp)
}
Expand All @@ -93,6 +115,13 @@ async function calculationDeleted(existingView: View) {
await publishEvent(Event.VIEW_CALCULATION_DELETED, properties)
}

async function viewJoinCreated(tableId: any, timestamp?: string | number) {
const properties: ViewJoinCreatedEvent = {
tableId,
}
await publishEvent(Event.VIEW_JOIN_CREATED, properties, timestamp)
}

export default {
created,
updated,
Expand All @@ -104,4 +133,5 @@ export default {
calculationCreated,
calculationUpdated,
calculationDeleted,
viewJoinCreated,
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ beforeAll(async () => {
jest.spyOn(events.view, "calculationCreated")
jest.spyOn(events.view, "calculationUpdated")
jest.spyOn(events.view, "calculationDeleted")
jest.spyOn(events.view, "viewJoinCreated")

jest.spyOn(events.plugin, "init")
jest.spyOn(events.plugin, "imported")
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/api/controllers/rowAction/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RowActionResponse,
RowActionsResponse,
} from "@budibase/types"
import { events } from "@budibase/backend-core"
import sdk from "../../../sdk"

async function getTable(ctx: Ctx) {
Expand Down Expand Up @@ -59,6 +60,8 @@ export async function create(
name: ctx.request.body.name,
})

await events.rowAction.created(createdAction)

ctx.body = {
tableId,
id: createdAction.id,
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/api/controllers/table/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ export async function updateTable(
inputs.created = true
}
try {
const { datasource, table } = await sdk.tables.external.save(
const { datasource, oldTable, table } = await sdk.tables.external.save(
datasourceId!,
inputs,
{ tableId, renaming }
)
builderSocket?.emitDatasourceUpdate(ctx, datasource)
return table
return { table, oldTable }
} catch (err: any) {
if (err instanceof Error) {
ctx.throw(400, err.message)
Expand Down
11 changes: 9 additions & 2 deletions packages/server/src/api/controllers/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,15 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
await events.table.created(savedTable)
} else {
const api = pickApi({ table })
savedTable = await api.updateTable(ctx, renaming)
await events.table.updated(savedTable)
const { table: updatedTable, oldTable } = await api.updateTable(
ctx,
renaming
)
savedTable = updatedTable

if (oldTable) {
await events.table.updated(oldTable, savedTable)
}
}
if (renaming) {
await sdk.views.renameLinkedViews(savedTable, renaming)
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/api/controllers/table/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export async function updateTable(
}

try {
const { table } = await sdk.tables.internal.save(tableToSave, {
const { table, oldTable } = await sdk.tables.internal.save(tableToSave, {
userId: ctx.user._id,
rowsToImport: rows,
tableId: ctx.request.body._id,
renaming,
})

return table
return { table, oldTable }
} catch (err: any) {
if (err instanceof Error) {
ctx.throw(400, err.message)
Expand Down
62 changes: 0 additions & 62 deletions packages/server/src/api/controllers/view/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import { builderSocket } from "../../../websockets"

const cloneDeep = require("lodash/cloneDeep")

import isEqual from "lodash/isEqual"

export async function fetch(ctx: Ctx) {
ctx.body = await getViews()
}
Expand Down Expand Up @@ -60,71 +58,11 @@ export async function save(ctx: Ctx) {
existingTable.views[viewName] = existingTable.views[originalName]
}
await db.put(table)
await handleViewEvents(
existingTable.views[viewName] as View,
table.views[viewName]
)

ctx.body = table.views[viewName]
builderSocket?.emitTableUpdate(ctx, table)
}

export async function calculationEvents(existingView: View, newView: View) {
const existingCalculation = existingView && existingView.calculation
const newCalculation = newView && newView.calculation

if (existingCalculation && !newCalculation) {
await events.view.calculationDeleted(existingView)
}

if (!existingCalculation && newCalculation) {
await events.view.calculationCreated(newView)
}

if (
existingCalculation &&
newCalculation &&
existingCalculation !== newCalculation
) {
await events.view.calculationUpdated(newView)
}
}

export async function filterEvents(existingView: View, newView: View) {
const hasExistingFilters = !!(
existingView &&
existingView.filters &&
existingView.filters.length
)
const hasNewFilters = !!(newView && newView.filters && newView.filters.length)

if (hasExistingFilters && !hasNewFilters) {
await events.view.filterDeleted(newView)
}

if (!hasExistingFilters && hasNewFilters) {
await events.view.filterCreated(newView)
}

if (
hasExistingFilters &&
hasNewFilters &&
!isEqual(existingView.filters, newView.filters)
) {
await events.view.filterUpdated(newView)
}
}

async function handleViewEvents(existingView: View, newView: View) {
if (!existingView) {
await events.view.created(newView)
} else {
await events.view.updated(newView)
}
await calculationEvents(existingView, newView)
await filterEvents(existingView, newView)
}

export async function destroy(ctx: Ctx) {
const db = context.getAppDB()
const viewName = decodeURIComponent(ctx.params.viewName)
Expand Down
Loading
Loading