-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
107 lines (89 loc) · 2.77 KB
/
server.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
package frain
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
)
// Data is a model to match the `getAllServices` JSON tag from frain backend
type Data struct {
All []Service `json:"getAllServices"`
}
// Result is a model to match the `data` JSON tag from frain backend
type Result struct {
Data `json:"data"`
}
// SingleData is a model to match the `getService` JSON tag from frain backend for a single service
type SingleData struct {
Service Service `json:"getService"`
}
// SingleResult is a model to match the `data` JSON tag from frain backend for a single service
type SingleResult struct {
SingleData `json:"data"`
}
var (
errHTTPPost = errors.New("Error: a network error occurred while fetching data")
errJSONDecode = errors.New("Error: failed to decode fetched data")
)
// GetService sends a POST request to the host server and then returns all information
// relating to a developer tool to check
func GetService(name string, startTime, endTime time.Time) (*Service, error) {
q := fmt.Sprintf(`{"query": "{getService(name:%s)`+
`{id, name, statusPageUrl, provider, indicator, isActive, createdAt, updatedAt,`+
` components`+
`{id, name, status, description},`+
` incidents(startTime:\"%s\", endTime:\"%s\")`+
`{id, name,impact, status, isActive, createdAt, shortlink, updatedAt, incidentUpdates{id, body, status, createdAt, updatedAt}},`+
` highLevelComponents`+
`{id, name, status, description}}}"}`, name, parseDate(&startTime), parseDate(&endTime))
query := bytes.NewBuffer([]byte(q))
host := os.Getenv("FRAIN_HOST")
if host == "" {
host = "https://frain-server.herokuapp.com/graphql"
}
resp, err := http.Post(host, "application/json", query)
if err != nil {
return nil, errHTTPPost
}
defer resp.Body.Close()
var result SingleResult
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, errJSONDecode
}
return &result.Service, nil
}
func parseDate(t *time.Time) string {
return fmt.Sprintf("%04d-%02d-%02d", t.Year(), int(t.Month()), t.Day())
}
// GetServiceList returns a list of services currently supported by frain
func GetServiceList() ([]string, error) {
query := bytes.NewBuffer([]byte(`{ "query": "{getAllServices {name}}" }`))
host := os.Getenv("FRAIN_HOST")
if host == "" {
host = "https://frain-server.herokuapp.com/graphql"
}
resp, err := http.Post(host, "application/json", query)
if err != nil {
return nil, errHTTPPost
}
defer resp.Body.Close()
var result Result
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, errJSONDecode
}
var services []string
var sMap = map[string]bool{}
for _, s := range result.All {
sMap[strings.ToLower(s.Name)] = true
}
for s := range sMap {
services = append(services, s)
}
return services, nil
}