From cff2e96915342da46826f7e06728f0e7430c1a81 Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 4 May 2021 21:14:48 +1000 Subject: [PATCH] add Count() method to PollStore show daily task count on debug page optimise count queries on debug page use Count() methods on debug page where ever possible --- common/pages.go | 12 +++++++++--- common/poll_store.go | 10 +++++++--- misc_test.go | 3 +++ routes/panel/debug.go | 41 +++++++++++++++++++++++++---------------- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/common/pages.go b/common/pages.go index ff2120ba..bd9c4c97 100644 --- a/common/pages.go +++ b/common/pages.go @@ -744,6 +744,7 @@ type DebugPageTasks struct { Second int FifteenMinute int Hour int + Day int Shutdown int } @@ -813,13 +814,18 @@ type PanelDebugPage struct { Disk DebugPageDisk } -type PanelDebugTaskTask struct { +type PanelTaskTask struct { Name string Type int // 0 = halfsec, 1 = sec, 2 = fifteenmin, 3 = hour, 4 = shutdown } -type PanelDebugTaskPage struct { +type PanelTaskType struct { + Name string + FAvg string +} +type PanelTaskPage struct { *BasePanelPage - Tasks []PanelDebugTaskTask + Tasks []PanelTaskTask + Types []PanelTaskType } type PageSimple struct { diff --git a/common/poll_store.go b/common/poll_store.go index 5cdb8504..9ced8e7b 100644 --- a/common/poll_store.go +++ b/common/poll_store.go @@ -29,7 +29,7 @@ type PollStore interface { ClearIPs() error Create(parent Pollable, pollType int, pollOptions map[int]string) (int, error) Reload(id int) error - //Count() int + Count() int SetCache(cache PollCache) GetCache() PollCache @@ -43,7 +43,7 @@ type DefaultPollStore struct { createPoll *sql.Stmt createPollOption *sql.Stmt delete *sql.Stmt - //count *sql.Stmt + count *sql.Stmt clearIPs *sql.Stmt } @@ -61,7 +61,7 @@ func NewDefaultPollStore(cache PollCache) (*DefaultPollStore, error) { exists: acc.Select(p).Columns("pollID").Where("pollID=?").Stmt(), createPoll: acc.Insert(p).Columns("parentID,parentTable,type,options").Fields("?,?,?,?").Prepare(), createPollOption: acc.Insert("polls_options").Columns("pollID,option,votes").Fields("?,?,0").Prepare(), - //count: acc.SimpleCount(p, "", ""), + count: acc.Count(p).Prepare(), clearIPs: acc.Update("polls_votes").Set("ip=''").Where("ip!=''").Stmt(), }, acc.FirstError() @@ -231,6 +231,10 @@ func (s *DefaultPollStore) Create(parent Pollable, pollType int, pollOptions map return id, parent.SetPoll(id) // TODO: Delete the poll (and options) if SetPoll fails } +func (s *DefaultPollStore) Count() int { + return Count(s.count) +} + func (s *DefaultPollStore) SetCache(cache PollCache) { s.cache = cache } diff --git a/misc_test.go b/misc_test.go index 2baa4b80..e0f32930 100644 --- a/misc_test.go +++ b/misc_test.go @@ -1500,6 +1500,7 @@ func TestPolls(t *testing.T) { shouldNotExist(-1) shouldNotExist(0) shouldNotExist(1) + exf(c.Polls.Count() == 0, "count should be %d not %d", 0, c.Polls.Count()) tid, e := c.Topics.Create(2, "Poll Test", "Filler Body", 1, "") expectNilErr(t, e) @@ -1513,6 +1514,7 @@ func TestPolls(t *testing.T) { expectNilErr(t, e) exf(pid == 1, "poll id should be 1 not %d", pid) ex(c.Polls.Exists(1), "poll 1 should exist") + exf(c.Polls.Count() == 1, "count should be %d not %d", 1, c.Polls.Count()) testPoll := func(p *c.Poll, id, parentID int, parentTable string, ptype int, antiCheat bool, voteCount int) { ef := exf @@ -1546,6 +1548,7 @@ func TestPolls(t *testing.T) { ex(!c.Polls.Exists(1), "poll 1 should no longer exist") _, e = c.Polls.Get(1) recordMustNotExist(t, e, "poll 1 should no longer exist") + exf(c.Polls.Count() == 0, "count should be %d not %d", 0, c.Polls.Count()) } func TestSearch(t *testing.T) { diff --git a/routes/panel/debug.go b/routes/panel/debug.go index 03c79948..134b3aed 100644 --- a/routes/panel/debug.go +++ b/routes/panel/debug.go @@ -10,8 +10,8 @@ import ( qgen "github.com/Azareal/Gosora/query_gen" ) -func Debug(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError { - basePage, ferr := buildBasePage(w, r, user, "debug", "debug") +func Debug(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError { + bp, ferr := buildBasePage(w, r, u, "debug", "debug") if ferr != nil { return ferr } @@ -46,7 +46,7 @@ func Debug(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError { cpus := runtime.NumCPU() httpConns := c.ConnWatch.Count() - debugTasks := c.DebugPageTasks{c.Tasks.HalfSec.Count(), c.Tasks.Sec.Count(), c.Tasks.FifteenMin.Count(), c.Tasks.Hour.Count(), c.Tasks.Shutdown.Count()} + debugTasks := c.DebugPageTasks{c.Tasks.HalfSec.Count(), c.Tasks.Sec.Count(), c.Tasks.FifteenMin.Count(), c.Tasks.Hour.Count(), c.Tasks.Day.Count(), c.Tasks.Shutdown.Count()} var memStats runtime.MemStats runtime.ReadMemStats(&memStats) @@ -69,11 +69,12 @@ func Debug(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError { debugCache := c.DebugPageCache{tlen, ulen, rlen, tcap, ucap, rcap, topicListThawed} var fErr error + acc := qgen.NewAcc() count := func(tbl string) int { if fErr != nil { return 0 } - c, err := qgen.NewAcc().Count(tbl).Total() + c, err := acc.Count(tbl).Total() fErr = err return c } @@ -81,12 +82,19 @@ func Debug(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError { // TODO: Call Count on an attachment store attachs := count("attachments") // TODO: Implement a PollStore and call Count on that instead - polls := count("polls") + //polls := count("polls") + polls := c.Polls.Count() + //pollsOptions := count("polls_options") // TODO: Add this + //pollsVotes := count("polls_votes") // TODO: Add this - loginLogs := count("login_logs") - regLogs := count("registration_logs") - modLogs := count("moderation_logs") - adminLogs := count("administration_logs") + //loginLogs := count("login_logs") + loginLogs := c.LoginLogs.Count() + //regLogs := count("registration_logs") + regLogs := c.RegLogs.Count() + //modLogs := count("moderation_logs") + modLogs := c.ModLogs.Count() + //adminLogs := count("administration_logs") + adminLogs := c.AdminLogs.Count() views := count("viewchunks") viewsAgents := count("viewchunks_agents") @@ -125,18 +133,19 @@ func Debug(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError { debugDisk := c.DebugPageDisk{staticSize, attachSize, uploadsSize, logsSize, backupsSize, gitSize} - pi := c.PanelDebugPage{basePage, goVersion, dbVersion, uptime, openConnCount, qgen.Builder.GetAdapter().GetName(), goroutines, cpus, httpConns, debugTasks, memStats, debugCache, debugDatabase, debugDisk} - return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "panel_dashboard_right", "debug_page", "panel_debug", pi}) + pi := c.PanelDebugPage{bp, goVersion, dbVersion, uptime, openConnCount, qgen.Builder.GetAdapter().GetName(), goroutines, cpus, httpConns, debugTasks, memStats, debugCache, debugDatabase, debugDisk} + return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "panel_dashboard_right", "debug_page", "panel_debug", pi}) } -func DebugTasks(w http.ResponseWriter, r *http.Request, user *c.User) c.RouteError { - basePage, ferr := buildBasePage(w, r, user, "debug", "debug") +func DebugTasks(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError { + bp, ferr := buildBasePage(w, r, u, "debug", "debug") if ferr != nil { return ferr } - var debugTasks []c.PanelDebugTaskTask + var tasks []c.PanelTaskTask + var taskTypes []c.PanelTaskType - pi := c.PanelDebugTaskPage{basePage, debugTasks} - return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "panel_dashboard_right", "debug_page", "panel_debug_task", pi}) + pi := c.PanelTaskPage{bp, tasks, taskTypes} + return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "panel_dashboard_right", "debug_page", "panel_debug_task", pi}) }