forked from pavel-paulau/weekly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
174 lines (143 loc) · 3.71 KB
/
data.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
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"github.com/imdario/mergo"
"gopkg.in/couchbase/gocb.v1"
log "gopkg.in/inconshreveable/log15.v2"
)
type dataStore struct {
bucket *gocb.Bucket
}
func newDataStore() *dataStore {
hostname := os.Getenv("CB_HOST")
if hostname == "" {
log.Error("missing Couchbase Server hostname")
os.Exit(1)
}
password := os.Getenv("CB_PASS")
if password == "" {
log.Error("missing password")
os.Exit(1)
}
username := os.Getenv("CB_USER")
if username == "" {
log.Error("missing username")
os.Exit(1)
}
connSpecStr := fmt.Sprintf("couchbases://%s", hostname)
cluster, err := gocb.Connect(connSpecStr)
if err != nil {
log.Error("failed to connect to Couchbase Server", "err", err)
os.Exit(1)
}
cluster.Authenticate(gocb.PasswordAuthenticator{
Username: username,
Password: password,
})
bucket, err := cluster.OpenBucket("weekly", "")
if err != nil {
log.Error("failed to connect to bucket", "err", err)
os.Exit(1)
}
return &dataStore{bucket}
}
type Build struct {
Build string `json:"build"`
}
func (d *dataStore) getBuilds() (*[]string, error) {
var builds []string
query := gocb.NewN1qlQuery(
"SELECT DISTINCT `build` " +
"FROM weekly " +
"WHERE `build` IS NOT MISSING " +
"ORDER BY `build` DESC;")
rows, err := ds.bucket.ExecuteN1qlQuery(query, []interface{}{})
if err != nil {
return nil, err
}
var row Build
for rows.Next(&row) {
builds = append(builds, row.Build)
}
return &builds, nil
}
// TestStatus characterizes the status of test execution in Jenkins
type TestStatus struct {
Failed int `json:"failed"`
Passed int `json:"passed"`
Missing int `json:"missing"`
Total int `json:"total"`
}
// JiraStatus captures a snapshot of JIRA tickets
type JiraStatus struct {
Created int `json:"created"`
Open int `json:"open"`
Resolved int `json:"resolved"`
}
// MetricStatus reports anomalies in collected metrics
type MetricStatus struct {
Changed int `json:"changed"`
Collected int `json:"collected"`
}
// KpiStatus records
type KpiStatus struct {
Passed int `json:"passed"`
Violated int `json:"violated"`
Defined int `json:"defined"`
}
// Status aggregates all build indicators for a given build
type Status struct {
Build string `json:"build"`
Component string `json:"component"`
JiraStatus JiraStatus `json:"jira_status"`
KpiStatus KpiStatus `json:"kpi_status"`
TestStatus TestStatus `json:"test_status"`
MetricStatus MetricStatus `json:"metric_status"`
}
func hash(strings ...string) string {
h := md5.New()
for _, s := range strings {
h.Write([]byte(s))
}
return hex.EncodeToString(h.Sum(nil))
}
func (d *dataStore) getStatus(docId string) (Status, error) {
var status Status
_, err := d.bucket.Get(docId, &status)
return status, err
}
func (d *dataStore) updateStatus(status Status) error {
docId := hash(status.Component, status.Build)
currStatus, err := d.getStatus(docId)
if err != gocb.ErrKeyNotFound {
mergo.Merge(&status, currStatus)
}
_, err = d.bucket.Upsert(docId, status, 0)
if err != nil {
log.Error("failed to update status", "err", err)
}
return err
}
func (d *dataStore) getBuildStatus(build string) (*[]Status, error) {
var statuses []Status
query := gocb.NewN1qlQuery(
"SELECT RAW weekly " +
"FROM weekly " +
"WHERE `build` = $1 " +
"ORDER BY `component`;")
params := []interface{}{build}
rows, err := ds.bucket.ExecuteN1qlQuery(query, params)
if err != nil {
return nil, err
}
var status Status
for rows.Next(&status) {
status.TestStatus.Missing = status.TestStatus.Total -
status.TestStatus.Passed - status.TestStatus.Failed
statuses = append(statuses, status)
}
return &statuses, nil
}