fix(automations): fix ExecutionStep status mismatch, branch operator precedence, and 10k row cap in getAutomationAnalytics

Open
#50 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, typescript

Research direction

Start with getAutomationAnalytics in apps/api/src/automations/automations.service.ts and compare its status checks with the writes in apps/workers/src/workers/comment-worker.ts and message-worker.ts. Inspect the Prisma models and existing aggregation patterns before replacing the capped queries with database aggregations. Done means both success statuses count, branch values resolve correctly, and analytics totals are no longer truncated by the row caps.

Written by the indexing model from the issue text.

Description

backend bug
Problem Description

In apps/api/src/automations/automations.service.ts (getAutomationAnalytics, lines 950-1014):

  1. Step successCount is always 0 ('SUCCESS' vs 'COMPLETED'):

    • Workers (apps/workers/src/workers/comment-worker.ts line 157 and apps/workers/src/workers/message-worker.ts line 294) write ExecutionStep.status = 'SUCCESS'.
    • getAutomationAnalytics (line 1006) checks if (step.status === 'COMPLETED'), so successful DM and comment-reply steps are never counted toward successCount.
  2. Operator precedence bug turns out.branch into 'undefined':

    • Line 1014 writes:
      const branchKey = out.branch || out.conditionResult !== undefined ? String(out.conditionResult) : null;
    • Because || binds tighter than !==, JavaScript evaluates (out.branch || out.conditionResult) !== undefined. Whenever out.branch is truthy and out.conditionResult is undefined, branchKey evaluates to the literal string 'undefined' instead of out.branch.
  3. Independent in-memory row caps skew totals at scale:

    • getAutomationAnalytics independently caps executions (take: 2000) and executionSteps (take: 10000) and aggregates in memory, causing totalTriggers and per-node step metrics to silently truncate and drift for high-volume automations.

Expected Behavior
  1. getAutomationAnalytics counts both 'SUCCESS' and 'COMPLETED' step statuses as successful.
  2. branchKey resolves out.branch when present, falling back to String(out.conditionResult) only when out.conditionResult !== undefined.
  3. Total trigger counts, execution status breakdowns, and per-node step counts are computed via database aggregations (count / groupBy) rather than loading up to 10,000 rows into Node memory.

Where to Look
  • apps/api/src/automations/automations.service.ts (getAutomationAnalytics, lines 950-1014)
  • apps/workers/src/workers/comment-worker.ts (line 157)
  • apps/workers/src/workers/message-worker.ts (line 294)

Solution / Implementation Guide
  1. Fix status check:
    • Count step.status === 'SUCCESS' || step.status === 'COMPLETED' (or standardize workers and analytics on the same status while keeping backward compatibility for existing 'SUCCESS' rows).
  2. Fix operator precedence on line 1014:
    • Update to:
      const branchKey = out.branch ? String(out.branch) : (out.conditionResult !== undefined ? String(out.conditionResult) : null);
  3. Use DB aggregations instead of take: 2000 / take: 10000 in-memory loops:
    • Use prisma.automationExecution.groupBy({ by: ['status'], ... }) and prisma.executionStep.groupBy({ by: ['nodeId', 'status'], ... }) for accurate totals, and only query output branch counts via targeted SQL aggregation.
Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
6m
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sumamakhan761/tryinstance

All issues in sumamakhan761/tryinstance

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.