Files
speckle-server/packages/server/modules/automate/services/subscriptions.ts
T
Kristaps Fabians Geikins bdf27f6218 feat: some mp analytics related to automate actions (#2299)
* fix(fe2): better resiliency for when mp cant be loaded

* WIP mixpanel track calls

* more resiliency improvements

* added all clientside tracking calls

* run finished event

* minor adjustment

* feat(automate): revert automationRunTriggerinAssociation

* feat(automate): track manual run triggers

* feat(automate): backend track automation run created events

* fix(automate): manual trigger type gql schema fix

* feat(automate): add source based filter to run trigger tracking

* fix(automate): fix trigger mock

* various minor adjustments

* remove comment

---------

Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
2024-06-07 10:21:24 +03:00

161 lines
5.4 KiB
TypeScript

import { automateLogger } from '@/logging/logging'
import { AutomationsEmitter } from '@/modules/automate/events/automations'
import { AutomateRunsEmitter } from '@/modules/automate/events/runs'
import {
VersionCreationTriggerType,
isVersionCreatedTriggerManifest
} from '@/modules/automate/helpers/types'
import { getAutomationRunFullTriggers } from '@/modules/automate/repositories/automations'
import {
ProjectAutomationsUpdatedMessageType,
ProjectTriggeredAutomationsStatusUpdatedMessageType
} from '@/modules/core/graph/generated/graphql'
import { ProjectSubscriptions, publish } from '@/modules/shared/utils/subscriptions'
import { isNonNullable } from '@speckle/shared'
// TODO: Update AutomateRuns subscription
export const setupAutomationUpdateSubscriptions = () => () => {
const quitters = [
AutomationsEmitter.listen(
AutomationsEmitter.events.Created,
async ({ automation }) => {
await publish(ProjectSubscriptions.ProjectAutomationsUpdated, {
projectId: automation.projectId,
projectAutomationsUpdated: {
type: ProjectAutomationsUpdatedMessageType.Created,
automationId: automation.id,
automation,
revision: null
}
})
}
),
AutomationsEmitter.listen(
AutomationsEmitter.events.Updated,
async ({ automation }) => {
await publish(ProjectSubscriptions.ProjectAutomationsUpdated, {
projectId: automation.projectId,
projectAutomationsUpdated: {
type: ProjectAutomationsUpdatedMessageType.Updated,
automationId: automation.id,
automation,
revision: null
}
})
}
),
AutomationsEmitter.listen(
AutomationsEmitter.events.CreatedRevision,
async ({ automation, revision }) => {
await publish(ProjectSubscriptions.ProjectAutomationsUpdated, {
projectId: automation.projectId,
projectAutomationsUpdated: {
type: ProjectAutomationsUpdatedMessageType.CreatedRevision,
automationId: automation.id,
automation,
revision
}
})
}
)
]
return () => quitters.forEach((quitter) => quitter())
}
export type SetupStatusUpdateSubscriptionsDeps = {
getAutomationRunFullTriggers: typeof getAutomationRunFullTriggers
}
export const setupStatusUpdateSubscriptions =
(deps: SetupStatusUpdateSubscriptionsDeps) => () => {
const { getAutomationRunFullTriggers } = deps
const quitters = [
AutomateRunsEmitter.listen(
AutomateRunsEmitter.events.Created,
async ({ manifests, run, automation }) => {
const validatedManifests = manifests
.map((manifest) => {
if (isVersionCreatedTriggerManifest(manifest)) {
return manifest
} else {
automateLogger.error('Unexpected run trigger manifest type', {
manifest
})
}
return null
})
.filter(isNonNullable)
await Promise.all(
validatedManifests.map(async (manifest) => {
await publish(
ProjectSubscriptions.ProjectTriggeredAutomationsStatusUpdated,
{
projectId: manifest.projectId,
projectTriggeredAutomationsStatusUpdated: {
...manifest,
run: {
...run,
automationId: automation.id,
functionRuns: run.functionRuns.map((functionRun) => ({
...functionRun,
runId: run.id
})),
triggers: run.triggers.map((trigger) => ({
...trigger,
automationRunId: run.id
}))
},
type: ProjectTriggeredAutomationsStatusUpdatedMessageType.RunCreated
}
}
)
})
)
}
),
AutomateRunsEmitter.listen(
AutomateRunsEmitter.events.StatusUpdated,
async ({ run, functionRun, automationId }) => {
const triggers = await getAutomationRunFullTriggers({
automationRunId: run.id
})
if (triggers[VersionCreationTriggerType].length) {
const versionCreation = triggers[VersionCreationTriggerType]
await Promise.all(
versionCreation.map(async (trigger) => {
await publish(
ProjectSubscriptions.ProjectTriggeredAutomationsStatusUpdated,
{
projectId: trigger.model.streamId,
projectTriggeredAutomationsStatusUpdated: {
projectId: trigger.model.streamId,
modelId: trigger.model.id,
versionId: trigger.version.id,
run: {
...run,
functionRuns: [functionRun],
automationId,
triggers: undefined
},
type: ProjectTriggeredAutomationsStatusUpdatedMessageType.RunUpdated
}
}
)
})
)
}
}
)
]
return () => quitters.forEach((quitter) => quitter())
}