-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathapi_retries.go
71 lines (61 loc) · 1.6 KB
/
api_retries.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
package workers
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
func (s *apiServer) Retries(w http.ResponseWriter, req *http.Request) {
page, pageSizeVal, query, err := parseURLQuery(req)
if err != nil {
s.logger.Println("couldn't retrieve retries filtering query:", err)
}
allRetries := []Retries{}
for _, m := range s.managers {
r, err := m.GetRetries(page, pageSizeVal, query)
if err != nil {
s.logger.Println("couldn't retrieve retries for manager:", err)
} else {
allRetries = append(allRetries, r)
}
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(allRetries)
}
// Retries stores retry information
type Retries struct {
TotalRetryCount int64 `json:"total_retry_count"`
RetryJobs []*Msg `json:"retry_jobs"`
}
func parseURLQuery(req *http.Request) (uint64, int64, string, error) {
query := req.URL.Query().Get("q")
if len(query) > 0 {
query = fmt.Sprintf("*" + query + "*")
} else {
return 0, 10, query, nil
}
var pageVal uint64
page := req.URL.Query().Get("page")
if len(page) > 0 {
pageVal, err := strconv.ParseUint(page, 10, 64)
if err != nil {
return pageVal, 10, query, nil
}
} else {
return 0, 10, query, nil
}
var pageSizeVal int64
pageSize := req.URL.Query().Get("page_size")
if len(pageSize) > 0 {
pageSizeVal, err := strconv.ParseInt(pageSize, 10, 64)
if err != nil {
return pageVal, pageSizeVal, query, nil
}
} else {
return pageVal, 10, query, nil
}
return pageVal, pageSizeVal, query, nil
}