django oracle 性能,4.利用Django在前端展示Oracle 状态趋势
利用Django在前端展示Oracle 狀態(tài)趨勢(shì)
2017-12-15 Python 宅必備
開發(fā)環(huán)境
操作系統(tǒng):CentOS 7.4
Python版本 :3.6
Django版本: 1.10.5
操作系統(tǒng)用戶:oms
數(shù)據(jù)處理:pandas
前端展示:highcharts
通過上面我們已介紹了如何定時(shí)獲取系統(tǒng)Oracle狀態(tài)語句以及如何利用pandas處理成highcharts識(shí)別的格式
這節(jié)講如何讓其在前端顯示
建立頁面的步驟
我們還是通過這張圖的步驟來說明如何建立頁面
1. urls.py頁面
from django.conf.urls import url, include
from monitor import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^oracle_command/$',views.oracle_command, name='oracle_command'),
url(r'^commandresult/$',views.commandresult, name='commandresult'),
url(r'^oracle_status$',views.oracle_status, name='oracle_status'),
url(r'^oracle_performance$',views.oracle_performance, name='oracle_performance'),
url(r'^performance$',views.performance, name='performance'),
url(r'^oracle_topevent$',views.oracle_topevent, name='oracle_topevent'),
url(r'^check_topsql$',views.check_topsql, name='check_topsql'),
url(r'^addbaseline$',views.addbaseline, name='addbaseline'),
url(r'^check_hitratio$',views.check_hitratio, name='check_hitratio'),
url(r'^linux_list$',views.linux_list, name='linux_list'),
]
oracle_performance分別為系統(tǒng)狀態(tài)趨勢(shì)的頁面(以天為單位)
performance分別為系統(tǒng)狀態(tài)趨勢(shì)的頁面(以小時(shí)為單位)
2. views.py
這里以oracle_performance函數(shù)做例子講解
def oracle_performance(request):
baseline=[]
ip=[]
ip1=oraclelist.objects.all().order_by('ipaddress')
for i in ip1:
ip.append(i.ipaddress+':'+i.tnsname)
if request.method == 'POST': # If the form has been submitted...
#return HttpResponse('ss')
form = charts_oracle_performance(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
starttime1 = request.POST['starttime']
endtime1 = request.POST['endtime']
performance_type= form.cleaned_data['performance_type']
ipaddress_tnsname_list=form.cleaned_data['ipaddress']
interval=request.POST['interval']
if starttime1 =='' or endtime1 =='':
return HttpResponse('Please give the Start and End time')
else:
starttime=int(str(time.mktime(time.strptime(starttime1,'%Y%m%d'))).split('.')[0])
endtime=int(str(time.mktime(time.strptime(endtime1,'%Y%m%d'))).split('.')[0])
if starttime>endtime:
return HttpResponse('The Start time must larger than the End time')
#starttime=int(str(time.mktime(time.strptime(starttime1,'%Y%m%d %H:%M:%S'))))
#endtime=int(str(time.mktime(time.strptime(endtime1,'%Y%m%d %H:%M:%S'))))
else:
title='Oracle Performance '+'-'+performance_type
subtitle=performance_type
title_y=' Blocks/Seconds'
if performance_type in ['PhysicalReads','LogicalReads']:
unit='blocks/s'
elif performance_type in ['RedoSize']:
unit='bytes/s'
elif performance_type in ['DBTime','CPUTime']:
unit='Minites'
else:
unit='times/s'
final_series=[]
#final_series=oracle_performance_day(performance_type,ipaddress_tnsname_list,starttime,endtime,interval)
#return HttpResponse(final_series)
if interval=='day':
final_series=oracle_performance_day(performance_type,ipaddress_tnsname_list,starttime,endtime,interval)
x_categories=final_series[0]['x']
elif interval=='week':
final_series=oracle_performance_week(performance_type,ipaddress_tnsname_list,starttime,endtime,interval)
x_categories=final_series[0]['x']
#return HttpResponse(final_series)
dic={'categories':x_categories,'series':final_series,'title':title,'subtitle':subtitle,'unit':unit,'title_y':title_y}
#return render_to_response('highcharts_histogram.html',dic) # Redirect after POST
#return HttpResponse (final_series)
return render_to_response('highcharts.html',dic) # Redirect after POST
else:
return render(request, 'oracle_performance.html', {'form': form})
else:
form = charts_oracle_performance() # An unbound form
d1=datetime.datetime.now()
etime= d1.strftime("%Y%m%d")
stime=(d1-datetime.timedelta(hours=720)).strftime("%Y%m%d")
#etime= d1.strftime("%Y%m%d %H")
#stime=(d1-datetime.timedelta(hours=24)).strftime("%Y%m%d %H")
dic={'form':form,'etime':etime,'stime':stime}
#dic={'form':form,'ip':ip,'ipaddress_checked':ipaddress_checked,'etime':etime,'stime':stime}
return render(request, 'oracle_performance.html', dic)
上面的url設(shè)定調(diào)用views.py里面的oracle_performance函數(shù),該函數(shù)講解如下:
首先判斷請(qǐng)求的方法是不是post(提交表單前),如果不是則打開oracle_performance.html頁面,charts_oracle_performance為定義的表單,可在forms.py中定義
如果請(qǐng)求方法為post(提交表單后),首先驗(yàn)證輸入是否正確,如果正確則獲取相應(yīng)的ipaddress,tnsname,performance_type等信息,
接下來根據(jù)performance_type的類型(物理讀,邏輯讀等)調(diào)用monitor/command/views_oracleperformance.py文件里面的方法來得到我們想要的數(shù)據(jù),這個(gè)方法的講解在昨天的公眾號(hào),大家可自行查看
最后我們將TOP SQL相關(guān)的內(nèi)容封裝成字典重定向到相應(yīng)的template文件中
3. template文件
這里我們使用highcharts.html文件來顯示趨勢(shì)圖
$(function () {
$('#container').highcharts({
title: {
text: '{{title|safe}}',
x: -20 //center
},
xAxis: {
categories: {{categories|safe}}
},
yAxis: {
title: {
text: '{{unit}}'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
plotOptions: {
series: {
cursor: 'pointer',
point: {
events: {
click: function(e) {
$("#test3").val(this.y+'s'+this.x);
//上面是當(dāng)前頁跳轉(zhuǎn),如果是要跳出新頁面,那就用
//window.open(e.point.url);
//這里的url要后面的data里給出
}
}
},
}
},
series:[
{% for i in series %}
{
name: '{{i.name}}',
data: {{i.data}}
},
{% endfor %}
]
});
});
這里通過highcharts來展現(xiàn)數(shù)據(jù)庫(kù)性能趨勢(shì)狀態(tài)
Django允許在html文件內(nèi)部使用程for循環(huán)的形式來迭代列表
關(guān)于oracle_peforance頁面就說到這了,performance頁面可自行參考源碼
4. 效果展示
1.數(shù)據(jù)庫(kù)系統(tǒng)狀態(tài)趨勢(shì)(天為單位)
首先是表單提交之前的界面
這里選擇需要查詢的數(shù)據(jù)庫(kù),支持多個(gè)數(shù)據(jù)庫(kù)同時(shí)查詢
然后是起止時(shí)間以及性能類型
最后可以選擇天和周為單位
點(diǎn)擊提交后會(huì)展示各個(gè)數(shù)據(jù)庫(kù)該時(shí)間段的趨勢(shì)圖
2.數(shù)據(jù)庫(kù)系統(tǒng)狀態(tài)趨勢(shì)(小時(shí)為單位)
首先是表單提交之前的界面
這里選擇需要查詢的數(shù)據(jù)庫(kù)
然后是起止時(shí)間以及性能類型
最后是是否比較基線,這個(gè)后續(xù)會(huì)有單獨(dú)介紹
點(diǎn)擊提交后會(huì)展示出每天各個(gè)時(shí)間段的趨勢(shì)圖
5. 源代碼位置
歡迎訪問我的github主頁查看源碼
總結(jié)
以上是生活随笔為你收集整理的django oracle 性能,4.利用Django在前端展示Oracle 状态趋势的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html自定义js程序,JS中微信小程序
- 下一篇: git merging 怎么处理_如何让