Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(api): project analytics leave out archived issues (#790)

Ilya Shkura6cb72c3d643d
diff --git a/apps/api/src/modules/analytics/__tests__/integration/analytics.test.ts b/apps/api/src/modules/analytics/__tests__/integration/analytics.test.ts
index 1f164f05..926b5d61 100644
--- a/apps/api/src/modules/analytics/__tests__/integration/analytics.test.ts
+++ b/apps/api/src/modules/analytics/__tests__/integration/analytics.test.ts
@@ -105,6 +105,35 @@ describe('analytics', () => {
expect(res.data?.unassigned).toBe(1);
});
+ it('leaves archived issues out of the current figures', async () => {
+ const { asOwner, col } = await setupProject();
+ await createIssue(asOwner, col.backlog);
+ for (const columnId of [col.backlog, col.started]) {
+ const archived = await createIssue(asOwner, columnId, { dueDate: '2000-01-01' });
+ await asOwner.issues({ issueId: archived.id }).archive.post();
+ }
+
+ const res = await asOwner.projects({ projectKey: 'MKT' }).analytics.stats.get();
+ expect(res.data).toMatchObject({
+ open: 1,
+ inProgress: 0,
+ backlog: 1,
+ overdue: 0,
+ unassigned: 1,
+ });
+ });
+
+ it('keeps counting a closing after the issue is archived', async () => {
+ const { asOwner, col } = await setupProject();
+ const closed = await createIssue(asOwner, col.started);
+ await moveIssue(asOwner, closed.id, col.completed);
+ const archived = await asOwner.issues({ issueId: closed.id }).archive.post();
+ expect(archived.data?.archivedAt).toBeTruthy();
+
+ const res = await asOwner.projects({ projectKey: 'MKT' }).analytics.stats.get();
+ expect(res.data?.closedLast7d).toBe(1);
+ });
+
it('counts issues moved to a completed column as closedLast7d', async () => {
const { asOwner, col } = await setupProject();
const issue = await createIssue(asOwner, col.started);
@@ -281,6 +310,31 @@ describe('analytics', () => {
});
});
+ it('leaves archived issues out of every grouping', async () => {
+ const { asOwner, col } = await setupProject();
+ await createIssue(asOwner, col.started, { priority: 'high' });
+ const archived = await createIssue(asOwner, col.started, { priority: 'high' });
+ await asOwner.issues({ issueId: archived.id }).archive.post();
+
+ for (const by of ['status', 'priority', 'type', 'assignee', 'delegate'] as const) {
+ const res = await asOwner.projects({ projectKey: 'MKT' }).analytics.breakdown.get({
+ query: { by },
+ });
+ expect(res.data?.reduce((total, item) => total + item.count, 0)).toBe(1);
+ }
+ });
+
+ it('keeps a column with only archived issues at zero', async () => {
+ const { asOwner, col } = await setupProject();
+ const archived = await createIssue(asOwner, col.started);
+ await asOwner.issues({ issueId: archived.id }).archive.post();
+
+ const res = await asOwner
+ .projects({ projectKey: 'MKT' })
+ .analytics.breakdown.get({ query: { by: 'status' } });
+ expect(res.data?.find((i) => i.key === String(col.started))?.count).toBe(0);
+ });
+
it('rejects an unknown group-by value', async () => {
const { asOwner } = await setupProject();
const res = await asOwner
@@ -771,6 +825,22 @@ describe('analytics', () => {
const row = res.data?.find((r) => r.agentId === agent.id);
expect(row?.delegatedOpen).toBe(1);
});
+
+ it('leaves archived issues out of the delegated count', async () => {
+ const { asOwner, col } = await setupProject();
+ const agent = await makeAgent(asOwner, 'helperbot');
+ const open = await createIssue(asOwner, col.started);
+ const archived = await createIssue(asOwner, col.started);
+ await asOwner.issues({ issueId: open.id }).patch({ delegateUserId: agent.userId });
+ await asOwner.issues({ issueId: archived.id }).patch({ delegateUserId: agent.userId });
+ await asOwner.issues({ issueId: archived.id }).archive.post();
+
+ const res = await asOwner
+ .projects({ projectKey: 'MKT' })
+ ['analytics']['agent-workload'].get();
+ const row = res.data?.find((r) => r.agentId === agent.id);
+ expect(row?.delegatedOpen).toBe(1);
+ });
});
describe('access', () => {
diff --git a/apps/api/src/modules/analytics/service.ts b/apps/api/src/modules/analytics/service.ts
index 91e7f4c1..ec4e760a 100644
--- a/apps/api/src/modules/analytics/service.ts
+++ b/apps/api/src/modules/analytics/service.ts
@@ -56,6 +56,12 @@ export interface StatsDto {
const OPEN_STATES = sql`${projectColumn.stateType} NOT IN ('completed', 'canceled')`;
+// Archived issues are off the board, so the current figures leave them out. A closing
+// still counts: archiving does not undo it.
+function onBoard(projectId: number) {
+ return and(eq(issue.projectId, projectId), isNull(issue.archivedAt));
+}
+
export async function getStats(projectId: number): Promise<StatsDto> {
// One row per state type with its issue count; the open/backlog/in-progress
// figures are derived from it.
@@ -63,7 +69,7 @@ export async function getStats(projectId: number): Promise<StatsDto> {
.select({ stateType: projectColumn.stateType, count: sql<number>`count(*)::int` })
.from(issue)
.innerJoin(projectColumn, eq(projectColumn.id, issue.columnId))
- .where(eq(issue.projectId, projectId))
+ .where(onBoard(projectId))
.groupBy(projectColumn.stateType);
let open = 0;
@@ -79,13 +85,13 @@ export async function getStats(projectId: number): Promise<StatsDto> {
.select({ overdue: sql<number>`count(*)::int` })
.from(issue)
.innerJoin(projectColumn, eq(projectColumn.id, issue.columnId))
- .where(and(eq(issue.projectId, projectId), sql`${issue.dueDate} < CURRENT_DATE`, OPEN_STATES));
+ .where(and(onBoard(projectId), sql`${issue.dueDate} < CURRENT_DATE`, OPEN_STATES));
const [{ unassigned }] = await db
.select({ unassigned: sql<number>`count(*)::int` })
.from(issue)
.innerJoin(projectColumn, eq(projectColumn.id, issue.columnId))
- .where(and(eq(issue.projectId, projectId), isNull(issue.assigneeUserId), OPEN_STATES));
+ .where(and(onBoard(projectId), isNull(issue.assigneeUserId), OPEN_STATES));
const closedRows = (await db.execute(sql`
SELECT count(*)::int AS count
@@ -126,7 +132,7 @@ export async function getBreakdown(projectId: number, by: BreakdownBy): Promise<
count: sql<number>`count(${issue.id})::int`,
})
.from(projectColumn)
- .leftJoin(issue, eq(issue.columnId, projectColumn.id))
+ .leftJoin(issue, and(eq(issue.columnId, projectColumn.id), isNull(issue.archivedAt)))
.where(eq(projectColumn.projectId, projectId))
.groupBy(projectColumn.id, projectColumn.name, projectColumn.color, projectColumn.position)
.orderBy(projectColumn.position);
@@ -140,7 +146,7 @@ export async function getBreakdown(projectId: number, by: BreakdownBy): Promise<
count: sql<number>`count(*)::int`,
})
.from(issue)
- .where(eq(issue.projectId, projectId))
+ .where(onBoard(projectId))
.groupBy(issue.priority)
.orderBy(desc(sql`count(*)`));
return rows.map((r) => ({
@@ -161,7 +167,7 @@ export async function getBreakdown(projectId: number, by: BreakdownBy): Promise<
})
.from(issue)
.leftJoin(issueType, eq(issueType.id, issue.typeId))
- .where(eq(issue.projectId, projectId))
+ .where(onBoard(projectId))
.groupBy(issueType.id, issueType.name, issueType.color)
.orderBy(desc(sql`count(${issue.id})`));
return rows.map((r) => ({
@@ -181,7 +187,7 @@ export async function getBreakdown(projectId: number, by: BreakdownBy): Promise<
})
.from(issue)
.leftJoin(user, eq(user.id, issue.delegateUserId))
- .where(eq(issue.projectId, projectId))
+ .where(onBoard(projectId))
.groupBy(user.id, user.name)
.orderBy(desc(sql`count(${issue.id})`));
return rows.map((r) => ({ key: r.key, label: r.label, count: r.count, color: null }));
@@ -196,7 +202,7 @@ export async function getBreakdown(projectId: number, by: BreakdownBy): Promise<
})
.from(issue)
.leftJoin(user, eq(user.id, issue.assigneeUserId))
- .where(eq(issue.projectId, projectId))
+ .where(onBoard(projectId))
.groupBy(user.id, user.name)
.orderBy(desc(sql`count(${issue.id})`));
return rows.map((r) => ({ key: r.key, label: r.label, count: r.count, color: null }));
@@ -784,7 +790,7 @@ export async function getAgentWorkload(projectId: number): Promise<AgentWorkload
.select({ userId: issue.delegateUserId, count: sql<number>`count(*)::int` })
.from(issue)
.innerJoin(projectColumn, eq(projectColumn.id, issue.columnId))
- .where(and(eq(issue.projectId, projectId), isNotNull(issue.delegateUserId), OPEN_STATES))
+ .where(and(onBoard(projectId), isNotNull(issue.delegateUserId), OPEN_STATES))
.groupBy(issue.delegateUserId);
const delegatedByUser = new Map(delegatedRows.map((r) => [r.userId, r.count]));