Ejemplo n.º 1
0
def system_page(request):
	## list rendering
	print('####### system page request ########')
	print(request.GET)

	levels = [ 'server', 'item' ]
	system_list = []
	item_list = [ 'brief', 'cpu', 'memory', 'swap', 'disk', 'net', 'resource' ]

	system_list = common.core.get_client_list()
	
	system_list.sort()
	js_chart_list = _make_static_chart_list(request.GET, 'system', levels, [ system_list, item_list ])

	# chart data
	chart = chart_js_renderer()
	js_chart_data = ''
	chart_data_list = []
	start_ts, end_ts = _get_ts(request.GET)

	if 'server' in request.GET and request.GET['server'] != '' and 'item' in request.GET and request.GET['item'] != '':
		loader_list_path = common.core.system_view(request.GET['server'], request.GET['item'])
		for loader in loader_list_path:
			ts = int(time.time())
			chart_data_list += loader.load(start_ts, end_ts)

		js_chart_data = ''
		for chart_data in chart_data_list:
			js_chart_data += chart.render(chart_data)
		
	## make view
	variables = RequestContext(request, { 'main_link':_make_main_link(), 'chart_list':js_chart_list, 'chart_data':js_chart_data } )
	return render_to_response('system_page.html', variables)
Ejemplo n.º 2
0
def chart_page(request):
	print('####### chart page request ########')

	param = request.GET
	print(param)

	## list rendering
	levels, chart_map = common.core.get_chart_list(param)
	if (len(levels) == 0):
		variables = RequestContext(request, { 'main_link':_make_main_link(), 'chart_list':'', 'chart_data':'' } )
		return render_to_response('chart_page.html', variables)
	
	js_chart_list = _make_dynamic_chart_list(param, 'chart', levels, chart_map)
	print(levels)

	# case 1. not selected anyone
	if levels[0] not in param:
		#print('## return chart map')
		variables = RequestContext(request, { 'main_link':_make_main_link(), 'chart_list':js_chart_list } )
		return render_to_response('chart_page.html', variables)

	# case 2. partialy selected
	if levels[-1] not in param:
		ret = chart_map
		for level in levels:
			if level in param:
				if isinstance(ret, dict) and param[level] in ret:
					ret = ret[param[level]]
				else:
					ret = []
					break
			else:
				break

		#print('## return json: ' + json.dumps(ret))
		return HttpResponse(json.dumps(ret))


	# case 3. select all (make data)
	ret = common.core.get_chart_data(param)
	#print(ret)

	if ret == None:
		variables = RequestContext(request, { 'main_link':_make_main_link(), 'chart_data':'Unknown chart id'} )
		return render_to_response('chart_page.html', variables)

	loaders = ret

	## chart rendering
	chart = chart_js_renderer()
	chart_data_list = []
	start_ts, end_ts = _get_ts(request.GET)

	if not isinstance(loaders, list):
		loaders = [loaders]

	for loader in loaders:
		chart_data_list += loader.load(start_ts, end_ts)

	js_chart_data = ''
	for chart_data in chart_data_list:
		js_chart_data += chart.render(chart_data)


	## make view
	variables = RequestContext(request, { 'main_link':_make_main_link(), 'chart_list':js_chart_list, 'chart_data':js_chart_data } )
	return render_to_response('chart_page.html', variables)
Ejemplo n.º 3
0
def expr_page(request):
	levels, chart_map = common.core.get_chart_list(request.GET) # for init (preload cloud map)

	print('####### expr page request ########')

	if request.method == 'POST':
		param = request.POST
	else:
		param = request.GET
	print(param)

	
	expr = ''
	expr_form = chart_expr_form(data=param)
	if expr_form.is_valid():
		expr = expr_form.cleaned_data['expr']

	print('## expr: %s' % expr)
	## make view

	## eval expression
	js_chart_data = ''
	if request.method == 'POST' and expr != '':
		try:
			x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
			if x_forwarded_for:
				ip = x_forwarded_for.split(',')[0]
			else:
				ip = request.META.get('REMOTE_ADDR')

			syslog('[hubblemon-expr_page:%s] %s' % (ip, query))
			loader = eval(expr)
			#print(loader)

			if isinstance(loader, list) or isinstance(loader, tuple):
				loaders = loader
			else:
				loaders = [loader]
		
			## chart rendering
			chart = chart_js_renderer()
			start_ts, end_ts = _get_ts(param)

			for loader in loaders:
				if hasattr(loader, 'load'):
					chart_data_list = loader.load(start_ts, end_ts)
					for chart_data in chart_data_list:
						js_chart_data += chart.render(chart_data)
				else:
					js_chart_data += str(loader)
				
		except Exception as e:
			js_chart_data = '''
				<p>evaluation error</p>
				<p>source: %s</p>
				<p>exception: %s</p>
			''' % (expr, str(e))

	## set time range
	start_date = ''
	end_date = ''
	if 'start_date' in param:
		start_date = param['start_date']
	if 'end_date' in param:
		end_date = param['end_date']
	date_range = '''<input type="hidden" name="start_date" value="%s">
			<input type="hidden" name="end_date" value="%s">''' % (start_date, end_date)
	js_chart_list = _make_time_range(param, "'/expr?expr=%s'" % urllib.parse.quote(expr))

	## make view
	variables = RequestContext(request, { 'main_link':_make_main_link(), 'expr_form':expr_form, 'date_range':date_range, 'chart_list':js_chart_list, 'chart_data':js_chart_data} )
	return render_to_response('expr_page.html', variables)