def main()

in tez-tools/swimlanes/swimlane.py [0:0]


def main(argv):
	(opts, args) = getopt(argv, "o:t:f:")
	out = sys.stdout
	ticks = -1 # precision of 1/tick
	fraction = -1
	for k,v in opts:
		if(k == "-o"):
			out = open(v, "w")
		if(k == "-t"):
			ticks = int(v)
		if(k == "-f"):
			if(int(v) < 100):
				fraction = int(v)/100.0
	if len(args) == 0:
		return usage()
	log = AMLog(args[0]).structure()
	lanes = [c.name for c in sorted(log.containers.values(), key=lambda a: a.start)]
	marginTop = 128
	marginRight = 100;
	laneSize = 24
	y = len(lanes)*laneSize
	items = attempts(log)
	maxx = max([a[4] for a in items])
	if ticks == -1:
		ticks = min(1000, (maxx - log.zero)/2048)
	xdomain = lambda t : (t - log.zero)/ticks 
	x = xdomain(maxx)
	svg = SVGHelper(x+2*marginRight+256, y+2*marginTop)
	a = marginTop
	svg.text(x/2, 32, log.name, style="font-size: 32px; text-anchor: middle")	
	containerMap = dict(zip(list(lanes), xrange(len(lanes))))
	svg.text(marginRight - 16, marginTop - 32, "Container ID", "text-anchor:end; font-size: 16px;")
	# draw a grid
	for l in lanes:
		a += laneSize
		svg.text(marginRight - 4, a, l, "text-anchor:end; font-size: 16px;")
		svg.line(marginRight, a, marginRight+x, a, "stroke: #ccc")
	for x1 in set(range(0, x, 10*ticks)) | set([x]):
		svg.text(marginRight+x1, marginTop-laneSize/2, "%0.2f s" % ((x1 *  ticks)/1000), "text-anchor: middle; font-size: 12px")
		svg.line(marginRight+x1, marginTop-laneSize/2, marginRight+x1, marginTop+y, "stroke: #ddd")
	svg.line(marginRight, marginTop, marginRight+x, marginTop)
	svg.line(marginRight, y+marginTop, marginRight+x, y+marginTop)
	svg.line(marginRight, marginTop, marginRight, y+marginTop)
	svg.line(marginRight+x, marginTop, marginRight+x, y+marginTop)
	
	colourman = ColourManager()
	for c in log.containers.values():
		y1 = marginTop+(containerMap[c.name]*laneSize)
		x1 = marginRight+xdomain(c.start)
		svg.line(x1, y1, x1, y1 + laneSize, style="stroke: green")
		if c.stop > c.start:
			x2 = marginRight+xdomain(c.stop)
			if (c.status == 0):
				svg.line(x2, y1, x2, y1 + laneSize, style="stroke: green")
			else: 
				svg.line(x2, y1, x2, y1 + laneSize, style="stroke: red")
				svg.text(x2, y1, "%d" % (c.status), style="text-anchor: right; font-size: 12px; stroke: red", transform="rotate(90, %d, %d)" % (x2, y1)) 
			svg.rect(x1, y1, x2, y1 + laneSize, style="fill: #ccc; opacity: 0.3")
		elif c.stop == -1:
			x2 = marginRight+x 
			svg.rect(x1, y1, x2, y1 + laneSize, style="fill: #ccc; opacity: 0.3")
	for dag in log.dags:
		x1 = marginRight+xdomain(dag.start)
		svg.line(x1, marginTop-24, x1, marginTop+y, "stroke: black;", stroke_dasharray="8,4")
		x2 = marginRight+xdomain(dag.finish)
		svg.line(x2, marginTop-24, x2, marginTop+y, "stroke: black;", stroke_dasharray="8,4")
		svg.line(x1, marginTop-24, x2, marginTop-24, "stroke: black")
		svg.text((x1+x2)/2, marginTop-32, "%s (%0.1f s)" % (dag.name, (dag.finish-dag.start)/1000.0) , "text-anchor: middle; font-size: 12px;")		
		vertexes = set([v.name for v in dag.vertexes])
		colourmap = dict([(v,colourman.next()) for v in list(vertexes)])
		for c in dag.attempts():
			colour = colourmap[c.vertex]
			y1 = marginTop+(containerMap[c.container]*laneSize)+1
			x1 = marginRight+xdomain(c.start)
			x2 = marginRight+xdomain(c.finish)
			y2 = y1 + laneSize - 2
			locality = (c.kvs.has_key("DATA_LOCAL_TASKS") * 1) + (c.kvs.has_key("RACK_LOCAL_TASKS")*2)
			#CompletedLogs may not be present in latest tez logs
			link = c.kvs.get("completedLogs", "")
			svg.rect(x1, y1, x2, y2, title=c.name, style="fill: %s; stroke: #ccc;" % (colour), link=link)
			if locality > 1: # rack-local (no-locality isn't counted)
				svg.rect(x1, y2-4, x2, y2, style="fill: #f00; fill-opacity: 0.5;", link=link)
			if x2 - x1 > 64:
				svg.text((x1+x2)/2, y2-12, "%s (%05d_%d)" % (c.vertex, c.tasknum, c.attemptnum), style="text-anchor: middle; font-size: 9px;")
			else:
				svg.text((x1+x2)/2, y2-12, "%s" % c.vertex, style="text-anchor: middle; font-size: 9px;")
		finishes = sorted([c.finish for c in dag.attempts()])
		if(len(finishes) > 10 and fraction > 0):
			percentX = finishes[int(len(finishes)*fraction)]
			svg.line(marginRight+xdomain(percentX), marginTop, marginRight+xdomain(percentX), y+marginTop, style="stroke: red")
			svg.text(marginRight+xdomain(percentX), y+marginTop+12, "%d%% (%0.1fs)" % (int(fraction*100), (percentX - dag.start)/1000.0), style="font-size:12px; text-anchor: middle")
	out.write(svg.flush())
	out.close()
	print("Output svg is written into: " + str(out))