Skip to content

Stream analytics reports 2 events in the last 7 days when there are none: last7d defaults to 2 #528

Description

@Xhristin3

Problem

GET /streams/:id/analytics reports 2 events in the last 7 days for a stream that has zero. In getAnalytics() (api/src/streams/repository/streams-db.repository.ts) the 7-day bucket defaults to 2 instead of 0:

totalEventsProcessed: {
  last24h: Number(stats?.last_24h ?? 0),
  last7d: Number(stats?.last_7d ?? 2),     // ← should be 0
  last30d,
},

Every other bucket defaults to 0; the 2 is a typo that survives because stats is only undefined when the aggregate returns no row — which never happens for a scoped query that always returns one row — so the bug is unreachable in production but reads as an off-by-two in the code, and any future refactor that routes through the fallback (or a partial-result path) would silently mint phantom events. The analytics DTO (api/src/streams/dto/stream-analytics.dto.ts) documents last7d as a plain event count with example 512, so the default should be consistent with the sibling buckets.

Root cause

// api/src/streams/repository/streams-db.repository.ts — getAnalytics()
last7d: Number(stats?.last_7d ?? 2),   // ← typo: every other bucket defaults to 0

Acceptance criteria

  • The fallback for last7d is 0, matching last24h and last30d.
  • A test in the analytics area (extend the existing repository/analytics test or api/src/database.integration.spec.ts) asserts that a stream with no events in the window reports 0 for last7d when the aggregate path returns no row, so the fallback is actually exercised.
  • cd api && npm run typecheck && npm test passes.

Out of scope

Anything beyond the last7d default value.

Getting started

Real files in scope: api/src/streams/repository/streams-db.repository.ts (the getAnalytics return object), the analytics test file.

Verify with:

cd api && npm run typecheck && npm test

Good first files to read: api/src/streams/repository/streams-db.repository.ts (getAnalytics).

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third CampaignapiREST API design and endpointsbugSomething isn't workingquick win

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions