Removed dirsize from the control panel dashboard.

Experimenting with dirsize.
This commit is contained in:
Azareal 2019-07-21 09:09:26 +10:00
parent 483ff06775
commit cb8e83ec79
1 changed files with 85 additions and 26 deletions

View File

@ -6,6 +6,10 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"runtime" "runtime"
"encoding/json"
"time"
"sync"
"sync/atomic"
c "github.com/Azareal/Gosora/common" c "github.com/Azareal/Gosora/common"
p "github.com/Azareal/Gosora/common/phrases" p "github.com/Azareal/Gosora/common/phrases"
@ -73,10 +77,10 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
var unknown = p.GetTmplPhrase("panel_dashboard_unknown") unknown := p.GetTmplPhrase("panel_dashboard_unknown")
// 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 cpustr := unknown
var cpuColour string var cpuColour string
lessThanSwitch := func(number int, lowerBound int, midBound int) string { lessThanSwitch := func(number int, lowerBound int, midBound int) string {
@ -105,7 +109,6 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
} else { } else {
totstr = fmt.Sprintf("%.1f", totalCount) totstr = fmt.Sprintf("%.1f", totalCount)
} }
if usedCount > totalCount { if usedCount > totalCount {
usedCount = totalCount usedCount = totalCount
} }
@ -153,31 +156,31 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
} }
// TODO: Allow for more complex phrase structures than just suffixes // TODO: Allow for more complex phrase structures than just suffixes
var postCount = extractStat(stmts.todaysPostCount) postCount := extractStat(stmts.todaysPostCount)
var postInterval = p.GetTmplPhrase("panel_dashboard_day_suffix") postInterval := p.GetTmplPhrase("panel_dashboard_day_suffix")
var postColour = greaterThanSwitch(postCount, 5, 25) postColour := greaterThanSwitch(postCount, 5, 25)
var topicCount = extractStat(stmts.todaysTopicCount) topicCount := extractStat(stmts.todaysTopicCount)
var topicInterval = p.GetTmplPhrase("panel_dashboard_day_suffix") topicInterval := p.GetTmplPhrase("panel_dashboard_day_suffix")
var topicColour = greaterThanSwitch(topicCount, 0, 8) topicColour := greaterThanSwitch(topicCount, 0, 8)
var reportCount = extractStat(stmts.weeklyTopicCountByForum, c.ReportForumID) reportCount := extractStat(stmts.weeklyTopicCountByForum, c.ReportForumID)
var reportInterval = p.GetTmplPhrase("panel_dashboard_week_suffix") reportInterval := p.GetTmplPhrase("panel_dashboard_week_suffix")
var newUserCount = extractStat(stmts.todaysNewUserCount) newUserCount := extractStat(stmts.todaysNewUserCount)
var newUserInterval = p.GetTmplPhrase("panel_dashboard_week_suffix") newUserInterval := p.GetTmplPhrase("panel_dashboard_week_suffix")
// Did any of the extractStats fail? // Did any of the extractStats fail?
if intErr != nil { if intErr != nil {
return c.InternalError(intErr, w, r) return c.InternalError(intErr, w, r)
} }
var grid1 = []GE{} grid1 := []GE{}
var addElem1 = func(id string, href string, body string, order int, class string, back string, textColour string, tooltip string) { addElem1 := func(id string, href string, body string, order int, class string, back string, textColour string, tooltip string) {
grid1 = append(grid1, GE{id,href,body,order,class,back,textColour,tooltip}) grid1 = append(grid1, GE{id,href,body,order,class,back,textColour,tooltip})
} }
var gridElements = []GE{} gridElements := []GE{}
var addElem = func(id string, href string, body string, order int, class string, back string, textColour string, tooltip string) { addElem := func(id string, href string, body string, order int, class string, back string, textColour string, tooltip string) {
gridElements = append(gridElements, GE{id,href,body,order,class,back,textColour,tooltip}) gridElements = append(gridElements, GE{id,href,body,order,class,back,textColour,tooltip})
} }
@ -190,12 +193,18 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
addElem1("dash-ram","", p.GetTmplPhrasef("panel_dashboard_ram",ramstr), 2, "grid_istat " + ramColour, "", "", p.GetTmplPhrase("panel_dashboard_ram_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")) addElem1("dash-memused","/panel/analytics/memory/", p.GetTmplPhrasef("panel_dashboard_memused",memCount, memUnit), 2, "grid_istat", "", "", p.GetTmplPhrase("panel_dashboard_memused_desc"))
dirSize, err := c.DirSize(".") /*dirSize := getDirSize()
if err != nil { if dirSize.Size != 0 {
return c.InternalError(err,w,r) 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()
} }
dirFloat, unit := c.ConvertByteUnit(float64(dirSize)) } else {
addElem1("dash-disk","", p.GetTmplPhrasef("panel_dashboard_disk",dirFloat, unit), 2, "grid_istat", "", "", p.GetTmplPhrase("panel_dashboard_disk_desc")) addElem1("dash-disk","", p.GetTmplPhrase("panel_dashboard_disk_unknown"), 2, "grid_istat", "", "", p.GetTmplPhrase("panel_dashboard_disk_desc"))
startDirSizeTask()
}*/
if c.EnableWebsockets { if c.EnableWebsockets {
uonline := c.WsHub.UserCount() uonline := c.WsHub.UserCount()
@ -203,9 +212,9 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
totonline := uonline + gonline totonline := uonline + gonline
//reqCount := 0 //reqCount := 0
var onlineColour = greaterThanSwitch(totonline, 3, 10) onlineColour := greaterThanSwitch(totonline, 3, 10)
var onlineGuestsColour = greaterThanSwitch(gonline, 1, 10) onlineGuestsColour := greaterThanSwitch(gonline, 1, 10)
var onlineUsersColour = greaterThanSwitch(uonline, 1, 5) onlineUsersColour := greaterThanSwitch(uonline, 1, 5)
totonline, totunit := c.ConvertFriendlyUnit(totonline) totonline, totunit := c.ConvertFriendlyUnit(totonline)
uonline, uunit := c.ConvertFriendlyUnit(uonline) uonline, uunit := c.ConvertFriendlyUnit(uonline)
@ -233,3 +242,53 @@ func Dashboard(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "panel_dashboard_right","","panel_dashboard", c.DashGrids{grid1,gridElements}}) return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "panel_dashboard_right","","panel_dashboard", c.DashGrids{grid1,gridElements}})
} }
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
}