Reduced the amount of boilerplate in panel.Dashboard.

Localised a few things in panel.Dashboard.

Added the GetTmplPhrasef function.
This commit is contained in:
Azareal 2018-10-02 16:56:24 +10:00
parent 6634f3f64b
commit 0039b9c851
4 changed files with 31 additions and 17 deletions

View File

@ -9,6 +9,7 @@ package common
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -237,6 +238,14 @@ func GetTmplPhrase(name string) string {
return res return res
} }
func GetTmplPhrasef(name string, params ...interface{}) string {
res, ok := currentLangPack.Load().(*LanguagePack).TmplPhrases[name]
if !ok {
return getPhrasePlaceholder("tmpl", name)
}
return fmt.Sprintf(res, params...)
}
func GetTmplPhrases() map[string]string { func GetTmplPhrases() map[string]string {
return currentLangPack.Load().(*LanguagePack).TmplPhrases return currentLangPack.Load().(*LanguagePack).TmplPhrases
} }

View File

@ -288,9 +288,9 @@ AdminStatLoop:
// nolint // nolint
// TODO: Use JSON for this to make things more portable and easier to convert to MessagePack, if need be? // TODO: Use JSON for this to make things more portable and easier to convert to MessagePack, if need be?
if !noStatUpdates { if !noStatUpdates {
w.Write([]byte("set #dash-totonline <span>" + strconv.Itoa(totonline) + totunit + " online</span>\r")) w.Write([]byte("set #dash-totonline <span>" + GetTmplPhrasef("panel_dashboard_online", totonline, totunit) + "</span>\r"))
w.Write([]byte("set #dash-gonline <span>" + strconv.Itoa(gonline) + gunit + " guests online</span>\r")) w.Write([]byte("set #dash-gonline <span>" + GetTmplPhrasef("panel_dashboard_guests_online", gonline, gunit) + "</span>\r"))
w.Write([]byte("set #dash-uonline <span>" + strconv.Itoa(uonline) + uunit + " users online</span>\r")) w.Write([]byte("set #dash-uonline <span>" + GetTmplPhrasef("panel_dashboard_users_online", uonline, uunit) + "</span>\r"))
w.Write([]byte("set #dash-reqs <span>" + strconv.Itoa(reqCount) + " reqs / second</span>\r")) w.Write([]byte("set #dash-reqs <span>" + strconv.Itoa(reqCount) + " reqs / second</span>\r"))
w.Write([]byte("set-class #dash-totonline grid_item grid_stat " + onlineColour + "\r")) w.Write([]byte("set-class #dash-totonline grid_item grid_stat " + onlineColour + "\r"))

View File

@ -629,6 +629,11 @@
"panel_menu_debug":"Debug", "panel_menu_debug":"Debug",
"panel_dashboard_head":"Dashboard", "panel_dashboard_head":"Dashboard",
"panel_dashboard_online": "%d%s online",
"panel_dashboard_guests_online":"%d%s guests online",
"panel_dashboard_users_online":"%d%s users online",
"panel_dashboard_day_suffix":" / day",
"panel_dashboard_week_suffix":" / week",
"panel_users_head":"Users", "panel_users_head":"Users",
"panel_users_profile":"Profile", "panel_users_profile":"Profile",

View File

@ -63,11 +63,10 @@ func dashMSSQLStmts() (stmts dashStmts, err error) {
//routePanelDashboard //routePanelDashboard
func Dashboard(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError { func Dashboard(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError {
header, stats, ferr := common.PanelUserCheck(w, r, &user) basePage, ferr := buildBasePage(w, r, &user, "dashboard", "dashboard")
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
header.Title = common.GetTitlePhrase("panel_dashboard")
// We won't calculate this on the spot anymore, as the system doesn't seem to like it if we do multiple fetches simultaneously. Should we constantly calculate this on a background thread? Perhaps, the watchdog to scale back heavy features under load? One plus side is that we'd get immediate CPU percentages here instead of waiting it to kick in with WebSockets // We won't calculate this on the spot anymore, as the system doesn't seem to like it if we do multiple fetches simultaneously. Should we constantly calculate this on a background thread? Perhaps, the watchdog to scale back heavy features under load? One plus side is that we'd get immediate CPU percentages here instead of waiting it to kick in with WebSockets
var cpustr = "Unknown" var cpustr = "Unknown"
@ -142,19 +141,20 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user common.User) common.
return common.InternalError(err, w, r) return common.InternalError(err, w, r)
} }
// TODO: Allow for more complex phrase structures than just suffixes
var postCount = extractStat(stmts.todaysPostCount) var postCount = extractStat(stmts.todaysPostCount)
var postInterval = "day" var postInterval = common.GetTmplPhrase("panel_dashboard_day_suffix")
var postColour = greaterThanSwitch(postCount, 5, 25) var postColour = greaterThanSwitch(postCount, 5, 25)
var topicCount = extractStat(stmts.todaysTopicCount) var topicCount = extractStat(stmts.todaysTopicCount)
var topicInterval = "day" var topicInterval = common.GetTmplPhrase("panel_dashboard_day_suffix")
var topicColour = greaterThanSwitch(topicCount, 0, 8) var topicColour = greaterThanSwitch(topicCount, 0, 8)
var reportCount = extractStat(stmts.todaysTopicCountByForum, common.ReportForumID) var reportCount = extractStat(stmts.todaysTopicCountByForum, common.ReportForumID)
var reportInterval = "week" var reportInterval = common.GetTmplPhrase("panel_dashboard_week_suffix")
var newUserCount = extractStat(stmts.todaysNewUserCount) var newUserCount = extractStat(stmts.todaysNewUserCount)
var newUserInterval = "week" var newUserInterval = common.GetTmplPhrase("panel_dashboard_week_suffix")
// Did any of the extractStats fail? // Did any of the extractStats fail?
if intErr != nil { if intErr != nil {
@ -188,19 +188,19 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user common.User) common.
uonline, uunit := common.ConvertFriendlyUnit(uonline) uonline, uunit := common.ConvertFriendlyUnit(uonline)
gonline, gunit := common.ConvertFriendlyUnit(gonline) gonline, gunit := common.ConvertFriendlyUnit(gonline)
addElement(common.GridElement{"dash-totonline", strconv.Itoa(totonline) + totunit + " online", 3, "grid_stat " + onlineColour, "", "", "The number of people who are currently online"}) addElement(common.GridElement{"dash-totonline", common.GetTmplPhrasef("panel_dashboard_online", totonline, totunit), 3, "grid_stat " + onlineColour, "", "", "The number of people who are currently online"})
addElement(common.GridElement{"dash-gonline", strconv.Itoa(gonline) + gunit + " guests online", 4, "grid_stat " + onlineGuestsColour, "", "", "The number of guests who are currently online"}) addElement(common.GridElement{"dash-gonline", common.GetTmplPhrasef("panel_dashboard_guests_online", gonline, gunit), 4, "grid_stat " + onlineGuestsColour, "", "", "The number of guests who are currently online"})
addElement(common.GridElement{"dash-uonline", strconv.Itoa(uonline) + uunit + " users online", 5, "grid_stat " + onlineUsersColour, "", "", "The number of logged-in users who are currently online"}) addElement(common.GridElement{"dash-uonline", common.GetTmplPhrasef("panel_dashboard_users_online", uonline, uunit), 5, "grid_stat " + onlineUsersColour, "", "", "The number of logged-in users who are currently online"})
addElement(common.GridElement{"dash-reqs", strconv.Itoa(reqCount) + " reqs / second", 7, "grid_stat grid_end_group " + topicColour, "", "", "The number of requests over the last 24 hours"}) addElement(common.GridElement{"dash-reqs", strconv.Itoa(reqCount) + " reqs / second", 7, "grid_stat grid_end_group " + topicColour, "", "", "The number of requests over the last 24 hours"})
} }
addElement(common.GridElement{"dash-postsperday", strconv.Itoa(postCount) + " posts / " + postInterval, 6, "grid_stat " + postColour, "", "", "The number of new posts over the last 24 hours"}) addElement(common.GridElement{"dash-postsperday", strconv.Itoa(postCount) + " posts" + postInterval, 6, "grid_stat " + postColour, "", "", "The number of new posts over the last 24 hours"})
addElement(common.GridElement{"dash-topicsperday", strconv.Itoa(topicCount) + " topics / " + topicInterval, 7, "grid_stat " + topicColour, "", "", "The number of new topics over the last 24 hours"}) addElement(common.GridElement{"dash-topicsperday", strconv.Itoa(topicCount) + " topics" + topicInterval, 7, "grid_stat " + topicColour, "", "", "The number of new topics over the last 24 hours"})
addElement(common.GridElement{"dash-totonlineperday", "20 online / day", 8, "grid_stat stat_disabled", "", "", "Coming Soon!" /*, "The people online over the last 24 hours"*/}) addElement(common.GridElement{"dash-totonlineperday", "20 online / day", 8, "grid_stat stat_disabled", "", "", "Coming Soon!" /*, "The people online over the last 24 hours"*/})
addElement(common.GridElement{"dash-searches", "8 searches / week", 9, "grid_stat stat_disabled", "", "", "Coming Soon!" /*"The number of searches over the last 7 days"*/}) addElement(common.GridElement{"dash-searches", "8 searches / week", 9, "grid_stat stat_disabled", "", "", "Coming Soon!" /*"The number of searches over the last 7 days"*/})
addElement(common.GridElement{"dash-newusers", strconv.Itoa(newUserCount) + " new users / " + newUserInterval, 10, "grid_stat", "", "", "The number of new users over the last 7 days"}) addElement(common.GridElement{"dash-newusers", strconv.Itoa(newUserCount) + " new users" + newUserInterval, 10, "grid_stat", "", "", "The number of new users over the last 7 days"})
addElement(common.GridElement{"dash-reports", strconv.Itoa(reportCount) + " reports / " + reportInterval, 11, "grid_stat", "", "", "The number of reports over the last 7 days"}) addElement(common.GridElement{"dash-reports", strconv.Itoa(reportCount) + " reports" + reportInterval, 11, "grid_stat", "", "", "The number of reports over the last 7 days"})
if false { if false {
addElement(common.GridElement{"dash-minperuser", "2 minutes / user / week", 12, "grid_stat stat_disabled", "", "", "Coming Soon!" /*"The average number of number of minutes spent by each active user over the last 7 days"*/}) addElement(common.GridElement{"dash-minperuser", "2 minutes / user / week", 12, "grid_stat stat_disabled", "", "", "Coming Soon!" /*"The average number of number of minutes spent by each active user over the last 7 days"*/})
@ -208,6 +208,6 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user common.User) common.
addElement(common.GridElement{"dash-postsperuser", "5 posts / user / week", 14, "grid_stat stat_disabled", "", "", "Coming Soon!" /*"The average number of posts made by each active user over the past week"*/}) addElement(common.GridElement{"dash-postsperuser", "5 posts / user / week", 14, "grid_stat stat_disabled", "", "", "Coming Soon!" /*"The average number of posts made by each active user over the past week"*/})
} }
pi := common.PanelDashboardPage{&common.BasePanelPage{header, stats, "dashboard", common.ReportForumID}, gridElements} pi := common.PanelDashboardPage{basePage, gridElements}
return renderTemplate("panel_dashboard", w, r, user, &pi) return renderTemplate("panel_dashboard", w, r, user, &pi)
} }