def charts_result_view(request, model_id=0): server_id = int(request.GET.get('server_id', 0)) usm = UserStateManager(request) the_user = usm.get_the_user() model_id = int(model_id) chart_result = ChartsResult.objects.get(id=model_id) list_charts_def = get_charts(model_id, 1) if usm.current_userRole_is_root(): server_list = center_cache.get_server_list() else: server_list = center_cache.get_user_server_list(the_user) if not server_list.__contains__(server_id) and 0 == server_list.__len__(): return HttpResponse(u'没有权限') if 0 == server_id: server_id = server_list[0].id summary_list = [] situation_list = [] charts = [] date_trend = [] top = [] pie = [] for item in list_charts_def: chart_type = item[2] if chart_type == 'summary': url = '/charts/summary/%s?server_id=%s' % (item[0], server_id) summary_list.append({'src': url, 'chart_height': item[3]}) elif chart_type == 'situation': url = '/charts/situation/%s?server_id=%s' % (item[0], server_id) situation_list.append({'src': url, 'chart_height': item[3]}) elif chart_type == 'spline_time_chart': url = '/charts/spline/time/%s?server_id=%s' % (item[0], server_id) charts.append({'src': url, 'chart_height': item[3]}) elif chart_type == 'date_trend': url = '/charts/result/view/analyse/%s?server_id=%s' % (item[0], server_id) date_trend.append({'src': url, 'chart_height': item[3]}) elif chart_type == 'top': url = '/charts/result/view/pie/%s?server_id=%s&d_type=2' % ( item[0], server_id) top.append({'src': url, 'chart_height': item[3]}) elif chart_type == 'pie': url = '/charts/result/view/pie/%s?server_id=%s&d_type=1' % ( item[0], server_id) pie.append({'src': url, 'chart_height': item[3]}) pargs = {} pargs['server_list'] = server_list pargs['situation_list'] = situation_list pargs['summary_list'] = summary_list pargs['charts'] = charts pargs['server_id'] = server_id pargs['date_trend'] = date_trend pargs['chart_result'] = chart_result pargs['top'] = top pargs['pie'] = pie return render_to_response('charts/result_view.html', pargs)
def sync_log_index(request, server_id=0): if server_id == 0: server_id = int(request.GET.get('server_id', '0')) log_list = [] server_list = [] if server_id == 0: log_list = LogDefine.objects.filter(status=ENUM_LOG_STATUS.SAVE_CENTER) else: server_list = center_cache.get_server_list() log_list = LogDefine.objects.filter(status=ENUM_LOG_STATUS.NORMAL) return render_to_response('log/sync_log_index.html', { "server_id": server_id, "log_list": log_list, "server_list": server_list })
def get_Top300list(request): s = int(request.GET.get('s', 0)) server_id = int(request.GET.get('server_id', 0)) if 0 == server_id: server_id = int(request.POST.get('server_id', 0)) server_list = center_cache.get_server_list() data_list = [] if 1 == s: req_type = 507 req_params = 'req_type=%d&server_id=%d' % (int(req_type), int(server_id)) result = http_post(game_server_url.GM_SERVER_URL, req_params, timeout_param=10) print result result = json.loads(result) if result['code'] == 0: content_list = result['content'] for item in content_list: for key in item: data_item = {} date = datetime.datetime.fromtimestamp(float(key)) data_item['date'] = date.strftime('%Y-%m-%d') data_item['player_list'] = item.get(key, []) data_list.append(data_item) parg = {} parg['server_list'] = server_list parg['data_list'] = data_list return render_to_response('game/arena_list.html', parg)
def manage_question_list(request, user_id=0, status= -1): ajax = request.GET.get('ajax', False) if status == -1 : status = int(request.GET.get('status', '-1')) search_type = int(request.GET.get('search_type', '-1')) question_type = int(request.GET.get('question_type', '-1')) group_id = int(request.GET.get('group_id', -1)) kefu_name = request.GET.get('kefu_name', '') vip = -1 try: vip = int(request.GET.get('vip', -1)) except: vip = -1 the_user_id = int(request.session.get('userid', '0')) the_user = Admin.objects.using('read').get(id=the_user_id) usm = UserStateManager(request) list_group = Group.objects.using('read').all() server_id = int(request.GET.get('server_id', '0')) if server_id == 0: server_id = int(request.session.get("serverId", '0')) if user_id == 0 or user_id == '0': user_id = request.GET.get('user_id', '') status = int(status) page_num = int(request.GET.get('page_num', '1')) page_size = 20 total_page = 1 if page_num < 1: page_num = 1 kefu_list = Admin.objects.using('read').filter(role__id = 3) for kefuItem in kefu_list: kefuServerList = kefuItem.server.all() kefuServerIdList = "{\'server\':[" for kefuServerItem in kefuServerList: if kefuServerIdList == "{\'server\':[": kefuServerIdList += "{\'serverId\':'%s',\'serverName\':'%s'}" % (kefuServerItem.id, kefuServerItem.name) else: kefuServerIdList += ",{\'serverId\':'%s',\'serverName\':'%s'}" % (kefuServerItem.id, kefuServerItem.name) kefuServerIdList += "]}" #print kefuServerIdList kefuItem.kefuServerIdList = kefuServerIdList # *** 过滤 服务器列表 *** if usm.current_userRole_is_root(): list_server = center_cache.get_server_list() else: list_server = center_cache.get_user_server_list(the_user) group_server_list = [] if group_id > 0 : group = Group.objects.using('read').get(id = group_id) for item in group.server.all(): group_server_list.append(item) tmp_list = [] for item in group_server_list: if list_server.__contains__(item): tmp_list.append(item) list_server = tmp_list tmp_list = None group_server_list = None # *** 过滤 服务器列表 END *** theUserServerId = [] itemServerList = {} for item in list_server: itemServerList[item.id] = item.name if len(the_user.server.filter(id=item.id)) > 0: item.is_show = 1 theUserServerId.append(item.id) query = Q() if user_id != '': if search_type == 1: query = query & Q(post_user=user_id) elif search_type == 2: query = query & Q(question__contains = user_id) elif search_type == 3: query = query & Q(score=user_id) if -1 != vip: query = query & Q(post_user_id = vip) if -1 != question_type: query = query & Q(question_type=question_type) if server_id > 0: query = query & Q(server_id=server_id) else: if not usm.current_userRole_is_root(): server_id_list = [item.id for item in list_server] query = query & Q(server_id__in = server_id_list) if '' != kefu_name: query = query & Q(reply_user = kefu_name) if not usm.current_userRole_is_root(): channel_list = center_cache.get_user_channel_list(the_user) channel_id_list = [item.id for item in channel_list] channel_id_list.append(0) query = query & Q(channel_id__in = channel_id_list) if status != -1: query = query & Q(status=status) total_record = Question.objects.using('read').filter(query).count() list_record = Question.objects.using('read').filter(query)[(page_num - 1) * page_size:page_num * page_size] for item in list_record: if item.server_id > 0: item.serverName = itemServerList.get(item.server_id, '--') else: item.serverName = "--" if total_record > page_size: total_page = total_record / page_size if total_record % page_size > 0: total_page += 1 parg = {} parg["server_id"] = server_id parg["list_server"] = list_server parg["user_id"] = user_id parg["usm"] = usm parg["kefu_list"] = kefu_list parg["the_user_id"] = the_user_id parg["list_record"] = list_record parg["search_type"] = search_type parg["status"] = status parg["list_group"] = list_group parg["kefu_name"] = kefu_name parg["page_num"] = page_num parg["page_size"] = page_size parg["total_record"] = total_record parg["question_type"] = question_type parg["group_id"] = group_id if ajax: return render_to_response('server/question_list_block.html', parg) return render_to_response('server/question_list.html', parg)
def query_view(request, query_id=0): query_id = int(query_id) if 0 == query_id: query_id = int(request.GET.get('id', request.POST.get('id', 0))) s_list = request.POST.getlist('s') if 0 == s_list.__len__(): s_list = request.GET.getlist('s') page_num = int(request.GET.get('page_num', '1')) sdate = request.GET.get('sdate', '') edate = request.GET.get('edate', '') query_channel = request.GET.getlist('channel_id') group_id = int(request.GET.get('group_id', '0')) ajax = request.GET.get('ajax', False) exprot = int(request.GET.get('exprot', '0')) close_export = int(request.GET.get('close_export', '0')) clear_export_old_file = int(request.GET.get('clear_export_old_file', '0')) is_post_back = request.GET.get('post_back', False) exprot_file_key = request.GET.get('exprot_file_key', '') exprot_file_key = exprot_file_key.strip() more_serv_exprot = False if exprot_file_key != '': more_serv_exprot = True begin_exec_time = datetime.datetime.now() page_size = 50 mc = MemcachedUtil() if exprot > 0: page_size = 500 list_group = [] usm = UserStateManager(request) the_user = usm.get_the_user() if query_id > 0: the_query = log_cache.get_query(query_id, mc) log_define = log_cache.get_logDefine(the_query.log_type, mc) #是否在中央服务器的查询 is_centerQuery = False if log.the_log_in_center(log_define): #check_user is_centerQuery = True is_select_server = False else: is_centerQuery = False is_select_server = True has_sql = False if the_query.sql != '': has_sql = True if not has_sql or -1 != the_query.sql.find('{{server_id}}'): is_select_server = True list_query = log_cache.get_query_list_by_logType(the_query.log_type, mc) if is_select_server: list_group = center_cache.get_group_list() if group_id != 0 and usm.current_userRole_is_root(): list_server = center_cache.get_server_list(group_id, mc) else: if usm.current_userRole_is_root(): list_server = center_cache.get_server_list(mc_util=mc) else: list_server = center_cache.get_user_server_list(the_user) #取出字段定义 list_field = log_cache.get_fielddef_list_by_logType(the_query.log_type, mc) server_id = int(request.GET.get('server_id', '0')) if is_centerQuery and s_list.__len__() == 0: s_list = [str(item.id) for item in list_server] if not usm.current_userRole_is_root() and s_list.__len__() == 0: return HttpResponse(u"非法操作") elif server_id == 0 and len(list_server) > 0: server_id = list_server[0].id if server_id == 0: return HttpResponse(u"非法操作") if usm.current_userRole_is_root(): list_channel = center_cache.get_channel_list(mc) else: list_channel = center_cache.get_user_channel_list(the_user, mc) #是否在页面上显示查询channel allowChannel = False if query_channel.__len__() == 0 and not usm.current_userRole_is_root(): query_channel = [] for item in list_channel: query_channel.append(str(item.id)) if (not has_sql and list_channel.__len__() > 0) or ( -1 != the_query.sql.find('{{qchannel')): allowChannel = True for item1 in list_channel: if query_channel.__len__() > 0: if str(item1.id) in query_channel: item1.is_show = 1 else: item1.is_show = 0 field_value = request.GET.get('field_value', '') field_value = filter_inject_sql(field_value) field_id = int(request.GET.get('field_id', '0')) if has_sql: field_value = request.GET.getlist('field_value') err_msg = '' pager_str = 'limit %s,%s' % ((page_num - 1) * page_size, page_size) fields = the_query.select.split(',') query_date = '' now = datetime.datetime.now() if not is_post_back: #如果第一次进入该页面默认时间范围是昨天数据 if sdate == '': sdate = (now - datetime.timedelta(days=1)).strftime('%Y-%m-%d 00:00:00') if edate == '': edate = now.strftime('%Y-%m-%d 00:00:00') if not has_sql or the_query.sql.find('{{keyword}}') != -1: is_search = True else: is_search = False has_sdate = True has_edate = True if the_query.sql != '': if -1 == the_query.sql.find('{{sdate}}'): has_sdate = False if -1 == the_query.sql.find('{{edate}}'): has_edate = False if -1 != the_query.sql.find('{{qdate}}'): has_sdate = True has_edate = True if -1 == the_query.sql.find('{{qchannel'): allowChannel = False keywords = [] lost_param = False if has_sql: sql = the_query.sql r_keyword_name = '(@keywords.+)' keyword_name_ary = re.findall(r_keyword_name, sql, re.I) if keyword_name_ary.__len__() != 0: keyword_name = keyword_name_ary[0] names_str = keyword_name.split(':')[1] names = names_str.split(',') for i in range(names.__len__()): name = names[i] value = '' if field_value.__len__() > i: value = field_value[i] keywords.append({"name": name, "value": value}) the_query.sql = re.sub(keyword_name, '', sql, re.I) else: k_len = the_query.sql.lower().count('{{keyword}}') for i in range(k_len): value = '' if field_value.__len__() > i: value = field_value[i] if value == '': lost_param = True keywords.append({"name": '输入框%s' % i, "value": value}) #print keywords.__len__() list_data = [] total_record = 0 parg = {} parg['keywords'] = keywords parg['has_sql'] = has_sql parg['is_centerQuery'] = is_centerQuery parg['has_sdate'] = has_sdate parg['has_edate'] = has_edate parg["allowChannel"] = allowChannel parg["the_query"] = the_query parg["usm"] = usm parg["list_query"] = list_query parg["is_select_server"] = is_select_server parg["list_group"] = list_group parg["list_server"] = list_server parg["list_channel"] = list_channel parg["s_list"] = s_list parg["server_id"] = server_id parg["query_id"] = query_id parg["is_search"] = is_search parg["group_id"] = group_id parg["field_id"] = field_id parg["field_value"] = field_value parg["list_field"] = list_field parg["sdate"] = sdate parg["edate"] = edate parg["fields"] = fields parg["page_num"] = page_num parg["page_size"] = page_size parg["total_record"] = total_record if not is_post_back and not ajax: #如果没有点击查询按钮并不是ajax提交则不查询数据(第一次进入页面不查询数据) ——zhenwei 2012-10-22 return render_to_response('log/query_view.html', parg) if is_select_server and not is_centerQuery: if server_id > 0: try: conn = getConn(server_id) except: err_msg = '数据库链接出错!' else: conn = connections['read'] if err_msg != '': return render_to_response('feedback.html', locals()) try: if sdate != '': sdate = datetime.datetime.strptime( sdate, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S') query_date = ' a.log_time>=\'%s\'' % sdate if edate != '': if query_date != '': query_date += ' and ' edate = datetime.datetime.strptime( edate, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S') query_date += ' a.log_time<=\'%s\'' % edate except: sdate = '' edate = '' #是否有channel 查询条件 channel_condition = query_channel.__len__() > 0 query_key = '' if not has_sql: query_field = the_query.select query_where = the_query.where query_order = the_query.order query_group = the_query.group field_name = '' for field_item in list_field: query_field = query_field.replace(field_item.name, field_item.field_name) query_where = query_where.replace(field_item.name, field_item.field_name) query_order = query_order.replace(field_item.name, field_item.field_name) query_group = query_group.replace(field_item.name, field_item.field_name) if field_id == field_item.id: field_name = field_item.field_name #处理字段值查询 if field_name != '': the_values = ValueDefine.objects.filter(field_id=field_id, value=field_value) if len(the_values) > 0: field_value = the_values[0].value_id if query_where != '': query_where += ' and ' query_where += u'a.%s=\'%s\'' % (field_name, field_value) if query_date != '': if query_where != '': query_where += ' and ' query_where += query_date if channel_condition: if query_where != '': query_where += ' and ' query_where += 'a.log_channel in(%s)' % (','.join(query_channel)) if query_where != '': query_where = 'where %s' % query_where if query_order != '': query_order = ' order by a.%s' % query_order if the_query.order_type == 1: query_order += ' desc' count_sql = 'select count(0) from log_%s a %s' % (log_define.key, query_where) query_sql = 'select %s from log_%s a %s %s %s' % ( query_field, log_define.key, query_where, query_order, pager_str) query_key = md5('%s_%s' % (query_sql, server_id)) if exprot_file_key == '': exprot_file_key = md5('%s_%s' % (query_sql, server_id)) #print(query_sql) else: query_sql = the_query.sql.replace('\r\n\t', ' ').replace('\r\n', ' ') if is_centerQuery and s_list.__len__() > 0: server_list_str = ' IN (%s) ' % (','.join(s_list)) query_sql = re.sub('=[\s]*{{server_id}}', server_list_str, query_sql) elif server_id > 0: query_sql = query_sql.replace('{{server_id}}', str(server_id)) if query_date != '': query_sql = query_sql.replace("{{qdate}}", query_date) query_sql = query_sql.replace('{{sdate}}', '%s' % sdate).replace( '{{edate}}', '%s' % edate) #支持自定对非log表的日期支持 -zhenwei 2012-10-25 else: query_sql = query_sql.replace('and {{qdate}}', '').replace('where {{qdate}}', '') query_sql = query_sql.replace( '\'{{sdate}}\'', 'DATE(\'2001-01-01\')').replace( '\'{{edate}}\'', 'NOW()') #支持自定对非log表的日期支持 -zhenwei 2012-10-25 if lost_param: err_msg = '请输入查询条件' return render_to_response('feedback.html', {"err_msg": err_msg}) # if field_value != '': # query_sql = query_sql.replace("{{keyword}}", field_value) # else: # if -1 != query_sql.find('{{keyword}}'): # err_msg = '请输入查询条件' # return render_to_response('feedback.html', {"err_msg":err_msg}) if channel_condition: query_sql = query_sql.replace( '{{qchannel}}', 'log_channel in(%s)' % (','.join(query_channel))) else: query_sql = query_sql.replace('and {{qchannel}}', '').replace('where {{qchannel}}', '') if channel_condition: query_sql = query_sql.replace( '{{qchannela}}', 'a.log_channel in(%s)' % (','.join(query_channel))) else: query_sql = query_sql.replace('and {{qchannela}}', '').replace('where {{qchannela}}', '') if channel_condition: query_sql = query_sql.replace( '{{qchannelb}}', 'b.log_channel in(%s)' % (','.join(query_channel))) else: query_sql = query_sql.replace('and {{qchannelb}}', '').replace('where {{qchannelb}}', '') if channel_condition: query_sql = query_sql.replace( '{{qchannelid}}', 'channel_id in (%s)' % (','.join(query_channel))) query_sql = filter_keyword(query_sql, field_value) count_sql = 'select count(0) from (%s) newTable' % query_sql if exprot_file_key == '': exprot_file_key = md5('%s_%s' % (query_sql, server_id)) if query_sql.find('limit') == -1: query_sql = '%s %s' % (query_sql, pager_str) query_key = md5('%s_%s' % (query_sql, server_id)) parg['has_sdate'] = has_sdate parg['has_edate'] = has_edate print count_sql print query_sql #raise Exception, count_sql #desc = cursor.description cursor = conn.cursor() # update log_create_role channelKey_id_dic = {} query_memcache = mc if the_query.cache_validate != None and 0 != the_query.cache_validate: query_memcache = MemcachedUtil(valid_date=the_query.cache_validate) for item1 in list_channel: channelKey_id_dic[item1.key] = int(item1.id) try: count_query_key = md5('%s_%s' % (count_sql, server_id)) total_record = log_cache.get_query_count(count_sql, count_query_key, cursor, query_memcache) except Exception, e: raise Exception, e print('query_view error:', e) return render_to_response('feedback.html', {"err_msg": "查询数据时出错"})
def get_result(content_json, reason_phrase, gm_def, result_code, usm): result = None result_def = json.loads(gm_def.result_define) if gm_def.result_type == 'json_result': result = {} result['content_json'] = json.dumps(content_json) elif gm_def.result_type == 'msg': result = result_def.get('msg_values', {}) #print '-===============================' #print result #print result.get(str(content_json)) result = result.get(str(content_json), u'错误,错误码:%s' % content_json) elif gm_def.result_type == 'form': list_infos = [] form_items = result_def['form_items'] form_items_list = [] for key, value in form_items.items(): #print key form_items_list.append({'key': key, 'json': value}) form_items = form_items_list form_items.sort(cmp=lambda x, y: cmp(x['json'].get('order', 0), y[ 'json'].get('order', 0))) for _item in form_items: key = _item.get('key') def_item = _item.get('json') allow_empty = def_item.get('allow_empty', 0) has_value = False content_json_type = type(content_json) if dict == content_json_type: for r_key, value in content_json.items(): if key != r_key: continue has_value = True item = FormItem(key, False, False, value, {}, 'text') item.name = def_item.get('name', key) item.is_modify = def_item.get('is_modify', False) item.is_textarea = def_item.get('is_textarea', False) item.is_append = def_item.get('is_append', False) item.attr_type = def_item.get('type', 'text') item.enum = def_item.get('enum', {}) list_infos.append(item) elif list == content_json_type: item = FormItem(key, False, False, value, {}, 'text') item.name = def_item.get('name', key) item.is_modify = def_item.get('is_modify', False) item.is_textarea = def_item.get('is_textarea', False) item.is_append = def_item.get('is_append', False) item.attr_type = def_item.get('type', 'text') item.enum = def_item.get('enum', {}) item.value = json.dumps(content_json) list_infos.append(item) if not has_value and allow_empty: item = FormItem(key, False, False, '', {}, 'text') item.name = def_item.get('name', key) item.is_modify = def_item.get('is_modify', False) item.is_textarea = def_item.get('is_textarea', False) item.is_append = def_item.get('is_append', False) item.attr_type = def_item.get('type', 'text') item.enum = def_item.get('enum', {}) list_infos.append(item) result = {} result['form_action'] = result_def.get('form_action', '') result['form_type'] = result_def.get('form_type', '') result['form_key'] = result_def.get('form_key', '') server_list_chkbox = result_def.get('server_list_chkbox', False) if server_list_chkbox: group_server_dic = {} if usm.current_userRole_is_root(): group_list = center_cache.get_group_list() for item in group_list: group_server_dic[item.name] = center_cache.get_server_list( item.id) else: group_server_dic['服务器'] = center_cache.get_user_server_list( usm.get_the_user()) result['group_server_dic'] = group_server_dic result['server_list_chkbox'] = server_list_chkbox result['list_infos'] = list_infos elif gm_def.result_type == 'list': list_items_def = result_def.get('list_items', {}) list_action = result_def.get('action', []) #排序 #先转数组 field_array = [] for key, value in list_items_def.items(): field_array.append({"key": key, "json": value}) #排序 try: field_array.sort(cmp=lambda x, y: cmp(x['json'].get('order', 0), y[ 'json'].get('order', 0))) except Exception, ex: print ex list_data = [] if list != type(content_json): try: print content_json content_json = json.loads(content_json) except Exception, ex: print 'get_reult type is list json loads error:' print ex content_json = []
def player_list(request, server_id=0): page_size = 30 page_num = int(request.GET.get("page_num", "1")) is_block = int(request.GET.get("block", 0)) group_id = int(request.GET.get("group_id", 0)) post_back = int(request.GET.get('post_back', '0')) list_group = center_cache.get_group_list() if (page_num < 1): page_num = 1 usm = UserStateManager(request) the_user = usm.get_the_user() list_channel = center_cache.get_channel_list() itemChannelList = {} for item in list_channel: itemChannelList[item.id] = item.name list_group_server = [] if group_id != 0: list_group_server = center_cache.get_group_server_list(group_id) if usm.current_userRole_is_root(): list_server = center_cache.get_server_list() else: list_server = center_cache.get_user_server_list(the_user) tmp_list_server = [] if 0 != list_group_server.__len__(): for item in list_group_server: if list_server.__contains__(item): tmp_list_server.append(item) list_server = tmp_list_server itemServerList = {} for item in list_server: itemServerList[item.id] = item.name player_key = request.GET.get('key', '') key_type = request.GET.get('key_type', '0') user_type = int(request.GET.get('user_type', '-1')) channel_id = int(request.session.get('channelId', '0')) server_id = int(request.GET.get("server_id", "0")) if server_id <= 0: server_id = int(request.session.get("serverId", "0")) if server_id <= 0 and len(list_server) > 0: server_id = list_server[0].id #账号状态 status_condition = 0 if is_block == 1: status_condition = -1 total_record = 0 player_list = [] player_list1 = [] if 0 != post_back and server_id > 0: conn = getConn(server_id) cursor = conn.cursor() query = [] query.append("status=%d" % status_condition) if channel_id > 0: query.append('channel_id=%d' % channel_id) if player_key != "": if key_type == '0': query.append('player_id=\'%s\'' % player_key) elif key_type == '1': query.append('player_name like \'%s%%\'' % player_key.encode('utf-8')) elif key_type == '2': query.append('link_key=\'%s\'' % player_key) elif key_type == '3': query.append('mobile_key=\'%s\'' % player_key) if user_type > -1: query.append('user_type=%d' % player_key) if not usm.current_userRole_is_root(): channel_list = center_cache.get_user_channel_list(the_user) channel_id_list_query = ' channel_id in (%s) ' % ','.join( [str(item.id) for item in channel_list]) query.append(channel_id_list_query) if len(query) > 0: sql1 = 'select count(1) from player_%d where %s' % ( server_id, ' and '.join(query)) sql2 = 'select player_id,player_name,channel_id,user_type,link_key,login_num,mobile_key,last_time,create_time,status from player_%d where %s order by id desc limit %d,%d' % ( server_id, ' and '.join(query), (page_num - 1) * page_size, page_num * page_size) else: sql1 = 'select count(1) from player_%d' % server_id sql2 = 'select player_id,player_name,channel_id,user_type,link_key,login_num,mobile_key,last_time,create_time,status from player_%d order by id desc limit %d,%d' % ( server_id, (page_num - 1) * page_size, page_num * page_size) print(sql1, sql2) cursor.execute(sql1) count_list = cursor.fetchone() total_record = int(count_list[0]) if total_record > 0: cursor.execute(sql2) player_list1 = cursor.fetchall() user_type_name = { 0: '游爱', 1: '当乐', 2: 'UC', 3: '91', 4: '云游', 5: '飞流', 6: '乐逗', 8: '小虎', 9: '4399', 10: 'facebook', 11: 'qq' } for item in player_list1: item = list(item) item[2] = itemChannelList.get(int(item[2]), item[2]) item[3] = user_type_name.get(int(item[3]), item[3]) player_list.append(item) cursor.close() parg = {} parg["server_id"] = server_id parg["list_group"] = list_group parg["list_server"] = list_server parg["player_key"] = player_key parg["server_id"] = server_id parg["player_list"] = player_list parg["is_block"] = is_block parg["usm"] = usm parg["page_num"] = page_num parg["page_size"] = page_size parg["total_record"] = total_record return render_to_response('player/player_list.html', parg)
def player_silver_detail(request): server_id = request.POST.get("server_id", "") query_incoming = request.POST.getlist("incoming_type") query_outcoming = request.POST.getlist("outcoming_type") sdate = request.POST.get("sdate", "") edate = request.POST.get("edate", "") page_num = int(request.GET.get('page_num', '1')) user_id = request.POST.get("user_id", "") user_name = request.POST.get("user_name", "") err_msg = '' page_size = 30 list_server = center_cache.get_server_list() if server_id == "": server_id = "0" server_id = int(server_id) if server_id < 1 and len(list_server) > 0: server_id = list_server[0].id if user_id == "": user_id = "0" user_id = int(user_id) query_where = " and 1=1" try: if sdate != "": sdate = datetime.datetime.strptime( sdate, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") query_where += " and a.log_time>='%s'" % sdate if edate != "": edate = datetime.datetime.strptime( edate, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") query_where += " and a.log_time<='%s'" % edate except: sdate = "" edate = "" logDefine = LogDefine.objects.get(key='silver') fieldDefine = FieldDefine.objects.get(log_type=logDefine.id, name=u'操作项目') incoming_list = ValueDefine.objects.filter(field_id=fieldDefine.id, value_id__lt=10000) silverType_Dic = {} for inItem in incoming_list: silverType_Dic[inItem.value_id] = inItem.value if len(query_incoming) > 0: if str(inItem.value_id) in query_incoming: inItem.is_show = True else: inItem.is_show = False else: if request.method == "GET": inItem.is_show = True else: inItem.is_show = False outcoming_list = ValueDefine.objects.filter(field_id=fieldDefine.id, value_id__gte=10000) for outItem in outcoming_list: silverType_Dic[outItem.value_id] = outItem.value if len(query_outcoming) > 0: if str(outItem.value_id) in query_outcoming: outItem.is_show = True else: outItem.is_show = False else: if request.method == "GET": outItem.is_show = True else: outItem.is_show = False query_type = [] if len(query_incoming) > 0: query_type = query_type + query_incoming if len(query_outcoming) > 0: query_type = query_type + query_outcoming # # if len(query_type)>0: # query_where+=" and log_data in (%s)"(','.join(query_type)) server_in = "" if len(query_type) > 0: server_in = "(" for typeItem in query_type: if server_in == "(": server_in += "%s" % typeItem else: server_in += ",%s" % typeItem server_in += ")" if server_in != "" and len(query_type) != (len(incoming_list) + len(outcoming_list)): query_where += " and a.log_data in %s" % server_in # if user_name!="": # query_where+=" and b.f1=%s"%user_name if server_id > 0: try: conn = getConn(server_id) except: err_msg = '数据库链接出错!' cursor = conn.cursor() if user_id < 1: if user_name != "": query_role_sql = "SELECT log_user FROM log_create_role WHERE f1='%s'" % user_name cursor.execute(query_role_sql) role_object = cursor.fetchone() if role_object != None: user_id = int(role_object[0]) if user_id > 0: query_where += " and a.log_user=%s" % user_id query_sql = "SELECT a.log_user,b.f1,a.log_time,a.log_result,a.f2,a.log_data,a.f3 FROM log_silver a,log_create_role b WHERE a.log_user=b.log_user %s order by a.id desc" % query_where query_count_sql = "SELECT count(1) FROM log_silver a where 1=1 %s" % query_where cursor.execute(query_count_sql) total_record = int(cursor.fetchone()[0]) pager_str = 'limit %s,%s' % ((page_num - 1) * page_size, page_size) query_sql = query_sql + ' ' + pager_str print query_count_sql print query_sql cursor.execute(query_sql) silver_list_temp = cursor.fetchall() fieldDefine_f3 = FieldDefine.objects.get(log_type=logDefine.id, name=u'备注') valueDefine_f3_list = ValueDefine.objects.filter( field_id=fieldDefine_f3.id) f3_dic = {} for vdItem in valueDefine_f3_list: f3_dic[vdItem.value_id] = vdItem.value silver_list = [] for silverItem in silver_list_temp: role_name = "" if silverItem[5] != "": role_name = silverType_Dic[int(silverItem[5])] detail_name = "" if silverItem[6] != "": detail_name = f3_dic[int(silverItem[6])] silver_list.append((silverItem[0], silverItem[1], silverItem[2], silverItem[3], silverItem[4], silverItem[5], role_name, silverItem[6], detail_name)) cursor.close() if user_id < 1: user_id = "" parg = {} parg["list_server"] = list_server parg["server_id"] = server_id parg["incoming_list"] = incoming_list parg["outcoming_list"] = outcoming_list parg["sdate"] = sdate parg["edate"] = edate parg["user_id"] = user_id parg["user_name"] = user_name parg["silver_list"] = silver_list parg["page_num"] = page_num parg["page_size"] = page_size parg["total_record"] = total_record return render_to_response('player/player_silver_detail.html', parg)
def charts_result_pie(request, query_id=0, server_channel='server', charts_type='pie', title='饼图'): title = title query_id = int(query_id) model = ChartsDefine.objects.get(id=query_id) list_statistic_sort = get_statistic_in_query(int( model.query_result_id)) #获取根据关联表ip排序的数据 join_results = [] item_results = [] now = datetime.datetime.now() sdate = request.GET.get('sdate', '') edate = request.GET.get('edate', '') query_item = int(request.GET.get('query_item', '0')) query_type = int(request.GET.get('query_type', '0')) request_server_list = request.GET.getlist('server_id') request_channel_list = request.GET.getlist('channel_id') display_type = int(request.GET.get('d_type', 1)) if request_server_list.__len__() >= 100: return HttpResponse(u'非法请求') if request_channel_list.__len__() >= 100: return HttpResponse(u'非法请求') if not sdate and not edate: sdate = now.strftime('%Y-%m-01 00:00:00') edate = now.strftime('%Y-%m-%d 23:59:59') query_date = ' AND r.`create_time` >= \'%s\' AND r.`create_time` <= \'%s\'' % ( sdate, edate) usm = UserStateManager(request) the_user = usm.get_the_user() server_list = [] channel_list = [] if usm.current_userRole_is_root(): server_list = center_cache.get_server_list() channel_list = center_cache.get_channel_list() else: server_list = center_cache.get_user_server_list(the_user) channel_list = center_cache.get_user_channel_list(the_user) if not usm.current_userRole_is_root: if 0 == server_list.__len__(): return HttpResponse(u'没有权限') if 0 == channel_list.__len__(): return HttpResponse(u'没有权限') #限制服务器和渠道选择的数量 limit_server_count = 10 limit_channel_count = 10 tmp_index = 0 if 0 == request_server_list.__len__(): request_server_list = [] for item in server_list: if tmp_index >= limit_server_count: break request_server_list.append(str(item.id)) tmp_index = tmp_index + 1 if 0 == request_channel_list.__len__(): tmp_index = 0 for item in channel_list: if tmp_index >= limit_channel_count: break request_channel_list.append(str(item.id)) tmp_index = tmp_index + 1 has_server_count = 0 for item in server_list: for server_id in request_server_list: server_id = int(server_id) if item.id == server_id: has_server_count = has_server_count + 1 item.selected = True has_channel_count = 0 for item in channel_list: for channel_id in request_channel_list: channel_id = int(channel_id) if item.id == channel_id: has_channel_count = has_channel_count + 1 item.selected = True if (has_server_count != request_server_list.__len__() or has_server_count != request_channel_list.__len__() ) and not usm.current_userRole_is_root: return HttpResponse(u'没有权限') query_server_id = ' AND r.server_id IN (%s)' % ','.join( request_server_list) query_channel_id = ' AND r.channel_id IN (%s)' % ','.join( request_channel_list) q_str = '' limit_count = 0 for item in list_statistic_sort: join_results.append(int(item[0])) #id item_results.append([item[0], item[1]]) cursor = connection.cursor() if query_type == 0: select_str = 's.`name`' q_str = query_server_id limit_count = limit_server_count else: select_str = 'c.`name`' q_str = query_channel_id limit_count = limit_channel_count if query_item != 0: select_str += ',sum(case when r.`statistic_id`=%d then result else 0 end) item' % ( query_item) else: select_str += ',sum(case when r.`statistic_id`=%d then result else 0 end) item' % ( join_results[0]) query_item = int(join_results[0]) query_item_name = Statistic.objects.values('name').get(id=query_item) if query_type == 0: query_sql = 'select %s from result r JOIN `servers` s ON r.`server_id` = s.`id` where r.`statistic_id` = %d %s %s GROUP BY r.`server_id` ORDER BY `item` DESC LIMIT %s' % ( select_str, query_item, query_date, q_str, limit_count) else: query_sql = 'select %s from result r JOIN `channel` c ON r.`channel_id` = c.`id` where r.`statistic_id` = %d %s %s GROUP BY r.`channel_id` ORDER BY `item` DESC LIMIT %s' % ( select_str, query_item, query_date, q_str, limit_count) count_sql = 'select count(0) result from (%s) newTable WHERE 1' % ( query_sql) cursor.execute(count_sql) total_record = int(cursor.fetchone()[0]) print query_sql list_record = [] if total_record > 0: cursor.execute(query_sql) list_record = cursor.fetchall() parg = {} template = '' if 1 == display_type: template = 'charts/result_pie.html' data = [] total = 0 if total_record > 0: for val in list_record: total += int(val[1]) print 'total', total for item in list_record: item = list(item) item[0] = item[0] data.append(['%s' % item[0], float(item[1]) / total]) data = str(data).replace('(', '[').replace(')', ']').replace('L', '') data = str(data).replace('u\'', '\'') else: charts_type = 'bar' template = 'charts/result_top.html' title = "TOP10" data = [] xAxis = [] if total_record > 0: for item in list_record: item = list(item) xAxis.append('%s' % item[0]) data.append(int(item[1])) xAxis = str(xAxis).replace('(', '[').replace(')', ']').replace('L', '') xAxis = str(xAxis).replace('u\'', '\'') parg["xAxis"] = xAxis parg["server_list"] = server_list parg["channel_list"] = channel_list parg["title"] = title parg["item_results"] = item_results parg["data"] = data parg["query_item"] = query_item parg["query_item_name"] = query_item_name parg["charts_type"] = charts_type parg["query_id"] = query_id parg["sdate"] = sdate[0:10] parg["edate"] = edate[0:10] parg["query_type"] = query_type parg["d_type"] = display_type return render_to_response(template, parg)
result_str = json.dumps({ 'is_error': is_error, 'is_reload': tmp_value, 'date': str(last_exec_time) }) return HttpResponse(result_str) #print 'views#log#statistic.py is_reload:::', is_reload if not is_reload: last_exec_time = datetime.datetime.strptime(r_date, '%Y-%m-%d %H:%M:%S') if not is_ajax_batch_execute and not the_statistic_in_center( statistic_id): server_list = center_cache.get_server_list() server_list = sorted(server_list, key=lambda item: item.id) server_flag = False server_list_last_index = server_list.__len__() - 1 if 0 == server_id and server_list_last_index >= 0: server_flag = True for index, item in enumerate(server_list): if server_flag: server_id = item.id break if item.id == server_id and server_list_last_index != index: server_flag = True