Refactor sysinfo page

This commit is contained in:
Gucheng Wang
2023-03-09 17:11:41 +08:00
parent 2de3f6772d
commit 07dc6bf7cd
6 changed files with 119 additions and 117 deletions

View File

@ -15,22 +15,9 @@
package controllers
import (
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
)
type SystemInfo struct {
MemoryUsed uint64 `json:"memory_used"`
MemoryTotal uint64 `json:"memory_total"`
CpuUsage []float64 `json:"cpu_usage"`
}
type GitRepoInfo struct {
AheadCnt int `json:"ahead_cnt"`
Commit string `json:"commit"`
Version string `json:"version"`
}
// GetSystemInfo
// @Title GetSystemInfo
// @Tag System API
@ -39,54 +26,32 @@ type GitRepoInfo struct {
// @Success 200 {object} object.SystemInfo The Response object
// @router /get-system-info [get]
func (c *ApiController) GetSystemInfo() {
id := c.GetString("id")
if id == "" {
id = c.GetSessionUsername()
}
user := object.GetUser(id)
if user == nil || !user.IsGlobalAdmin {
c.ResponseError(c.T("auth:Unauthorized operation"))
_, ok := c.RequireAdmin()
if !ok {
return
}
cpuUsage, err := util.GetCpuUsage()
systemInfo, err := util.GetSystemInfo()
if err != nil {
c.ResponseError(err.Error())
return
}
memoryUsed, memoryTotal, err := util.GetMemoryUsage()
if err != nil {
c.ResponseError(err.Error())
return
}
c.Data["json"] = SystemInfo{
CpuUsage: cpuUsage,
MemoryUsed: memoryUsed,
MemoryTotal: memoryTotal,
}
c.ServeJSON()
c.ResponseOk(systemInfo)
}
// GitRepoVersion
// @Title GitRepoVersion
// GetVersionInfo
// @Title GetVersionInfo
// @Tag System API
// @Description get local github repo's latest release version info
// @Success 200 {string} local latest version hash of casdoor
// @router /get-release [get]
func (c *ApiController) GitRepoVersion() {
aheadCnt, commit, version, err := util.GetGitRepoVersion()
// @Description get local git repo's latest release version info
// @Success 200 {string} local latest version hash of Casdoor
// @router /get-version-info [get]
func (c *ApiController) GetVersionInfo() {
versionInfo, err := util.GetVersionInfo()
if err != nil {
c.ResponseError(err.Error())
return
}
c.Data["json"] = GitRepoInfo{
AheadCnt: aheadCnt,
Commit: commit,
Version: version,
}
c.ServeJSON()
c.ResponseOk(versionInfo)
}