-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobs.go
219 lines (196 loc) · 6.07 KB
/
jobs.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
211
212
213
214
215
216
217
218
219
package proxy
// Copyright 2022 Severalnines AB
//
// This file is part of cmon-proxy.
//
// cmon-proxy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2 of the License.
//
// cmon-proxy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with cmon-proxy. If not, see <https://www.gnu.org/licenses/>.
import (
"fmt"
"net/http"
"sort"
"strconv"
"github.com/gin-gonic/gin"
cmonapi "github.com/severalnines/cmon-proxy/cmon/api"
"github.com/severalnines/cmon-proxy/proxy/api"
)
func (p *Proxy) RPCJobsStatus(ctx *gin.Context) {
var req api.SimpleFilteredRequest
if ctx.Request.Method == http.MethodPost {
if err := ctx.BindJSON(&req); err != nil {
cmonapi.CtxWriteError(ctx,
cmonapi.NewError(cmonapi.RequestStatusInvalidRequest,
fmt.Sprint("Invalid request:", err.Error())))
return
}
}
resp := &api.JobsStatus{
JobCounts: make(map[string]int),
JobCommands: make(map[string]int),
JobCountsByController: make(map[string]*api.JobsStatus),
ByClusterType: make(map[string]*api.JobsStatus),
}
p.r.GetLastJobs(false)
for _, url := range p.r.Urls() {
data := p.r.Cmon(url)
if data == nil || data.Clusters == nil {
continue
}
countsByCtrl := &api.JobsStatus{
JobCounts: make(map[string]int),
JobCommands: make(map[string]int),
}
// iterate by clusterIds... one by one..
for _, job := range data.Jobs {
// tags filtration is possible here too
fn := func() []string { return data.ClusterTags(job.ClusterID) }
if !api.PassTagsFilterLazy(req.Filters, fn) {
continue
}
clusterType := data.ClusterType(job.ClusterID)
if stat, found := resp.ByClusterType[clusterType]; !found || stat == nil {
resp.ByClusterType[clusterType] =
&api.JobsStatus{
JobCounts: make(map[string]int),
JobCommands: make(map[string]int),
JobCountsByController: make(map[string]*api.JobsStatus),
}
}
if jbc, found := resp.ByClusterType[clusterType].JobCountsByController[url]; !found || jbc == nil {
resp.ByClusterType[clusterType].JobCountsByController[url] =
&api.JobsStatus{
JobCounts: make(map[string]int),
JobCommands: make(map[string]int),
}
}
resp.JobCounts[job.Status]++
resp.JobCommands[job.Command()]++
resp.ByClusterType[clusterType].JobCounts[job.Status]++
resp.ByClusterType[clusterType].JobCommands[job.Command()]++
countsByCtrl.JobCounts[job.Status]++
countsByCtrl.JobCommands[job.Command()]++
resp.ByClusterType[clusterType].JobCountsByController[url].JobCounts[job.Status]++
resp.ByClusterType[clusterType].JobCountsByController[url].JobCommands[job.Command()]++
}
resp.JobCountsByController[url] = countsByCtrl
}
ctx.JSON(http.StatusOK, resp)
}
// RPCJobsList gives back a list of clusters
func (p *Proxy) RPCJobsList(ctx *gin.Context) {
var req api.JobListRequest
var resp api.JobListReply
if ctx.Request.Method == http.MethodPost {
if err := ctx.BindJSON(&req); err != nil {
cmonapi.CtxWriteError(ctx,
cmonapi.NewError(cmonapi.RequestStatusInvalidRequest, fmt.Sprint("Invalid request:", err.Error())))
return
}
}
resp.Jobs = make([]*api.JobExt, 0, 32)
resp.LastUpdated = make(map[string]*cmonapi.NullTime)
p.r.GetLastJobs(false)
for _, url := range p.r.Urls() {
data := p.r.Cmon(url)
if data == nil || data.Jobs == nil {
continue
}
if !api.PassFilter(req.Filters, "controller_id", data.ControllerID()) ||
!api.PassFilter(req.Filters, "controller_url", url) {
continue
}
resp.LastUpdated[url] = &cmonapi.NullTime{
T: data.LastJobsRefresh,
}
for _, job := range data.Jobs {
if !api.PassFilter(req.Filters, "cluster_id", strconv.FormatUint(job.ClusterID, 10)) {
continue
}
if !api.PassFilter(req.Filters, "job_id", strconv.FormatUint(job.JobID, 10)) {
continue
}
if !api.PassFilterLazy(req.Filters, "cluster_type",
func() string { return data.ClusterType(job.ClusterID) }) {
continue
}
fn := func() []string { return data.ClusterTags(job.ClusterID) }
if !api.PassTagsFilterLazy(req.Filters, fn) {
continue
}
if !api.PassFilter(req.Filters, "job_command", job.Command()) {
continue
}
if !api.PassFilter(req.Filters, "status", job.Status) {
continue
}
resp.Jobs = append(resp.Jobs,
&api.JobExt{
WithControllerID: &api.WithControllerID{
ControllerID: data.ControllerID(),
ControllerURL: url,
},
Job: job,
},
)
}
}
// handle sorting && pagination
resp.Page = req.Page
resp.PerPage = req.PerPage
resp.Total = uint64(len(resp.Jobs))
// sort first
order, desc := req.GetOrder()
switch order {
case "cluster_id":
sort.Slice(resp.Jobs[:], func(i, j int) bool {
if desc {
i, j = j, i
}
return resp.Jobs[i].ClusterID < resp.Jobs[j].ClusterID
})
case "job_command":
sort.Slice(resp.Jobs[:], func(i, j int) bool {
if desc {
i, j = j, i
}
return resp.Jobs[i].Command() < resp.Jobs[j].Command()
})
case "job_id":
sort.Slice(resp.Jobs[:], func(i, j int) bool {
if desc {
i, j = j, i
}
return resp.Jobs[i].JobID < resp.Jobs[j].JobID
})
case "status":
sort.Slice(resp.Jobs[:], func(i, j int) bool {
if desc {
i, j = j, i
}
return resp.Jobs[i].Status < resp.Jobs[j].Status
})
case "title":
sort.Slice(resp.Jobs[:], func(i, j int) bool {
if desc {
i, j = j, i
}
return resp.Jobs[i].Title < resp.Jobs[j].Title
})
case "created":
sort.Slice(resp.Jobs[:], func(i, j int) bool {
if desc {
i, j = j, i
}
return resp.Jobs[i].Created.T.Before(resp.Jobs[j].Created.T)
})
}
if req.ListRequest.PerPage > 0 {
// then handle the pagination
from, to := api.Paginate(req.ListRequest, int(resp.Total))
resp.Jobs = resp.Jobs[from:to]
}
ctx.JSON(http.StatusOK, &resp)
}