2018-10-02 05:03:20 +00:00
package panel
2017-04-05 14:15:22 +00:00
2017-05-07 08:31:41 +00:00
import (
2017-12-30 05:47:46 +00:00
"database/sql"
2017-09-03 04:50:31 +00:00
"fmt"
"net/http"
"strconv"
2019-05-01 06:59:51 +00:00
"runtime"
2019-07-20 23:09:26 +00:00
"encoding/json"
"time"
"sync"
"sync/atomic"
2017-05-07 08:31:41 +00:00
2019-04-19 07:25:49 +00:00
c "github.com/Azareal/Gosora/common"
2019-04-29 01:28:55 +00:00
p "github.com/Azareal/Gosora/common/phrases"
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/query_gen"
2017-07-17 10:23:42 +00:00
"github.com/Azareal/gopsutil/mem"
2018-10-02 05:03:20 +00:00
"github.com/pkg/errors"
2017-06-28 12:05:26 +00:00
)
2017-04-05 14:15:22 +00:00
2018-10-02 05:03:20 +00:00
type dashStmts struct {
todaysPostCount * sql . Stmt
todaysTopicCount * sql . Stmt
todaysTopicCountByForum * sql . Stmt
todaysNewUserCount * sql . Stmt
2019-04-29 01:28:55 +00:00
weeklyTopicCountByForum * sql . Stmt
2018-01-10 03:32:48 +00:00
}
2018-10-02 05:03:20 +00:00
// TODO: Stop hard-coding these queries
func dashMySQLStmts ( ) ( stmts dashStmts , err error ) {
db := qgen . Builder . GetConn ( )
2019-04-29 01:28:55 +00:00
var prepareStmt = func ( table string , ext string , dur string ) * sql . Stmt {
2018-10-02 05:03:20 +00:00
if err != nil {
return nil
}
2019-04-29 01:28:55 +00:00
stmt , ierr := db . Prepare ( "select count(*) from " + table + " where createdAt BETWEEN (utc_timestamp() - interval 1 " + dur + ") and utc_timestamp() " + ext )
2018-10-02 05:03:20 +00:00
err = errors . WithStack ( ierr )
return stmt
2018-01-10 03:32:48 +00:00
}
2018-10-02 05:03:20 +00:00
2019-04-29 01:28:55 +00:00
stmts . todaysPostCount = prepareStmt ( "replies" , "" , "day" )
stmts . todaysTopicCount = prepareStmt ( "topics" , "" , "day" )
stmts . todaysNewUserCount = prepareStmt ( "users" , "" , "day" )
stmts . todaysTopicCountByForum = prepareStmt ( "topics" , " and parentID = ?" , "day" )
stmts . weeklyTopicCountByForum = prepareStmt ( "topics" , " and parentID = ?" , "week" )
2018-10-02 05:03:20 +00:00
return stmts , err
}
// TODO: Stop hard-coding these queries
func dashMSSQLStmts ( ) ( stmts dashStmts , err error ) {
db := qgen . Builder . GetConn ( )
2019-04-29 01:28:55 +00:00
var prepareStmt = func ( table string , ext string , dur string ) * sql . Stmt {
2018-10-02 05:03:20 +00:00
if err != nil {
return nil
}
2019-04-29 01:28:55 +00:00
stmt , ierr := db . Prepare ( "select count(*) from " + table + " where createdAt >= DATEADD(" + dur + ", -1, GETUTCDATE())" + ext )
2018-10-02 05:03:20 +00:00
err = errors . WithStack ( ierr )
return stmt
2018-01-10 03:32:48 +00:00
}
2018-10-02 05:03:20 +00:00
2019-04-29 01:28:55 +00:00
stmts . todaysPostCount = prepareStmt ( "replies" , "" , "DAY" )
stmts . todaysTopicCount = prepareStmt ( "topics" , "" , "DAY" )
stmts . todaysNewUserCount = prepareStmt ( "users" , "" , "DAY" )
stmts . todaysTopicCountByForum = prepareStmt ( "topics" , " and parentID = ?" , "DAY" )
stmts . weeklyTopicCountByForum = prepareStmt ( "topics" , " and parentID = ?" , "WEEK" )
2018-10-02 05:03:20 +00:00
return stmts , err
2018-01-11 08:03:17 +00:00
}
2018-01-10 03:32:48 +00:00
2019-04-29 01:28:55 +00:00
type GE = c . GridElement
2019-04-19 07:25:49 +00:00
func Dashboard ( w http . ResponseWriter , r * http . Request , user c . User ) c . RouteError {
2018-10-02 06:56:24 +00:00
basePage , ferr := buildBasePage ( w , r , & user , "dashboard" , "dashboard" )
2017-10-30 09:57:08 +00:00
if ferr != nil {
return ferr
2017-04-05 14:15:22 +00:00
}
2019-07-20 23:09:26 +00:00
unknown := p . GetTmplPhrase ( "panel_dashboard_unknown" )
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
2019-07-20 23:09:26 +00:00
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 {
2019-06-03 00:35:25 +00:00
ramstr = unknown
2017-05-07 08:31:41 +00:00
} else {
2019-04-19 07:25:49 +00:00
totalCount , totalUnit := c . ConvertByteUnit ( float64 ( memres . Total ) )
usedCount := c . 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-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 )
}
2019-05-01 06:59:51 +00:00
var m runtime . MemStats
runtime . ReadMemStats ( & m )
memCount , memUnit := c . ConvertByteUnit ( float64 ( m . Sys ) )
2017-11-23 05:37:08 +00:00
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 )
2018-10-02 05:03:20 +00:00
if err != nil && err != sql . ErrNoRows {
2017-12-30 05:47:46 +00:00
intErr = err
}
return stat
2017-05-07 08:31:41 +00:00
}
2017-12-30 05:47:46 +00:00
2018-10-02 05:03:20 +00:00
var stmts dashStmts
switch qgen . Builder . GetAdapter ( ) . GetName ( ) {
case "mysql" :
stmts , err = dashMySQLStmts ( )
case "mssql" :
stmts , err = dashMSSQLStmts ( )
default :
2019-04-19 07:25:49 +00:00
return c . InternalError ( errors . New ( "Unknown database adapter on dashboard" ) , w , r )
2018-10-02 05:03:20 +00:00
}
if err != nil {
2019-04-19 07:25:49 +00:00
return c . InternalError ( err , w , r )
2018-10-02 05:03:20 +00:00
}
2018-10-02 06:56:24 +00:00
// TODO: Allow for more complex phrase structures than just suffixes
2019-07-20 23:09:26 +00:00
postCount := extractStat ( stmts . todaysPostCount )
postInterval := p . GetTmplPhrase ( "panel_dashboard_day_suffix" )
postColour := greaterThanSwitch ( postCount , 5 , 25 )
2017-05-29 14:52:37 +00:00
2019-07-20 23:09:26 +00:00
topicCount := extractStat ( stmts . todaysTopicCount )
topicInterval := p . GetTmplPhrase ( "panel_dashboard_day_suffix" )
topicColour := greaterThanSwitch ( topicCount , 0 , 8 )
2017-05-29 14:52:37 +00:00
2019-07-20 23:09:26 +00:00
reportCount := extractStat ( stmts . weeklyTopicCountByForum , c . ReportForumID )
reportInterval := p . GetTmplPhrase ( "panel_dashboard_week_suffix" )
2017-05-29 14:52:37 +00:00
2019-07-20 23:09:26 +00:00
newUserCount := extractStat ( stmts . todaysNewUserCount )
newUserInterval := p . GetTmplPhrase ( "panel_dashboard_week_suffix" )
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 {
2019-04-19 07:25:49 +00:00
return c . InternalError ( intErr , w , r )
2017-12-30 05:47:46 +00:00
}
2019-07-20 23:09:26 +00:00
grid1 := [ ] GE { }
addElem1 := func ( id string , href string , body string , order int , class string , back string , textColour string , tooltip string ) {
2019-06-03 05:26:27 +00:00
grid1 = append ( grid1 , GE { id , href , body , order , class , back , textColour , tooltip } )
}
2019-07-20 23:09:26 +00:00
gridElements := [ ] GE { }
addElem := func ( id string , href string , body string , order int , class string , back string , textColour string , tooltip string ) {
2019-05-09 06:58:55 +00:00
gridElements = append ( gridElements , GE { id , href , body , order , class , back , textColour , tooltip } )
}
2018-06-26 04:54:20 +00:00
2019-05-09 06:58:55 +00:00
// TODO: Implement a check for new versions of Gosora
// TODO: Localise this
2019-06-03 05:26:27 +00:00
//addElem1("dash-version", "", "v" + version.String(), 0, "grid_istat stat_green", "", "", "Gosora is up-to-date :)")
addElem1 ( "dash-version" , "" , "v" + c . SoftwareVersion . String ( ) , 0 , "grid_istat" , "" , "" , "" )
addElem1 ( "dash-cpu" , "" , p . GetTmplPhrasef ( "panel_dashboard_cpu" , cpustr ) , 1 , "grid_istat " + cpuColour , "" , "" , p . GetTmplPhrase ( "panel_dashboard_cpu_desc" ) )
addElem1 ( "dash-ram" , "" , p . GetTmplPhrasef ( "panel_dashboard_ram" , ramstr ) , 2 , "grid_istat " + ramColour , "" , "" , p . GetTmplPhrase ( "panel_dashboard_ram_desc" ) )
addElem1 ( "dash-memused" , "/panel/analytics/memory/" , p . GetTmplPhrasef ( "panel_dashboard_memused" , memCount , memUnit ) , 2 , "grid_istat" , "" , "" , p . GetTmplPhrase ( "panel_dashboard_memused_desc" ) )
2019-05-01 06:59:51 +00:00
2019-07-20 23:09:26 +00:00
/ * dirSize := getDirSize ( )
if dirSize . Size != 0 {
dirFloat , unit := c . ConvertByteUnit ( float64 ( dirSize . Size ) )
addElem1 ( "dash-disk" , "" , p . GetTmplPhrasef ( "panel_dashboard_disk" , dirFloat , unit ) , 2 , "grid_istat" , "" , "" , p . GetTmplPhrase ( "panel_dashboard_disk_desc" ) )
dur := time . Since ( dirSize . Time )
if dur . Seconds ( ) > 3 {
startDirSizeTask ( )
}
} else {
addElem1 ( "dash-disk" , "" , p . GetTmplPhrase ( "panel_dashboard_disk_unknown" ) , 2 , "grid_istat" , "" , "" , p . GetTmplPhrase ( "panel_dashboard_disk_desc" ) )
startDirSizeTask ( )
} * /
2017-05-29 14:52:37 +00:00
2019-04-19 07:25:49 +00:00
if c . EnableWebsockets {
uonline := c . WsHub . UserCount ( )
gonline := c . WsHub . GuestCount ( )
2017-05-11 13:04:43 +00:00
totonline := uonline + gonline
2019-04-29 01:28:55 +00:00
//reqCount := 0
2017-05-29 14:52:37 +00:00
2019-07-20 23:09:26 +00:00
onlineColour := greaterThanSwitch ( totonline , 3 , 10 )
onlineGuestsColour := greaterThanSwitch ( gonline , 1 , 10 )
onlineUsersColour := greaterThanSwitch ( uonline , 1 , 5 )
2017-05-29 14:52:37 +00:00
2019-04-19 07:25:49 +00:00
totonline , totunit := c . ConvertFriendlyUnit ( totonline )
uonline , uunit := c . ConvertFriendlyUnit ( uonline )
gonline , gunit := c . ConvertFriendlyUnit ( gonline )
2017-05-29 14:52:37 +00:00
2019-05-09 06:58:55 +00:00
addElem ( "dash-totonline" , "" , p . GetTmplPhrasef ( "panel_dashboard_online" , totonline , totunit ) , 3 , "grid_stat " + onlineColour , "" , "" , p . GetTmplPhrase ( "panel_dashboard_online_desc" ) )
addElem ( "dash-gonline" , "" , p . GetTmplPhrasef ( "panel_dashboard_guests_online" , gonline , gunit ) , 4 , "grid_stat " + onlineGuestsColour , "" , "" , p . GetTmplPhrase ( "panel_dashboard_guests_online_desc" ) )
addElem ( "dash-uonline" , "" , p . GetTmplPhrasef ( "panel_dashboard_users_online" , uonline , uunit ) , 5 , "grid_stat " + onlineUsersColour , "" , "" , p . GetTmplPhrase ( "panel_dashboard_users_online_desc" ) )
//addElem("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
2019-06-03 00:35:25 +00:00
addElem ( "dash-postsperday" , "" , p . GetTmplPhrasef ( "panel_dashboard_posts" , postCount , postInterval ) , 6 , "grid_stat " + postColour , "" , "" , p . GetTmplPhrase ( "panel_dashboard_posts_desc" ) )
addElem ( "dash-topicsperday" , "" , p . GetTmplPhrasef ( "panel_dashboard_topics" , topicCount , topicInterval ) , 7 , "grid_stat " + topicColour , "" , "" , p . GetTmplPhrase ( "panel_dashboard_topics_desc" ) )
addElem ( "dash-totonlineperday" , "" , p . GetTmplPhrasef ( "panel_dashboard_online_day" ) , 8 , "grid_stat stat_disabled" , "" , "" , p . GetTmplPhrase ( "panel_dashboard_coming_soon" ) /*, "The people online over the last 24 hours"*/ )
2017-05-29 14:52:37 +00:00
2019-06-03 00:35:25 +00:00
addElem ( "dash-searches" , "" , p . GetTmplPhrasef ( "panel_dashboard_searches_day" ) , 9 , "grid_stat stat_disabled" , "" , "" , p . GetTmplPhrase ( "panel_dashboard_coming_soon" ) /*"The number of searches over the last 7 days"*/ )
addElem ( "dash-newusers" , "" , p . GetTmplPhrasef ( "panel_dashboard_new_users" , newUserCount , newUserInterval ) , 10 , "grid_stat" , "" , "" , p . GetTmplPhrasef ( "panel_dashboard_new_users_desc" ) )
addElem ( "dash-reports" , "" , p . GetTmplPhrasef ( "panel_dashboard_reports" , reportCount , reportInterval ) , 11 , "grid_stat" , "" , "" , p . GetTmplPhrasef ( "panel_dashboard_reports_desc" ) )
2017-05-29 14:52:37 +00:00
2017-12-30 05:47:46 +00:00
if false {
2019-05-09 06:58:55 +00:00
addElem ( "dash-minperuser" , "" , "?? minutes / user / week" , 12 , "grid_stat stat_disabled" , "" , "" , p . GetTmplPhrase ( "panel_dashboard_coming_soon" ) /*"The average number of number of minutes spent by each active user over the last 7 days"*/ )
addElem ( "dash-visitorsperweek" , "" , "?? visitors / week" , 13 , "grid_stat stat_disabled" , "" , "" , p . GetTmplPhrase ( "panel_dashboard_coming_soon" ) /*"The number of unique visitors we've had over the last 7 days"*/ )
addElem ( "dash-postsperuser" , "" , "?? posts / user / week" , 14 , "grid_stat stat_disabled" , "" , "" , p . GetTmplPhrase ( "panel_dashboard_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
2019-06-03 05:26:27 +00:00
return renderTemplate ( "panel" , w , r , basePage . Header , c . Panel { basePage , "panel_dashboard_right" , "" , "panel_dashboard" , c . DashGrids { grid1 , gridElements } } )
2017-04-05 14:15:22 +00:00
}
2019-07-20 23:09:26 +00:00
type dirSize struct {
Size int
Time time . Time
}
func init ( ) {
cachedDirSize . Store ( dirSize { 0 , time . Now ( ) } )
}
var cachedDirSize atomic . Value
var dstMu sync . Mutex
var dstMuGuess = 0
func startDirSizeTask ( ) {
if dstMuGuess == 1 {
return
}
dstMu . Lock ( )
dstMuGuess = 1
go func ( ) {
defer func ( ) {
dstMuGuess = 0
dstMu . Unlock ( )
} ( )
dDirSize , err := c . DirSize ( "." )
if err != nil {
c . LogWarning ( err )
}
cachedDirSize . Store ( dirSize { dDirSize , time . Now ( ) } )
} ( )
}
func getDirSize ( ) dirSize {
return cachedDirSize . Load ( ) . ( dirSize )
}
type StatsDiskJson struct {
Total string ` json:"total" `
}
func StatsDisk ( w http . ResponseWriter , r * http . Request , user c . User ) c . RouteError {
dirSize := getDirSize ( )
dirFloat , unit := c . ConvertByteUnit ( float64 ( dirSize . Size ) )
u := p . GetTmplPhrasef ( "unit" , dirFloat , unit )
oBytes , err := json . Marshal ( StatsDiskJson { u } )
if err != nil {
return c . InternalErrorJS ( err , w , r )
}
w . Write ( oBytes )
return nil
}