2017-04-05 14:15:22 +00:00
package main
2017-05-07 08:31:41 +00:00
import (
2017-12-30 05:47:46 +00:00
"database/sql"
2017-05-07 08:31:41 +00:00
"errors"
2017-09-03 04:50:31 +00:00
"fmt"
"log"
"net/http"
"strconv"
2017-05-07 08:31:41 +00:00
2017-11-11 04:06:16 +00:00
"./common"
2017-07-17 10:23:42 +00:00
"github.com/Azareal/gopsutil/mem"
2017-06-28 12:05:26 +00:00
)
2017-04-05 14:15:22 +00:00
2018-01-10 03:32:48 +00:00
// 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
}
2018-01-11 08:03:17 +00:00
func panelRenderTemplate ( tmplName string , w http . ResponseWriter , r * http . Request , user common . User , pi interface { } ) common . RouteError {
2018-02-19 04:26:01 +00:00
if common . RunPreRenderHook ( "pre_render_" + tmplName , w , r , & user , pi ) {
return nil
2018-01-10 03:32:48 +00:00
}
2018-01-11 08:03:17 +00:00
err := common . Templates . ExecuteTemplate ( w , tmplName + ".html" , pi )
2018-01-10 03:32:48 +00:00
if err != nil {
return common . InternalError ( err , w , r )
}
return nil
2018-01-11 08:03:17 +00:00
}
2018-01-10 03:32:48 +00:00
2018-01-22 08:15:45 +00:00
func routePanelDashboard ( w http . ResponseWriter , r * http . Request , user common . User ) common . RouteError {
2018-06-06 00:21:22 +00:00
header , stats , ferr := common . PanelUserCheck ( w , r , & user )
2017-10-30 09:57:08 +00:00
if ferr != nil {
return ferr
2017-04-05 14:15:22 +00:00
}
2018-06-06 00:21:22 +00:00
header . Title = common . GetTitlePhrase ( "panel_dashboard" )
2017-05-29 14:52:37 +00:00
2017-07-17 10:23:42 +00:00
// 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
2017-09-03 04:50:31 +00:00
var cpustr = "Unknown"
2017-07-17 10:23:42 +00:00
var cpuColour string
2017-11-23 05:37:08 +00:00
lessThanSwitch := func ( number int , lowerBound int , midBound int ) string {
switch {
case number < lowerBound :
return "stat_green"
case number < midBound :
return "stat_orange"
}
return "stat_red"
}
2017-05-07 08:31:41 +00:00
var ramstr , ramColour string
memres , err := mem . VirtualMemory ( )
if err != nil {
ramstr = "Unknown"
} else {
2017-11-11 04:06:16 +00:00
totalCount , totalUnit := common . ConvertByteUnit ( float64 ( memres . Total ) )
usedCount := common . ConvertByteInUnit ( float64 ( memres . Total - memres . Available ) , totalUnit )
2017-05-29 14:52:37 +00:00
2017-05-07 08:31:41 +00:00
// Round totals with .9s up, it's how most people see it anyway. Floats are notoriously imprecise, so do it off 0.85
var totstr string
2017-09-03 04:50:31 +00:00
if ( totalCount - float64 ( int ( totalCount ) ) ) > 0.85 {
usedCount += 1.0 - ( totalCount - float64 ( int ( totalCount ) ) )
totstr = strconv . Itoa ( int ( totalCount ) + 1 )
2017-05-07 08:31:41 +00:00
} else {
2017-09-03 04:50:31 +00:00
totstr = fmt . Sprintf ( "%.1f" , totalCount )
2017-05-07 08:31:41 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
if usedCount > totalCount {
usedCount = totalCount
2017-05-07 08:31:41 +00:00
}
2017-09-03 04:50:31 +00:00
ramstr = fmt . Sprintf ( "%.1f" , usedCount ) + " / " + totstr + totalUnit
2017-05-29 14:52:37 +00:00
2017-05-07 08:31:41 +00:00
ramperc := ( ( memres . Total - memres . Available ) * 100 ) / memres . Total
2017-11-23 05:37:08 +00:00
ramColour = lessThanSwitch ( int ( ramperc ) , 50 , 75 )
}
greaterThanSwitch := func ( number int , lowerBound int , midBound int ) string {
switch {
case number > midBound :
return "stat_green"
case number > lowerBound :
return "stat_orange"
2017-05-07 08:31:41 +00:00
}
2017-11-23 05:37:08 +00:00
return "stat_red"
2017-05-07 08:31:41 +00:00
}
2017-05-29 14:52:37 +00:00
2017-11-23 05:37:08 +00:00
// TODO: Add a stat store for this?
2017-12-30 05:47:46 +00:00
var intErr error
2018-06-06 00:21:22 +00:00
var extractStat = func ( stmt * sql . Stmt , args ... interface { } ) ( stat int ) {
err := stmt . QueryRow ( args ... ) . Scan ( & stat )
2017-12-30 05:47:46 +00:00
if err != nil && err != ErrNoRows {
intErr = err
}
return stat
2017-05-07 08:31:41 +00:00
}
2017-12-30 05:47:46 +00:00
var postCount = extractStat ( stmts . todaysPostCount )
2017-09-03 04:50:31 +00:00
var postInterval = "day"
2017-11-23 05:37:08 +00:00
var postColour = greaterThanSwitch ( postCount , 5 , 25 )
2017-05-29 14:52:37 +00:00
2017-12-30 05:47:46 +00:00
var topicCount = extractStat ( stmts . todaysTopicCount )
2017-09-03 04:50:31 +00:00
var topicInterval = "day"
2017-11-23 05:37:08 +00:00
var topicColour = greaterThanSwitch ( topicCount , 0 , 8 )
2017-05-29 14:52:37 +00:00
2018-06-06 00:21:22 +00:00
var reportCount = extractStat ( stmts . todaysTopicCountByForum , common . ReportForumID )
2017-09-03 04:50:31 +00:00
var reportInterval = "week"
2017-05-29 14:52:37 +00:00
2017-12-30 05:47:46 +00:00
var newUserCount = extractStat ( stmts . todaysNewUserCount )
2017-09-03 04:50:31 +00:00
var newUserInterval = "week"
2017-05-29 14:52:37 +00:00
2017-12-30 05:47:46 +00:00
// Did any of the extractStats fail?
if intErr != nil {
return common . InternalError ( intErr , w , r )
}
2018-06-06 00:21:22 +00:00
// TODO: Localise these
2017-11-11 04:06:16 +00:00
var gridElements = [ ] common . GridElement {
2018-06-26 04:54:20 +00:00
// 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" , "" , "" , "" } ,
2017-11-11 04:06:16 +00:00
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" } ,
2017-05-11 13:04:43 +00:00
}
2018-06-06 00:21:22 +00:00
var addElement = func ( element common . GridElement ) {
gridElements = append ( gridElements , element )
}
2017-05-29 14:52:37 +00:00
2018-03-08 03:59:47 +00:00
if common . EnableWebsockets {
uonline := common . WsHub . UserCount ( )
gonline := common . WsHub . GuestCount ( )
2017-05-11 13:04:43 +00:00
totonline := uonline + gonline
2017-12-30 05:47:46 +00:00
reqCount := 0
2017-05-29 14:52:37 +00:00
2017-11-23 05:37:08 +00:00
var onlineColour = greaterThanSwitch ( totonline , 3 , 10 )
var onlineGuestsColour = greaterThanSwitch ( gonline , 1 , 10 )
var onlineUsersColour = greaterThanSwitch ( uonline , 1 , 5 )
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
totonline , totunit := common . ConvertFriendlyUnit ( totonline )
uonline , uunit := common . ConvertFriendlyUnit ( uonline )
gonline , gunit := common . ConvertFriendlyUnit ( gonline )
2017-05-29 14:52:37 +00:00
2018-06-06 00:21:22 +00:00
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-gonline" , strconv . Itoa ( gonline ) + gunit + " guests online" , 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-reqs" , strconv . Itoa ( reqCount ) + " reqs / second" , 7 , "grid_stat grid_end_group " + topicColour , "" , "" , "The number of requests over the last 24 hours" } )
2017-05-07 08:31:41 +00:00
}
2017-05-29 14:52:37 +00:00
2018-06-06 00:21:22 +00:00
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-totonlineperday" , "20 online / day" , 8 , "grid_stat stat_disabled" , "" , "" , "Coming Soon!" /*, "The people online over the last 24 hours"*/ } )
2017-05-29 14:52:37 +00:00
2018-06-06 00:21:22 +00:00
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-reports" , strconv . Itoa ( reportCount ) + " reports / " + reportInterval , 11 , "grid_stat" , "" , "" , "The number of reports over the last 7 days" } )
2017-05-29 14:52:37 +00:00
2017-12-30 05:47:46 +00:00
if false {
2018-06-06 00:21:22 +00:00
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-visitorsperweek" , "2 visitors / week" , 13 , "grid_stat stat_disabled" , "" , "" , "Coming Soon!" /*"The number of unique visitors we've had over the last 7 days"*/ } )
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"*/ } )
2017-12-30 05:47:46 +00:00
}
2017-05-29 14:52:37 +00:00
2018-06-06 00:21:22 +00:00
pi := common . PanelDashboardPage { & common . BasePanelPage { header , stats , "dashboard" , common . ReportForumID } , gridElements }
2018-01-11 08:03:17 +00:00
return panelRenderTemplate ( "panel_dashboard" , w , r , user , & pi )
2017-04-05 14:15:22 +00:00
}
2017-11-11 04:06:16 +00:00
func routePanelThemesSetDefault ( w http . ResponseWriter , r * http . Request , user common . User , uname string ) common . RouteError {
_ , ferr := common . SimplePanelUserCheck ( w , r , & user )
2017-10-30 09:57:08 +00:00
if ferr != nil {
return ferr
2017-04-05 14:15:22 +00:00
}
2017-06-16 10:41:30 +00:00
if ! user . Perms . ManageThemes {
2017-11-11 04:06:16 +00:00
return common . NoPermissions ( w , r , user )
2017-04-05 14:15:22 +00:00
}
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
theme , ok := common . Themes [ uname ]
2017-04-05 14:15:22 +00:00
if ! ok {
2017-11-11 04:06:16 +00:00
return common . LocalError ( "The theme isn't registered in the system" , w , r , user )
2017-04-05 14:15:22 +00:00
}
if theme . Disabled {
2017-11-11 04:06:16 +00:00
return common . LocalError ( "You must not enable this theme" , w , r , user )
2017-04-05 14:15:22 +00:00
}
2017-05-29 14:52:37 +00:00
2017-04-05 14:15:22 +00:00
var isDefault bool
2017-11-05 09:55:34 +00:00
err := stmts . isThemeDefault . QueryRow ( uname ) . Scan ( & isDefault )
2017-06-28 12:05:26 +00:00
if err != nil && err != ErrNoRows {
2017-11-11 04:06:16 +00:00
return common . InternalError ( err , w , r )
2017-04-05 14:15:22 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
hasTheme := err != ErrNoRows
if hasTheme {
2017-04-05 14:15:22 +00:00
if isDefault {
2017-11-11 04:06:16 +00:00
return common . LocalError ( "The theme is already active" , w , r , user )
2017-04-05 14:15:22 +00:00
}
2017-11-05 09:55:34 +00:00
_ , err = stmts . updateTheme . Exec ( 1 , uname )
2017-04-05 14:15:22 +00:00
} else {
2017-11-11 23:34:27 +00:00
_ , err = stmts . addTheme . Exec ( uname , 1 )
}
if err != nil {
return common . InternalError ( err , w , r )
2017-04-05 14:15:22 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Make this less racey
2017-11-11 04:06:16 +00:00
// TODO: Move this to common
common . ChangeDefaultThemeMutex . Lock ( )
defaultTheme := common . DefaultThemeBox . Load ( ) . ( string )
2017-11-05 09:55:34 +00:00
_ , err = stmts . updateTheme . Exec ( 0 , defaultTheme )
2017-04-05 14:15:22 +00:00
if err != nil {
2017-11-11 04:06:16 +00:00
return common . InternalError ( err , w , r )
2017-04-05 14:15:22 +00:00
}
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
log . Printf ( "Setting theme '%s' as the default theme" , theme . Name )
2017-04-05 14:15:22 +00:00
theme . Active = true
2017-11-11 04:06:16 +00:00
common . Themes [ uname ] = theme
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
dTheme , ok := common . Themes [ defaultTheme ]
2017-04-05 14:15:22 +00:00
if ! ok {
2017-11-11 04:06:16 +00:00
return common . InternalError ( errors . New ( "The default theme is missing" ) , w , r )
2017-04-05 14:15:22 +00:00
}
dTheme . Active = false
2017-11-11 04:06:16 +00:00
common . Themes [ defaultTheme ] = dTheme
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
common . DefaultThemeBox . Store ( uname )
common . ResetTemplateOverrides ( )
2017-12-01 02:04:29 +00:00
theme . MapTemplates ( )
2017-11-11 04:06:16 +00:00
common . ChangeDefaultThemeMutex . Unlock ( )
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
http . Redirect ( w , r , "/panel/themes/" , http . StatusSeeOther )
2017-10-30 09:57:08 +00:00
return nil
2017-04-05 14:15:22 +00:00
}