Skip to content

Commit

Permalink
Merge pull request #348 from baifendian/fix-333
Browse files Browse the repository at this point in the history
登录首页概览页面重构
  • Loading branch information
qqzxingchen authored Dec 12, 2016
2 parents 8b3a2a4 + 287a316 commit b1df56e
Show file tree
Hide file tree
Showing 16 changed files with 719 additions and 221 deletions.
60 changes: 24 additions & 36 deletions Aries/codis/rests.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,37 +233,7 @@ def post(self, request, format=None):
"msg":"Error"
}
finally:
return packageResponse(result)


class CodisOverview(APIView):
'''
获取概览信息
'''
def get(self, request, format=None):
query_url = opentsdb_url + "/api/query/"
host_list = Host.objects.all()
allcodis_count = Codis.objects.all().count()
badcodis_count = 0
memory_used_count = 0
memory_total_count = 0
for host in host_list:
memory_used_count += host.memory_used
memory_total_count += host.memory_total
badcodis_query_args = {"start":"6h-ago","end":"","queries":[{"metric":"codis.badcluster","aggregator": "sum",\
"tags":{"bad":"true"}}]}
badcodis = requests.post(query_url,data=json.dumps(badcodis_query_args),timeout=10)
for k,v in json.loads(badcodis.text)[0]['dps'].items():
badcodis_count = v
break
data = {"memory_used_count":memory_used_count,"memory_total_count":memory_total_count,\
"all_codis_count":allcodis_count,"nice_codis_count":allcodis_count-badcodis_count}
result={
"msg":"OK",
"code":200,
"data":data
}
return packageResponse(result)
return packageResponse(result)


class CodisOverview(APIView):
Expand All @@ -287,19 +257,37 @@ def get(self, request, format=None):
for k,v in json.loads(badcodis.text)[0]['dps'].items():
badcodis_count = v
break
data = {"memory_used_count":memory_used_count,"memory_total_count":memory_total_count,\
"all_codis_count":allcodis_count,"nice_codis_count":allcodis_count-badcodis_count}
data = {
"codis_cluster": {
"lives": allcodis_count - badcodis_count,
"dead": badcodis_count,
},
"codis_memory": {
"used": memory_used_count,
"nonUsed": memory_total_count - memory_used_count,
"unit": "GB",
}
}
result={
"msg":"OK",
"code":200,
"data":data
}
except Exception, e:
data = {"memory_used_count": memory_used_count, "memory_total_count": memory_total_count, \
"all_codis_count": allcodis_count, "nice_codis_count": allcodis_count - badcodis_count}
data = {
"codis_cluster": {
"lives": allcodis_count - badcodis_count,
"dead": badcodis_count,
},
"codis_memory": {
"used": memory_used_count,
"nonUsed": memory_total_count - memory_used_count,
"unit": "GB",
}
}
result = {
"msg": "Error, error request from opentsdb",
"code": 200,
"code": 201,
"data": data
}
ac_logger.error("Error, error request from opentsdb %s" % e)
Expand Down
10 changes: 7 additions & 3 deletions Aries/hdfs/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def list_status_share(self,request,path):
'name': source_path.split("/")[-1],
'create_time': datetime.datetime.fromtimestamp(item.get('modificationTime')/1000).strftime("%Y-%m-%d %H:%M:%S"),
'is_dir': 0,
'size' : unitTransform(item.get('length'),0,unit) if item.get('type') == "FILE" else "-"
'size' : self.getSize(item.get('length'),0,unit) if item.get('type') == "FILE" else "-"
}]
else:
#目录分享
Expand All @@ -242,7 +242,7 @@ def list_status_share(self,request,path):
'name': item.get('pathSuffix'),
'create_time': datetime.datetime.fromtimestamp(item.get('modificationTime')/1000).strftime("%Y-%m-%d %H:%M:%S"),
'is_dir': 0 if item.get('type') == "FILE" else 1,
'size': unitTransform(item.get('length'),0,unit) if item.get('type') == "FILE" else "-",
'size': self.getSize(item.get('length'),0,unit) if item.get('type') == "FILE" else "-",
} for item in result if item.get('pathSuffix') != ".Trash"
]
else:
Expand Down Expand Up @@ -295,13 +295,17 @@ def list_status(self, path, request):
'name': item.get('pathSuffix'),
'create_time': datetime.datetime.fromtimestamp(item.get('modificationTime')/1000).strftime("%Y-%m-%d %H:%M:%S"),
'is_dir': 0 if item.get('type') == "FILE" else 1,
'size': unitTransform(item.get('length'),0,unit) if item.get('type') == "FILE" else "-",
'size': self.getSize(item.get('length'),0,unit) if item.get('type') == "FILE" else "-",
} for item in result if item.get('pathSuffix') != ".Trash"
]
self.returned['data'] = {"totalList":totalList,"totalPageNum":len(totalList),"currentPage":1}
hdfs_logger.info("liststatus:%s" %self.returned['data'])
return self.returned

