github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/routes/panel/debug.go (about)

     1  package panel
     2  
     3  import (
     4  	"net/http"
     5  	"runtime"
     6  	"strconv"
     7  	"time"
     8  
     9  	c "github.com/Azareal/Gosora/common"
    10  	qgen "github.com/Azareal/Gosora/query_gen"
    11  )
    12  
    13  func Debug(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
    14  	bp, ferr := buildBasePage(w, r, u, "debug", "debug")
    15  	if ferr != nil {
    16  		return ferr
    17  	}
    18  
    19  	goVersion := runtime.Version()
    20  	dbVersion := qgen.Builder.DbVersion()
    21  	upDur := time.Since(c.StartTime)
    22  	hours := int(upDur.Hours())
    23  	mins := int(upDur.Minutes())
    24  	secs := int(upDur.Seconds())
    25  	var uptime string
    26  	if hours > 24 {
    27  		days := hours / 24
    28  		hours -= days * 24
    29  		uptime += strconv.Itoa(days) + "d"
    30  		uptime += strconv.Itoa(hours) + "h"
    31  	} else if hours >= 1 {
    32  		mins -= hours * 60
    33  		uptime += strconv.Itoa(hours) + "h"
    34  		uptime += strconv.Itoa(mins) + "m"
    35  	} else if mins >= 1 {
    36  		secs -= mins * 60
    37  		uptime += strconv.Itoa(mins) + "m"
    38  		uptime += strconv.Itoa(secs) + "s"
    39  	}
    40  
    41  	dbStats := qgen.Builder.GetConn().Stats()
    42  	openConnCount := dbStats.OpenConnections
    43  	// Disk I/O?
    44  	// TODO: Fetch the adapter from Builder rather than getting it from a global?
    45  	goroutines := runtime.NumGoroutine()
    46  	cpus := runtime.NumCPU()
    47  	httpConns := c.ConnWatch.Count()
    48  
    49  	debugTasks := c.DebugPageTasks{c.Tasks.HalfSec.Count(), c.Tasks.Sec.Count(), c.Tasks.FifteenMin.Count(), c.Tasks.Hour.Count(), c.Tasks.Day.Count(), c.Tasks.Shutdown.Count()}
    50  	var memStats runtime.MemStats
    51  	runtime.ReadMemStats(&memStats)
    52  
    53  	var tlen, ulen, rlen int
    54  	var tcap, ucap, rcap int
    55  	tc := c.Topics.GetCache()
    56  	if tc != nil {
    57  		tlen, tcap = tc.Length(), tc.GetCapacity()
    58  	}
    59  	uc := c.Users.GetCache()
    60  	if uc != nil {
    61  		ulen, ucap = uc.Length(), uc.GetCapacity()
    62  	}
    63  	rc := c.Rstore.GetCache()
    64  	if rc != nil {
    65  		rlen, rcap = rc.Length(), rc.GetCapacity()
    66  	}
    67  	topicListThawed := c.TopicListThaw.Thawed()
    68  
    69  	debugCache := c.DebugPageCache{tlen, ulen, rlen, tcap, ucap, rcap, topicListThawed}
    70  
    71  	var fErr error
    72  	acc := qgen.NewAcc()
    73  	count := func(tbl string) int {
    74  		if fErr != nil {
    75  			return 0
    76  		}
    77  		c, err := acc.Count(tbl).Total()
    78  		fErr = err
    79  		return c
    80  	}
    81  
    82  	// TODO: Call Count on an attachment store
    83  	attachs := count("attachments")
    84  	// TODO: Implement a PollStore and call Count on that instead
    85  	//polls := count("polls")
    86  	polls := c.Polls.Count()
    87  	//pollsOptions := count("polls_options") // TODO: Add this
    88  	//pollsVotes := count("polls_votes") // TODO: Add this
    89  
    90  	//loginLogs := count("login_logs")
    91  	loginLogs := c.LoginLogs.Count()
    92  	//regLogs := count("registration_logs")
    93  	regLogs := c.RegLogs.Count()
    94  	//modLogs := count("moderation_logs")
    95  	modLogs := c.ModLogs.Count()
    96  	//adminLogs := count("administration_logs")
    97  	adminLogs := c.AdminLogs.Count()
    98  
    99  	views := count("viewchunks")
   100  	viewsAgents := count("viewchunks_agents")
   101  	viewsForums := count("viewchunks_forums")
   102  	viewsLangs := count("viewchunks_langs")
   103  	viewsReferrers := count("viewchunks_referrers")
   104  	viewsSystems := count("viewchunks_systems")
   105  	postChunks := count("postchunks")
   106  	topicChunks := count("topicchunks")
   107  	if fErr != nil {
   108  		return c.InternalError(fErr, w, r)
   109  	}
   110  
   111  	debugDatabase := c.DebugPageDatabase{c.Topics.Count(), c.Users.Count(), c.Rstore.Count(), c.Prstore.Count(), c.Activity.Count(), c.Likes.Count(), attachs, polls, loginLogs, regLogs, modLogs, adminLogs, views, viewsAgents, viewsForums, viewsLangs, viewsReferrers, viewsSystems, postChunks, topicChunks}
   112  
   113  	dirSize := func(path string) int {
   114  		if fErr != nil {
   115  			return 0
   116  		}
   117  		c, err := c.DirSize(path)
   118  		fErr = err
   119  		return c
   120  	}
   121  
   122  	staticSize := dirSize("./public/")
   123  	attachSize := dirSize("./attachs/")
   124  	uploadsSize := dirSize("./uploads/")
   125  	logsSize := dirSize(c.Config.LogDir)
   126  	backupsSize := dirSize("./backups/")
   127  	if fErr != nil {
   128  		return c.InternalError(fErr, w, r)
   129  	}
   130  	// TODO: How can we measure this without freezing up the entire page?
   131  	//gitSize, _ := c.DirSize("./.git")
   132  	gitSize := 0
   133  
   134  	debugDisk := c.DebugPageDisk{staticSize, attachSize, uploadsSize, logsSize, backupsSize, gitSize}
   135  
   136  	pi := c.PanelDebugPage{bp, goVersion, dbVersion, uptime, openConnCount, qgen.Builder.GetAdapter().GetName(), goroutines, cpus, httpConns, debugTasks, memStats, debugCache, debugDatabase, debugDisk}
   137  	return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "panel_dashboard_right", "debug_page", "panel_debug", pi})
   138  }
   139  
   140  func DebugTasks(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
   141  	bp, ferr := buildBasePage(w, r, u, "debug", "debug")
   142  	if ferr != nil {
   143  		return ferr
   144  	}
   145  
   146  	var tasks []c.PanelTaskTask
   147  	var taskTypes []c.PanelTaskType
   148  
   149  	pi := c.PanelTaskPage{bp, tasks, taskTypes}
   150  	return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "panel_dashboard_right", "debug_page", "panel_debug_task", pi})
   151  }