/
menu.go
179 lines (157 loc) · 5.68 KB
/
menu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package agentutils
import (
"fmt"
"github.com/TeaWeb/build/internal/teaconfigs/agents"
"github.com/TeaWeb/build/internal/teadb"
"github.com/TeaWeb/build/internal/teaweb/utils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"net/http"
)
func AddTabbar(actionWrapper actions.ActionWrapper) {
if actionWrapper.Object().Request.Method != http.MethodGet {
return
}
action := actionWrapper.Object()
action.Data["teaMenu"] = "agents"
// 子菜单
menuGroup := utils.NewMenuGroup()
isIndex := !action.HasPrefix("/agents/addAgent", "/agents/cluster/add", "/agents/groups")
if isIndex {
agentId := action.ParamString("agentId")
if len(agentId) == 0 {
agentId = "local"
}
actionCode := "board"
if action.HasPrefix("/agents/apps") {
actionCode = "apps"
} else if action.HasPrefix("/agents/settings") {
actionCode = "settings"
} else if action.HasPrefix("/agents/delete") {
actionCode = "delete"
} else if action.HasPrefix("/agents/notices") {
actionCode = "notices"
}
topSubName := ""
if lists.ContainsAny([]string{"/agents/board", "/agents/menu"}, action.Request.URL.Path) {
topSubName = ""
}
defaultGroup := agents.SharedGroupList().FindGroup("default")
state := FindAgentState("local")
menu := menuGroup.FindMenu("default", defaultGroup.Name+topSubName)
if state.IsActive {
subName := "已连接"
if state != nil && len(state.OsName) > 0 {
subName = state.OsName
}
item := menu.Add("本地", subName, "/agents/"+actionCode+"?agentId=local", agentId == "local" && !action.HasPrefix("/agents/addAgent", "/agents/cluster/add", "/agents/groups"))
item.SubColor = "green"
} else {
menu.Add("本地", "", "/agents/"+actionCode+"?agentId=local", agentId == "local" && !action.HasPrefix("/agents/addAgent", "/agents/cluster/add", "/agents/groups"))
}
// agent列表
allAgents := agents.SharedAgents()
counterMapping := map[string]int{} // groupId => count
maxCount := 50
for _, agent := range allAgents {
state := FindAgentState(agent.Id)
var menu *utils.Menu = nil
if len(agent.GroupIds) > 0 {
group := agents.SharedGroupList().FindGroup(agent.GroupIds[0])
if group == nil {
menu = menuGroup.FindMenu("default", defaultGroup.Name+topSubName)
} else {
menu = menuGroup.FindMenu(group.Id, group.Name)
menu.Index = group.Index
// 计算数量
_, found := counterMapping[group.Id]
if found {
counterMapping[group.Id] ++
} else {
counterMapping[group.Id] = 1
}
if counterMapping[group.Id] > maxCount {
if counterMapping[group.Id] == maxCount+1 {
menu.AddSpecial("[更多主机]", "", "/agents/groups/detail?groupId="+group.Id, false)
}
continue
}
}
} else {
menu = menuGroup.FindMenu("default", defaultGroup.Name+topSubName)
// 计算数量
groupId := ""
_, found := counterMapping[groupId]
if found {
counterMapping[groupId] ++
} else {
counterMapping[groupId] = 1
}
if counterMapping[groupId] > maxCount {
if counterMapping[groupId] == maxCount+1 {
menu.AddSpecial("[更多主机]", "", "/agents/groups/detail?groupId="+groupId, false)
}
continue
}
}
if state.IsActive {
subName := "已连接"
if state != nil && len(state.OsName) > 0 {
subName = state.OsName
}
item := menu.Add(agent.Name, subName, "/agents/"+actionCode+"?agentId="+agent.Id, agentId == agent.Id)
item.Id = agent.Id
item.IsSortable = true
item.SubColor = "green"
} else if !agent.On {
item := menu.Add(agent.Name, "未启用", "/agents/"+actionCode+"?agentId="+agent.Id, agentId == agent.Id)
item.Id = agent.Id
item.IsSortable = true
} else {
item := menu.Add(agent.Name, "已断开连接", "/agents/"+actionCode+"?agentId="+agent.Id, agentId == agent.Id)
item.Id = agent.Id
item.IsSortable = true
item.SubColor = "red"
}
}
// Tabbar
if !action.HasPrefix("/agents/addAgent", "/agents/cluster/add", "/agents/groups") {
agent := agents.NewAgentConfigFromId(agentId)
if agent != nil {
tabbar := utils.NewTabbar()
// 看板和Apps
tabbar.Add("看板", "", "/agents/board?agentId="+agentId, "dashboard", action.HasPrefix("/agents/board"))
tabbar.Add("Apps", fmt.Sprintf("%d", len(agent.Apps)), "/agents/apps?agentId="+agentId, "gem outline", action.HasPrefix("/agents/apps"))
// 通知
countUnreadNotices, err := teadb.NoticeDAO().CountUnreadNoticesForAgent(agentId)
if err != nil && err != teadb.ErrorDBUnavailable {
logs.Error(err)
}
if countUnreadNotices > 0 {
tabbar.Add("通知", fmt.Sprintf("%d", countUnreadNotices), "/agents/notices?agentId="+agentId, "bell blink orange", action.HasPrefix("/agents/notices"))
} else {
tabbar.Add("通知", fmt.Sprintf("%d", countUnreadNotices), "/agents/notices?agentId="+agentId, "bell", action.HasPrefix("/agents/notices"))
}
// 设置和删除
tabbar.Add("设置", "", "/agents/settings?agentId="+agentId, "setting", action.HasPrefix("/agents/settings"))
if agentId != "local" {
tabbar.Add("删除", "", "/agents/delete?agentId="+agentId, "trash", action.HasPrefix("/agents/delete"))
}
utils.SetTabbar(actionWrapper, tabbar)
}
}
}
// 操作按钮
{
menu := menuGroup.FindMenu("operations", "[操作]")
menu.AlwaysActive = true
menuGroup.AlwaysMenu = menu
menu.Index = 10000
menu.Add("主机", "", "/agents", isIndex)
menu.Add("+添加新主机", "", "/agents/addAgent", action.HasPrefix("/agents/addAgent", "/agents/cluster/add"))
menu.Add("分组管理", "", "/agents/groups", action.HasPrefix("/agents/groups"))
}
menuGroup.Sort()
utils.SetSubMenu(action, menuGroup)
}