import json from collections import Counter def gen_work_station_cross_data_list(cross_data_list,cross_info): res_list = [] for cross_data in cross_data_list: cross_id = cross_data['crossid'] jam_index = round(float(cross_data['jam_index']),2) if 'jam_index' in cross_data and cross_data['jam_index'] else 0.0 unbalance_index = round(float(cross_data['unbalance_index']),2) if 'unbalance_index' in cross_data and cross_data['unbalance_index'] else 0.0 flow = int(cross_data['flow']) if 'flow' in cross_data and cross_data['flow'] else 0 queue_len = int(cross_data['queue_len']) if 'queue_len' in cross_data and cross_data['queue_len'] else -1 if queue_len == -1: queue_len = '-' stop_times = round(float(cross_data['stop_times']),2) if 'stop_times' in cross_data and cross_data['stop_times'] else 0.0 delay_time = int(cross_data['delay_time']) if 'delay_time' in cross_data and cross_data['delay_time'] else 0.0 res_list.append({ 'id': cross_id, 'name': cross_info[cross_id]['name'] if cross_info.get(cross_id) else '', 'jam_index': jam_index, 'unbalance_index': unbalance_index, 'flow': flow, 'queue_len': queue_len, 'stop_times': stop_times, 'delay_time': delay_time }) return res_list def gen_work_station_artery_data_list(artery_data_list): res_list = [] for artery_data in artery_data_list: artery_id = artery_data['arteryid'] arteryname = (workstation_db_pool.query_artery_name(artery_id))[0]['name'] jam_index = float(artery_data['jam_index']) if 'jam_index' in artery_data and artery_data['jam_index'] else 0.0 speed = float(artery_data['speed']) if 'speed' in artery_data and artery_data['speed'] else 0.0 stop_times = float(artery_data['stop_times']) if 'stop_times' in artery_data and artery_data['stop_times'] else 0.0 un_stop_pass = float(artery_data['un_stop_pass']) if 'un_stop_pass' in artery_data and artery_data['un_stop_pass'] else 0.0 travel_time = float(artery_data['travel_time']) if 'travel_time' in artery_data and artery_data['travel_time'] else 0.0 delay_time = float(artery_data['delay_time']) if 'delay_time' in artery_data and artery_data['delay_time'] else 0.0 res_list.append({ 'id': artery_id, 'name': arteryname, 'jam_index': jam_index, 'speed': speed, 'stop_times': stop_times, 'un_stop_pass': un_stop_pass, 'travel_time': travel_time, 'delay_time': delay_time }) return res_list def cal_task_type_num(data_list): items_num = len(data_list) num1, num2, num3, num4, num5 = 0, 0, 0, 0, 0 for item in data_list: task_type = item['task_type'] if task_type == 1: num1 += 1 elif task_type == 2: num2 += 1 elif task_type == 3: num3 += 1 elif task_type == 4: num4 += 1 elif task_type == 5: num5 += 1 res = { 'list_len': items_num, 'detail': [ { 'label': '方案优化', 'value': num1 }, { 'label': '交办任务', 'value': num2 }, { 'label': '交通舆情', 'value': num3 }, { 'label': '档案优化', 'value': num4 }, { 'label': '效果巡检', 'value': num5 } ], 'task_list': data_list } return res def gen_task_statistics_res(last_month_should_done, last_month_done, last_month_should_done_but_not, need_todo, last_week_should_done, last_week_done, last_week_should_done_but_not, week_need_todo, yesterday_res, last_day_of_last_month_obj, formatted_last_week, yesterday_date): month_detail = gen_task_done_detail(last_month_done) week_detail = gen_task_done_detail(last_week_done) last_month = last_day_of_last_month_obj.strftime('%Y年%m月') yesterday_data = [] for row in yesterday_res: task_name = row['task_name'] op_info = json.loads(row['content']) progress_info, content = '', '' if op_info['operation'] == '编辑任务': if 'progress_info' in op_info['content'].keys(): progress_info = op_info['content']['progress_info'] if 'content' in op_info['content'].keys(): content = op_info['content']['content'] yesterday_data.append({ 'task_name': task_name, 'progress_info': progress_info, 'content': content }) res = { 'month': { 'date': '上月-' + last_month, 'data_list': [ { 'label': '已完成', 'value': len(last_month_done), 'detail_list': month_detail }, { 'label': '应完成逾期', 'value': last_month_should_done_but_not }, { 'label': '后续待完成', 'value': need_todo }, { 'label': '应完成', 'value': last_month_should_done } ] }, 'week': { 'date': '上周-' + formatted_last_week, 'data_list': [ { 'label': '已完成', 'value': len(last_week_done), 'detail_list': week_detail }, { 'label': '应完成逾期', 'value': last_week_should_done_but_not }, { 'label': '后续待完成', 'value': week_need_todo }, { 'label': '应完成', 'value': last_week_should_done } ] }, 'yesterday':{ 'date': '昨天-' + yesterday_date, 'data_list': yesterday_data } } return res def gen_task_done_detail(data_list): counter = Counter(data_list) task_type_dict = {1: '方案优化', 2: '交办任务', 3: '交通舆情', 4: '档案优化', 5: '效果巡检'} res = [] for element, count in counter.items(): res.append({'type': task_type_dict.get(element), 'count': count}) return res