def getSize(self, size, index, unit):
size, index, unit = unitTransform(size, index, unit)
return "%s %s" % (size, unit)

def _copy_file(self, src_path, dest_path, username):
o_type = FileOperatorType.objects.get(name='cp')
log_pk = self._insert_operate_log(
Expand Down
60 changes: 37 additions & 23 deletions Aries/hdfs/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from hdfs.function import HDFS
from django.http import HttpResponse
from tools import *
import math

import requests
from requests.auth import HTTPBasicAuth
Expand Down Expand Up @@ -575,37 +576,50 @@ def getOverview(request):
result = {}
result["code"] = StatusCode["SUCCESS"]
data = {}
#使用百分比
space = getObjByAttr(Space,"name",spaceName)
unitList = ["GB", "TB", "PB"]
# 使用百分比
space = getObjByAttr(Space, "name", spaceName)
hdfs_disk = {
"used": 0,
"nonUsed": 0,
"unit": "GB"
}
if space:
capacity = space[0].capacity
capacity = eval(capacity)
used = capacity["used"]
total = capacity["total"]
try:
hdfs_disk_used = round(float(used)/float(total),2)
except Exception,e:
ac_logger.error(e)
hdfs_disk_used = 0
else:
hdfs_disk_used = 0
#分享个数
used = float(capacity["used"])
total = float(capacity["total"])
total, index, unit = unitTransform(total, 0, unitList)
total = round(total, 2)
used = round(used/math.pow(1024, index), 2)
non_used = round(total - used, 2)
hdfs_disk["used"] = used
hdfs_disk["nonUsed"] = non_used
hdfs_disk["unit"] = unit

# 分享个数
try:
hdfs_shares = DataShare.objects.filter(space_name = spaceName).count()
except Exception,e:
ac_logger.error("hdfs_shares is get failed. so set default 0.")
hdfs_shares = DataShare.objects.filter(space_name=spaceName).count()
except Exception, e:
ac_logger.error("hdfs_shares is get failed. so set default 0. %s" %e)
hdfs_shares = 0
#datanode状态个数

# datanode状态个数
hdfs_datanodes = {
"lives": 0,
"dead": 0
}
try:
extendUrl = "services/HDFS/components/DATANODE"
dic = requestEval(extendUrl)
extend_url = "services/HDFS/components/DATANODE"
dic = requestEval(extend_url)
total_count = dic["ServiceComponentInfo"]["total_count"]
started_count = dic["ServiceComponentInfo"]["started_count"]
hdfs_datanodes = "{0}/{1}".format(started_count,total_count)
except Exception,e:
lives = dic["ServiceComponentInfo"]["started_count"]
dead = total_count - lives
hdfs_datanodes["lives"] = lives
hdfs_datanodes["dead"] = dead
except Exception, e:
ac_logger.error(e)
hdfs_datanodes = "0/0"
data["hdfs_disk_used"] = hdfs_disk_used
data["hdfs_disk"] = hdfs_disk
data["hdfs_shares"] = hdfs_shares
data["hdfs_datanodes"] = hdfs_datanodes
result["data"] = data
Expand Down
16 changes: 9 additions & 7 deletions Aries/hdfs/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,19 @@ def sharePath(shareId,path):
real_path = os.path.realpath("%s/%s/%s/%s" %(os.path.sep,space_path,source_path,path))
return real_path,source_path

def unitTransform(size,index,unit):
def unitTransform(size, index, unit):
if size/1024.0 > 1024:
b = size/1024.0
index = index+1
return unitTransform(b,index,unit)
return unitTransform(b, index, unit)
elif size/1024.0 < 1:
return "{0} {1}".format(round(size,1),unit[index])
elif size/1024.0 >1 and size/1024.0 < 1024:
# return "{0} {1}".format(round(size,1),unit[index])
return round(size, 1), index, unit[index]
elif (size/1024.0 > 1) and (size/1024.0 < 1024):
b = size/1024.0
index = index+1
return "{0} {1}".format(round(b,1),unit[index])
index = index + 1
return round(b, 1), index, unit[index]
# return "{0} {1}".format(round(b,1),unit[index])

@print_request
def test(a,b):
Expand All @@ -151,7 +153,7 @@ def test2(a,b):
print a,b

if __name__=="__main__":
unitTransform(1025,0,["KB","MB","GB","TB"])
print unitTransform(1025,0,["KB","MB","GB","TB"])
#print "aaa"
#test(3,4)
#test2(3,4)
5 changes: 5 additions & 0 deletions Aries/kd_agent/toolsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def get_ingress_detail_info( namespace,params={} ):
url = '/apis/extensions/v1beta1/namespaces/%s/ingresses' % namespace
return K8sRequestManager.get_k8s_data( url,params )

@staticmethod
def get_node_detail_info():
url = '/api/v1/nodes'
return K8sRequestManager.get_k8s_data( url,{} )

# 根据原生的API获取k8s的数据
@staticmethod
def get_k8s_data(url,params = {},timeout = 10 ):
Expand Down
43 changes: 33 additions & 10 deletions Aries/kd_agent/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ def get_overview_k8s_pod_info(namespace):
if pod_detail_info['code'] == RETU_INFO_ERROR:
kd_logger.error( 'call get_overview_k8s_pod_info query k8s pod data error : %s' % pod_detail_info['msg'] )
else:
total = len(pod_detail_info['data']['items'])
count = 0
total = 0
for item in pod_detail_info['data']['items']:
containerStatuses = item['status'].get('containerStatuses',[])
total += len(containerStatuses)

is_all_container_running = True
for cItem in containerStatuses:
if cItem['state'].get( 'running' ) != None:
count += 1
if cItem['state'].get( 'running' ) == None:
is_all_container_running = False
break

count += 1 if is_all_container_running else 0

retu_data['count'] = count
retu_data['total'] = total
return retu_data
Expand Down Expand Up @@ -70,19 +75,37 @@ def get_overview_k8s_rc_info(namespace):
return retu_data

# node要从influxdb中获取数量,但是当前无法获取,因此该函数的实现暂时先搁置。
def get_overview_k8s_node_info(namespace):
retu_data = { 'count':0 }
def get_overview_k8s_node_info():
retu_data = { 'total':0,'count':0 }
node_detail_data = KRM.get_node_detail_info()
if node_detail_data['code'] == RETU_INFO_ERROR:
kd_logger.error( 'call get_overview_k8s_node_info query k8s node data error : %s' % node_detail_data['msg'] )
else:
total = len(node_detail_data['data']['items'])
count = 0
for item in node_detail_data['data']['items']:
for condition in item['status']['conditions']:
if condition['type'] == 'Ready' and condition['status'] == 'True':
count += 1
break
retu_data['count'] = count
retu_data['total'] = total
return retu_data

@csrf_exempt
@return_http_json
@trans_return_json
def get_k8soverview_info(request,namespace):
pod = get_overview_k8s_pod_info(namespace)
rc = get_overview_k8s_rc_info(namespace)
service = get_overview_k8s_service_info(namespace)
node = get_overview_k8s_node_info()

retu_data = {
'pod': get_overview_k8s_pod_info(namespace) ,
'rc': get_overview_k8s_rc_info(namespace),
'service': get_overview_k8s_service_info(namespace),
'node': get_overview_k8s_node_info(namespace)
'k8sp_pod':{ 'lives':pod['count'],'dead':pod['total']-pod['count'] },
'k8sp_rc':{ 'current':rc['count'],'desired':rc['total'] },
'k8sp_service':{ 'count':service['count'] } ,
'k8sp_nodes': { 'lives':node['count'],'dead':node['total']-node['count'] }
}
kd_logger.info( 'call get_overview_k8s_rc_info query k8s overview info : %s' % retu_data )
return generate_success( data=retu_data )
Expand Down
9 changes: 5 additions & 4 deletions Aries/openstack/rests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ def get(self,request,format=None):

class overview(APIView):
def get(self, request, format=None):
ret = {'vm':'0/0','image':0,'volume':0}
ret = {'openstack_vm':{"lives":0,"dead":0},'openstack_image':0,'openstack_disk':0}
login(request)
username = request.user.username
total,running = num_get_vm(request,username)
ret['vm'] = "%s/%s"%(running,total)
ret['image'] = num_get_image(request,username)
ret['volume'] = num_get_volume(request,username)
ret['openstack_vm']['lives'] = running
ret['openstack_vm']['dead'] = total - running
ret['openstack_image'] = num_get_image(request,username)
ret['openstack_disk'] = num_get_volume(request,username)
ret = json_data(ret)
return packageResponse(ret)

Expand Down
2 changes: 1 addition & 1 deletion Aries/user_auth/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def getOverview(request):
except Exception,e:
ac_logger.info(e)
count = 0;
data["count"] = count
data["userAuth_member"] = count
result["code"] = 200
result["data"] = data
return result
Expand Down
1 change: 1 addition & 0 deletions package/Aries/src/App.less
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ html, body, #app {
}
> .content {
padding: 20px;
margin-bottom: 20px;
> div {
animation: fade .25s cubic-bezier(.455,.03,.515,.955);
}
Expand Down
Loading

0 comments on commit b1df56e

Please sign in to comment.