/
history_controller.go
210 lines (195 loc) · 5.29 KB
/
history_controller.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package manager
import (
"net/http"
"html/template"
"github.com/brokercap/Bifrost/server/history"
"github.com/brokercap/Bifrost/server"
"encoding/json"
"log"
)
func init(){
addRoute("/history/list",history_list_controller)
addRoute("/history/add",history_add_controller)
addRoute("/history/stop",history_kill_controller)
addRoute("/history/del",history_del_controller)
addRoute("/history/start",history_start_controller)
addRoute("/history/checkwhere",history_check_where_controller)
}
func history_list_controller(w http.ResponseWriter,req *http.Request){
req.ParseForm()
dbname := req.Form.Get("dbname")
tablename := req.Form.Get("table_name")
schema := req.Form.Get("schema_name")
var status history.HisotryStatus
switch req.Form.Get("status") {
case "close":
status = history.HISTORY_STATUS_CLOSE
break
case "running":
status = history.HISTORY_STATUS_RUNNING
break
case "selectOver":
status = history.HISTORY_STATUS_SELECT_OVER
break
case "over":
status = history.HISTORY_STATUS_OVER
break
case "halfway":
status = history.HISTORY_STATUS_HALFWAY
break
case "killed":
status = history.HISTORY_STATUS_KILLED
break
default:
status = history.HISTORY_STATUS_ALL
break
}
HistoryList := history.GetHistoryList(dbname,schema,tablename,status)
if req.Form.Get("format") == "json"{
b, _:= json.Marshal(HistoryList)
w.Write(b)
return
}
type HistoryListInfo struct {
TemplateHeader
DbName string
SchemaName string
TableName string
HistoryList []history.History
DbList map[string]server.DbListStruct
StatusList []history.HisotryStatus
Status history.HisotryStatus
}
var data HistoryListInfo
data = HistoryListInfo{
DbName:dbname,
TableName:tablename,
SchemaName:schema,
HistoryList:HistoryList,
DbList:server.GetListDb(),
StatusList:[]history.HisotryStatus{
history.HISTORY_STATUS_ALL,
history.HISTORY_STATUS_CLOSE,
history.HISTORY_STATUS_RUNNING,
history.HISTORY_STATUS_HALFWAY,
history.HISTORY_STATUS_SELECT_OVER,
history.HISTORY_STATUS_OVER,
history.HISTORY_STATUS_KILLED},
Status:status,
}
data.Title = "History List - Bifrost"
t, err := template.ParseFiles(TemplatePath("manager/template/history.list.html"),TemplatePath("manager/template/header.html"),TemplatePath("manager/template/footer.html"))
if err != nil{
log.Println(err)
}
t.Execute(w, data)
}
func history_add_controller(w http.ResponseWriter,req *http.Request){
req.ParseForm()
dbname := req.Form.Get("dbname")
tablename := req.Form.Get("table_name")
schema := req.Form.Get("schema_name")
property := req.Form.Get("property")
ToServerIDList := req.Form.Get("ToserverIds")
if tansferTableName(tablename) == "*"{
w.Write(returnDataResult(false,"不能给 AllTables 添加全量任务!",0))
return
}
var err error
var Property history.HistoryProperty
err = json.Unmarshal([]byte(property),&Property)
if err != nil{
w.Write(returnDataResult(false,err.Error(),0))
return
}
var ToserverIds []int
err = json.Unmarshal([]byte(ToServerIDList),&ToserverIds)
if err != nil {
w.Write(returnDataResult(false, err.Error(), 0))
return
}
if len(ToserverIds) == 0 {
w.Write(returnDataResult(false, "ToserverIds error", 0))
return
}
err = history.CheckWhere(dbname,schema,tablename,Property.Where)
if err != nil{
w.Write(returnDataResult(false,err.Error(),0))
return
}
var ID int
ID,err = history.AddHistory(dbname,schema,tablename,Property,ToserverIds)
if err != nil{
w.Write(returnDataResult(false,err.Error(),0))
}else{
w.Write(returnDataResult(true,"success",ID))
}
}
func history_del_controller(w http.ResponseWriter,req *http.Request){
req.ParseForm()
req.ParseForm()
dbname := req.Form.Get("dbname")
id := GetFormInt(req,"id")
if id == 0{
w.Write(returnResult(false,"id error not be int"))
return
}
b := history.DelHistory(dbname,id)
if b == false {
w.Write(returnResult(false,"del error"))
}else{
w.Write(returnResult(true,"success"))
}
}
func history_start_controller(w http.ResponseWriter,req *http.Request){
req.ParseForm()
dbname := req.Form.Get("dbname")
id := GetFormInt(req,"id")
if id == 0{
w.Write(returnResult(false,"id error not be int"))
}
err := history.Start(dbname,id)
if err != nil{
w.Write(returnResult(false,err.Error()))
}else{
w.Write(returnResult(true,"success"))
}
}
func history_kill_controller(w http.ResponseWriter,req *http.Request){
req.ParseForm()
dbname := req.Form.Get("dbname")
id := GetFormInt(req,"id")
if id == 0{
w.Write(returnResult(false,"id error not be int"))
}
err := history.KillHistory(dbname,id)
if err != nil{
w.Write(returnResult(false,err.Error()))
}else{
w.Write(returnResult(true,"success"))
}
}
func history_check_where_controller(w http.ResponseWriter,req *http.Request){
req.ParseForm()
dbname := req.Form.Get("dbname")
tablename := req.Form.Get("table_name")
schema := req.Form.Get("schema_name")
property := req.Form.Get("property")
var err error
var Property history.HistoryProperty
err = json.Unmarshal([]byte(property),&Property)
if err != nil{
w.Write(returnResult(false,err.Error()))
return
}
if Property.Where == ""{
w.Write(returnResult(true,"success"))
return
}
err = history.CheckWhere(dbname,schema,tablename,Property.Where)
if err != nil{
w.Write(returnResult(false,err.Error()))
return
}
w.Write(returnResult(true,"success"))
}