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

fix(impact): run counts on the Impact dashboard no longer split or drop after refreshing (#16466)

Bartosz Majewskia04936e40ee4
diff --git a/packages/server/api/src/app/analytics/platform-analytics-report.service.ts b/packages/server/api/src/app/analytics/platform-analytics-report.service.ts
index 9fcc3610634..125d532d5c7 100644
--- a/packages/server/api/src/app/analytics/platform-analytics-report.service.ts
+++ b/packages/server/api/src/app/analytics/platform-analytics-report.service.ts
@@ -146,26 +146,21 @@ async function listRuns(projectIds: string[], afterDate: string | null, currentD
currentDate,
})
}
- const runs = await query.getRawMany()
+ const runs = await query.getRawMany<{ flowId: string, day: Date, runs: string }>()
return runs.map((run) => {
return {
flowId: run.flowId,
- day: run.day,
+ day: dayjs(run.day).toISOString(),
runs: Number(run.runs),
}
})
}
function mergeRuns(existing: AnalyticsRunsUsageItem[], incoming: AnalyticsRunsUsageItem[]): AnalyticsRunsUsageItem[] {
- const map = new Map(existing.map(run => [`${run.flowId}-${run.day}`, { ...run }]))
- for (const run of incoming) {
+ const map = new Map<string, AnalyticsRunsUsageItem>()
+ for (const run of [...existing, ...incoming]) {
const key = `${run.flowId}-${run.day}`
- if (map.has(key)) {
- map.get(key)!.runs += run.runs
- }
- else {
- map.set(key, { ...run })
- }
+ map.set(key, { ...run, runs: (map.get(key)?.runs ?? 0) + run.runs })
}
return Array.from(map.values())
}
diff --git a/packages/server/api/test/integration/ce/analytics/platform-analytics.test.ts b/packages/server/api/test/integration/ce/analytics/platform-analytics.test.ts
index 71dd43696a6..8ea82aebc34 100644
--- a/packages/server/api/test/integration/ce/analytics/platform-analytics.test.ts
+++ b/packages/server/api/test/integration/ce/analytics/platform-analytics.test.ts
@@ -1,4 +1,5 @@
-import { DefaultProjectRole, FlowStatus, FlowVersionState, PlatformRole, ProjectType, RunEnvironment } from '@activepieces/shared'
+import { apId } from '@activepieces/core-utils'
+import { AnalyticsRunsUsageItem, DefaultProjectRole, FlowStatus, FlowVersionState, PlatformRole, ProjectType, RunEnvironment } from '@activepieces/shared'
import dayjs from 'dayjs'
import { FastifyInstance } from 'fastify'
import { StatusCodes } from 'http-status-codes'
@@ -57,6 +58,13 @@ async function seedTwoProjects(ctx: TestContext) {
return { foreignUser, foreignProject, ownFlow, foreignFlow }
}
+async function refreshRunsOfFlow({ ctx, flowId }: { ctx: TestContext, flowId: string }) {
+ const response = await ctx.post('/v1/analytics/refresh')
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ const runs: AnalyticsRunsUsageItem[] = response!.json().runs.filter((run: AnalyticsRunsUsageItem) => run.flowId === flowId)
+ return { entries: runs.length, total: runs.reduce((sum, run) => sum + run.runs, 0) }
+}
+
describe('Platform Analytics API', () => {
describe('GET /v1/analytics', () => {
it('should return the whole platform report to a platform admin', async () => {
@@ -212,4 +220,71 @@ describe('POST /v1/analytics/refresh', () => {
const flowIds = response!.json().flows.map((flow: { flowId: string }) => flow.flowId)
expect(flowIds).toContain(foreignFlow.id)
})
+
+ it('should keep one entry per flow and day with the right total across repeated refreshes', async () => {
+ const ctx = await createTestContext(app!, {
+ plan: { analyticsEnabled: true },
+ })
+ const flow = createMockFlow({ projectId: ctx.project.id, status: FlowStatus.ENABLED })
+ await db.save('flow', flow)
+ const flowVersion = createMockFlowVersion({ flowId: flow.id, state: FlowVersionState.DRAFT })
+ await db.save('flow_version', flowVersion)
+ const start = dayjs().subtract(1, 'day').startOf('hour')
+ const addRuns = ({ count, created }: { count: number, created: dayjs.Dayjs }) => db.save('flow_run', Array.from({ length: count }, () => createMockFlowRun({
+ projectId: ctx.project.id,
+ flowId: flow.id,
+ flowVersionId: flowVersion.id,
+ environment: RunEnvironment.PRODUCTION,
+ created: created.toISOString(),
+ })))
+ await addRuns({ count: 3, created: start })
+
+ const first = await refreshRunsOfFlow({ ctx, flowId: flow.id })
+ const report = await db.findOneByOrFail<{ id: string }>('platform_analytics_report', { platformId: ctx.platform.id })
+ await db.update('platform_analytics_report', report.id, { cachedAt: start.add(1, 'minute').toISOString() })
+ await addRuns({ count: 1, created: start.add(2, 'minute') })
+ const second = await refreshRunsOfFlow({ ctx, flowId: flow.id })
+ const third = await refreshRunsOfFlow({ ctx, flowId: flow.id })
+
+ expect({ first, second, third }).toEqual({
+ first: { entries: 1, total: 3 },
+ second: { entries: 1, total: 4 },
+ third: { entries: 1, total: 4 },
+ })
+ })
+
+ it('should merge same-day entries already split in a saved report', async () => {
+ const ctx = await createTestContext(app!, {
+ plan: { analyticsEnabled: true },
+ })
+ const flowId = apId()
+ const otherFlowId = apId()
+ const day = dayjs().startOf('day').toISOString()
+ const previousDay = dayjs().startOf('day').subtract(1, 'day').toISOString()
+ await db.save('platform_analytics_report', {
+ id: apId(),
+ platformId: ctx.platform.id,
+ cachedAt: dayjs().toISOString(),
+ outdated: false,
+ runs: [
+ { flowId, day, runs: 3 },
+ { flowId, day, runs: 1 },
+ { flowId, day: previousDay, runs: 2 },
+ { flowId: otherFlowId, day, runs: 5 },
+ ],
+ flows: [],
+ users: [],
+ })
+
+ const response = await ctx.post('/v1/analytics/refresh')
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ const runs = response!.json().runs
+ expect(runs).toHaveLength(3)
+ expect(runs).toEqual(expect.arrayContaining([
+ { flowId, day, runs: 4 },
+ { flowId, day: previousDay, runs: 2 },
+ { flowId: otherFlowId, day, runs: 5 },
+ ]))
+ })
})