From 6694c23859663cbdeb7c6233118b336360a81a41 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 25 Nov 2018 08:47:40 -0500 Subject: [PATCH] better query --- contrib/backends/srndv2/src/srnd/database.go | 16 +++++--- contrib/backends/srndv2/src/srnd/model.go | 3 ++ contrib/backends/srndv2/src/srnd/postgres.go | 17 ++++++++ .../srndv2/src/srnd/templates_impl.go | 39 ++++++++----------- 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/contrib/backends/srndv2/src/srnd/database.go b/contrib/backends/srndv2/src/srnd/database.go index 0cfb6de..0fa10f8 100644 --- a/contrib/backends/srndv2/src/srnd/database.go +++ b/contrib/backends/srndv2/src/srnd/database.go @@ -47,13 +47,12 @@ func (self PostEntry) Count() int64 { return self[1] } +type PostEntryList []PostEntry + + // stats about newsgroup postings type NewsgroupStats struct { - Posted []PostEntry - Delted []PostEntry - Hits []PostEntry - Start time.Time - End time.Time + PPD int64 Name string } @@ -70,6 +69,8 @@ type NewsgroupListEntry [3]string type NewsgroupList []NewsgroupListEntry + + type Database interface { Close() CreateTables() @@ -81,7 +82,7 @@ type Database interface { GetAllArticlesInGroup(group string, send chan ArticleEntry) CountAllArticlesInGroup(group string) (int64, error) GetAllArticles() []ArticleEntry - + SetConnectionLifetime(seconds int) SetMaxOpenConns(n int) SetMaxIdleConns(n int) @@ -124,6 +125,9 @@ type Database interface { // if N <= 0 then count all we have now CountPostsInGroup(group string, time_frame int64) int64 + // get the stats for the overview page + GetNewsgroupStats() ([]NewsgroupStats, error) + // get all replies to a thread // if last > 0 then get that many of the last replies // start at reply number start diff --git a/contrib/backends/srndv2/src/srnd/model.go b/contrib/backends/srndv2/src/srnd/model.go index a177f85..d4967e0 100644 --- a/contrib/backends/srndv2/src/srnd/model.go +++ b/contrib/backends/srndv2/src/srnd/model.go @@ -224,6 +224,8 @@ type boardPageRow struct { Hour int64 Day int64 All int64 + Hi int64 + Lo int64 } type boardPageRows []boardPageRow @@ -242,6 +244,7 @@ func (self boardPageRows) Swap(i, j int) { self[i], self[j] = self[j], self[i] } + type postsGraphRow struct { day time.Time Num int64 diff --git a/contrib/backends/srndv2/src/srnd/postgres.go b/contrib/backends/srndv2/src/srnd/postgres.go index 2fadb7c..29385b2 100644 --- a/contrib/backends/srndv2/src/srnd/postgres.go +++ b/contrib/backends/srndv2/src/srnd/postgres.go @@ -151,9 +151,11 @@ const GetCitesByPostHashLike = "GetCitesByPostHashLike" const GetYearlyPostHistory = "GetYearlyPostHistory" const GetNewsgroupList = "GetNewsgroupList" const CountUkko = "CountUkko" +const GetNewsgroupStats = "GetNewsgroupStats" func (self *PostgresDatabase) prepareStatements() { self.stmt = map[string]string{ + GetNewsgroupStats: "SELECT COUNT(message_id), newsgroup FROM articleposts WHERE time_posted > (EXTRACT(epoch FROM NOW()) - (24*3600)) GROUP BY newsgroup", NewsgroupBanned: "SELECT 1 FROM BannedGroups WHERE newsgroup = $1", ArticleBanned: "SELECT 1 FROM BannedArticles WHERE message_id = $1", GetAllNewsgroups: "SELECT name FROM Newsgroups WHERE name NOT IN ( SELECT newsgroup FROM BannedGroups )", @@ -2039,6 +2041,21 @@ func (self *PostgresDatabase) GetUkkoPageCount(perpage int) (count int64, err er return } +func (self *PostgresDatabase) GetNewsgroupStats() (stats []NewsgroupStats, err error) { + var rows *sql.Rows + rows, err = self.conn.Query(self.stmt[GetNewsgroupStats]) + if err == nil { + for rows.Next() { + var s NewsgroupStats + rows.Scan(&s.PPD, &s.Name) + stats = append(stats, s) + } + rows.Close() + } + return +} + + func (self *PostgresDatabase) FindHeaders(group, headername string, lo, hi int64) (hdr ArticleHeaders, err error) { hdr = make(ArticleHeaders) q := "SELECT header_value FROM nntpheaders WHERE header_name = $1 AND header_article_message_id IN ( SELECT message_id FROM articleposts WHERE newsgroup = $2 )" diff --git a/contrib/backends/srndv2/src/srnd/templates_impl.go b/contrib/backends/srndv2/src/srnd/templates_impl.go index ff9153f..7541933 100644 --- a/contrib/backends/srndv2/src/srnd/templates_impl.go +++ b/contrib/backends/srndv2/src/srnd/templates_impl.go @@ -475,36 +475,29 @@ func (self *templateEngine) genGraphs(prefix string, wr io.Writer, db Database, func (self *templateEngine) genBoardList(prefix, name string, wr io.Writer, db Database, i18n *I18N) { // the graph for the front page - var frontpage_graph boardPageRows + var graph boardPageRows - // for each group - groups := db.GetAllNewsgroups() - for _, group := range groups { - // exclude banned - banned, _ := db.NewsgroupBanned(group) - if banned { - continue - } - // posts this hour - hour := db.CountPostsInGroup(group, 3600) - // posts today - day := db.CountPostsInGroup(group, 86400) - // posts total - all := db.CountPostsInGroup(group, 0) - frontpage_graph = append(frontpage_graph, boardPageRow{ - All: all, - Day: day, - Hour: hour, - Board: group, + stats, err := db.GetNewsgroupStats() + if err != nil { + log.Println("error getting board list", err) + io.WriteString(wr, err.Error()) + return + } + + for idx := range stats { + graph = append(graph, boardPageRow{ + Board: stats[idx].Name, + Day: stats[idx].PPD, }) } + param := map[string]interface{}{ "prefix": prefix, "frontend": name, } - sort.Sort(frontpage_graph) - param["graph"] = frontpage_graph - _, err := io.WriteString(wr, self.renderTemplate("boardlist", param, i18n)) + sort.Sort(graph) + param["graph"] = graph + _, err = io.WriteString(wr, self.renderTemplate("boardlist", param, i18n)) if err != nil { log.Println("error writing board list page", err) }