Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

7.8 KB
Raw Blame
import { ActivepiecesError, ApId, apId, ErrorCode, isNil, SeekPage, tryCatch } from '@activepieces/core-utils'
import { apDayjsDuration } from '@activepieces/server-utils'
import { Alert, AlertChannel, ApEdition, FailedStep, flowStructureUtil, ListAlertsParams, ProjectType } from '@activepieces/shared'

import dayjs from 'dayjs'
import timezone from 'dayjs/plugin/timezone'
import { FastifyBaseLogger } from 'fastify'
import { userIdentityService } from '../../authentication/user-identity/user-identity-service'
import { repoFactory } from '../../core/db/repo-factory'
import { redisConnections } from '../../database/redis-connections'
import { flowService } from '../../flows/flow/flow.service'
import { flowVersionService } from '../../flows/flow-version/flow-version.service'
import { domainHelper } from '../../helper/domain-helper'
import { buildPaginator } from '../../helper/pagination/build-paginator'
import { paginationHelper } from '../../helper/pagination/pagination-utils'
import { system } from '../../helper/system/system'
import { projectService } from '../../project/project-service'
import { userService } from '../../user/user-service'
import { emailService } from '../helper/email/email-service'
import { AlertEntity } from './alerts-entity'

dayjs.extend(timezone)

const repo = repoFactory(AlertEntity)
const DAY_IN_SECONDS = apDayjsDuration(1, 'day').asSeconds()
const alertEventKey = (flowVersionId: string) => `flow_fail_count:${flowVersionId}`
const paidEditions = [ApEdition.CLOUD, ApEdition.ENTERPRISE].includes(system.getEdition())

export const alertsService = (log: FastifyBaseLogger) => ({
    async sendAlertOnRunFinish({
        issueToAlert,
        flowRunId,
        failedStep,
    }: {
        issueToAlert: IssueToAlert
        flowRunId: string
        failedStep: FailedStep
    }): Promise<void> {
        if (!paidEditions) {
            return
        }

        const redisConnection = await redisConnections.useExisting()
        const failureKey = alertEventKey(issueToAlert.flowVersionId)
        const numberOfFailures = await redisConnection.incrby(failureKey, 1)
        await redisConnection.expire(failureKey, DAY_IN_SECONDS)

        if (numberOfFailures > 1) {
            return
        }

        const project = await projectService(log).getOneOrThrow(issueToAlert.projectId)
        const flowVersion = await flowVersionService(log).getOneOrThrow(issueToAlert.flowVersionId)

        const flowOwnerEmail = project.notifyFlowOwnerOnFailure
            ? await getFlowOwnerEmail({ log, flowId: issueToAlert.flowId, projectId: issueToAlert.projectId })
            : undefined

        const failedStepNumber = flowStructureUtil.getStepNumber(flowVersion.trigger, failedStep.name)
        const alertsInfo: IssueParams = {
            flowVersionId: flowVersion.id,
            flowRunId,
            projectId: issueToAlert.projectId,
            platformId: project.platformId,
            projectName: project.displayName,
            flowId: issueToAlert.flowId,
            flowName: flowVersion.displayName,
            createdAt: dayjs(issueToAlert.created)
                .tz('America/Los_Angeles')
                .format('DD MMM YYYY, HH:mm [PT]'),
            failedStepDisplayName: failedStep.displayName,
            failedStepNumber: failedStepNumber > 0 ? failedStepNumber : undefined,
            failedStepMessage: failedStep.message,
            flowOwnerEmail,
        }

        await sendAlertOnFlowFailure(log, alertsInfo)
    },
    async add({ projectId, channel, receiver }: AddPrams): Promise<void> {
        const normalizedReceiver = receiver.toLowerCase()
        const project = await projectService(log).getOneOrThrow(projectId)
        if (project.type === ProjectType.PERSONAL) {
            const owner = await userService(log).getOneOrFail({ id: project.ownerId })
            const identity = await userIdentityService(log).getOneOrFail({ id: owner.identityId })
            if (identity.email.toLowerCase() !== normalizedReceiver) {
                throw new ActivepiecesError({
                    code: ErrorCode.VALIDATION,
                    params: {
                        message: 'Personal projects only allow the project owner as alert receiver',
                    },
                })
            }
        }
        const alertId = apId()
        const existingAlert = await repo()
            .createQueryBuilder('alert')
            .where('alert."projectId" = :projectId', { projectId })
            .andWhere('LOWER(alert.receiver) = :receiver', { receiver: normalizedReceiver })
            .getOne()

        if (existingAlert) {
            throw new ActivepiecesError({
                code: ErrorCode.EXISTING_ALERT_CHANNEL,
                params: {
                    email: normalizedReceiver,
                },
            })
        }

        await repo().createQueryBuilder()
            .insert()
            .into(AlertEntity)
            .values({
                id: alertId,
                channel,
                projectId,
                receiver: normalizedReceiver,
                created: dayjs().toISOString(),
            })
            .execute()
    },
    async list({ projectId, cursor, limit }: ListAlertsParams): Promise<SeekPage<Alert>> {
        const decodedCursor = paginationHelper.decodeCursor(cursor ?? null)
        const paginator = buildPaginator({
            entity: AlertEntity,
            query: {
                limit,
                order: 'ASC',
                afterCursor: decodedCursor.nextCursor,
                beforeCursor: decodedCursor.previousCursor,
            },
        })

        const query = repo().createQueryBuilder(AlertEntity.options.name).where({
            projectId,
        })

        const { data, cursor: newCursor } = await paginator.paginate(query)
        return paginationHelper.createPage<Alert>(data, newCursor)
    },
    async delete({ alertId }: { alertId: ApId }): Promise<void> {
        await repo().delete({
            id: alertId,
        })
    },
    async deleteByProjectId({ projectId }: { projectId: ApId }): Promise<void> {
        await repo().delete({ projectId })
    },
})

async function getFlowOwnerEmail({ log, flowId, projectId }: { log: FastifyBaseLogger, flowId: string, projectId: string }): Promise<string | undefined> {
    const { data: email, error } = await tryCatch(async () => {
        const flow = await flowService(log).getOne({ id: flowId, projectId })
        if (isNil(flow) || isNil(flow.ownerId)) {
            return undefined
        }
        const owner = await userService(log).getOneOrFail({ id: flow.ownerId })
        const identity = await userIdentityService(log).getOneOrFail({ id: owner.identityId })
        return identity.email.toLowerCase()
    })
    if (error) {
        log.warn({ error, flow: { id: flowId }, project: { id: projectId } }, '[alertsService#getFlowOwnerEmail] failed to resolve flow owner email')
        return undefined
    }
    return email ?? undefined
}

async function sendAlertOnFlowFailure(log: FastifyBaseLogger, params: IssueParams): Promise<void> {
    const { flowRunId, projectId } = params

    const runUrl = await domainHelper.getInternalUrl({
        path: `projects/${projectId}/runs/${flowRunId}`,
    })

    await emailService(log).sendIssueCreatedNotification({
        ...params,
        runUrl,
    })
}

type AddPrams = {
    projectId: string
    channel: AlertChannel
    receiver: string
}

type IssueParams = {
    projectId: string
    flowVersionId: string
    projectName: string
    platformId: string
    flowId: string
    flowRunId: string
    flowName: string
    createdAt: string
    failedStepDisplayName: string
    failedStepNumber?: number
    failedStepMessage?: string
    flowOwnerEmail?: string
}

type IssueToAlert = {
    flowVersionId: string
    projectId: string
    flowId: string
    created: string
}