Skip to content

feat(analytics): add date range filtering to analytics endpoints usin… - #868

Open
jikrana1 wants to merge 1 commit into
Userunknown84:mainfrom
jikrana1:feat/add-date-range-filtering-analytics-854
Open

feat(analytics): add date range filtering to analytics endpoints usin…#868
jikrana1 wants to merge 1 commit into
Userunknown84:mainfrom
jikrana1:feat/add-date-range-filtering-analytics-854

Conversation

@jikrana1

Copy link
Copy Markdown
Contributor

Closes #854

Problem description

The analytics endpoints currently process the user's complete history without allowing any date filtering. As the amount of stored history grows, analytics queries become slower and users cannot generate reports for specific periods such as the last 7 days, last 30 days, 90 days, or a custom date range.

Proposed solution

Added optional query parameter support (from, to, last) to the getSummary, getTrends, getBreakdown, and getPersonalSummary endpoints.

Implementation details:

  • Created a reusable buildDateFilter helper function to parse and validate from, to, and last.
  • Applied strict validation: from/to must be valid dates, last must be a positive integer.
  • Return 400 Bad Request with descriptive error messages for invalid combinations.
  • Merged the date filters into the $match stage of all 4 aggregation pipelines, ensuring createdAt is filtered efficiently before any grouping occurs.
  • Preserved the existing behavior perfectly when no date filters are provided.

Benefits

  • ✅ Improves analytics performance by reducing dataset size.
  • ✅ Allows users to analyze specific time periods.
  • ✅ Makes the analytics API more flexible and user-friendly.

Testing instructions

  1. Call /analytics/summary?last=7. It should return stats for the last 7 days.
  2. Call /analytics/summary?from=2026-01-01&to=2026-06-30. It should return stats for that specific 6-month range.
  3. Call /analytics/summary?last=7&from=2026-01-01. It should return a 400 Bad Request error (cannot combine last with from/to).
  4. Call the endpoints without any parameters. They should continue working exactly as before.

@vercel

vercel Bot commented Jul 14, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Aditya Sharma's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Userunknown84 Userunknown84 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge Conflict shown kindly fix this

@Userunknown84 Userunknown84 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge conflict shown kindly fix this.

const counts = await History.aggregate([
{
$match: {
user: userId,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye part delete h sabhi parts me

@Userunknown84

Copy link
Copy Markdown
Owner

@jikrana1 fix the issue then i will merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Date Range Filtering to Analytics Endpoints

2 participants