summaryrefslogtreecommitdiff
path: root/web-app/server.go
blob: efbff702fce53accbddde706eacee9669911f70f (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package main

import (
        "fmt"
	"flag"
	"github.com/gorilla/mux"
	"github.com/jmcvetta/neoism"
	"encoding/json"
	"log"
	"net/http"
	"strconv"
)


var (
	addr = flag.String("addr", ":8081", "http service address")
	log_root = flag.String("log_root", "/home/clarkl/lava_pull/logs", "log root path")
	neo4j_server = flag.String("dbserver", "http://neo4j:linaro@localhost:7474/db/data/", "Neo4J server endpoint")
)


func Results_AllDevstack(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")

        db, err := neoism.Connect(*neo4j_server)
        if err != nil {
                log.Println("error connecting to database: ", err)
        }

        res := [] struct {
                Name string `json:"n.name"`
        }{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (n:Devstack) RETURN n.name`,
                Result: &res,
        }

        err = db.Cypher(&cq)
        if err != nil {
                log.Println("query error: ", err)
        }

        log.Printf("%#v", res)

        enc := json.NewEncoder(w)
        enc.Encode(res)
}


func Results_AllOS(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")

	db, err := neoism.Connect(*neo4j_server)
	if err != nil {
		log.Println("error connecting to database: ", err)
	}

	res := [] struct {
                Name string `json:"n.name"`
                Version string `json:"n.version"`
                Distro string `json:"n.distro"`
        }{}

	cq := neoism.CypherQuery{
		Statement: `MATCH (n:OS) RETURN n.name, n.version, n.distro`,
		Result: &res,
	}

	err = db.Cypher(&cq)
	if err != nil {
		log.Println("query error: ", err)
	}

        log.Printf("%#v", res)

	enc := json.NewEncoder(w)
	enc.Encode(res)
}


func Results_Tempest_Summary(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")

        db, err := neoism.Connect(*neo4j_server)
        if err != nil {
                log.Println("error connecting to database: ", err)
        }

        res := [] struct {
                Number_Of_Runs int32 `json:"Number_Of_Runs"`
                OS_Name string `json:"OS_Name"`
                Devstack_Branch string `json:"Devstack_Branch"`
                Average_Pct_Failing float64 `json:"Average_Pct_Failing"`
		Average_Pct_Passing float64 `json:"Average_Pct_Passing"`
		Average_Pct_Skipped float64 `json:"Average_Pct_Skipped"`
        }{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (d:Devstack)<-[:USING]-(t:TempestRun)-[:ON]->(o:OS)
				RETURN count(t) as Number_Of_Runs, o.name as OS_Name, d.name as Devstack_Branch,
				ROUND(avg(toFloat(t.failing_tests)/t.tests_run) * 100) as Average_Pct_Failing,
				ROUND(avg(toFloat(t.passing_tests)/t.tests_run) * 100) as Average_Pct_Passing,
				ROUND(avg(toFloat(t.skipped_tests)/t.tests_run) * 100) as Average_Pct_Skipped`,
                Result: &res,
        }

        err = db.Cypher(&cq)
        if err != nil {
                log.Println("query error: ", err)
        }

        log.Printf("%#v", res)

        enc := json.NewEncoder(w)
        enc.Encode(res)
}

func Results_Tempest_LastN(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")

	count_param := mux.Vars(r)["count"]
        count, err := strconv.Atoi(count_param)
        if err != nil {
                log.Println("error converting parameter from string to int: ", err)
        }

	if count < 1 {
		count = 1
	}

	if count > 60 {
		count = 60
	}

        db, err := neoism.Connect(*neo4j_server)
        if err != nil {
                log.Println("error connecting to database: ", err)
        }

        res := [] struct {
                LAVA_Job int32 `json:"t.lava_job"`
                Date string `json:"t.date"`
                SHA1 string `json:"t.sha1"`
                All_tests int32 `json:"t.all_tests"`
                Passing_tests int32 `json:"t.passing_tests"`
                Failing_tests int32 `json:"t.failing_tests"`
                Tests_run int32 `json:"t.tests_run"`
                Skipped_tests int32 `json:"t.skipped_tests"`
		OS_Distro string `json:"o.distro"`
		OS_Version string `json:"o.version"`
		Devstack_Branch string `json:"d.name"`
                Epoch_time int64 `json:"t.epoch_time"`
        }{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (d:Devstack)<-[:USING]-(t:TempestRun)-[:ON]->(o:OS)
                                RETURN t.lava_job, t.date, t.sha1, t.all_tests,
                                        t.passing_tests, t.failing_tests,
                                        t.tests_run, t.skipped_tests,
                                        t.epoch_time, o.distro, o.version, d.name
                                ORDER BY t.epoch_time DESC
                                LIMIT {count}`,
                Parameters: neoism.Props{"count": count},
                Result: &res,
        }

        err = db.Cypher(&cq)
        if err != nil {
                log.Println("query error: ", err)
        }

        log.Printf("%#v", res)

        enc := json.NewEncoder(w)
        enc.Encode(res)
}


func Results_Tempest_Job_Summary(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")

	job_param := mux.Vars(r)["job"]
	job, err := strconv.Atoi(job_param)
	if err != nil {
		log.Println("error converting parameter from string to int: ", err)
	}

	db, err := neoism.Connect(*neo4j_server)
	if err != nil {
		log.Println("error connecting to database: ", err)
	}

	res := [] struct {
                LAVA_Job int32 `json:"t.lava_job"`
                Date string `json:"t.date"`
                SHA1 string `json:"t.sha1"`
                All_tests int32 `json:"t.all_tests"`
                Passing_tests int32 `json:"t.passing_tests"`
                Failing_tests int32 `json:"t.failing_tests"`
		Tests_run int32 `json:"t.tests_run"`
                Skipped_tests int32 `json:"t.skipped_tests"`
                OS_Distro string `json:"o.distro"`
                OS_Version string `json:"o.version"`
                Devstack_Branch string `json:"d.name"`
                Epoch_time int64 `json:"t.epoch_time"`
        }{}

	cq := neoism.CypherQuery{
		Statement: `MATCH (d:Devstack)<-[:USING]-(t:TempestRun)-[:ON]->(o:OS)
				WHERE t.lava_job = {job}
                        	RETURN t.lava_job, t.date, t.sha1, t.all_tests,
                                	t.passing_tests, t.failing_tests,
                                	t.tests_run, t.skipped_tests,
                                	t.epoch_time, o.distro, o.version, d.name`,
		Parameters: neoism.Props{"job": job},
		Result: &res,
	}

	err = db.Cypher(&cq)
	if err != nil {
		log.Println("query error: ", err)
	}

        log.Printf("%#v", res)

	enc := json.NewEncoder(w)
	enc.Encode(res)
}


func Results_Tempest_AllJobIds(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")

        db, err := neoism.Connect(*neo4j_server)
        if err != nil {
                log.Println("error connecting to database: ", err)
        }

        res := [] struct{
		Job_Ids []int32 `json:"lava_job_ids"`
	}{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (n:TempestRun) WITH n.lava_job AS lava_job ORDER BY n.epoch_time DESC
				RETURN COLLECT(lava_job) as lava_job_ids`,
                Result: &res,
        }

        err = db.Cypher(&cq)
        if err != nil {
                log.Println("query error: ", err)
        }

        log.Printf("%#v", res[0].Job_Ids)

        enc := json.NewEncoder(w)
        enc.Encode(res[0].Job_Ids)
}


func Results_Tempest_OS(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "URL: %s", r.URL.Path)
}


