/
maintain.go
163 lines (153 loc) · 4.21 KB
/
maintain.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
package controllers
import (
"encoding/json"
"math"
"strconv"
"strings"
"time"
"github.com/astaxie/beego"
"github.com/Qihoo360/doraemon/cmd/alert-gateway/common"
"github.com/Qihoo360/doraemon/cmd/alert-gateway/logs"
"github.com/Qihoo360/doraemon/cmd/alert-gateway/models"
)
type MaintainController struct {
beego.Controller
}
func (c *MaintainController) URLMapping() {
c.Mapping("GetAllProms", c.GetAllMaintains)
c.Mapping("AddProm", c.AddMaintain)
c.Mapping("UpdateProm", c.UpdateMaintain)
c.Mapping("DeleteProm", c.DeleteMaintain)
}
// @router / [get]
func (c *MaintainController) GetAllMaintains() {
var Maintain *models.Maintains
maintains := Maintain.GetAllMaintains()
c.Data["json"] = &common.Res{
Code: 0,
Msg: "",
Data: maintains,
}
c.ServeJSON()
}
// @router /:id/hosts [get]
func (c *MaintainController) GetHosts() {
var Host *models.Hosts
mid := c.Ctx.Input.Param(":id")
hosts := Host.GetHosts(mid)
c.Data["json"] = &common.Res{
Code: 0,
Msg: "",
Data: hosts,
}
c.ServeJSON()
}
// @router / [post]
func (c *MaintainController) AddMaintain() {
var data struct {
Flag bool `json:"flag"`
TimeStart string `json:"time_start"`
TimeEnd string `json:"time_end"`
Month string `json:"month"`
DayStart int8 `json:"day_start"`
DayEnd int8 `json:"day_end"`
Valid string `json:"valid"`
Hosts string `json:"hosts"`
}
var ans common.Res
err := json.Unmarshal(c.Ctx.Input.RequestBody, &data)
if err != nil {
logs.Error("Unmarshal prom error:%v", err)
ans.Code = 1
ans.Msg = "Unmarshal error"
} else {
var maintain models.Maintains
maintain.TimeStart = data.TimeStart
maintain.TimeEnd = data.TimeEnd
maintain.DayStart = data.DayStart
maintain.DayEnd = data.DayEnd
if data.TimeStart > data.TimeEnd {
maintain.Flag = true
} else {
maintain.Flag = false
}
validTime, _ := time.ParseInLocation("2006-01-02 15:04:05", data.Valid, time.Local)
maintain.Valid = &validTime
monthList := strings.Split(data.Month, ",")
for _, m := range monthList {
e, _ := strconv.ParseFloat(m, 64)
maintain.Month = maintain.Month | int(math.Pow(2, e))
}
err = maintain.AddMaintains(data.Hosts)
if err != nil {
ans.Code = 1
ans.Msg = err.Error()
}
logs.Logger.Info("%s %s %s %v", c.GetSession("username"), c.Ctx.Request.RequestURI, c.Ctx.Request.Method, data)
}
c.Data["json"] = &ans
c.ServeJSON()
}
// @router /:id [put]
func (c *MaintainController) UpdateMaintain() {
var data struct {
Flag bool `json:"flag"`
TimeStart string `json:"time_start"`
TimeEnd string `json:"time_end"`
Month string `json:"month"`
DayStart int8 `json:"day_start"`
DayEnd int8 `json:"day_end"`
Valid string `json:"valid"`
Hosts string `json:"hosts"`
}
var ans common.Res
mid := c.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(mid, 10, 64)
err := json.Unmarshal(c.Ctx.Input.RequestBody, &data)
if err == nil {
var maintain models.Maintains
maintain.Id = id
maintain.TimeStart = data.TimeStart
maintain.TimeEnd = data.TimeEnd
maintain.DayStart = data.DayStart
maintain.DayEnd = data.DayEnd
if data.TimeStart > data.TimeEnd {
maintain.Flag = true
} else {
maintain.Flag = false
}
validTime, _ := time.ParseInLocation("2006-01-02 15:04:05", data.Valid, time.Local)
maintain.Valid = &validTime
monthList := strings.Split(data.Month, ",")
for _, m := range monthList {
e, _ := strconv.ParseFloat(m, 64)
maintain.Month = maintain.Month | int(math.Pow(2, e))
}
//fmt.Println([]byte(data.Hosts))
err = maintain.UpdateMaintains(data.Hosts)
if err != nil {
ans.Code = 1
ans.Msg = err.Error()
}
logs.Logger.Info("%s %s %s %v", c.GetSession("username"), c.Ctx.Request.RequestURI, c.Ctx.Request.Method, data)
} else {
ans.Code = 1
ans.Msg = "Unmarshal error"
}
c.Data["json"] = &ans
c.ServeJSON()
}
// @router /:id [delete]
func (c *MaintainController) DeleteMaintain() {
id := c.Ctx.Input.Param(":id")
var maintain *models.Maintains
var ans common.Res
err := maintain.DeleteMaintains(id)
if err != nil {
ans.Code = 1
ans.Msg = err.Error()
}
logs.Logger.Info("%s %s %s %s", c.GetSession("username"), c.Ctx.Request.RequestURI, c.Ctx.Request.Method, id)
c.Data["json"] = &ans
c.ServeJSON()
}