def sqlworkflow(request): """SQL上线工单列表页面""" user = request.user # 过滤筛选项的数据 filter_dict = dict() # 管理员,可查看所有工单 if user.is_superuser or user.has_perm('sql.audit_user'): pass # 非管理员,拥有审核权限、资源组粒度执行权限的,可以查看组内所有工单 elif user.has_perm('sql.sql_review') or user.has_perm( 'sql.sql_execute_for_resource_group'): # 先获取用户所在资源组列表 group_list = user_groups(user) group_ids = [group.group_id for group in group_list] filter_dict['group_id__in'] = group_ids # 其他人只能查看自己提交的工单 else: filter_dict['engineer'] = user.username instance_id = SqlWorkflow.objects.filter( **filter_dict).values('instance_id').distinct() instance = Instance.objects.filter(pk__in=instance_id).order_by( Convert('instance_name', 'gbk').asc()) resource_group_id = SqlWorkflow.objects.filter( **filter_dict).values('group_id').distinct() resource_group = ResourceGroup.objects.filter( group_id__in=resource_group_id) return render( request, 'sqlworkflow.html', { 'status_list': SQL_WORKFLOW_CHOICES, 'instance': instance, 'resource_group': resource_group })
def archive(request): """归档列表页面""" # 获取资源组 group_list = user_groups(request.user) ins_list = user_instances(request.user, db_type=['mysql']).order_by( Convert('instance_name', 'gbk').asc()) return render(request, 'archive.html', { 'group_list': group_list, 'ins_list': ins_list })
def user_all_instances(request): """获取用户所有实例列表(通过资源组间接关联)""" user = request.user type = request.GET.get('type') db_type = request.GET.getlist('db_type[]') tag_codes = request.GET.getlist('tag_codes[]') instances = user_instances(user, type, db_type, tag_codes).order_by( Convert('instance_name', 'gbk').asc()).values('id', 'type', 'db_type', 'instance_name') rows = [row for row in instances] result = {'status': 0, 'msg': 'ok', "data": rows} return HttpResponse(json.dumps(result), content_type='application/json')
def lists(request): """获取实例列表""" limit = int(request.POST.get('limit')) offset = int(request.POST.get('offset')) type = request.POST.get('type') db_type = request.POST.get('db_type') tags = request.POST.getlist('tags[]') limit = offset + limit search = request.POST.get('search', '') sortName = str(request.POST.get('sortName')) sortOrder = str(request.POST.get('sortOrder')).lower() # 组合筛选项 filter_dict = dict() # 过滤搜索 if search: filter_dict['instance_name__icontains'] = search # 过滤实例类型 if type: filter_dict['type'] = type # 过滤数据库类型 if db_type: filter_dict['db_type'] = db_type instances = Instance.objects.filter(**filter_dict) # 过滤标签,返回同时包含全部标签的实例,TODO 循环会生成多表JOIN,如果数据量大会存在效率问题 if tags: for tag in tags: instances = instances.filter(instance_tag=tag, instance_tag__active=True) count = instances.count() if sortName == 'instance_name': instances = instances.order_by( getattr(Convert(sortName, 'gbk'), sortOrder)())[offset:limit] else: instances = instances.order_by('-' + sortName if sortOrder == 'desc' else sortName)[offset:limit] instances = instances.values("id", "instance_name", "db_type", "type", "host", "port", "user") # QuerySet 序列化 rows = [row for row in instances] result = {"total": count, "rows": rows} return HttpResponse(json.dumps(result, cls=ExtendJSONEncoder, bigint_as_string=True), content_type='application/json')
def instances(request): """获取资源组关联实例列表""" group_name = request.POST.get('group_name') group_id = ResourceGroup.objects.get(group_name=group_name).group_id tag_code = request.POST.get('tag_code') db_type = request.POST.get('db_type') # 先获取资源组关联所有实例列表 ins = ResourceGroup.objects.get(group_id=group_id).instance_set.all() # 过滤项 filter_dict = dict() # db_type if db_type: filter_dict['db_type'] = db_type if tag_code: filter_dict['instance_tag__tag_code'] = tag_code filter_dict['instance_tag__active'] = True ins = ins.filter(**filter_dict).order_by( Convert('instance_name', 'gbk').asc()).values('id', 'type', 'db_type', 'instance_name') rows = [row for row in ins] result = {'status': 0, 'msg': 'ok', "data": rows} return HttpResponse(json.dumps(result), content_type='application/json')