def to_json_dict(self): j = { 'status': self.status, 'content_type': self.content_type, 'headers_id': text_type(self.headers.grid_id), 'body_id': text_type(self.body.grid_id) } if check_text_content_type(self.content_type): j['text_type'] = True else: j['text_type'] = False return j
def check_text_content_type(content_type): """ 检查content_type 是否是文本类型 :param content_type: :return: """ content_type = text_type(content_type).lower() text_content_type = [ 'text', 'application/json', 'application/x-javascript', 'application/x-www-form-urlencoded' ] return any(map(content_type.startswith, text_content_type))
def to_json_dict(self): j = { 'id': text_type(self.id), 'forward_url': self.forward_url, 'timestamp': datetime_to_timestamp(self.accessed_at), 'accessed_at': datetime_to_str(self.accessed_at, '%Y-%m-%d %H:%M:%S'), 'elapsed': self.elapsed, 'result_code': self.result_code, 'result_msg': self.result_msg, 'ip': self.ip, 'request': self.request.to_json_dict(), 'response': self.response.to_json_dict(), 'client': self.client.to_json_dict(), 'endpoint': self.endpoint.to_json_dict() } return j
def _render_time_frame_data(unit, time_frame, name_map, data, data_type, count_list_dict): count_data = [] if unit == 'hour': dk_format = '%Y-%m-%d %H' else: dk_format = '%Y-%m-%d' if data_type == 'total': count_dict = {} for t in data: k = t.date.strftime(dk_format) count_dict[k] = t.count temp_data = [] for t in time_frame: temp_data.append(count_dict.get(t, 0)) name = name_map.get('total', '全部应用') count_data.append([name, temp_data]) elif data_type in ['client', 'endpoint']: # count_list_dict = {} k_field = data_type + '_id' for t in data: k = t['_id'][k_field] dk = t['_id']['date'].strftime(dk_format) if k in count_list_dict: count_list_dict[k][dk] = t['count'] else: count_list_dict[k] = {dk: t['count']} for k, v in count_list_dict.items(): temp_data = [] for t in time_frame: temp_data.append(v.get(t, 0)) name = name_map[data_type].get(text_type(k), text_type(k)) count_data.append([name, temp_data]) elif data_type == 'client_endpoint': # count_list_dict = {} for t in data: k = '%s/%s' % (t['_id']['client_id'], t['_id']['endpoint_id']) dk = t['_id']['date'].strftime(dk_format) if k in count_list_dict: count_list_dict[k][dk] = t['count'] else: count_list_dict[k] = {dk: t['count']} for k, v in count_list_dict.items(): temp_data = [] for t in time_frame: temp_data.append(v.get(t, 0)) name = name_map[data_type].get(text_type(k), text_type(k)) count_data.append([name, temp_data]) elif data_type == 'result_code': # count_list_dict = {} for t in data: k = t['_id']['code'] dk = t['_id']['date'].strftime(dk_format) if k in count_list_dict: count_list_dict[k][dk] = t['count'] else: count_list_dict[k] = {dk: t['count']} for k, v in count_list_dict.items(): temp_data = [] for t in time_frame: temp_data.append(v.get(t, 0)) name = name_map[data_type].get(text_type(k), text_type(k)) count_data.append([name, temp_data]) return count_data