func Results_Tempest_OS_Summary(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "URL: %s", r.URL.Path)
}


func Results_Tempest_Branch(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "URL: %s", r.URL.Path)
}


func Results_Tempest_Branch_Summary(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "URL: %s", r.URL.Path)
}


func Results_Tempest_Job_Failures(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
       
        job_param := mux.Vars(r)["job"]
        job, err := strconv.Atoi(job_param)
        if err != nil {
                log.Println("error converting parameter from string to int: ", err)
        }
 
        db, err := neoism.Connect(*neo4j_server)
        if err != nil {
                log.Println("error connecting to database: ", err)
        }       
        
        res := [] struct{
                Test_Name string `json:"t.name"`
		Test_Status string `json:"t.status"`
		Test_Class string `json:"t.test_class"`
		Start_Time string `json:"t.start_time"`
		Stop_Time string `json:"t.stop_time"`
        }{}     
        
        cq := neoism.CypherQuery{
                Statement: `MATCH (n:TempestRun)-[:HAS_TEST {status:"failure"}]-(t:Test)
				WHERE n.lava_job = {job}
				RETURN t.name, t.status, t.start_time, t.stop_time, t.test_class`,
		Parameters: neoism.Props{"job" : job},
                Result: &res,   
        }       
        
        err = db.Cypher(&cq)
        if err != nil {
                log.Println("query error: ", err)
        }       
        
        log.Printf("%#v", res)
        
        enc := json.NewEncoder(w)
        enc.Encode(res)
}


