summaryrefslogtreecommitdiff
path: root/developer_dashboard_generate.py
blob: 8efff524306f340e7636ea40c8017403a1b02dd2 (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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#!/usr/bin/python3

# Usage :
#   gen-ci-status.py ci-status.config.yaml ci-status-example.html
#

import sys
import json
import os
import yaml
import datetime
import re
import tempfile

scripts_dir=os.path.dirname(sys.argv[0])

########################################################################################################################
# Basic, low level functions
def nice_print(data):
	json_formatted_str = json.dumps(data, indent=4)
	print(json_formatted_str)

def download_and_open(url):
	tmpf, tmpnm=tempfile.mkstemp()
	os.system("wget " + "-o /dev/null " + "-O " + tmpnm + " " + url)
	tmpf = open(tmpnm,'r')
	return tmpf, tmpnm

def close_and_remove(tmpf, tmpnm):
	tmpf.close()
	os.remove(tmpnm)

dt_now=datetime.datetime.now()
def days_since(timestamp):
	return round(((int(dt_now.strftime('%s')) - round(timestamp/1000)) / 3600) / 24)
	
########################################################################################################################
## CONFIG FILE
ci_url="https://ci.linaro.org/"
ci_url_view=ci_url+"view/"
ci_url_job=ci_url+"job/"
ci={}

"""
Read and load yaml config file as it is."""
def get_config(config_name):
	with open(config_name, 'r') as file:
	    config = yaml.safe_load(file)
	config_sanity_check(config)
	# nice_print(config)
	all_configs=config_instantiate_on_all_pattern(config)
	# nice_print(all_configs)
	return all_configs

def config_sanity_check(config):
	# TO BE DONE
	# nice_print(config)
	if 'format' not in config:
		assert("format not exists")
	if 'pattern' not in config:
		assert("pattern not exists")

def config_instantiate_on_all_pattern(config):
	all_configs=[]
	all_jobs=get_ci_page("https://ci.linaro.org", request="/api/json?tree=jobs[name]")
	for pattern in config['pattern']:
		config_fmt=config['format']
		instantiated_config={}
		instantiated_config['filename']=re.sub("@pattern@", pattern, config_fmt['filename'])

		# summary_table
		instantiated_config['summary_table']=config_fmt['summary_table']

		instantiated_config['links']=[]
		for j in range(0, len(config_fmt['links'])):
			if re.search('@pattern@', config_fmt['links'][j]):
				# Get matching pattern on all config[pattern] list
				link_pattern=re.sub("@pattern@", pattern, config_fmt['links'][j])
				for pat in config['pattern']:
					if link_pattern==pat or not re.search(pattern, pat):
						continue

					if re.search(link_pattern+"-", pat): char="-"
					elif re.search(link_pattern+"_", pat): char="_"

					# print("ref=%s tst=%s (char=%s)" %(pattern,pat,char))
					if pat.count(char)>link_pattern.count(char)+1:
						continue
					instantiated_config['links'].append(pat)
					
			else:
				instantiated_config['links'].append( config_fmt['links'][j] )

		# details_table
		instantiated_config['details_table']={}
		instantiated_config['details_table']['columns']=config_fmt['details_table']['columns']
		instantiated_config['details_table']['lines']=[]
		for j in range(0, len(config_fmt['details_table']['lines'])):
			if re.search('@pattern@', config_fmt['details_table']['lines'][j]) or re.search('\*', config_fmt['details_table']['lines'][j]):
				# Get matching pattern on all CI jobs
				if pattern=="tcwg": pattern="tcwg_"
				pjt_pattern = re.sub("@pattern@", pattern, config_fmt['details_table']['lines'][j])
				for job in all_jobs['jobs']:
					if re.search(pjt_pattern, job['name']):
						instantiated_config['details_table']['lines'].append( job['name'] )
			else:
				instantiated_config['details_table']['lines'].append( config_fmt['details_table']['lines'][j] )

		all_configs.append(instantiated_config)

	return all_configs


########################################################################################################################
## COMPUTE MESSAGE ROUTINES
"""
Compute best message to display using internal ci-status representation
- compute_smart_status()
- compute_smart_diag()
- compute_color()
"""

########################
# compute_smart_status
"""
compute_smart_status()

Status reported can any stage of RR algorithm :
   init / success / reducing / bisected / forced / failure
"""
def compute_smart_status(build):
	ret_attr={'text':"-", 'hlink':"", 'class':"", 'color':""}

	# default status (Success, failure, aborted)
	ret_attr['text']=build['result']
	ret_attr['color']=compute_color(build, ret_attr['text'])
	
	# refine with , displayName, nb_components
	displayname=build['displayName']
	components=re.sub("^#[0-9]*(.*-)R.*",r'\1',displayname)
	nb_components=components.count("-")-1

	if re.search(r'.*-force', displayname):
		ret_attr['text']="FORCED"
	elif re.search(r'.*-init', displayname):
		ret_attr['text']="INIT"
	elif re.search(r'.*-trigger-bisect', displayname):
		ret_attr['text']="BISECTED"
	elif nb_components==1:
		ret_attr['text']="REDUCING"
	elif re.search(r'slowed down|grew in size|vect reduced|sve reduced|failed to build', displayname):
		ret_attr['text']="REGRESSED"
		
	return ret_attr;

########################
# compute_smart_diag
"""
compute_smart_diag()

It mainly reads lastBuild project, artifacts/results, and console to compute the best diag for this build
"""
def compute_smart_diag(project, build):
	ret_attr={'text':"-", 'hlink':"", 'class':"", 'color':""}
	#ret_attr['hlink']=ci_url_job+project['project_name']+"/lastCompletedBuild"+"/artifact/artifacts/results"+"/*view*/"
	if 'result' in build and build['result'] == "SUCCESS":
		return ret_attr

	# return diag if found
	file, tmpf = download_and_open(ci_url_job+project['project_name']+"/"+str(build['number'])+"/artifact/artifacts/results")
	last_step="-"
	for items in file:
		if re.search("Benchmarking infra is offline", items):
			ret_attr['text']="Board is offline"
			ret_attr['class']='diag'
			break
		elif re.search("slowed down", items):
			ret_attr['text']="slowed down"
			ret_attr['class']='diag'
			break
		elif re.search("speeds up", items):
			ret_attr['text']="speeds up"
			ret_attr['class']='diag'
			break
		elif re.search("grew in size", items):
			ret_attr['text']="grew in size"
			ret_attr['class']='diag'
			break
		elif re.search(r'vect reduced|sve reduced', items):
			ret_attr['text']="vect/sve reduced"
			ret_attr['class']='diag'
			break

		elif re.search("build errors in logs", items):
			ret_attr['text']="Build errors in : "+last_step
			break

		elif re.search("internal compiler error", items):
			ret_attr['text']="ICE in : "+last_step
			break

		elif re.search(r'^# .*(reset_artifacts|build_abe|build_llvm|benchmark|linux_n_obj)', items):
			last_step=re.sub("^# ","", items)
			last_step=re.sub(" --.*","", last_step)
			last_step=re.sub(":","", last_step)
			ret_attr['text']=last_step
	close_and_remove(file, tmpf)

	file, tmpf = download_and_open(ci_url_job+project['project_name']+"/"+str(build['number'])+"/console")
	build_machine=""
	for items in file:
		if re.search("No space left on device", items):
			ret_attr['text']="No space left on device "+build_machine
			ret_attr['class']='diag'
			break
		elif re.search(r'java.*Exception', items):
			ret_attr['text']="Java Exception "+build_machine
			ret_attr['class']='diag'
			break
			break
		elif re.search("Build timed out", items):
			ret_attr['text']="Build timed out"
			ret_attr['class']='diag'
			break
	close_and_remove(file, tmpf)

	if ret_attr['color'] == "":
		ret_attr['color']=compute_color(build, ret_attr['text'])

	if ret_attr['text'] != "-":
		return ret_attr

	# Otherwise returns last stepg
	ret_attr['text']=last_step
	return ret_attr
	
	
"""
compute_color()

Choose best color
"""
def compute_color(pjt_info_build, text):
	# CI failure
	if re.search(r'ABORTED', pjt_info_build['result']):
		return "purple"
	elif re.search(r'Board is offline|Java Exception|Board is offline|No space left on device', text):
		return "purple"

	# failure (normal flow)
	elif re.search(r'FAILURE', text):
		return "red"
	elif re.search(r'REGRESSED', text):
		return "boldorange"
	elif re.search(r'REDUCING', text):
		return "boldorange"
	elif re.search(r'BISECTED', text):
		return "boldorange"

	# Sucess (normal flow)
	elif re.search(r'FORCED', text):
		return "green"
	elif re.search(r'INIT', text):
		return "green"
	elif re.search(r'SUCCESS', pjt_info_build['result']):
		return "green"
	return ""



########################################################################################################################
## BUILD CI STATUS REPRESENTATION
"""
Get info from CI server and build ci-status representation
- get_ci_page()
- get_ci_project_infos()
- get_ci_project_attribute()
- get_ci_project()
- get_ci()
"""

"""
get_ci_page()

Download CI page as json.
"""
ci_pages={}
def get_ci_page(url, request=""):
	global ci_pages
	if url+request in ci_pages:
		return ci_pages[url+request]
	else:
		#print(".", end='')
		try:
			print("wget "+url)
			os.system("wget " + "-o /dev/null " + "-O /tmp/download.json " + url + "/api/json" + request)
			f = open('/tmp/download.json')
			ci_pages[url+request]=json.load(f)
			f.close()
		except:
			ci_pages[url+request]={}
		return ci_pages[url+request]


"""
get_ci_project_infos()

Retrieve the information from a given project out of the CI server
"""
def get_ci_project_infos(pjt_name):
	ci_pjt={}
	usual_requests="?tree=number,result,timestamp,displayName,builds[number,result,timestamp,displayName]"
	
	ci_pjt=get_ci_page(ci_url_job+pjt_name, request=usual_requests)
	ci_pjt['project_name']=pjt_name

	if 'builds' not in ci_pjt: return ci_pjt

	for bld in ci_pjt['builds']:
		if 'lastCompletedBuild' not in ci_pjt and bld['result']:
			ci_pjt['lastCompletedBuild']=bld
		if 'lastSuccessfulBuild' not in ci_pjt and bld['result'] and bld['result']=='SUCCESS':
			ci_pjt['lastSuccessfulBuild']=bld
		if 'lastFailedBuild' not in ci_pjt and bld['result'] and not bld['result']=='SUCCESS':
			ci_pjt['lastFailedBuild']=bld
		if 'lastForcedBuild' not in ci_pjt and re.match('.*-force', bld['displayName']):
			ci_pjt['lastForcedBuild']=bld
			bld=get_ci_page(ci_url_job+pjt_name+"/"+str(bld['number']), request="?tree=actions[causes[upstreamUrl,upstreamBuild]]")
			for action in bld['actions']:
				if 'causes' in action:
					for cause in action['causes']:
						if 'upstreamUrl' in cause:
							ci_pjt['lastForcedBuild']['upstreamUrl']=cause['upstreamUrl']
							ci_pjt['lastForcedBuild']['upstreamBuild']=cause['upstreamBuild']
							bld=get_ci_page(ci_url+cause['upstreamUrl']+str(cause['upstreamBuild']), request=usual_requests)
							ci_pjt['lastForcedBuild']['upstreamBuildName']=bld['displayName']


	return ci_pjt


"""
get_ci_project_attribute()

Get one info from the CI server (1 project - 1 attribute)
"""
def get_ci_project_attribute(pjt_infos, attr_name):
	ret_attr={'text':"-", 'hlink':"", 'class':"", 'color':""}
	
	try:
		if attr_name=="project":
			ret_attr['text']=pjt_infos['project_name']
			ret_attr['hlink']=ci_url_job+pjt_infos['project_name']

		elif attr_name=="display_name" or attr_name=="last_title":
			ret_attr['text']=pjt_infos['lastCompletedBuild']['displayName']
			ret_attr['hlink']=ci_url_job+pjt_infos['project_name']+"/"+str(pjt_infos['lastCompletedBuild']['number'])

		elif attr_name=="build_number":
			ret_attr['text']=pjt_infos['lastCompletedBuild']['number']
			ret_attr['hlink']=ci_url_job+pjt_infos['project_name']+"/"+str(pjt_infos['lastCompletedBuild']['number'])

		elif attr_name=="pure_status":
			ret_attr=compute_smart_status( pjt_infos['lastCompletedBuild'] )

		elif attr_name=="diag":
			ret_attr=compute_smart_diag( pjt_infos, pjt_infos['lastCompletedBuild'] )

		elif attr_name=="status":
			ret_status=compute_smart_status( pjt_infos['lastCompletedBuild'] )
			ret_diag=compute_smart_diag( pjt_infos, pjt_infos['lastCompletedBuild'] )
			if ret_diag['text'] != "-":
				ret_attr['text']=ret_status['text']+" ("+ret_diag['text']+")"
			else:
				ret_attr['text']=ret_status['text']
			ret_attr['color']=compute_color(pjt_infos['lastCompletedBuild'], ret_attr['text'])

		elif attr_name=="since_last_build":
			timestamp=pjt_infos['lastCompletedBuild']['timestamp']
			ret_attr['text']=str(days_since(timestamp)) + " days"

		elif attr_name=="since_last_success":
			timestamp=pjt_infos['lastSuccessfulBuild']['timestamp']
			ret_attr['text']=str(days_since(timestamp)) + " days"

		elif attr_name=="since_last_fail":
			timestamp=pjt_infos['lastFailedBuild']['timestamp']
			ret_attr['text']=str(days_since(timestamp)) + " days"

		elif attr_name=="since_last_force":
			timestamp=pjt_infos['lastForcedBuild']['timestamp']
			ret_attr['text']=str(days_since(timestamp)) + " days"

		elif attr_name=="nb_force":
			total=0
			forced=0
			for bld in pjt_infos['builds']:
				total=total+1
				if re.match('.*-force', bld['displayName']):
					forced=forced+1
			ret_attr['text']=str(forced) + " out of " + str(total)

		elif attr_name=="nb_fail":
			total=0
			failed=0
			nice_print(pjt_infos['builds'])
			for bld in pjt_infos['builds']:
				total=total+1
				if not bld['result']=='SUCCESS':
					failed=failed+1
			ret_attr['text']=str(failed) + " out of " + str(total)

		# Useful links 
		elif attr_name=="useful_links":
			lnk=ci_url_job+pjt_infos['project_name']+"/"+str(pjt_infos['lastCompletedBuild']['number'])+"/artifact/artifacts/results/*view*/"
			ret_attr['text']="<a href="+lnk+">res</a>"

			lnk=ci_url_job+pjt_infos['project_name']+"/"+str(pjt_infos['lastCompletedBuild']['number'])+"/console"
			ret_attr['text']+=" / <a href="+lnk+">console</a>"

		elif attr_name=="last_force":
			if 'lastForcedBuild' in pjt_infos:
				lnk=ci_url_job+pjt_infos['project_name']+"/"+str(pjt_infos['lastForcedBuild']['number'])
				ret_attr['text']="<a href="+lnk+">build</a>"

				lnk=ci_url+pjt_infos['lastForcedBuild']['upstreamUrl']+str(pjt_infos['lastForcedBuild']['upstreamBuild'])
				ret_attr['text']+="/ <a href="+lnk+">bisect</a>"

				bldname=pjt_infos['lastForcedBuild']['upstreamBuildName']
				if re.match(r'.*spurious|.*baseline', bldname):
					bldname=re.sub("#([0-9]*)-(.*)-(.*)", r'\2-\3', bldname)
					ret_attr['text']+=" / "+bldname
				else:
					compon=re.sub("#([0-9]*)-(.*)-(.*)", r'\2', bldname)
					sha1=re.sub("#([0-9]*)-(.*)-(.*)", r'\3', bldname)

					lnk="https://git.linaro.org/toolchain/ci/interesting-commits.git/tree/%s/sha1/%s"%(compon, sha1)
					ret_attr['text']+=" / <a href="+lnk+">regression</a>"

		elif attr_name=="result_file":
			lnk=ci_url_job+pjt_infos['project_name']+"/"+str(pjt_infos['lastCompletedBuild']['number'])+"/artifact/artifacts/results/*view*/"
			ret_attr['text']="<a href="+lnk+">res</a>"
		elif attr_name=="console":
			lnk=ci_url_job+pjt_infos['project_name']+"/"+str(pjt_infos['lastCompletedBuild']['number'])+"/console"
			ret_attr['text']="<a href="+lnk+">console</a>"


		# TODO 
		elif attr_name=="mail_reported":
			ret_attr=compute_smart_diag(pjt_infos)
			if re.search(r'slowed down|grew in size|vect reduced|sve reduced', ret_attr['text']):
				ret_attr['text']="reported"
				ret_attr['hlink']=ci_url_job+pjt_infos['project_name']+"/"+str(pjt_infos['lastCompletedBuild']['number'])+"/artifact/artifacts/mail/mail-body.txt/*view*/"
			else:
				ret_attr['text']="-"

		elif attr_name=="bmk_job":
			file, tmpf = download_and_open(ci_url_job+pjt_infos['project_name']+"/lastCompletedBuild"+"/artifact/artifacts/results_id")
			for items in file:
				ret_attr['text']=re.sub(".*/","", items)
				ret_attr['hlink']=ci_url_job+"tcwg-benchmark"+"/"+ret_attr['text']
			close_and_remove(file, tmpf)
				
		elif attr_name=="components":
			ret_attr['text']=re.sub("^#[0-9]*-(.*)-R.*",r'\1',pjt_infos['lastCompletedBuild']['displayName'])

		elif attr_name=="failing_step":
			ret_attr=compute_smart_diag(pjt_infos)

	except:
		ret_attr['text']="-"

	return ret_attr

"""
get_ci_project()

Get ci-status internal representation for a project (PJT)
Iterates over the CONFIG columns, and request the infos for each one
"""
def get_ci_project(config, ci, pjt_name):
	ci_project={}
	ci_project['data']=get_ci_project_infos(pjt_name)
	for attr in config['details_table']['columns']:
		ci_project[attr] = get_ci_project_attribute(ci_project['data'], attr)
	return ci_project


def count_jobs(ci, requested_status, mindays, maxdays):
	nb=0
	for job in ci:
		if not 'lastCompletedBuild' in ci[job]['data']:
			continue
		days=days_since(ci[job]['data']['lastCompletedBuild']['timestamp'])
		if days>=mindays and days<=maxdays and re.search(requested_status, ci[job]['status']['text']): 
			nb=nb+1
	if nb==0: return "-"
	else: return nb

"""
get_ci_summary_laps()
"""
def get_ci_summary_laps(ci, timelaps, attr_name):
	ret_attr={'text':"-", 'hlink':"", 'class':"", 'color':""}

	mindays=0
	maxdays=9999
	if re.search(r'day-([0-9]*)$', timelaps): 
		maxdays=int(re.sub("day-([0-9]*)$",r'\1', timelaps))
		mindays=maxdays
	if re.search(r'last-([0-9]*)-days', timelaps):
		maxdays=int(re.sub("last-([0-9]*)-days",r'\1', timelaps))
	
	try:
		if attr_name=="timelaps":
			ret_attr['text']=timelaps
			ret_attr['color']="blue"
		elif attr_name=="nb_success":
			ret_attr['text']=count_jobs(ci, 'SUCCESS', mindays, maxdays)
			ret_attr['color']="green"
		elif attr_name=="nb_failure":
			ret_attr['text']=count_jobs(ci, 'FAILURE', mindays, maxdays)
			ret_attr['color']="red"
		elif attr_name=="nb_reducing":
			ret_attr['text']=count_jobs(ci, 'REDUCING', mindays, maxdays)
			ret_attr['color']="red"
		elif attr_name=="nb_bisected":
			ret_attr['text']=count_jobs(ci, 'BISECTED', mindays, maxdays)
			ret_attr['color']="red"
		elif attr_name=="nb_forced":
			ret_attr['text']=count_jobs(ci, 'FORCED', mindays, maxdays)+count_jobs(ci, 'INIT', mindays, maxdays)
			ret_attr['color']="green"
		elif attr_name=="nb_aborted":
			ret_attr['text']=count_jobs(ci, 'ABORTED', mindays, maxdays)
			ret_attr['color']="purple"

	except:
		ret_attr['text']="-"

	return ret_attr
		

"""
get_ci()

Get ci-status internal representation main routine.
Iterates over the CONFIG project, and request the infos for each one
"""
def get_ci(config):
	ci={}
	for line in config['details_table']['lines']:
		all_jobs=get_ci_page("https://ci.linaro.org", request="/api/json?tree=jobs[name]")
		for job in all_jobs['jobs']:
			if re.search(line, job['name']):
				ci[job['name']]=get_ci_project(config, ci, job['name'])
			
	summary={}
	for laps in config['summary_table']['lines']:
		summary[laps]={}
		for attr in config['summary_table']['columns']:
			summary[laps][attr]=get_ci_summary_laps(ci, laps, attr)
			
	ci['summary_table']=summary
	return ci




########################################################################################################################
## DUMP HTML

"""
dump html routines
- dump_html_one_line
- dump_html
"""

html_header = """<html>
<style>
  table, td, th {
    border-collapse: collapse;
  }
  tbody tr:nth-child(even) td {
    background-color: #ededed;
  }
</style>
<head>
<title>CI Status - %s</title>
<link rel="stylesheet" type="text/css" href="sorting-table-css/example.css" />
</head>
<body>
<h1>CI Status - %s</h1>
"""


html_footer = """
 <script>
   var table = document.querySelector('.massive')
   var tbody = table.tBodies[0]
   var rows = [].slice.call(tbody.rows, 0)
   var fragment = document.createDocumentFragment()

   for (var k = 0; k < 50; k++) {
     for (var i = 0; i < rows.length; i++) {
       fragment.appendChild(rows[i].cloneNode(true))
     }
   }
   tbody.innerHTML = ''
   tbody.appendChild(fragment)
 </script>
 <!-- <script type="text/javascript" src="sortable.js"></script> -->
 <script src="sorting-table-css/sortable.js"></script>
 <script>
   function prepareAdvancedTable() {
     function convertSizeToBytes(str) {
       var matches = str.match(/^([0-9.]+)(\w+)$/)
       if (matches) {
         var vals = {
           kB: 1, // 1024 B
           KiB: 1,// 1024 B
           MB: 2, // 1024 * 1024 B
           GB: 3, // 1024 * 1024 * 1024 B
           TB: 4, // 1024 * 1024 * 1024 *1024 B
         }
         return (matches[1] || 0) * Math.pow(1024, vals[matches[2]])
       }
       return str
     }

     var size_table = document.querySelector('.advanced-table')
     var rows = size_table.tBodies[0].rows
     for (let i = 0; i < rows.length; i++) {
       const date_element = rows[i].cells[2]
       const size_element = rows[i].cells[1]
       date_element.setAttribute('data-sort', date_element.innerText.replace(/(\d+)\/(\d+)\/(\d+)/, '$3$1$2'))
       size_element.setAttribute('data-sort', convertSizeToBytes(size_element.innerText))
     }
   }
   prepareAdvancedTable()
 </script>
</body>
</html>
"""

def dump_html_one_line(f, config_table, ci_pjt):
	f.write(" <tr>\n")
	for attr in config_table['columns']:
		f.write("  <td>")
		if ci_pjt[attr]['color']:
			f.write("<font color=\""+ci_pjt[attr]['color']+"\">")
		if ci_pjt[attr]['hlink']:
			f.write("<a href='"+ci_pjt[attr]['hlink']+"'>")
		f.write(str(ci_pjt[attr]['text']))
		if ci_pjt[attr]['hlink']:
			f.write("</a>")
		if ci_pjt[attr]['color']:
			f.write("</font>")
		f.write("</td>\n");
	f.write(" </tr>\n")

def dump_html_util(config_name, output_dirname):
	print("# Copy yaml : "+ config_name)
	os.system("cp " + config_name + " " + output_dirname)
	
	print("# Copy css  : sorting-table-css")
	os.system("cp -ar "+scripts_dir+"/developer_dashboard_generate/sorting-table-css " + output_dirname)
	
def dump_html(config, ci):
	print("# Emit html : "+ config['filename'])
	f = open(output_dirname+"/"+config['filename'], 'w')

	f.write(html_header % (os.path.basename(config['filename']), os.path.basename(config['filename'])))
	f.write("<p> date = "+str(dt_now)+"</p>\n")
	f.write("<p> config = <a href="+os.path.basename(config_name)+">"+os.path.basename(config_name)+"</a></p>\n")
	
	# SUMMARY_TABLE
	f.write("<h2> SUMMARY_TABLE </h2>\n")
	f.write("<p> statistics on every last completed builds</p>\n")
	f.write("<table border=1 cellspacing=1 cellpadding=3 class=\"sortable\">\n")
	f.write(" <thead>\n")
	for col in config['summary_table']['columns']:
		f.write("  <th>"+col+"</th>\n")
	f.write(" </thead>\n")
	for laps in config['summary_table']['lines']:
		dump_html_one_line(f, config['summary_table'], ci['summary_table'][laps])
	f.write("</table>\n\n")

	# LINKS
	f.write("<h2> LINKS </h2>\n")
	f.write("<ul>\n")
	for lnk in config['links']:
		f.write("  <li><a href=\""+lnk+".html\">"+lnk+".html</a></li>\n")
	f.write("</ul>\n\n")

	# DETAILS_TABLE
	f.write("<h2> DETAILS_TABLE </h2>\n")
	f.write("<table border=1 cellspacing=1 cellpadding=3 class=\"sortable\">\n")
	f.write(" <thead>\n")
	for col in config['details_table']['columns']:
		f.write("  <th>"+col+"</th>\n")
	f.write(" </thead>\n")

	for pjt in config['details_table']['lines']:
		dump_html_one_line(f, config['details_table'], ci[pjt])

	f.write("</table>\n")

	f.write(html_footer)
	f.close()

########################################################################################################################
## BASIC ASCII DUMP ROUTINES

"""
Basic ascii dump routines
- dump_ascii_one_line
- dump_ascii
"""
def dump_ascii_fmt(k):
	if k=="project":
		return "%70s,"
	else:
		return "%20s,"

def dump_ascii_one_line(config_table, ci, pjt_name):
	for attr in config_table['columns']:
		fmt=dump_ascii_fmt(attr)
		print(fmt % (ci[pjt_name][attr]['text']), end='')
	print("")


def dump_ascii(config, ci):
	i=0
	print("\n\n=== DETAILS TABLE - %s" %(config['filename']))

	# Array header
	for col in config['details_table']['columns']:
		fmt=dump_ascii_fmt(col)
		print(fmt % (col), end='');
	print("")

	for pjt in config['details_table']['lines']:
		dump_ascii_one_line(config['details_table'], ci, pjt)


########################################################################################################################
## MAIN PROCEDURE
def usage():
	print("USAGE : gen-ci-status.py report-config.yaml <report-directory>")
	exit(1)
	
if __name__ == "__main__":
	if len(sys.argv) <= 2:
		usage()
	config_name=sys.argv[1]
	if len(sys.argv) == 2:
		output_dirname=os.path.basename(config_name)
	else:
		if os.path.isdir(sys.argv[2]):
			output_dirname=sys.argv[2]
		else:
			usage()

	all_configs=get_config(config_name)
	
	for config in all_configs:
		ci=get_ci(config)
		# dump_ascii(config, ci)
		dump_html(config, ci)
	dump_html_util(config_name, output_dirname)