-
Notifications
You must be signed in to change notification settings - Fork 140
Add time-based filtering to Popular Links #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import ( | |
| "path/filepath" | ||
| "regexp" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| texttemplate "text/template" | ||
|
|
@@ -45,6 +46,7 @@ import ( | |
|
|
||
| const ( | ||
| defaultHostname = "go" | ||
| maxStatsPeriod = 365 * 24 * time.Hour | ||
|
|
||
| // Used as a placeholder short name for generating the XSRF defense token, | ||
| // when creating new links. | ||
|
|
@@ -344,6 +346,7 @@ type homeData struct { | |
| XSRF string | ||
| ReadOnly bool | ||
| User string | ||
| Period string | ||
| } | ||
|
|
||
| // deleteData is the data used by deleteTmpl. | ||
|
|
@@ -535,14 +538,32 @@ func serveHandler() http.Handler { | |
| func serveHome(w http.ResponseWriter, r *http.Request, short string) { | ||
| var clicks []visitData | ||
|
|
||
| stats.mu.Lock() | ||
| for short, numClicks := range stats.clicks { | ||
| clicks = append(clicks, visitData{ | ||
| Short: short, | ||
| NumClicks: numClicks, | ||
| }) | ||
| period := r.URL.Query().Get("period") | ||
| if periodDuration, ok := parseStatsPeriod(period); ok { | ||
| since := db.Now().Add(-periodDuration) | ||
|
|
||
| clickStats, err := db.LoadStatsSince(since) | ||
| if err != nil { | ||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
| for s, numClicks := range clickStats { | ||
| clicks = append(clicks, visitData{ | ||
| Short: s, | ||
| NumClicks: numClicks, | ||
| }) | ||
| } | ||
| } else { | ||
| period = "" | ||
| stats.mu.Lock() | ||
| for short, numClicks := range stats.clicks { | ||
| clicks = append(clicks, visitData{ | ||
| Short: short, | ||
| NumClicks: numClicks, | ||
| }) | ||
| } | ||
| stats.mu.Unlock() | ||
|
Comment on lines
+542
to
+565
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: I know it's in the PR description of a 1 minute potential staleness. A comment here describing the delta would help also for future devs. |
||
| } | ||
| stats.mu.Unlock() | ||
|
|
||
| sort.Slice(clicks, func(i, j int) bool { | ||
| if clicks[i].NumClicks != clicks[j].NumClicks { | ||
|
|
@@ -580,9 +601,26 @@ func serveHome(w http.ResponseWriter, r *http.Request, short string) { | |
| XSRF: xsrftoken.Generate(xsrfKey, cu.login, newShortName), | ||
| ReadOnly: *readonly, | ||
| User: cu.login, | ||
| Period: period, | ||
| }) | ||
| } | ||
|
|
||
| func parseStatsPeriod(period string) (time.Duration, bool) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue(minor, non-blocking): I think there's a subtle UI bug here that may or may not be worth addressing. If someone hits the url with But if that URL is hit with some other period that is 7 days |
||
| if period == "" { | ||
| return 0, false | ||
| } | ||
| if d, err := time.ParseDuration(period); err == nil && d > 0 && d <= maxStatsPeriod { | ||
| return d, true | ||
| } | ||
| if days, ok := strings.CutSuffix(period, "d"); ok { | ||
| n, err := strconv.Atoi(days) | ||
| if err == nil && n > 0 && n <= int(maxStatsPeriod/(24*time.Hour)) { | ||
| return time.Duration(n) * 24 * time.Hour, true | ||
| } | ||
| } | ||
| return 0, false | ||
| } | ||
|
|
||
| func serveAll(w http.ResponseWriter, _ *http.Request) { | ||
| if err := flushStats(); err != nil { | ||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.