日期:2014-05-16 浏览次数:20474 次
原始代码
?
def ioBandwidth(request): beginDate = request.GET.get("beginDate") collection = getCollection() data = collection.find().sort("InsertDate",pymongo.ASCENDING) mscombi2d.categories = {"category": [{"label":time.strftime("%H:%M",time.strptime(d["InsertDate"],"%Y-%m-%d %H:%M:%S"))} for d in data]} data.rewind()#因为开始是用find返回的,所以是游标,取完后要回退到开始才能重新取 temp = data.clone()#因为后面有两次,只能先把游标拷贝,以方便后面的取值 mscombi2d.dataset = [ {"seriesname": u"入带宽", "parentyaxis": "P", "color": "5a885e","renderas":"Area", "data": [{"value": (d["InBandwidth"])} for d in data] }, {"seriesname": u"出带宽", "parentyaxis": "S", "renderas": "Line", "color": "dca657", "anchorbgcolor": "dca657", "data": [{"value": (d["OutBandwidth"])} for d in temp] } ] res = json.dumps(mscombi2d.to_dict(), ensure_ascii=False, sort_keys=True, indent=4) return HttpResponse(res)
?
修改后代码
?
?
def ioBandwidth(request): beginDate = request.GET.get("beginDate") collection = getCollection() data = [d for d in collection.find()]#把游标内数据存入list mscombi2d = MSCombi2D(u"大节点带宽",beginDate) mscombi2d.categories = {"category": []} mscombi2d.dataset = [ {"seriesname": u"入带宽", "parentyaxis": "P", "color": "5a885e","renderas":"Area", "data": [] }, {"seriesname": u"出带宽", "parentyaxis": "S", "renderas": "Line", "color": "dca657", "anchorbgcolor": "dca657", "data":[] } ] labels = []#时间标签 inband = []#入带宽 outband = []#出带宽 for d in data: labels.append({"label":time.strftime("%H:%M",time.strptime(d["InsertDate"],"%Y-%m-%d %H:%M:%S"))}) inband.append({"value":"%s"%(d["InBandwidth"])}) outband.append({"value":"%s"%(d["OutBandwidth"])}) mscombi2d.dataset[0]["data"] = inband mscombi2d.dataset[1]["data"] = outband res = json.dumps(mscombi2d.to_dict(), ensure_ascii=False, sort_keys=True, indent=4) return HttpResponse(res)
?
?
1)从游标取出数据存入list,不需要再rewind和clone了
2)只用了一次循环,不像前面循环了三次。
3)查找用索引,而不要用sort