Fix feed pagination

And tweak perf log output
This commit is contained in:
Ben Visness 2021-04-28 22:34:22 -05:00
parent ce582df610
commit 314ae26e18
5 changed files with 15 additions and 14 deletions

View File

@ -118,8 +118,6 @@ func PeriodicallyDeleteExpiredSessions(ctx context.Context, conn *pgxpool.Pool)
if err == nil {
if n > 0 {
logging.Info().Int64("num deleted sessions", n).Msg("Deleted expired sessions")
} else {
logging.Debug().Msg("no sessions to delete")
}
} else {
logging.Error().Err(err).Msg("Failed to delete expired sessions")

View File

@ -7,15 +7,17 @@ import (
type RequestPerf struct {
Route string
Path string // the path actually matched
Start time.Time
End time.Time
Blocks []PerfBlock
}
func MakeNewRequestPerf(route string) *RequestPerf {
func MakeNewRequestPerf(route string, path string) *RequestPerf {
return &RequestPerf{
Start: time.Now(),
Route: route,
Path: path,
}
}

View File

@ -1,6 +1,7 @@
package website
import (
"fmt"
"math"
"net/http"
"strconv"
@ -8,7 +9,6 @@ import (
"git.handmade.network/hmn/hmn/src/db"
"git.handmade.network/hmn/hmn/src/hmnurl"
"git.handmade.network/hmn/hmn/src/logging"
"git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/oops"
"git.handmade.network/hmn/hmn/src/templates"
@ -62,7 +62,10 @@ func Feed(c *RequestContext) ResponseData {
Current: page,
Total: numPages,
// TODO: urls
FirstUrl: hmnurl.Url("/feed", nil),
LastUrl: hmnurl.Url(fmt.Sprintf("/feed/%d", numPages), nil),
NextUrl: hmnurl.Url(fmt.Sprintf("/feed/%d", page+1), nil),
PreviousUrl: hmnurl.Url(fmt.Sprintf("/feed/%d", page-1), nil),
}
var currentUserId *int
@ -128,7 +131,6 @@ func Feed(c *RequestContext) ResponseData {
}
parents := postResult.Cat.GetHierarchy(c.Context(), c.Conn)
logging.Debug().Interface("parents", parents).Msg("")
var breadcrumbs []templates.Breadcrumb
breadcrumbs = append(breadcrumbs, templates.Breadcrumb{

View File

@ -68,11 +68,7 @@ func (rb *RouteBuilder) POST(regexStr string, h Handler) {
}
func (rb *RouteBuilder) StdHandler(regexStr string, h http.Handler) {
rb.Router.Routes = append(rb.Router.Routes, Route{
Method: "",
Regex: regexp.MustCompile(regexStr),
Handler: WrapStdHandler(h),
})
rb.Handle("", regexStr, WrapStdHandler(h))
}
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
@ -88,7 +84,7 @@ func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
c := &RequestContext{
Route: "", // TODO
Route: route.Regex.String(),
Logger: logging.GlobalLogger(),
Req: req,
}

View File

@ -72,6 +72,9 @@ func NewWebsiteRoutes(conn *pgxpool.Pool, perfCollector *perf.PerfCollector) htt
})
mainRoutes.GET(`^/feed(/(?P<page>.+)?)?$`, Feed)
// mainRoutes.GET(`^/(?P<cats>forums(/cat)*)$`, Category)
// mainRoutes.GET(`^/(?P<cats>forums(/cat)*)/t/(?P<threadid>\d+)/p/(?P<postid>\d+)$`, ForumPost)
mainRoutes.GET("^/assets/project.css$", ProjectCSS)
mainRoutes.AnyMethod("", FourOhFour)
@ -212,7 +215,7 @@ func getCurrentUser(c *RequestContext, sessionId string) (*models.User, error) {
}
func TrackRequestPerf(c *RequestContext, perfCollector *perf.PerfCollector) (after func()) {
c.Perf = perf.MakeNewRequestPerf(c.Route)
c.Perf = perf.MakeNewRequestPerf(c.Route, c.Req.URL.Path)
return func() {
c.Perf.EndRequest()
log := logging.Info()
@ -224,7 +227,7 @@ func TrackRequestPerf(c *RequestContext, perfCollector *perf.PerfCollector) (aft
log.Str(fmt.Sprintf("At %9.2fms", c.Perf.MsFromStart(&block)), fmt.Sprintf("%*.s[%s] %s (%.4fms)", len(blockStack)*2, "", block.Category, block.Description, block.DurationMs()))
blockStack = append(blockStack, block.End)
}
log.Msg(fmt.Sprintf("Served %s in %.4fms", c.Perf.Route, float64(c.Perf.End.Sub(c.Perf.Start).Nanoseconds())/1000/1000))
log.Msg(fmt.Sprintf("Served %s in %.4fms", c.Perf.Path, float64(c.Perf.End.Sub(c.Perf.Start).Nanoseconds())/1000/1000))
perfCollector.SubmitRun(c.Perf)
}
}