forked from casdoor/casdoor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add system info page (casdoor#1033)
* feat: add system info page * feat: add some code * fix
- Loading branch information
1 parent
67a5adf
commit ef4c383
Showing
17 changed files
with
439 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
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"` | ||
} | ||
|
||
// GetSystemInfo | ||
// @Title GetSystemInfo | ||
// @Tag System API | ||
// @Description get user's system info | ||
// @Param id query string true "The id of the user" | ||
// @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("You are not authorized to access this resource") | ||
} | ||
|
||
cpuUsage, err := util.GetCpuUsage() | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
} | ||
|
||
memoryUsed, memoryTotal, err := util.GetMemoryUsage() | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
} | ||
|
||
c.Data["json"] = SystemInfo{ | ||
CpuUsage: cpuUsage, | ||
MemoryUsed: memoryUsed, | ||
MemoryTotal: memoryTotal, | ||
} | ||
c.ServeJSON() | ||
} | ||
|
||
// GitRepoVersion | ||
// @Title GitRepoVersion | ||
// @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() { | ||
version, err := util.GetGitRepoVersion() | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
} | ||
|
||
c.Data["json"] = version | ||
c.ServeJSON() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/shirou/gopsutil/cpu" | ||
"github.com/shirou/gopsutil/mem" | ||
) | ||
|
||
// get cpu usage | ||
func GetCpuUsage() ([]float64, error) { | ||
usage, err := cpu.Percent(time.Second, true) | ||
return usage, err | ||
} | ||
|
||
var fileDate, version string | ||
|
||
// get memory usage | ||
func GetMemoryUsage() (uint64, uint64, error) { | ||
virtualMem, err := mem.VirtualMemory() | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
var m runtime.MemStats | ||
runtime.ReadMemStats(&m) | ||
|
||
return m.TotalAlloc, virtualMem.Total, nil | ||
} | ||
|
||
// get github repo release version | ||
func GetGitRepoVersion() (string, error) { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
fileInfos, err := ioutil.ReadDir(pwd + "/.git/refs/heads") | ||
for _, v := range fileInfos { | ||
if v.Name() == "master" { | ||
if v.ModTime().String() == fileDate { | ||
return version, nil | ||
} else { | ||
fileDate = v.ModTime().String() | ||
break | ||
} | ||
} | ||
} | ||
|
||
content, err := ioutil.ReadFile(pwd + "/.git/refs/heads/master") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Convert to full length | ||
temp := string(content) | ||
version = strings.ReplaceAll(temp, "\n", "") | ||
|
||
return version, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetCpuUsage(t *testing.T) { | ||
usage, err := GetCpuUsage() | ||
assert.Nil(t, err) | ||
t.Log(usage) | ||
} | ||
|
||
func TestGetMemoryUsage(t *testing.T) { | ||
used, total, err := GetMemoryUsage() | ||
assert.Nil(t, err) | ||
t.Log(used, total) | ||
} |
Oops, something went wrong.