fix: fix get version error (#1044)

* feat: fix get version error

* feat: more safe

* fix
This commit is contained in:
q1anx1
2022-08-21 10:47:36 +08:00
committed by GitHub
parent feec6abd88
commit f3b3376a3c
10 changed files with 59 additions and 16 deletions

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import {Card, Col, Divider, Progress, Row} from "antd";
import {Card, Col, Divider, Progress, Row, Spin} from "antd";
import * as SystemBackend from "./backend/SystemInfo";
import React from "react";
import * as Setting from "./Setting";
@ -26,8 +26,10 @@ class SystemInfo extends React.Component {
cpuUsage: [],
memUsed: 0,
memTotal: 0,
latestVersion: "v1.0.0",
latestVersion: undefined,
intervalId: null,
href: "",
loading: true,
};
}
@ -37,6 +39,7 @@ class SystemInfo extends React.Component {
cpuUsage: res.cpu_usage,
memUsed: res.memory_used,
memTotal: res.memory_total,
loading: false,
});
const id = setInterval(() => {
@ -46,6 +49,8 @@ class SystemInfo extends React.Component {
memUsed: res.memory_used,
memTotal: res.memory_total,
});
}).catch(error => {
Setting.showMessage("error", `System info failed to get: ${error}`);
});
}, 1000 * 3);
this.setState({intervalId: id});
@ -54,17 +59,37 @@ class SystemInfo extends React.Component {
});
SystemBackend.getGitHubLatestReleaseVersion().then(res => {
this.setState({latestVersion: res});
const href = res && res.length >= 8 ? `https://github.com/casdoor/casdoor/commit/${res}` : "";
const versionText = res && res.length >= 8 ? res.substring(0, 8) : i18next.t("system:Unknown Version");
this.setState({latestVersion: versionText, href: href});
}).catch(err => {
Setting.showMessage("error", `get latest commit version failed: ${err}`);
});
}
componentWillUnmount() {
clearInterval(this.state.intervalId);
if (this.state.intervalId !== null) {
clearInterval(this.state.intervalId);
}
}
render() {
const CPUInfo = this.state.cpuUsage?.length > 0 ?
this.state.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")
);
return (
<Row>
<Col span={6}></Col>
@ -72,21 +97,12 @@ class SystemInfo extends React.Component {
<Row gutter={[10, 10]}>
<Col span={12}>
<Card title={i18next.t("system:CPU Usage")} bordered={true} style={{textAlign: "center"}}>
{
this.state.cpuUsage.length !== 0 &&
this.state.cpuUsage.map((usage, i) => {
return (
<Progress key={i} percent={Number(usage.toFixed(1))} />
);
})
}
{this.state.loading ? <Spin size="large" /> : CPUInfo}
</Card>
</Col>
<Col span={12}>
<Card title={i18next.t("system:Memory Usage")} bordered={true} style={{textAlign: "center"}}>
{(Number(this.state.memUsed) / 1024 / 1024).toFixed(2)} MB / {(Number(this.state.memTotal) / 1024 / 1024 / 1024).toFixed(2)} GB
<br /> <br />
<Progress type="circle" percent={Number((Number(this.state.memUsed) / Number(this.state.memTotal) * 100).toFixed(2))} />
{this.state.loading ? <Spin size="large" /> : MemInfo}
</Card>
</Col>
</Row>
@ -95,7 +111,7 @@ class SystemInfo extends React.Component {
<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>
<br />
{i18next.t("system:Version")}: <a href={`https://github.com/casdoor/casdoor/commit/${this.state.latestVersion}`}>{this.state.latestVersion.substring(0, 8)}</a>
{i18next.t("system:Version")}: <a href={this.state.href}>{this.state.latestVersion}</a>
<br />
{i18next.t("system:Official Website")}: <a href="https://casdoor.org/">casdoor.org</a>
<br />