mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Refactor sysinfo page
This commit is contained in:
parent
2de3f6772d
commit
07dc6bf7cd
@ -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)
|
||||
}
|
||||
|
@ -225,5 +225,5 @@ func initAPI() {
|
||||
beego.Router("/api/webauthn/signin/finish", &controllers.ApiController{}, "Post:WebAuthnSigninFinish")
|
||||
|
||||
beego.Router("/api/get-system-info", &controllers.ApiController{}, "GET:GetSystemInfo")
|
||||
beego.Router("/api/get-release", &controllers.ApiController{}, "GET:GitRepoVersion")
|
||||
beego.Router("/api/get-version-info", &controllers.ApiController{}, "GET:GetVersionInfo")
|
||||
}
|
||||
|
@ -26,16 +26,26 @@ import (
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
)
|
||||
|
||||
// get cpu usage
|
||||
func GetCpuUsage() ([]float64, error) {
|
||||
type SystemInfo struct {
|
||||
CpuUsage []float64 `json:"cpuUsage"`
|
||||
MemoryUsed uint64 `json:"memoryUsed"`
|
||||
MemoryTotal uint64 `json:"memoryTotal"`
|
||||
}
|
||||
|
||||
type VersionInfo struct {
|
||||
Version string `json:"version"`
|
||||
CommitId string `json:"commitId"`
|
||||
CommitOffset int `json:"commitOffset"`
|
||||
}
|
||||
|
||||
// getCpuUsage 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) {
|
||||
// getMemoryUsage get memory usage
|
||||
func getMemoryUsage() (uint64, uint64, error) {
|
||||
virtualMem, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
@ -47,21 +57,46 @@ func GetMemoryUsage() (uint64, uint64, error) {
|
||||
return m.TotalAlloc, virtualMem.Total, nil
|
||||
}
|
||||
|
||||
// get github current commit and repo release version
|
||||
func GetGitRepoVersion() (int, string, string, error) {
|
||||
func GetSystemInfo() (*SystemInfo, error) {
|
||||
cpuUsage, err := getCpuUsage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
memoryUsed, memoryTotal, err := getMemoryUsage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := &SystemInfo{
|
||||
CpuUsage: cpuUsage,
|
||||
MemoryUsed: memoryUsed,
|
||||
MemoryTotal: memoryTotal,
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetVersionInfo get git current commit and repo release version
|
||||
func GetVersionInfo() (*VersionInfo, error) {
|
||||
res := &VersionInfo{
|
||||
Version: "",
|
||||
CommitId: "",
|
||||
CommitOffset: -1,
|
||||
}
|
||||
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
rootPath := path.Dir(path.Dir(filename))
|
||||
r, err := git.PlainOpen(rootPath)
|
||||
if err != nil {
|
||||
return -1, "", "", err
|
||||
return res, err
|
||||
}
|
||||
ref, err := r.Head()
|
||||
if err != nil {
|
||||
return -1, "", "", err
|
||||
return res, err
|
||||
}
|
||||
tags, err := r.Tags()
|
||||
if err != nil {
|
||||
return -1, "", "", err
|
||||
return res, err
|
||||
}
|
||||
tagMap := make(map[plumbing.Hash]string)
|
||||
err = tags.ForEach(func(t *plumbing.Reference) error {
|
||||
@ -74,29 +109,34 @@ func GetGitRepoVersion() (int, string, string, error) {
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return -1, "", "", err
|
||||
return res, err
|
||||
}
|
||||
|
||||
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
|
||||
|
||||
aheadCnt := 0
|
||||
releaseVersion := ""
|
||||
commitOffset := 0
|
||||
version := ""
|
||||
// iterates over the commits
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
tag, ok := tagMap[c.Hash]
|
||||
if ok {
|
||||
if releaseVersion == "" {
|
||||
releaseVersion = tag
|
||||
if version == "" {
|
||||
version = tag
|
||||
}
|
||||
}
|
||||
if releaseVersion == "" {
|
||||
aheadCnt++
|
||||
if version == "" {
|
||||
commitOffset++
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return -1, "", "", err
|
||||
return res, err
|
||||
}
|
||||
|
||||
return aheadCnt, ref.Hash().String(), releaseVersion, nil
|
||||
res = &VersionInfo{
|
||||
Version: version,
|
||||
CommitId: ref.Hash().String(),
|
||||
CommitOffset: commitOffset,
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
@ -29,21 +29,21 @@ import (
|
||||
)
|
||||
|
||||
func TestGetCpuUsage(t *testing.T) {
|
||||
usage, err := GetCpuUsage()
|
||||
usage, err := getCpuUsage()
|
||||
assert.Nil(t, err)
|
||||
t.Log(usage)
|
||||
}
|
||||
|
||||
func TestGetMemoryUsage(t *testing.T) {
|
||||
used, total, err := GetMemoryUsage()
|
||||
used, total, err := getMemoryUsage()
|
||||
assert.Nil(t, err)
|
||||
t.Log(used, total)
|
||||
}
|
||||
|
||||
func TestGetGitRepoVersion(t *testing.T) {
|
||||
aheadCnt, commit, version, err := GetGitRepoVersion()
|
||||
versionInfo, err := GetVersionInfo()
|
||||
assert.Nil(t, err)
|
||||
t.Log(aheadCnt, commit, version)
|
||||
t.Log(versionInfo)
|
||||
}
|
||||
|
||||
func TestGetVersion(t *testing.T) {
|
@ -23,31 +23,24 @@ class SystemInfo extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
cpuUsage: [],
|
||||
memUsed: 0,
|
||||
memTotal: 0,
|
||||
latestVersion: undefined,
|
||||
systemInfo: {cpuUsage: [], memoryUsed: 0, memoryTotal: 0},
|
||||
versionInfo: {},
|
||||
intervalId: null,
|
||||
href: "",
|
||||
loading: true,
|
||||
};
|
||||
}
|
||||
|
||||
UNSAFE_componentWillMount() {
|
||||
SystemBackend.getSystemInfo(this.props.account?.owner, this.props.account?.name).then(res => {
|
||||
SystemBackend.getSystemInfo().then(res => {
|
||||
this.setState({
|
||||
cpuUsage: res.cpu_usage,
|
||||
memUsed: res.memory_used,
|
||||
memTotal: res.memory_total,
|
||||
systemInfo: res.data,
|
||||
loading: false,
|
||||
});
|
||||
|
||||
const id = setInterval(() => {
|
||||
SystemBackend.getSystemInfo(this.props.account?.owner, this.props.account?.name).then(res => {
|
||||
SystemBackend.getSystemInfo().then(res => {
|
||||
this.setState({
|
||||
cpuUsage: res.cpu_usage,
|
||||
memUsed: res.memory_used,
|
||||
memTotal: res.memory_total,
|
||||
systemInfo: res.data,
|
||||
});
|
||||
}).catch(error => {
|
||||
Setting.showMessage("error", `System info failed to get: ${error}`);
|
||||
@ -58,12 +51,12 @@ class SystemInfo extends React.Component {
|
||||
Setting.showMessage("error", `System info failed to get: ${error}`);
|
||||
});
|
||||
|
||||
SystemBackend.getGitHubLatestReleaseVersion().then(res => {
|
||||
const href = res.version && res.version.length > 0 ? `https://github.com/casdoor/casdoor/releases/tag/${res.version}` : "";
|
||||
const versionText = res.version && res.version.length > 0 ? res.version : i18next.t("system:Unknown Version");
|
||||
this.setState({latestVersion: versionText, href: href});
|
||||
SystemBackend.getVersionInfo().then(res => {
|
||||
this.setState({
|
||||
versionInfo: res.data,
|
||||
});
|
||||
}).catch(err => {
|
||||
Setting.showMessage("error", `get latest commit version failed: ${err}`);
|
||||
Setting.showMessage("error", `Version info failed to get: ${err}`);
|
||||
});
|
||||
}
|
||||
|
||||
@ -74,21 +67,25 @@ class SystemInfo extends React.Component {
|
||||
}
|
||||
|
||||
render() {
|
||||
const CPUInfo = this.state.cpuUsage?.length > 0 ?
|
||||
this.state.cpuUsage.map((usage, i) => {
|
||||
const cpuUi = this.state.systemInfo.cpuUsage?.length <= 0 ? i18next.t("system:Get CPU Usage Failed") :
|
||||
this.state.systemInfo.cpuUsage.map((usage, i) => {
|
||||
return (
|
||||
<Progress key={i} percent={Number(usage.toFixed(1))} />
|
||||
);
|
||||
}) : i18next.t("system:Get CPU Usage Failed");
|
||||
});
|
||||
|
||||
const MemInfo = (
|
||||
this.state.memUsed && this.state.memTotal && this.state.memTotal !== 0 ?
|
||||
<div>
|
||||
{Setting.getFriendlyFileSize(this.state.memUsed)} / {Setting.getFriendlyFileSize(this.state.memTotal)}
|
||||
<br /> <br />
|
||||
<Progress type="circle" percent={Number((Number(this.state.memUsed) / Number(this.state.memTotal) * 100).toFixed(2))} />
|
||||
</div> : i18next.t("system:Get Memory Usage Failed")
|
||||
);
|
||||
const memUi = this.state.systemInfo.memoryUsed && this.state.systemInfo.memoryTotal && this.state.systemInfo.memoryTotal <= 0 ? i18next.t("system:Get Memory Usage Failed") :
|
||||
<div>
|
||||
{Setting.getFriendlyFileSize(this.state.systemInfo.memoryUsed)} / {Setting.getFriendlyFileSize(this.state.systemInfo.memoryTotal)}
|
||||
<br /> <br />
|
||||
<Progress type="circle" percent={Number((Number(this.state.systemInfo.memoryUsed) / Number(this.state.systemInfo.memoryTotal) * 100).toFixed(2))} />
|
||||
</div>;
|
||||
|
||||
const link = this.state.versionInfo?.version !== "" ? `https://github.com/casdoor/casdoor/releases/tag/${this.state.versionInfo?.version}` : "";
|
||||
let versionText = this.state.versionInfo?.version !== "" ? this.state.versionInfo?.version : i18next.t("system:Unknown Version");
|
||||
if (this.state.versionInfo?.commitOffset > 0) {
|
||||
versionText += ` (ahead+${this.state.versionInfo?.commitOffset})`;
|
||||
}
|
||||
|
||||
if (!Setting.isMobile()) {
|
||||
return (
|
||||
@ -98,25 +95,25 @@ class SystemInfo extends React.Component {
|
||||
<Row gutter={[10, 10]}>
|
||||
<Col span={12}>
|
||||
<Card title={i18next.t("system:CPU Usage")} bordered={true} style={{textAlign: "center", height: "100%"}}>
|
||||
{this.state.loading ? <Spin size="large" /> : CPUInfo}
|
||||
{this.state.loading ? <Spin size="large" /> : cpuUi}
|
||||
</Card>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Card title={i18next.t("system:Memory Usage")} bordered={true} style={{textAlign: "center", height: "100%"}}>
|
||||
{this.state.loading ? <Spin size="large" /> : MemInfo}
|
||||
{this.state.loading ? <Spin size="large" /> : memUi}
|
||||
</Card>
|
||||
</Col>
|
||||
</Row>
|
||||
<Divider />
|
||||
<Card title={i18next.t("system:About Casdoor")} bordered={true} style={{textAlign: "center"}}>
|
||||
<div>{i18next.t("system:An Identity and Access Management (IAM) / Single-Sign-On (SSO) platform with web UI supporting OAuth 2.0, OIDC, SAML and CAS")}</div>
|
||||
GitHub: <a href="https://github.com/casdoor/casdoor">casdoor</a>
|
||||
GitHub: <a target="_blank" rel="noreferrer" href="https://github.com/casdoor/casdoor">Casdoor</a>
|
||||
<br />
|
||||
{i18next.t("system:Version")}: <a href={this.state.href}>{this.state.latestVersion}</a>
|
||||
{i18next.t("system:Version")}: <a target="_blank" rel="noreferrer" href={link}>{versionText}</a>
|
||||
<br />
|
||||
{i18next.t("system:Official Website")}: <a href="https://casdoor.org/">casdoor.org</a>
|
||||
{i18next.t("system:Official Website")}: <a target="_blank" rel="noreferrer" href="https://casdoor.org">https://casdoor.org</a>
|
||||
<br />
|
||||
{i18next.t("system:Community")}: <a href="https://casdoor.org/#:~:text=Casdoor%20API-,Community,-GitHub">contact us</a>
|
||||
{i18next.t("system:Community")}: <a target="_blank" rel="noreferrer" href="https://casdoor.org/#:~:text=Casdoor%20API-,Community,-GitHub">Get in Touch!</a>
|
||||
</Card>
|
||||
</Col>
|
||||
<Col span={6}></Col>
|
||||
@ -127,24 +124,24 @@ class SystemInfo extends React.Component {
|
||||
<Row gutter={[16, 0]}>
|
||||
<Col span={24}>
|
||||
<Card title={i18next.t("system:CPU Usage")} bordered={true} style={{textAlign: "center", width: "100%"}}>
|
||||
{this.state.loading ? <Spin size="large" /> : CPUInfo}
|
||||
{this.state.loading ? <Spin size="large" /> : cpuUi}
|
||||
</Card>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Card title={i18next.t("system:Memory Usage")} bordered={true} style={{textAlign: "center", width: "100%"}}>
|
||||
{this.state.loading ? <Spin size="large" /> : MemInfo}
|
||||
{this.state.loading ? <Spin size="large" /> : memUi}
|
||||
</Card>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Card title={i18next.t("system:About Casdoor")} bordered={true} style={{textAlign: "center"}}>
|
||||
<div>{i18next.t("system:An Identity and Access Management (IAM) / Single-Sign-On (SSO) platform with web UI supporting OAuth 2.0, OIDC, SAML and CAS")}</div>
|
||||
GitHub: <a href="https://github.com/casdoor/casdoor">casdoor</a>
|
||||
GitHub: <a target="_blank" rel="noreferrer" href="https://github.com/casdoor/casdoor">Casdoor</a>
|
||||
<br />
|
||||
{i18next.t("system:Version")}: <a href={this.state.href}>{this.state.latestVersion}</a>
|
||||
{i18next.t("system:Version")}: <a target="_blank" rel="noreferrer" href={link}>{versionText}</a>
|
||||
<br />
|
||||
{i18next.t("system:Official Website")}: <a href="https://casdoor.org/">casdoor.org</a>
|
||||
{i18next.t("system:Official Website")}: <a target="_blank" rel="noreferrer" href="https://casdoor.org">https://casdoor.org</a>
|
||||
<br />
|
||||
{i18next.t("system:Community")}: <a href="https://casdoor.org/#:~:text=Casdoor%20API-,Community,-GitHub">contact us</a>
|
||||
{i18next.t("system:Community")}: <a target="_blank" rel="noreferrer" href="https://casdoor.org/#:~:text=Casdoor%20API-,Community,-GitHub">Get in Touch!</a>
|
||||
</Card>
|
||||
</Col>
|
||||
</Row>
|
||||
|
@ -14,8 +14,8 @@
|
||||
|
||||
import * as Setting from "../Setting";
|
||||
|
||||
export function getSystemInfo(owner, name) {
|
||||
return fetch(`${Setting.ServerUrl}/api/get-system-info?id=${owner}/${encodeURIComponent(name)}`, {
|
||||
export function getSystemInfo() {
|
||||
return fetch(`${Setting.ServerUrl}/api/get-system-info`, {
|
||||
method: "GET",
|
||||
credentials: "include",
|
||||
headers: {
|
||||
@ -24,8 +24,8 @@ export function getSystemInfo(owner, name) {
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function getGitHubLatestReleaseVersion() {
|
||||
return fetch(`${Setting.ServerUrl}/api/get-release`, {
|
||||
export function getVersionInfo() {
|
||||
return fetch(`${Setting.ServerUrl}/api/get-version-info`, {
|
||||
method: "GET",
|
||||
credentials: "include",
|
||||
headers: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user