Skip to content

Add time-based filtering to Popular Links#228

Open
dfcarney wants to merge 4 commits into
mainfrom
dfcarney/popular-links-time-filter
Open

Add time-based filtering to Popular Links#228
dfcarney wants to merge 4 commits into
mainfrom
dfcarney/popular-links-time-filter

Conversation

@dfcarney
Copy link
Copy Markdown
Member

@dfcarney dfcarney commented Apr 9, 2026

Summary

  • Adds 7-day, 30-day, and all-time filter buttons to the Popular Links section on the home page
  • Adds LoadStatsSince DB method backed by a single Stats/Links join query and an index on Stats(Created) for efficient time-filtered aggregation
  • Parses period as a bounded duration so pre-populated filters and arbitrary positive periods up to 365 days are supported without allowing unbounded full-history DB scans
  • Time-filtered queries read directly from the DB without calling flushStats, avoiding serialization of home page requests under load (stats are at most ~1 minute stale via the periodic flush loop)
  • Closes DB result sets in the new query path and adjacent DB readers

Test plan

  • New unit test for LoadStatsSince in db_test.go, including stale stats for missing links
  • New integration test TestServeHomePeriodFilter in golink_test.go covering all-time, 7d, 30d, arbitrary duration, and invalid period fallback
  • New unit test coverage for parseStatsPeriod, including bounds and overflow cases
  • go test ./...
  • go test -race ./...
  • Local UI screenshots captured for all-time and 7-day filter states

@dfcarney dfcarney requested a review from mikeodr April 9, 2026 11:19
@dfcarney dfcarney force-pushed the dfcarney/popular-links-time-filter branch from 5d26dad to 6c8099a Compare April 9, 2026 11:19
@mikeodr mikeodr requested a review from Copilot April 9, 2026 12:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds time-based filtering (7-day, 30-day, all-time) to the “Popular Links” section on the home page, backed by a new DB query path and an index to support efficient time-windowed aggregation.

Changes:

  • Adds period filter links to the home page template and plumbs the selected period through homeData.
  • Implements SQLiteDB.LoadStatsSince(since) and wires serveHome to use it for 7d/30d filters.
  • Updates DB schema with an index on Stats(Created) and adds unit/integration tests for the new filtering behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tmpl/home.html Adds UI filter links and active-state styling based on .Period.
golink.go Reads ?period= and uses DB-backed stats for 7d/30d; passes .Period to the template.
db.go Introduces LoadStatsSince to aggregate clicks since a given timestamp.
schema.sql Adds an index on Stats.Created for time-based filtering queries.
db_test.go Adds unit test coverage for LoadStatsSince.
golink_test.go Adds integration test coverage for home page period filtering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread db.go
Comment thread db.go Outdated
Comment thread db.go Outdated
Comment thread golink_test.go Outdated
Comment thread golink_test.go Outdated
Copy link
Copy Markdown
Contributor

@mikeodr mikeodr left a comment

Choose a reason for hiding this comment

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

My concerns largely align with the copilot check.

There's some cleanup missing in the queries, and doing a full load could be a bit more optimized.

Any screenshots of what the UIs look like would be helpful as well.

Comment thread db.go Outdated
Comment thread golink.go Outdated
@dfcarney dfcarney force-pushed the dfcarney/popular-links-time-filter branch from 8472512 to 481475b Compare May 20, 2026 15:38
dfcarney added 4 commits May 20, 2026 11:38
Allow users to filter the home page Popular Links table by 7 days,
30 days, or all time via a `?period=` query parameter. This uses
a new `LoadStatsSince` DB method with a `WHERE Created >= ?` clause,
backed by an index on `Stats.Created` for performance.

Signed-off-by: David Carney <dfcarney@tailscale.com>
Avoid calling flushStats() on every ?period=7d/30d request, which
would serialize home page requests under load. The periodic flush
loop (every ~1 minute) keeps the DB sufficiently up to date.

Signed-off-by: David Carney <dfcarney@tailscale.com>
Signed-off-by: David Carney <dfcarney@tailscale.com>
Signed-off-by: David Carney <dfcarney@tailscale.com>
@dfcarney dfcarney force-pushed the dfcarney/popular-links-time-filter branch from 481475b to a54090a Compare May 20, 2026 15:39
@dfcarney dfcarney requested a review from mikeodr May 20, 2026 15:40
@dfcarney
Copy link
Copy Markdown
Member Author

Updated this branch and addressed the outstanding review feedback.

Changes since the prior review:

  • Rebased onto the latest main so the branch is no longer out of date.
  • Reworked LoadStatsSince to use a single Stats/Links join, which removes the per-request LoadAll and naturally excludes stats for deleted/missing links.
  • Added defer rows.Close() in the new query path and adjacent DB readers.
  • Changed period parsing to support arbitrary positive durations with a 365-day cap.
  • Fixed ignored errors in the new home-period integration test.
  • Added tests for missing-link stats, arbitrary duration periods, invalid fallback, bounds, and overflow cases.

Local verification:

  • go test ./...
  • go test -race ./...
  • UI spot check for all-time and 7-day filter states using a seeded dev DB.

@dfcarney dfcarney requested a review from willnorris May 20, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants