def dmaMapStatistic(self): ''' DMA 在线监控页面 dma分区的统计信息 ''' month_list = generat_year_month() # print(month_list) dmareport = self.dma_statistic2(month_list) water_in = dmareport['water_in'] monthly_sale = dmareport['monthly_sale'] # print('monthly_sale',monthly_sale) today = datetime.date.today() month_str = today.strftime("%Y-%m") lastmonth = datetime.date.today().replace(day=1) - datetime.timedelta( days=1) lastmonth_str = lastmonth.strftime("%Y-%m") if self.belongto.organlevel == '1': dma_level = '2' else: dma_level = self.belongto.organlevel return { "dma_name": self.dma_name, "dma_no": self.dma_no, "belongto": self.belongto.name, "belongto_cid": self.belongto.cid, "dma_level": dma_level, #"二级", "state": "在线", "water_in": round(float(water_in), 2), "readtime": today.strftime("%Y-%m-%d"), "month_sale": round(float(monthly_sale[month_str]), 2), "last_month_sale": round(float(monthly_sale[lastmonth_str]), 2), "last_add_ratio": "34%", "leakerate": random.choice([ 9.65, 13.46, 11.34, 24.56, 32.38, 7.86, 10.45, 17.89, 23.45, 36, 78 ]) }
def dmareport(request): print("dmareport:", request.GET) stationid = request.GET.get("station") or '' # DMABaseinfo pk endTime = request.GET.get("endTime") or '' treetype = request.GET.get("treetype") or '' today = datetime.date.today() endTime = today.strftime("%Y-%m") lastyear = datetime.datetime(year=today.year - 1, month=today.month, day=today.day) startTime = lastyear.strftime("%Y-%m") data = [] sub_dma_list = [] # echart_data = {} # def month_year_iter( start_month, start_year, end_month, end_year ): # ym_start= 12*start_year + start_month # ym_end= 12*end_year + end_month # for ym in range( ym_start, ym_end ): # y, m = divmod( ym, 12 ) # # yield y, m+1 # yield '{}-{:02d}'.format(y,m+1) # month_list = list(month_year_iter(lastyear.month,lastyear.year,today.month,today.year)) # # print(month_list) # for m in month_list: # # print (m) # if m not in echart_data.keys(): # echart_data[m] = 0 # hdates = [f[-2:] for f in echart_data.keys()] # hdates = hdates[::-1] if treetype == 'dma': # distict = District.objects.get(id=int(stationid)) # bigmeter = distict.bigmeter.first() dmas = DMABaseinfo.objects.filter(pk=int(stationid)) else: # dma = DMABaseinfo.objects.first() # dmastation = dma.dmastation.first() # comaddr = dmastation.station_id if treetype == '': organ = Organizations.objects.first() if treetype == 'group': organ = Organizations.objects.get(cid=stationid) organs = organ.get_descendants(include_self=True) dmas = None for o in organs: if dmas is None: dmas = o.dma.all() else: dmas |= o.dma.all() print('dmas:', dmas) total_influx = 0 total_outflux = 0 total_total = 0 total_leak = 0 total_uncharg = 0 total_sale = 0 total_cxc = 0 total_cxc_percent = 0 total_leak_percent = 0 total_broken_pipe = 0 total_mnf = 0 total_back_leak = 0 huanbi = 0 tongbi = 0 mnf = 1.2 back_leak = 1.8 broken_pipe = 0 other_leak = 0 if len(dmas) == 0: ret = { "exceptionDetailMsg": "null", "msg": "没有dma分区信息", "obj": { "online": data, #reverse "influx": round(total_influx, 2), "total": round(total_total, 2), "leak": round(total_leak, 2), "uncharg": round(total_uncharg, 2), "sale": round(total_sale, 2), "cxc": round(total_cxc, 2), "cxc_percent": round(total_cxc_percent, 2), "broken_pipe": broken_pipe, "back_leak": back_leak, "mnf": mnf, "leak_percent": round(total_leak_percent, 2), "stationsstastic": sub_dma_list }, "success": 0 } return HttpResponse(json.dumps(ret)) month_list = generat_year_month() hdates = [f[-2:] for f in month_list] total_monthly_in = ZERO_monthly_dict(month_list) total_monthly_out = ZERO_monthly_dict(month_list) total_monthly_sale = ZERO_monthly_dict(month_list) total_monthly_uncount = ZERO_monthly_dict(month_list) def same_dict_value_add(dict1, dict2): ret = {} for k in dict1.keys(): ret[k] = dict1[k] + dict2[k] return ret for dma in dmas: # dma = dmas.first() dmareport = dma.dma_statistic(month_list) water_in = dmareport['water_in'] water_out = dmareport['water_out'] water_sale = dmareport['water_sale'] water_uncount = dmareport['water_uncount'] monthly_in = dmareport['monthly_in'] #进水表每月流量 monthly_out = dmareport['monthly_out'] #出水表每月流量 monthly_sale = dmareport['monthly_sale'] #贸易结算表每月流量 monthly_uncount = dmareport['monthly_uncount'] #未计费水表每月流量 total_monthly_in = same_dict_value_add(total_monthly_in, monthly_in) total_monthly_out = same_dict_value_add(total_monthly_out, monthly_out) total_monthly_sale = same_dict_value_add(total_monthly_sale, monthly_sale) total_monthly_uncount = same_dict_value_add(total_monthly_uncount, monthly_uncount) if len(monthly_in) == 0: monthly_in_flow = [0 for i in range(12)] else: monthly_in_flow = [monthly_in[k] for k in monthly_in.keys()] if len(monthly_out) == 0: monthly_out_flow = [0 for i in range(12)] else: monthly_out_flow = [monthly_out[k] for k in monthly_out.keys()] # 供水量 (分区内部进水表要减去自己内部出水表才等于这个分区的供水量) monthly_water = [ monthly_in_flow[i] - monthly_out_flow[i] for i in range(len(monthly_in_flow)) ] # 售水量 if len(monthly_sale) == 0: monthly_sale_flow = [0 for i in range(12)] else: monthly_sale_flow = [monthly_sale[k] for k in monthly_sale.keys()] # 未计费水量 if len(monthly_uncount) == 0: monthly_uncount_flow = [0 for i in range(12)] else: monthly_uncount_flow = [ monthly_uncount[k] for k in monthly_uncount.keys() ] # 漏损量 = 供水量-售水量-未计费水量 monthly_leak_flow = [ monthly_water[i] - monthly_sale_flow[i] - monthly_uncount_flow[i] for i in range(len(monthly_water)) ] influx = sum(monthly_in_flow) #进水总量 influx /= 10000 outflux = sum(monthly_out_flow) #出水总量 outflux /= 10000 total = influx - outflux #供水量 # total /= 10000 sale = sum(monthly_sale_flow) #售水量 sale /= 10000 uncharg = sum(monthly_uncount_flow) #未计费水量 uncharg /= 10000 leak = sum(monthly_leak_flow) #漏损量 leak /= 10000 cxc = total - sale #产销差 = 供水量-售水量 #产销差率 = (供水量-售水量)/月供水量*100% if total != 0: cxc_percent = (cxc / total) * 100 else: cxc_percent = 0 huanbi = 0 if total != 0: leak_percent = (leak * 100) / total else: leak_percent = 0 tongbi = 0 mnf = 1.2 back_leak = 1.8 broken_pipe = 0 other_leak = 0 total_influx += influx total_outflux += outflux total_total += total total_leak += leak total_uncharg += uncharg total_sale += sale total_cxc += cxc # print('total_influx',total_influx,water_in) # print('total_outflux',total_outflux,water_out) # print('total_total',total_total) # print('total_leak',total_leak) # print('total_uncharg',total_uncharg,water_uncount) # print('total_sale',total_sale,water_sale) # print('total_cxc',total_cxc) # dma 每个月统计 if treetype == "dma": for m in month_list: m_in = monthly_in[m] / 10000 m_out = monthly_out[m] / 10000 m_sale = monthly_sale[m] / 10000 m_uncount = monthly_uncount[m] / 10000 m_total = m_in - m_out #供水量=进水总量-出水总量 m_leak = m_total - m_sale - m_uncount # 漏损量 = 供水量-售水量-未计费水量 m_cxc = m_total - m_sale #产销差 = 供水量-售水量 if m_total == 0: m_cxc_percent = 0 else: m_cxc_percent = (m_cxc / m_total) * 100 #产销差率 = (供水量-售水量)/供水量*100% if m_total != 0: m_leak_percent = (m_leak * 100) / m_total else: m_leak_percent = 0 sub_dma_list.append({ "organ": dma.dma_name, # "influx":round(influx,2), "total": round(m_total, 2), #供水量 "sale": round(m_sale, 2), #售水量 "uncharg": round(m_uncount, 2), #未计费水量 "leak": round(m_leak, 2), #漏损量 "cxc": round(m_cxc, 2), #产销差 = 供水量-售水量 "cxc_percent": round(m_cxc_percent, 2), #产销差率 "huanbi": round(huanbi, 2), "leak_percent": round(m_leak_percent, 2), "tongbi": round(tongbi, 2), "mnf": round(mnf, 2), "back_leak": round(back_leak, 2), "other_leak": round(other_leak, 2), "statis_date": m, }) else: #记录每个dma分区的统计信息 sub_dma_list.append({ "organ": dma.dma_name, # "influx":round(influx,2), "total": round(total, 2), "sale": round(sale, 2), "uncharg": round(uncharg, 2), "leak": round(leak, 2), "cxc": round(cxc, 2), "cxc_percent": round(cxc_percent, 2), "huanbi": round(huanbi, 2), "leak_percent": round(leak_percent, 2), "tongbi": round(tongbi, 2), "mnf": round(mnf, 2), "back_leak": round(back_leak, 2), "other_leak": round(other_leak, 2), "statis_date": endTime, }) # 产销差 = (供水量-售水量)/月供水量*100% # echart data filling total_monthly_in_flow = [ total_monthly_in[k] for k in total_monthly_in.keys() ] total_monthly_out_flow = [ total_monthly_out[k] for k in total_monthly_out.keys() ] total_monthly_sale_flow = [ total_monthly_sale[k] for k in total_monthly_sale.keys() ] total_monthly_uncount_flow = [ total_monthly_uncount[k] for k in total_monthly_uncount.keys() ] total_monthly_water = [ total_monthly_in_flow[i] - total_monthly_out_flow[i] for i in range(len(total_monthly_in_flow)) ] total_monthly_leak_flow = [ total_monthly_water[i] - total_monthly_sale_flow[i] - total_monthly_uncount_flow[i] for i in range(len(total_monthly_water)) ] dma_name = '歙县自来水公司' for i in range(len(monthly_in_flow)): cp = 0 if monthly_water[i] != 0: cp = (total_monthly_water[i] - total_monthly_sale_flow[i]) / total_monthly_water[i] * 100 data.append({ "hdate": hdates[i], "dosage": total_monthly_sale_flow[i] / 10000, "assignmentName": dma_name, "color": "红色", "ratio": "null", "leak": total_monthly_leak_flow[i] / 10000, "uncharged": total_monthly_uncount_flow[i] / 10000, "cp_month": round(cp, 2) }) if total_total != 0: total_cxc = total_total - total_sale total_cxc_percent = (total_cxc / total_total) * 100 total_leak_percent = (total_leak * 100) / total_total ret = { "exceptionDetailMsg": "null", "msg": "null", "obj": { "online": data, #reverse "influx": round(total_influx, 2), "outflux": round(total_outflux, 2), "total": round(total_total, 2), "leak": round(total_leak, 2), "uncharg": round(total_uncharg, 2), "sale": round(total_sale, 2), "cxc": round(total_cxc, 2), "cxc_percent": round(total_cxc_percent, 2), "broken_pipe": broken_pipe, "back_leak": back_leak, "mnf": mnf, "leak_percent": round(total_leak_percent, 2), "stationsstastic": sub_dma_list[::-1] }, "success": 1 } return HttpResponse(json.dumps(ret))