func Results_Tempest_Job_Skipped(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")

        job_param := mux.Vars(r)["job"]
        job, err := strconv.Atoi(job_param)
        if err != nil {
                log.Println("error converting parameter from string to int: ", err)
        }

        db, err := neoism.Connect(*neo4j_server)
        if err != nil {
                log.Println("error connecting to database: ", err)
        }

        res := [] struct{
                Test_Name string `json:"t.name"`
                Test_Status string `json:"t.status"`
                Test_Class string `json:"t.test_class"`
                Start_Time string `json:"t.start_time"`
                Stop_Time string `json:"t.stop_time"`
        }{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (n:TempestRun)-[:HAS_TEST {status:"skip"}]-(t:Test)
                                WHERE n.lava_job = {job}
                                RETURN t.name, t.status, t.start_time, t.stop_time, t.test_class`,
                Parameters: neoism.Props{"job" : job},
                Result: &res,
        }

        err = db.Cypher(&cq)
        if err != nil {
                log.Println("query error: ", err)
        }

        log.Printf("%#v", res)

        enc := json.NewEncoder(w)
        enc.Encode(res)
}


func main() {
	//
	// handle routes for access to data
	//

	r := mux.NewRouter()
        s := r.Methods("GET").PathPrefix("/results").Subrouter()	

	// global data
	s.HandleFunc("/devstack-branches", Results_AllDevstack)
	s.HandleFunc("/operating-systems", Results_AllOS)

	// tempest
	s.HandleFunc("/tempest", Results_Tempest_LastN).Queries("count", "{count:[0-9]+}")     // DONE
        s.HandleFunc("/tempest", Results_Tempest_Summary)        // DONE
        s.HandleFunc("/tempest/jobs", Results_Tempest_AllJobIds)     // DONE
	s.HandleFunc("/tempest/operating-system/{os-distro}/{os-version}", Results_Tempest_OS).Queries("count", "{count:[0-9]+}")
	s.HandleFunc("/tempest/operating-system/{os-distro}/{os-version}", Results_Tempest_OS_Summary)
        s.HandleFunc("/tempest/devstack-branch/{branch}", Results_Tempest_Branch).Queries("count", "{count:[0-9]+}")
	s.HandleFunc("/tempest/devstack-branch/{branch}", Results_Tempest_Branch_Summary)
	s.HandleFunc("/tempest/job/{job:[0-9]+}", Results_Tempest_Job_Summary)      // DONE
	s.HandleFunc("/tempest/job/{job:[0-9]+}/failures", Results_Tempest_Job_Failures)    // DONE
	s.HandleFunc("/tempest/job/{job:[0-9]+}/skipped", Results_Tempest_Job_Skipped)    // DONE	
	//s.HandleFunc("/tempest/test/class/{class}", Results_Tempest_Class_LastN).Queries("count", "{count:[0-9]+}")
	//s.HandleFunc("/tempest/test/class/{class}", Results_Tempest_Class_Summary)
	//s.HandleFunc("/tempest/test/{test}", Results_Tempest_Test_LastN).Queries("count", "{count:[0-9]+}")
	//s.HandleFunc("/tempest/test/{test}", Results_Tempest_Test_Summary)

	http.Handle("/results/", r)

	//
	// handle logs
	//

	http.Handle("/logs", http.FileServer(http.Dir("log_root")))

	//
	// handle static content
	//

	http.Handle("/", http.FileServer(http.Dir("./static/")))

	// GO!
	err := http.ListenAndServe(*addr, nil)
	if err != nil {
		log.Fatal("http.ListenAndServe: ", err)
	}
}