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

Implement automateFunctionRunStatusReport #2262

Merged
merged 10 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/frontend-2/lib/common/generated/gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,14 @@ export type AutomateRunCollection = {
};

export enum AutomateRunStatus {
Canceled = 'CANCELED',
Exception = 'EXCEPTION',
Failed = 'FAILED',
Initializing = 'INITIALIZING',
Pending = 'PENDING',
Running = 'RUNNING',
Succeeded = 'SUCCEEDED'
Succeeded = 'SUCCEEDED',
Timeout = 'TIMEOUT'
}

export enum AutomateRunTriggerType {
Expand Down Expand Up @@ -1307,7 +1311,7 @@ export type MutationAppUpdateArgs = {


export type MutationAutomateFunctionRunStatusReportArgs = {
input: Array<AutomateFunctionRunStatusReportInput>;
input: AutomateFunctionRunStatusReportInput;
};


Expand Down
6 changes: 5 additions & 1 deletion packages/server/assets/automate/typedefs/automate.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
enum AutomateRunStatus {
PENDING
INITIALIZING
RUNNING
SUCCEEDED
FAILED
EXCEPTION
TIMEOUT
CANCELED
}

enum AutomateRunTriggerType {
Expand Down Expand Up @@ -324,7 +328,7 @@ extend type Query {

extend type Mutation {
automateFunctionRunStatusReport(
input: [AutomateFunctionRunStatusReportInput!]!
input: AutomateFunctionRunStatusReportInput!
): Boolean!
@hasServerRole(role: SERVER_GUEST)
@hasScope(scope: "automate:report-results")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const triggerAutomationRun = async (params: {
functionId: functionRun.functionId,
functionReleaseId: functionRun.functionReleaseId,
functionInputs: functionRun.functionInputs,
functionRunId: functionRun.runId
functionRunId: functionRun.id
}
})

Expand Down
7 changes: 6 additions & 1 deletion packages/server/modules/automate/errors/runs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { BaseError } from '@/modules/shared/errors/base'

export class FunctionRunReportStatusesError extends BaseError {
export class FunctionRunNotFoundError extends BaseError {
static defaultMessage = 'Could not find function run with given id'
static code = 'FUNCTION_RUN_NOT_FOUND'
}

export class FunctionRunReportStatusError extends BaseError {
static defaultMessage =
'An error occurred while updating function run report statuses'
static code = 'FUNCTION_RUN_REPORT_STATUSES_ERROR'
Expand Down
34 changes: 32 additions & 2 deletions packages/server/modules/automate/graph/resolvers/automate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import {
getAutomationRunsItems,
getAutomationRunsTotalCount,
getAutomationTriggerDefinitions,
getFunctionRun,
getLatestVersionAutomationRuns,
getProjectAutomationsItems,
getProjectAutomationsTotalCount,
storeAutomation,
storeAutomationRevision,
updateAutomation as updateDbAutomation
updateAutomationRun,
updateAutomation as updateDbAutomation,
upsertAutomationFunctionRun
} from '@/modules/automate/repositories/automations'
import {
createAutomation,
Expand Down Expand Up @@ -53,6 +56,10 @@ import {
manuallyTriggerAutomation,
triggerAutomationRevisionRun
} from '@/modules/automate/services/trigger'
import {
reportFunctionRunStatus,
ReportFunctionRunStatusDeps
} from '@/modules/automate/services/runsManagement'
import {
AutomateFunctionReleaseNotFoundError,
FunctionNotFoundError
Expand All @@ -62,7 +69,6 @@ import {
dbToGraphqlTriggerTypeMap,
functionTemplateRepos
} from '@/modules/automate/helpers/executionEngine'
import { mapDbStatusToGqlStatus } from '@/modules/automate/services/runsManagement'
import { authorizeResolver } from '@/modules/shared'
import {
AutomationRevisionFunctionForInputRedaction,
Expand All @@ -79,6 +85,10 @@ import {
ProjectSubscriptions,
filteredSubscribe
} from '@/modules/shared/utils/subscriptions'
import {
mapDbStatusToGqlStatus,
mapGqlStatusToDbStatus
} from '@/modules/automate/utils/automateFunctionRunStatus'

/**
* TODO:
Expand Down Expand Up @@ -524,6 +534,26 @@ export = {
}
},
Mutation: {
async automateFunctionRunStatusReport(_parent, { input }) {
const deps: ReportFunctionRunStatusDeps = {
getAutomationFunctionRunRecord: getFunctionRun,
upsertAutomationFunctionRunRecord: upsertAutomationFunctionRun,
automationRunUpdater: updateAutomationRun
}

const payload = {
...input,
contextView: input.contextView ?? null,
results: (input.results as Automate.AutomateTypes.ResultsSchema) ?? null,
runId: input.functionRunId,
status: mapGqlStatusToDbStatus(input.status),
statusMessage: input.statusMessage ?? null
}

const result = await reportFunctionRunStatus(deps)(payload)

return result
},
automateMutations: () => ({})
},
Subscription: {
Expand Down
18 changes: 12 additions & 6 deletions packages/server/modules/automate/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ export type AutomationRevisionRecord = {

export type AutomationRunStatus =
| 'pending'
| 'initializing'
| 'running'
| 'success'
| 'failure'
| 'error'
| 'succeeded'
| 'failed'
| 'exception'
| 'timeout'
| 'canceled'

export const AutomationRunStatuses: Record<AutomationRunStatus, AutomationRunStatus> = {
pending: 'pending',
initializing: 'initializing',
running: 'running',
success: 'success',
failure: 'failure',
error: 'error'
succeeded: 'succeeded',
failed: 'failed',
exception: 'exception',
timeout: 'timeout',
canceled: 'canceled'
}

export type AutomationRunRecord = {
Expand Down
36 changes: 26 additions & 10 deletions packages/server/modules/automate/repositories/automations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ export async function getFullAutomationRevisionMetadata(
}
}

export type InsertableAutomationFunctionRun = Pick<
AutomationFunctionRunRecord,
'id' | 'runId' | 'status' | 'statusMessage' | 'contextView' | 'results'
>

export async function upsertAutomationFunctionRun(
automationFunctionRun: InsertableAutomationFunctionRun
) {
await AutomationFunctionRuns.knex()
.insert(
_.pick(automationFunctionRun, AutomationFunctionRuns.withoutTablePrefix.cols)
)
.onConflict(AutomationFunctionRuns.withoutTablePrefix.col.id)
.merge([
AutomationFunctionRuns.withoutTablePrefix.col.contextView,
AutomationFunctionRuns.withoutTablePrefix.col.elapsed,
AutomationFunctionRuns.withoutTablePrefix.col.results,
AutomationFunctionRuns.withoutTablePrefix.col.status,
AutomationFunctionRuns.withoutTablePrefix.col.statusMessage
])
}

export type InsertableAutomationRun = AutomationRunRecord & {
triggers: Omit<AutomationRunTriggerRecord, 'automationRunId'>[]
functionRuns: Omit<AutomationFunctionRunRecord, 'runId'>[]
Expand Down Expand Up @@ -130,10 +152,7 @@ export async function upsertAutomationRun(automationRun: InsertableAutomationRun
return
}

export async function getFunctionRuns(params: { functionRunIds: string[] }) {
const { functionRunIds } = params
if (!functionRunIds.length) return []

export async function getFunctionRun(functionRunId: string) {
const q = AutomationFunctionRuns.knex()
.select<
Array<
Expand All @@ -147,7 +166,7 @@ export async function getFunctionRuns(params: { functionRunIds: string[] }) {
AutomationRuns.col.automationRevisionId,
AutomationRevisions.col.automationId
])
.whereIn(AutomationFunctionRuns.col.id, functionRunIds)
.where(AutomationFunctionRuns.col.id, functionRunId)
.innerJoin(
AutomationRuns.name,
AutomationRuns.col.id,
Expand All @@ -159,12 +178,9 @@ export async function getFunctionRuns(params: { functionRunIds: string[] }) {
AutomationRuns.col.automationRevisionId
)

return await q
}
const runs = await q

export async function getFunctionRun(functionRunId: string) {
const runs = await getFunctionRuns({ functionRunIds: [functionRunId] })
return (runs[0] || null) as (typeof runs)[0] | null
return (runs[0] ?? null) as (typeof runs)[0] | null
}

export type GetFunctionRunsForAutomationRunIdsItem = AutomationFunctionRunRecord & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ export const getAutomationsStatus =

const failedAutomations = runsWithUpdatedStatus.filter(
(a) =>
a.status === AutomationRunStatuses.failure ||
a.status === AutomationRunStatuses.error
a.status === AutomationRunStatuses.failed ||
a.status === AutomationRunStatuses.exception
)

const runningAutomations = runsWithUpdatedStatus.filter(
Expand All @@ -453,17 +453,17 @@ export const getAutomationsStatus =
(a) => a.status === AutomationRunStatuses.pending
)

let status = AutomationRunStatuses.success
let status = AutomationRunStatuses.succeeded
let statusMessage = 'All automations have succeeded'

if (failedAutomations.length) {
status = AutomationRunStatuses.failure
status = AutomationRunStatuses.failed
statusMessage = 'Some automations have failed:'
for (const fa of failedAutomations) {
for (const functionRunStatus of fa.functionRuns) {
if (
functionRunStatus.status === AutomationRunStatuses.failure ||
functionRunStatus.status === AutomationRunStatuses.error
functionRunStatus.status === AutomationRunStatuses.failed ||
functionRunStatus.status === AutomationRunStatuses.exception
)
statusMessage += `\n${functionRunStatus.statusMessage}`
}
Expand Down
Loading