summaryrefslogtreecommitdiff
path: root/web-app/server.go
blob: ee10eac7f85e6f321b642c558cf0b0cd53172544 (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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package main

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


var (
	listen_addr = flag.String("listen", ":8080", "http service address")
        logs_server = flag.String("logs_server", "http://localhost:80/logs/", "logs server")
	neo4j_server = flag.String("db_server", "http://neo4j:linaro@localhost:7474/db/data/", "Neo4J server endpoint")
)


type SkippedMode int
const (
	Skipped SkippedMode = iota
	Ignored
)


func Results_AllCategorization(w http.ResponseWriter, r *http.Request) {

	unionres := struct {
		Branch interface{}
		OS interface{}
                Device interface{}
	}{}

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

	// branches

        resBranches := [] struct {
                Name string `json:"n.name"`
                Occurances int32 `json:"occurances"`
        }{}

        cq1 := neoism.CypherQuery{
                Statement: `MATCH (n:Branch)-[:USING]-(t:Tempest:Run)
				WHERE HAS(t.all_tests)
				RETURN n.name, count(t) as occurances ORDER BY occurances DESC`,
                Result: &resBranches,
        }

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

	// OSes

        resOSes := [] struct {
                Name string `json:"n.name"`
                Version string `json:"n.version"`
                Distro string `json:"n.distro"`
                Occurances int32 `json:"occurances"`
        }{}

        cq2 := neoism.CypherQuery{
                Statement: `MATCH (n:OS)-[:ON]-(t:Tempest:Run)
				WHERE HAS(t.all_tests)
				RETURN n.name, n.version, n.distro, count(t) as occurances ORDER BY occurances DESC`,
                Result: &resOSes,
        }

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

        // devices

        resDevices := [] struct {
                Name string `json:"n.name"`
                Occurances int32 `json:"occurances"`
        }{}

        cq3 := neoism.CypherQuery{
                Statement: `MATCH (n:Device)-[:ON]-(t:Tempest:Run)
				WHERE HAS(t.all_tests)
				RETURN n.name, count(t) as occurances ORDER BY occurances DESC`,
                Result: &resDevices,
        }

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

	unionres.Branch = resBranches
	unionres.OS = resOSes
        unionres.Device = resDevices

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


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"`
                Branch string `json:"Branch"`
		Device string `json:"Device"`
                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 (b:Branch)<-[:USING]-(t:Tempest:Run)-[:ON]->(o:OS)
				WHERE HAS(t.all_tests)
				WITH t,b,o
				MATCH (d:Device)<-[:ON]-(t:Tempest:Run)
				RETURN count(t) as Number_Of_Runs,
				d.name as Device, o.name as OS_Name, b.name as Branch,
				ROUND(avg(toFloat(t.failing_tests)/(t.tests_run - t.ignored_tests)) * 100) as Average_Pct_Failing,
				ROUND(avg(toFloat(t.passing_tests)/(t.tests_run - t.ignored_tests)) * 100) as Average_Pct_Passing,
				ROUND(avg(toFloat(t.skipped_tests)/(t.tests_run - t.ignored_tests)) * 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_SinglePermutation(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
	}

	//log.Println("osdistro=",mux.Vars(r)["osdistro"])
	//log.Println("osversion=",mux.Vars(r)["osversion"])
	//log.Println("branch=",mux.Vars(r)["branch"])
	//log.Println("device=",mux.Vars(r)["device"])

        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"`
		Ignored_tests int32 `json:"t.ignored_tests"`
		OS_Distro string `json:"o.distro"`
		OS_Version string `json:"o.version"`
		Branch string `json:"b.name"`
                Device string `json:"d.name"`
		Epoch_time int64 `json:"t.epoch_time"`
		Enabled_Services []string `json:"t.enabled_services"`
		Disabled_Services []string `json:"t.disabled_services"`
        }{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (b:Branch)<-[:USING]-(t:Tempest:Run)-[:ON]->(o:OS)
				WITH t,b,o
				MATCH (d:Device)<-[:ON]-(t:Tempest:Run)
				WHERE o.distro = {osdistro} AND o.version = {osversion}
                                AND b.name = {branch} AND d.name = {device}
                                AND HAS(t.all_tests)
				RETURN t.lava_job, t.date, t.sha1, t.all_tests,
				t.passing_tests, t.failing_tests, t.tests_run, t.skipped_tests,
				t.ignored_tests, t.enabled_services, t.disabled_services, t.epoch_time,
				o.distro, o.version, b.name, d.name
				ORDER BY t.epoch_time DESC
                                LIMIT {count}`,
                Parameters: neoism.Props{
				"count": count,
				"osdistro": mux.Vars(r)["osdistro"],
				"osversion": mux.Vars(r)["osversion"],
				"branch": mux.Vars(r)["branch"],
				"device": mux.Vars(r)["device"],
		},
                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"`
                Ignored_tests int32 `json:"t.ignored_tests"`
		OS_Distro string `json:"o.distro"`
                OS_Version string `json:"o.version"`
                Device string `json:"d.name"`
		Branch string `json:"b.name"`
                Epoch_time int64 `json:"t.epoch_time"`
		Enabled_Services []string `json:"t.enabled_services"`
		Disabled_Services []string `json:"t.disabled_services"`
        }{}

	cq := neoism.CypherQuery{
		Statement: `MATCH (b:Branch)<-[:USING]-(t:Tempest:Run)-[:ON]->(o:OS)
				WITH t,b,o
				MATCH (d:Device)<-[:ON]-(t:Tempest:Run)
				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.ignored_tests,
					t.epoch_time, t.enabled_services, t.disabled_services,
					o.distro, o.version, b.name, 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_Id int32 `json:"t.lava_job"`
		Date string `json:"t.date"`
		Distro string `json:"o.distro"`
		Version string `json:"o.version"`
		Branch string `json:"b.name"`
		Device string `json:"d.name"`
	}{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (b:Branch)<-[:USING]-(t:Tempest:Run)-[:ON]->(o:OS)
				WHERE HAS(t.all_tests)
				WITH t,b,o
				MATCH (d:Device)<-[:ON]-(t:Tempest:Run)
                        	RETURN t.lava_job, t.date,
					o.distro, o.version, b.name, d.name ORDER BY t.epoch_time DESC`,
                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_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_Category string `json:"t.test_category"`
		Test_FullName string `json:"t.full_name"`
		Test_Status string `json:"r.status"`
		Test_Class string `json:"t.test_class"`
		Duration string `json:"r.time"`
		Test_Display_Name string `json:"test_display_name"`
        }{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (n:Tempest:Run)-[r:HAS_TEST {status:"fail"}]-(t:Test)
				WHERE n.lava_job = {job}
				RETURN t.name, r.status, r.time, t.full_name, t.test_category, t.test_class, t.test_class + "::" + t.name as test_display_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_Job_Skipped(w http.ResponseWriter, r *http.Request) {
	Results_Tempest_Job_Skipped_Common(Skipped, w, r)
}


func Results_Tempest_Job_Ignored(w http.ResponseWriter, r *http.Request) {
	Results_Tempest_Job_Skipped_Common(Ignored, w, r)
}

func Results_Tempest_Job_Skipped_Common(mode SkippedMode, 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)
        }

        resSkippedTests := [] struct{
                Test_Name string `json:"t.name"`
                Test_Category string `json:"t.test_category"`
                Test_FullName string `json:"t.full_name"`
                Test_Status string `json:"r.status"`
                Test_Class string `json:"t.test_class"`
                Duration string `json:"r.time"`
                Reason string `json:"r.reason"`
		Test_Display_Name string `json:"test_display_name"`
        }{}

	status := "skip"
        if mode == Ignored {
		status = "ignore"
	}

	cq := neoism.CypherQuery{
		Statement: `MATCH (n:Tempest:Run)-[r:HAS_TEST {status:"` + status + `"}]-(t:Test) WHERE n.lava_job = {job}
				RETURN t.name, r.status, r.time, t.full_name, t.test_category, t.test_class, r.reason, t.test_class + "::" + t.name as test_display_name`,
		Parameters: neoism.Props{"job" : job},
		Result: &resSkippedTests,
	}

	//log.Println(cq.Statement)

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

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


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


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

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

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

        res := [] struct {
                Ref_LAVA_Job int32 `json:"ref_lavajob"`
		Ref_Status string `json:"ref_status"`
		Test_Name string `json:"test_name"`
		Change_Desc string `json:"change_desc"`
		Other_Status string `json:"other_status"`
		Other_LAVA_Job int32 `json:"other_lavajob"`
        }{}

        cq := neoism.CypherQuery{
                Statement: `MATCH (ref:Tempest:Run)-[refr:HAS_TEST]-(test:Test)-[otherr:HAS_TEST]-(other:Tempest:Run)
				WHERE ref.lava_job = {ref} AND other.lava_job = {other} AND refr.status <> otherr.status
				RETURN ref.lava_job as ref_lavajob, refr.status as ref_status, test.key as test_name,
						otherr.status as other_status, other.lava_job as other_lavajob,
						refr.status + " -> " + otherr.status as change_desc
					UNION ALL
				MATCH (other:Tempest:Run)-[otherr:HAS_TEST]-(test2:Test)
				WHERE other.lava_job = {other}
				WITH COLLECT(test2) as other_tests
				MATCH (ref:Tempest:Run)-[refr:HAS_TEST]-(test1:Test)
				WHERE ref.lava_job = {ref}
				WITH ref, refr, test1
				WHERE NOT (test1 IN other_tests)
				RETURN ref.lava_job as ref_lavajob, refr.status as ref_status, test1.key as test_name,
						null as other_status, null as other_lavajob,
						refr.status + " -> missing" as change_desc
					UNION ALL
				MATCH (ref:Tempest:Run)-[refr:HAS_TEST]-(test1:Test)
				WHERE ref.lava_job = {ref}
				WITH COLLECT(test1) as ref_tests
				MATCH (other:Tempest:Run)-[otherr:HAS_TEST]-(test2:Test)
				WHERE other.lava_job = {other}
				WITH other, otherr, test2
				WHERE NOT (test2 IN ref_tests)
				RETURN null as ref_lavajob, null as ref_status, test2.key as test_name,
						otherr.status as other_status, other.lava_job as other_lavajob,
						"missing -> " + otherr.status as change_desc`,
                Parameters: neoism.Props{
				"ref": ref,
				"other": other,
		},
                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 Global_Environment(w http.ResponseWriter, r *http.Request) {

	res := struct{
		StaticLogsServer string
	}{}

	res.StaticLogsServer = *logs_server

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


func main() {

        flag.Parse()
 	log.Println("listening on:", *listen_addr)
        log.Println("neo4j server:", *neo4j_server)
	log.Println("logs server:", *logs_server)

	//
	// handle routes for access to data
	//

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

	// global results data
	s.HandleFunc("/all-categorization", Results_AllCategorization)

	// tempest
	s.HandleFunc("/tempest", Results_Tempest_SinglePermutation).Queries("count", "{count:[0-9]+}", "branch", "{branch}", "device", "{device}", "osdistro", "{osdistro}", "osversion", "{osversion}")
	s.HandleFunc("/tempest/summary", Results_Tempest_Summary)
        s.HandleFunc("/tempest/jobs", Results_Tempest_AllJobIds)
	s.HandleFunc("/tempest/job/{job:[0-9]+}", Results_Tempest_Job_Summary)
	s.HandleFunc("/tempest/job/{job:[0-9]+}/failures", Results_Tempest_Job_Failures)
	s.HandleFunc("/tempest/job/{job:[0-9]+}/skipped", Results_Tempest_Job_Skipped)
	s.HandleFunc("/tempest/job/{job:[0-9]+}/ignored", Results_Tempest_Job_Ignored)
	s.HandleFunc("/tempest/jobs/changes", Results_Tempest_Changes).Queries("ref", "{ref:[0-9]+}", "other", "{other:[0-9]+}")

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

	//
	// handle global config content
	//

	http.HandleFunc("/env", Global_Environment)

	//
	// handle static content
	//

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

	// GO!

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