From 6634f3f64b5c85aad9c25c58abddf6b731c8b57a Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 2 Oct 2018 15:03:20 +1000 Subject: [PATCH] All the control panel routes have been moved to /routes/panel/ now. Moved the software version to common.go for better accessibility. --- common/common.go | 2 + gen_mssql.go | 4 - gen_mysql.go | 4 - gen_pgsql.go | 4 - gen_router.go | 8 +- main.go | 3 +- mssql.go | 29 +----- mysql.go | 25 ------ query_gen/lib/mssql.go | 4 - query_gen/lib/mysql.go | 4 - query_gen/lib/pgsql.go | 4 - router_gen/routes.go | 2 +- panel_routes.go => routes/panel/dashboard.go | 93 +++++++++++++++----- 13 files changed, 79 insertions(+), 107 deletions(-) rename panel_routes.go => routes/panel/dashboard.go (75%) diff --git a/common/common.go b/common/common.go index 5a7f4273..24c44c89 100644 --- a/common/common.go +++ b/common/common.go @@ -9,6 +9,8 @@ import ( "../query_gen/lib" ) +var SoftwareVersion = Version{Major: 0, Minor: 2, Patch: 0, Tag: "dev"} + // nolint I don't want to write comments for each of these o.o const Hour int = 60 * 60 const Day int = Hour * 24 diff --git a/gen_mssql.go b/gen_mssql.go index 378f4f1a..8c6d0bcd 100644 --- a/gen_mssql.go +++ b/gen_mssql.go @@ -20,10 +20,6 @@ type Stmts struct { getActivityFeedByWatcher *sql.Stmt getActivityCountByWatcher *sql.Stmt - todaysPostCount *sql.Stmt - todaysTopicCount *sql.Stmt - todaysTopicCountByForum *sql.Stmt - todaysNewUserCount *sql.Stmt Mocks bool } diff --git a/gen_mysql.go b/gen_mysql.go index 2a940d83..d9138e70 100644 --- a/gen_mysql.go +++ b/gen_mysql.go @@ -22,10 +22,6 @@ type Stmts struct { getActivityFeedByWatcher *sql.Stmt getActivityCountByWatcher *sql.Stmt - todaysPostCount *sql.Stmt - todaysTopicCount *sql.Stmt - todaysTopicCountByForum *sql.Stmt - todaysNewUserCount *sql.Stmt Mocks bool } diff --git a/gen_pgsql.go b/gen_pgsql.go index 15a1cf8d..9a479f97 100644 --- a/gen_pgsql.go +++ b/gen_pgsql.go @@ -16,10 +16,6 @@ type Stmts struct { getActivityFeedByWatcher *sql.Stmt getActivityCountByWatcher *sql.Stmt - todaysPostCount *sql.Stmt - todaysTopicCount *sql.Stmt - todaysTopicCountByForum *sql.Stmt - todaysNewUserCount *sql.Stmt Mocks bool } diff --git a/gen_router.go b/gen_router.go index fa1faf59..8ec77f83 100644 --- a/gen_router.go +++ b/gen_router.go @@ -101,7 +101,7 @@ var RouteMap = map[string]interface{}{ "panel.LogsRegs": panel.LogsRegs, "panel.LogsMod": panel.LogsMod, "panel.Debug": panel.Debug, - "routePanelDashboard": routePanelDashboard, + "panel.Dashboard": panel.Dashboard, "routes.AccountEdit": routes.AccountEdit, "routes.AccountEditPassword": routes.AccountEditPassword, "routes.AccountEditPasswordSubmit": routes.AccountEditPasswordSubmit, @@ -232,7 +232,7 @@ var routeMapEnum = map[string]int{ "panel.LogsRegs": 75, "panel.LogsMod": 76, "panel.Debug": 77, - "routePanelDashboard": 78, + "panel.Dashboard": 78, "routes.AccountEdit": 79, "routes.AccountEditPassword": 80, "routes.AccountEditPasswordSubmit": 81, @@ -361,7 +361,7 @@ var reverseRouteMapEnum = map[int]string{ 75: "panel.LogsRegs", 76: "panel.LogsMod", 77: "panel.Debug", - 78: "routePanelDashboard", + 78: "panel.Dashboard", 79: "routes.AccountEdit", 80: "routes.AccountEditPassword", 81: "routes.AccountEditPasswordSubmit", @@ -1442,7 +1442,7 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u err = panel.Debug(w,req,user) default: counters.RouteViewCounter.Bump(78) - err = routePanelDashboard(w,req,user) + err = panel.Dashboard(w,req,user) } case "/user": switch(req.URL.Path) { diff --git a/main.go b/main.go index f6fa6b21..7e8451ff 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,6 @@ import ( "github.com/pkg/errors" ) -var version = common.Version{Major: 0, Minor: 2, Patch: 0, Tag: "dev"} var router *GenRouter var logWriter = io.MultiWriter(os.Stderr) @@ -224,7 +223,7 @@ func main() { } logWriter = io.MultiWriter(os.Stderr, f) log.SetOutput(logWriter) - log.Print("Running Gosora v" + version.String()) + log.Print("Running Gosora v" + common.SoftwareVersion.String()) fmt.Println("") common.StartTime = time.Now() diff --git a/mssql.go b/mssql.go index dcde2cba..dd6bafd4 100644 --- a/mssql.go +++ b/mssql.go @@ -10,7 +10,6 @@ package main import ( "database/sql" - "log" "net/url" "./common" @@ -74,7 +73,7 @@ func initMSSQL() (err error) { // TODO: Is there a less noisy way of doing this for tests? /*log.Print("Preparing getActivityFeedByWatcher statement.") - stmts.getActivityFeedByWatcherStmt, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID FROM [activity_stream_matches] INNER JOIN [activity_stream] ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE [watcher] = ? ORDER BY activity_stream.asid DESC OFFSET 0 ROWS FETCH NEXT 8 ROWS ONLY") + stmts.getActivityFeedByWatcherStmt, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID FROM [activity_stream_matches] INNER JOIN [activity_stream] ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE [watcher] = ? ORDER BY activity_stream.asid DESC OFFSET 0 ROWS FETCH NEXT 16 ROWS ONLY") if err != nil { return err } @@ -84,31 +83,7 @@ func initMSSQL() (err error) { if err != nil { return err } - - log.Print("Preparing todaysPostCount statement.") - stmts.todaysPostCountStmt, err = db.Prepare("select count(*) from replies where createdAt >= DATEADD(DAY, -1, GETUTCDATE())") - if err != nil { - return err - } - - log.Print("Preparing todaysTopicCount statement.") - stmts.todaysTopicCountStmt, err = db.Prepare("select count(*) from topics where createdAt >= DATEADD(DAY, -1, GETUTCDATE())") - if err != nil { - return err - }*/ - - log.Print("Preparing todaysTopicCountByForum statement.") - // TODO: Stop hard-coding this query - stmts.todaysTopicCountByForum, err = db.Prepare("select count(*) from topics where createdAt >= DATEADD(DAY, -1, GETUTCDATE()) and parentID = ?") - if err != nil { - return err - } - - /*log.Print("Preparing todaysNewUserCount statement.") - stmts.todaysNewUserCountStmt, err = db.Prepare("select count(*) from users where createdAt >= DATEADD(DAY, -1, GETUTCDATE())") - if err != nil { - return err - }*/ + */ return nil } diff --git a/mysql.go b/mysql.go index ffe7c128..3bc0020c 100644 --- a/mysql.go +++ b/mysql.go @@ -64,30 +64,5 @@ func initMySQL() (err error) { return errors.WithStack(err) } - log.Print("Preparing todaysPostCount statement.") - stmts.todaysPostCount, err = db.Prepare("select count(*) from replies where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()") - if err != nil { - return errors.WithStack(err) - } - - log.Print("Preparing todaysTopicCount statement.") - stmts.todaysTopicCount, err = db.Prepare("select count(*) from topics where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()") - if err != nil { - return errors.WithStack(err) - } - - log.Print("Preparing todaysTopicCountByForum statement.") - // TODO: Stop hard-coding this query - stmts.todaysTopicCountByForum, err = db.Prepare("select count(*) from topics where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp() and parentID = ?") - if err != nil { - return errors.WithStack(err) - } - - log.Print("Preparing todaysNewUserCount statement.") - stmts.todaysNewUserCount, err = db.Prepare("select count(*) from users where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()") - if err != nil { - return errors.WithStack(err) - } - return nil } diff --git a/query_gen/lib/mssql.go b/query_gen/lib/mssql.go index 6ee88d97..e7d3fe02 100644 --- a/query_gen/lib/mssql.go +++ b/query_gen/lib/mssql.go @@ -1150,10 +1150,6 @@ type Stmts struct { ` + stmts + ` getActivityFeedByWatcher *sql.Stmt getActivityCountByWatcher *sql.Stmt - todaysPostCount *sql.Stmt - todaysTopicCount *sql.Stmt - todaysTopicCountByForum *sql.Stmt - todaysNewUserCount *sql.Stmt Mocks bool } diff --git a/query_gen/lib/mysql.go b/query_gen/lib/mysql.go index 9d7aa59c..591cd222 100644 --- a/query_gen/lib/mysql.go +++ b/query_gen/lib/mysql.go @@ -756,10 +756,6 @@ type Stmts struct { ` + stmts + ` getActivityFeedByWatcher *sql.Stmt getActivityCountByWatcher *sql.Stmt - todaysPostCount *sql.Stmt - todaysTopicCount *sql.Stmt - todaysTopicCountByForum *sql.Stmt - todaysNewUserCount *sql.Stmt Mocks bool } diff --git a/query_gen/lib/pgsql.go b/query_gen/lib/pgsql.go index 5501bdcf..1f420b83 100644 --- a/query_gen/lib/pgsql.go +++ b/query_gen/lib/pgsql.go @@ -438,10 +438,6 @@ type Stmts struct { ` + stmts + ` getActivityFeedByWatcher *sql.Stmt getActivityCountByWatcher *sql.Stmt - todaysPostCount *sql.Stmt - todaysTopicCount *sql.Stmt - todaysTopicCountByForum *sql.Stmt - todaysNewUserCount *sql.Stmt Mocks bool } diff --git a/router_gen/routes.go b/router_gen/routes.go index 261d2699..0aff7d11 100644 --- a/router_gen/routes.go +++ b/router_gen/routes.go @@ -142,7 +142,7 @@ func buildAccountRoutes() { func buildPanelRoutes() { panelGroup := newRouteGroup("/panel/").Before("SuperModOnly") panelGroup.Routes( - View("routePanelDashboard", "/panel/"), + View("panel.Dashboard", "/panel/"), View("panel.Forums", "/panel/forums/"), Action("panel.ForumsCreateSubmit", "/panel/forums/create/"), Action("panel.ForumsDelete", "/panel/forums/delete/", "extraData"), diff --git a/panel_routes.go b/routes/panel/dashboard.go similarity index 75% rename from panel_routes.go rename to routes/panel/dashboard.go index 22c637fc..7b8f6aa1 100644 --- a/panel_routes.go +++ b/routes/panel/dashboard.go @@ -1,4 +1,4 @@ -package main +package panel import ( "database/sql" @@ -6,31 +6,63 @@ import ( "net/http" "strconv" - "./common" + "../../common" + "../../query_gen/lib" "github.com/Azareal/gopsutil/mem" + "github.com/pkg/errors" ) -// We're trying to reduce the amount of boilerplate in here, so I added these two functions, they might wind up circulating outside this file in the future -func panelSuccessRedirect(dest string, w http.ResponseWriter, r *http.Request, isJs bool) common.RouteError { - if !isJs { - http.Redirect(w, r, dest, http.StatusSeeOther) - } else { - w.Write(successJSONBytes) - } - return nil -} -func panelRenderTemplate(tmplName string, w http.ResponseWriter, r *http.Request, user common.User, pi interface{}) common.RouteError { - if common.RunPreRenderHook("pre_render_"+tmplName, w, r, &user, pi) { - return nil - } - err := common.Templates.ExecuteTemplate(w, tmplName+".html", pi) - if err != nil { - return common.InternalError(err, w, r) - } - return nil +type dashStmts struct { + todaysPostCount *sql.Stmt + todaysTopicCount *sql.Stmt + todaysTopicCountByForum *sql.Stmt + todaysNewUserCount *sql.Stmt } -func routePanelDashboard(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError { +// TODO: Stop hard-coding these queries +func dashMySQLStmts() (stmts dashStmts, err error) { + db := qgen.Builder.GetConn() + + var prepareStmt = func(table string, ext string) *sql.Stmt { + if err != nil { + return nil + } + stmt, ierr := db.Prepare("select count(*) from " + table + " where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp() " + ext) + err = errors.WithStack(ierr) + return stmt + } + + stmts.todaysPostCount = prepareStmt("replies", "") + stmts.todaysTopicCount = prepareStmt("topics", "") + stmts.todaysNewUserCount = prepareStmt("users", "") + stmts.todaysTopicCountByForum = prepareStmt("topics", " and parentID = ?") + + return stmts, err +} + +// TODO: Stop hard-coding these queries +func dashMSSQLStmts() (stmts dashStmts, err error) { + db := qgen.Builder.GetConn() + + var prepareStmt = func(table string, ext string) *sql.Stmt { + if err != nil { + return nil + } + stmt, ierr := db.Prepare("select count(*) from " + table + " where createdAt >= DATEADD(DAY, -1, GETUTCDATE())" + ext) + err = errors.WithStack(ierr) + return stmt + } + + stmts.todaysPostCount = prepareStmt("replies", "") + stmts.todaysTopicCount = prepareStmt("topics", "") + stmts.todaysNewUserCount = prepareStmt("users", "") + stmts.todaysTopicCountByForum = prepareStmt("topics", " and parentID = ?") + + return stmts, err +} + +//routePanelDashboard +func Dashboard(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError { header, stats, ferr := common.PanelUserCheck(w, r, &user) if ferr != nil { return ferr @@ -91,12 +123,25 @@ func routePanelDashboard(w http.ResponseWriter, r *http.Request, user common.Use var intErr error var extractStat = func(stmt *sql.Stmt, args ...interface{}) (stat int) { err := stmt.QueryRow(args...).Scan(&stat) - if err != nil && err != ErrNoRows { + if err != nil && err != sql.ErrNoRows { intErr = err } return stat } + var stmts dashStmts + switch qgen.Builder.GetAdapter().GetName() { + case "mysql": + stmts, err = dashMySQLStmts() + case "mssql": + stmts, err = dashMSSQLStmts() + default: + return common.InternalError(errors.New("Unknown database adapter on dashboard"), w, r) + } + if err != nil { + return common.InternalError(err, w, r) + } + var postCount = extractStat(stmts.todaysPostCount) var postInterval = "day" var postColour = greaterThanSwitch(postCount, 5, 25) @@ -120,7 +165,7 @@ func routePanelDashboard(w http.ResponseWriter, r *http.Request, user common.Use var gridElements = []common.GridElement{ // TODO: Implement a check for new versions of Gosora //common.GridElement{"dash-version", "v" + version.String(), 0, "grid_istat stat_green", "", "", "Gosora is up-to-date :)"}, - common.GridElement{"dash-version", "v" + version.String(), 0, "grid_istat", "", "", ""}, + common.GridElement{"dash-version", "v" + common.SoftwareVersion.String(), 0, "grid_istat", "", "", ""}, common.GridElement{"dash-cpu", "CPU: " + cpustr, 1, "grid_istat " + cpuColour, "", "", "The global CPU usage of this server"}, common.GridElement{"dash-ram", "RAM: " + ramstr, 2, "grid_istat " + ramColour, "", "", "The global RAM usage of this server"}, @@ -164,5 +209,5 @@ func routePanelDashboard(w http.ResponseWriter, r *http.Request, user common.Use } pi := common.PanelDashboardPage{&common.BasePanelPage{header, stats, "dashboard", common.ReportForumID}, gridElements} - return panelRenderTemplate("panel_dashboard", w, r, user, &pi) + return renderTemplate("panel_dashboard", w, r, user, &pi) }