2
2
<n-card bordered shadow =" always" >
3
3
<n-grid x-gap =" 12" :cols =" 2" >
4
4
<n-gi >
5
- <p >XCPC-TOOLS 心跳上报状态:<n-tag :type =" onHeartbeat ? 'success' : 'error'" >{{ onHeartbeat ? '已开启' : '未开启' }}</n-tag ></p >
6
- <p >当前上报中心:<n-tag :type =" !heartbeaturl ? 'error' : 'success'" >{{ heartbeaturl }}</n-tag ></p >
5
+ <p >上报中心:<n-tag :type =" !nowHeartbeat ? 'error' : 'success'" >{{ nowHeartbeat || 'no center' }}</n-tag ></p >
6
+ <n-space >
7
+ <n-tag :type =" onHeartbeat ? 'success' : 'error'" >{{ onHeartbeat ? '已开启上报' : '未开启上报' }}</n-tag >
8
+ <n-button type =" warning" size =" small" @click =" " >获取中心状态</n-button >
9
+ </n-space >
7
10
</n-gi >
8
11
<n-gi >
9
- <n-input placeholder =" 请输入心跳上报URL " v-model:value =" heartbeaturl " size =" large" style =" width : 100% ; margin-bottom : .5em ;" />
10
- <n-grid x-gap =" 12" :cols =" 2 " style =" width : 100% ;" >
12
+ <n-input placeholder =" IP/HOST/URL " v-model:value =" editHeartbeat " size =" large" style =" width : 100% ; margin-bottom : .5em ;" />
13
+ <n-grid x-gap =" 12" :cols =" 3 " style =" width : 100% ;" >
11
14
<n-gi >
12
- <n-button type =" primary" @click =" saveHeartbeat" style =" width : 100% ;" >保存</n-button >
15
+ <n-button type =" primary" @click =" saveHeartbeat(false) " style =" width : 100% ;" >保存</n-button >
13
16
</n-gi >
14
17
<n-gi >
15
- <n-button type =" info" @click =" runHeartbeat" style =" width : 100% ;" >测试</n-button >
18
+ <n-button type =" info" @click =" testHeartbeat" style =" width : 100% ;" >测试</n-button >
19
+ </n-gi >
20
+ <n-gi >
21
+ <n-button type =" warning" @click =" saveHeartbeat(true)" style =" width : 100% ;" >强制保存</n-button >
16
22
</n-gi >
17
23
</n-grid >
18
24
</n-gi >
@@ -25,44 +31,76 @@ import { filesystem, os } from '@neutralinojs/lib';
25
31
import { NCard , NGrid , NGi , NButton , NInput } from ' naive-ui' ;
26
32
import { onMounted , ref } from ' vue' ;
27
33
28
- const heartbeaturl = ref <string >(' ' );
34
+ const editHeartbeat = ref <string >(' ' );
35
+ const nowHeartbeat = ref <string >(' ' );
29
36
const onHeartbeat = ref <boolean >(false );
30
37
31
- const saveHeartbeat = async () => {
38
+ const getRealHeartbeatUrl = async () => {
39
+ const iporHost = editHeartbeat .value .trim ();
40
+ if (iporHost .includes (' http' )) return iporHost ;
41
+ if (! iporHost .includes (' :' )) return ` http://${iporHost }:5283/report ` ;
42
+ return ` http://${iporHost }/report ` ;
43
+ };
44
+
45
+ const getHeartbeatVersion = async (url : string ) => {
32
46
try {
33
- console .log (' save heartbeat' , heartbeaturl .value );
34
- const res = await os .execCommand (` HEARTBEATURL=${heartbeaturl .value } /usr/sbin/icpc-heartbeat ` );
35
- if (res .stdErr || res .exitCode ) throw new Error (res .stdErr );
36
- console .log (' run heartbeat on save' , res );
37
- await filesystem .writeFile (' /etc/default/icpc-heartbeat' , ` HEARTBEATURL=${heartbeaturl .value } ` );
38
- const res2 = await os .execCommand (' systemctl enable heartbeat.timer' );
39
- console .log (' run enable heartbeat on save' , res2 );
47
+ const version = await fetch (url .replace (' /report' , ' /version' )).then (res => res .json ());
48
+ if (! version ) throw new Error (' 无法获取上报中心版本' );
49
+ window .$notification .success ({ title: ' 连接上报中心成功' , content: ` 上报中心版本:${version .version } ` , duration: 3000 });
40
50
} catch (error ) {
41
- console .error (` save heartbeat error: ${error }` );
42
- window .$notification .error ({ title: ' 保存心跳上报URL失败 ' , content: (error as any ).message });
51
+ console .error (` get heartbeat error: ${error }` );
52
+ window .$notification .error ({ title: ' 获取中心版本失败 ' , content: (error as any ).message , duration: 3000 });
43
53
}
44
54
};
45
55
46
- const runHeartbeat = async () => {
56
+ const runHeartbeat = async (url : string ) => {
47
57
try {
48
- const version = await fetch (heartbeaturl .value .replace (' /report' , ' /version' )).then (res => res .json ());
49
- if (! version ) throw new Error (' 无法获取上报中心版本' );
50
- window .$notification .success ({ title: ' 连接上报中心成功' , content: ` 上报中心版本:${version .version } ` });
51
- const res = await os .execCommand (` HEARTBEATURL=${heartbeaturl .value } /usr/sbin/icpc-heartbeat ` );
58
+ const res = await os .execCommand (` HEARTBEATURL=${url } /usr/sbin/icpc-heartbeat ` );
52
59
if (res .stdErr || res .exitCode ) throw new Error (res .stdErr );
53
60
console .log (' run heartbeat on test' , res );
54
61
} catch (error ) {
55
62
console .error (` run heartbeat error: ${error } ` );
56
- window .$notification .error ({ title: ' 测试心跳上报URL失败' , content: (error as any ).message });
63
+ window .$notification .error ({ title: ' 测试心跳上报URL失败' , content: (error as any ).message , duration: 3000 });
64
+ }
65
+ };
66
+
67
+ const testHeartbeat = async () => {
68
+ const url = await getRealHeartbeatUrl ();
69
+ try {
70
+ await getHeartbeatVersion (url );
71
+ await runHeartbeat (url );
72
+ } catch (error ) {
73
+ console .error (` test heartbeat error: ${error } ` );
74
+ window .$notification .error ({ title: ' 测试心跳上报URL失败' , content: (error as any ).message , duration: 3000 });
57
75
}
58
76
};
59
77
78
+ const saveHeartbeat = async (force = false ) => {
79
+ const url = await getRealHeartbeatUrl ();
80
+ try {
81
+ if (! force ) {
82
+ await getHeartbeatVersion (url );
83
+ await runHeartbeat (url );
84
+ }
85
+ console .log (' save heartbeat' , url );
86
+ await filesystem .writeFile (' /etc/default/icpc-heartbeat' , ` HEARTBEATURL=${url } ` );
87
+ const res2 = await os .execCommand (' systemctl enable heartbeat.timer' );
88
+ console .log (' run enable heartbeat on save' , res2 );
89
+ nowHeartbeat .value = url ;
90
+ } catch (error ) {
91
+ console .error (` save heartbeat error: ${error } ` );
92
+ window .$notification .error ({ title: ' 保存心跳上报URL失败' , content: (error as any ).message , duration: 3000 });
93
+ }
94
+ };
95
+
96
+
97
+
60
98
onMounted (async () => {
61
99
try {
62
100
const res = await filesystem .readFile (' /etc/default/icpc-heartbeat' );
63
101
console .log (' icpc-heartbeat' , res );
64
- heartbeaturl .value = res .split (' =' )[1 ].trim ();
65
- if (! heartbeaturl .value ) {
102
+ nowHeartbeat .value = res .split (' =' )[1 ].trim ();
103
+ if (! nowHeartbeat .value ) {
66
104
const res = await os .execCommand (' systemctl disable heartbeat.timer' );
67
105
console .log (' disable heartbeat.timer' , res );
68
106
onHeartbeat .value = false ;
0 commit comments