Clean up the table-within-a-table abstraction for printing
the status counts in a box - move to display.py

Signed-off-by: Martin J. Bligh <mbligh@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@761 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/tko/display.py b/tko/display.py
index caba11f..4578033 100755
--- a/tko/display.py
+++ b/tko/display.py
@@ -39,6 +39,43 @@
 					(box_html, self.color, data, box_html)
 
 
+def status_html(db, status_count):
+	"""
+	status_count: dict mapping from status (integer key) to count
+	eg. { 'GOOD' : 4, 'FAIL' : 1 }
+	"""
+#	total = sum(status_count.values())
+#	status_pct = {}
+#	for status in status_count.keys():
+#		status_pct[status] = (100 * status_count[status]) / total
+	rows = []
+	for status in sorted(status_count.keys(), reverse = True):
+		status_word = db.status_word[status]
+		# string = "%d&nbsp(%d%%)" % (status_count[status], status_pct[status])
+		string = "%d&nbsp;%s" % (status_count[status], status_word)
+		rows.append("<tr>%s</tr>" % box(string, status_word).html())
+	return '<table>%s</table>' % '\n'.join(rows)
+
+
+def status_count_box(db, tests, link = None):
+	"""
+	Display a table within a box, representing the status count of
+	the group of tests (e.g. 10 GOOD, 2 WARN, 3 FAIL)
+	"""
+	if not tests:
+		return box(None, None)
+
+	status_count = {}
+	for test in tests:
+		count = status_count.get(test.status_num, 0)
+		status_count[test.status_num] = count + 1
+	worst = sorted(status_count.keys())[0]
+	html = status_html(db, status_count)
+	if link:
+		html = '<a href="%s">%s</a>' % (link, html)
+	return box(html, db.status_word[worst])
+	
+
 def print_table(matrix):
 	"""
 	matrix: list of lists of boxes, giving a matrix of data
diff --git a/tko/machine_kernel.cgi b/tko/machine_kernel.cgi
index d3426b5..2cb58b3 100755
--- a/tko/machine_kernel.cgi
+++ b/tko/machine_kernel.cgi
@@ -21,21 +21,6 @@
 	print_machines_vs_all_kernels(machines)
 
 
-def status_html(status_count):
-	total = sum(status_count.values())
-	status_pct = {}
-	for status in status_count.keys():
-		status_pct[status] = (100 * status_count[status]) / total
-	rows = []
-	for status in sorted(status_pct.keys(), reverse = True):
-		status_word = db.status_word[status]
-		# string = "%d&nbsp(%d%%)" % (status_count[status], status_pct[status])
-		string = "%d&nbsp;%s" % (status_count[status], status_word)
-		box = display.box(string, status_word)
-		rows.append("<tr>%s</tr>" % box.html())
-	return '<table>%s</table>' % '\n'.join(rows)
-
-
 def kernel_machines_box(kernel, machines):
 	status = None
 	status_word = ''
@@ -44,26 +29,10 @@
 		where = { 'kernel_idx':kernel.idx , 'machine_idx':machine.idx }
 		tests += frontend.test.select(db, where)
 
-	status_count = {}
-	for t in tests:
-		print t
-		if status_count.has_key(t.status_num):
-			status_count[t.status_num] +=1
-		else:
-			status_count[t.status_num] = 1
-
-		if not status or t.status_num < status:
-			status = t.status_num
-			status_word = db.status_word[status]
-
 	machine_idxs = ['%d' % machine.idx for machine in machines]
 	link = 'machine_kernel_test.cgi?machine=%s&kernel=%s' % \
 					(','.join(machine_idxs), kernel.idx)
-	if status_word:
-		html = '<a href="%s">%s</a>' % (link, status_html(status_count))
-	else:
-		html = None
-	return display.box(html, color_key = status_word)
+	return display.status_count_box(db, tests, link)
 
 
 def kernel_encode(kernel):