Merge pull request #53 from ilay1678/master

添加标签功能,默认主题可根据标签分组 #32

Co-authored-by: ilay1678 <7021399+ilay1678@users.noreply.github.com>
This commit is contained in:
naiba 2021-01-12 16:41:37 +08:00 committed by GitHub
commit a397d58c7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 135 additions and 78 deletions

View File

@ -87,6 +87,7 @@ type serverForm struct {
Name string `binding:"required"` Name string `binding:"required"`
DisplayIndex int DisplayIndex int
Secret string Secret string
Tag string
} }
func (ma *memberAPI) addOrEditServer(c *gin.Context) { func (ma *memberAPI) addOrEditServer(c *gin.Context) {
@ -102,6 +103,7 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
s.Secret = sf.Secret s.Secret = sf.Secret
s.DisplayIndex = sf.DisplayIndex s.DisplayIndex = sf.DisplayIndex
s.ID = sf.ID s.ID = sf.ID
s.Tag = sf.Tag
if sf.ID == 0 { if sf.ID == 0 {
s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID)) s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID))
s.Secret = s.Secret[:10] s.Secret = s.Secret[:10]

View File

@ -14,7 +14,7 @@ type Server struct {
Name string Name string
DisplayIndex int // 展示权重,越大越靠前 DisplayIndex int // 展示权重,越大越靠前
Secret string `json:"-"` Secret string `json:"-"`
Tag string
Host *Host `gorm:"-"` Host *Host `gorm:"-"`
State *State `gorm:"-"` State *State `gorm:"-"`
LastActive time.Time LastActive time.Time

View File

@ -8,6 +8,10 @@
<label>备注</label> <label>备注</label>
<input type="text" name="name" placeholder="爱因斯坦-光速1号"> <input type="text" name="name" placeholder="爱因斯坦-光速1号">
</div> </div>
<div class="field">
<label>标签</label>
<input type="text" name="Tag" placeholder="服务器标签">
</div>
<div class="field"> <div class="field">
<label>展示权重</label> <label>展示权重</label>
<input type="number" name="DisplayIndex" placeholder="越大越靠前"> <input type="number" name="DisplayIndex" placeholder="越大越靠前">

View File

@ -15,6 +15,7 @@
<tr> <tr>
<th>权重</th> <th>权重</th>
<th>备注</th> <th>备注</th>
<th>标签</th>
<th>IP</th> <th>IP</th>
<th>ID</th> <th>ID</th>
<th>密钥</th> <th>密钥</th>
@ -26,6 +27,7 @@
<tr> <tr>
<td>{{$server.DisplayIndex}}</td> <td>{{$server.DisplayIndex}}</td>
<td>{{$server.Name}}</td> <td>{{$server.Name}}</td>
<td>{{$server.Tag}}</td>
<td>{{$server.Host.IP}}</td> <td>{{$server.Host.IP}}</td>
<td>{{$server.ID}}</td> <td>{{$server.ID}}</td>
<td>{{$server.Secret}}</td> <td>{{$server.Secret}}</td>

View File

@ -6,8 +6,11 @@
{{template "common/menu" .}} {{template "common/menu" .}}
<div class="nb-container"> <div class="nb-container">
<div class="ui container"> <div class="ui container">
<div class="ui four stackable status cards"> <div id="app">
<div v-for='server in servers' :id="server.ID" class="card">
<div class="ui styled fluid accordion" v-for="group in groups">
<div class="active title"><i class="dropdown icon"></i>@#(group.Tag!==''?group.Tag:'默认')#@</div>
<div class="active content"><div class="ui four stackable status cards"> <div v-for='server in group.data' :id="server.ID" class="card">
<div class="content" v-if='server.Host'> <div class="content" v-if='server.Host'>
<div class="header"><i :class="server.Host.CountryCode + ' flag'"></i><i <div class="header"><i :class="server.Host.CountryCode + ' flag'"></i><i
v-if='server.Host.Platform == "darwin"' class="apple icon"></i><i v-if='server.Host.Platform == "darwin"' class="apple icon"></i><i
@ -35,7 +38,8 @@
<div class="three wide column">CPU</div> <div class="three wide column">CPU</div>
<div class="thirteen wide column"> <div class="thirteen wide column">
<div :class="formatPercent(server.live,server.State.CPU, 100).class"> <div :class="formatPercent(server.live,server.State.CPU, 100).class">
<div class="bar" :style="formatPercent(server.live,server.State.CPU, 100).style"> <div class="bar"
:style="formatPercent(server.live,server.State.CPU, 100).style">
<small>@#formatPercent(server.live,server.State.CPU,100).percent#@%</small> <small>@#formatPercent(server.live,server.State.CPU,100).percent#@%</small>
</div> </div>
</div> </div>
@ -88,6 +92,7 @@
<p>@#server.Name#@</p> <p>@#server.Name#@</p>
<p>节点已离线</p> <p>节点已离线</p>
</div> </div>
</div></div></div>
</div> </div>
</div> </div>
</div> </div>
@ -96,12 +101,16 @@
<script> <script>
const initData = {{.Servers}}; const initData = {{.Servers}};
var statusCards = new Vue({ var statusCards = new Vue({
el: 'div.status.cards', el: '#app',
delimiters: ['@#', '#@'], delimiters: ['@#', '#@'],
data: { data: {
servers: initData, data: initData,
groups:[],
cache: [], cache: [],
}, },
created() {
this.group()
},
mounted() { mounted() {
$('.yellow.info.icon').popup({ $('.yellow.info.icon').popup({
popup: '.ui.content.popup', popup: '.ui.content.popup',
@ -109,6 +118,10 @@
}); });
}, },
methods: { methods: {
group() {
this.groups = groupingData(this.data, "Tag")
console.log(this.groups)
},
formatPercent(live, used, total) { formatPercent(live, used, total) {
const percent = live ? (parseInt(used / total * 100) || 0) : -1 const percent = live ? (parseInt(used / total * 100) || 0) : -1
if (!this.cache[percent]) { if (!this.cache[percent]) {
@ -159,6 +172,38 @@
} }
} }
}) })
function groupingData(data, filed){
let map = {};
let dest = [];
data.forEach(item => {
if (!map[item[filed]]) {
dest.push({
[filed]: item[filed],
data: [item]
});
map[item[filed]] = item;
} else {
dest.forEach(dItem => {
if (dItem[filed] == item[filed]) {
dItem.data.push(item);
}
});
}
})
return dest;
}
const wsProtocol = window.location.protocol == "https:" ? "wss" : "ws" const wsProtocol = window.location.protocol == "https:" ? "wss" : "ws"
const ws = new WebSocket(wsProtocol + '://' + window.location.host + '/ws'); const ws = new WebSocket(wsProtocol + '://' + window.location.host + '/ws');
ws.onopen = function (evt) { ws.onopen = function (evt) {
@ -185,6 +230,7 @@
} }
} }
} }
statusCards.groups=groupingData(statusCards.servers,"Tag")
} }
ws.onclose = function () { ws.onclose = function () {
$.suiAlert({ $.suiAlert({
@ -195,5 +241,8 @@
position: 'top-center', position: 'top-center',
}); });
} }
$('.ui.accordion')
.accordion({"exclusive":false})
;
</script> </script>
{{end}} {{end}}