def get_working_task_info():
    """
    根据用户名获取得到任务信息,用于openrestry代理模块取得最新未结束的任务,接口无需鉴权
    :param user_name: 
    :return: 
    """
    try:
        user_name = request.args.get("user_name")
        user = RedisService.get_user(user_name)
        tasks = TaskService.get_working_tasks(user.id)
        descriptions = ["该任务处于激活状态", "该任务正在进行扫描", "该任务已经扫描完成", "该任务已被用户关闭"]
        if tasks is None or len(tasks) == 0:
            return jsonify(status=200,
                           message="查询成功",
                           data={
                               "id": -1,
                               "status": TaskStatus.NONE,
                               "description": "不存在正在工作的任务,请登录平台先创建任务"
                           })
        current_working_task = tasks[0]
        return jsonify(status=200,
                       message="查询成功",
                       data={
                           "id":
                           current_working_task.id,
                           "status":
                           current_working_task.task_status,
                           "description":
                           descriptions[current_working_task.task_status]
                       })

    except Exception as e:
        logger.exception("get_task_info raise error")
        if isinstance(e, KeyError):
            return jsonify(status=400,
                           message="查询失败",
                           data={"extra_info": "未传递参数user_name"})
        elif isinstance(e, UserNotFoundInRedisException):
            return jsonify(status=400,
                           message="查询失败",
                           data={"extra_info": "未找到该用户信息"})
        return jsonify(status=500,
                       message="未知异常",
                       data={"extra_info": "发生未知异常,请联系管理员查看异常日志"})
Exemple #2
0
 def modify_user_info_cache_session(user_name, db_user):
     """
     认证成功之后,修改redis中用户信息并设置session
     :return: 
     """
     # 存入到redis,和数据库中的数据一致,用户id是最关键的
     RedisService.update_user(
         user_name, {
             "id": db_user.id,
             "user_name": db_user.user_name,
             "full_name": db_user.full_name,
             "dept_name": db_user.dept_name,
             "role": db_user.role,
             "mobile_phone": db_user.mobile_phone,
             "email": db_user.email
         })
     try:
         current_task = TaskService.get_working_tasks(user_id=db_user.id)[0]
         RedisService.update_user(user_name,
                                  {"current_task_id": current_task.id})
     except IndexError:
         pass
     # 设置session
     session["user_name"] = user_name
def show_current_task():
    """
    显示当前任务正在运行的任务
    :return: 
    """
    try:
        current_user_name = session["user_name"]
        current_user = RedisService.get_user(current_user_name)
        current_task = TaskService.get_working_tasks(
            user_id=current_user.id)[0]
        if current_task:
            hook_rule = RedisService.get_task(current_task.id)["hook_rule"]
            unscaned_url_num = UrlService.count(
                where=(Url.task_id == current_task.id,
                       Url.status != TaskStatus.DONE))
            scaned_url_num = UrlService.count(
                where=(Url.task_id == current_task.id,
                       Url.status == TaskStatus.DONE))
            total_url_num = unscaned_url_num + scaned_url_num
            if current_task.task_status in [
                    TaskStatus.KILLED, TaskStatus.DONE
            ]:
                percent = 100
            else:
                percent = 0 if total_url_num == 0 else (scaned_url_num /
                                                        total_url_num) * 100
            response_data = jsonify(
                status=200,
                message="查询成功",
                data={
                    'receiver_emails':
                    current_task.receivers_email,
                    'task_name':
                    current_task.task_name,
                    'create_time':
                    current_task.created_time.strftime("%Y-%m-%d %H:%M"),
                    'percent':
                    percent,
                    'unscaned_url_num':
                    unscaned_url_num,
                    'scaned_url_num':
                    scaned_url_num,
                    'total_url_num':
                    total_url_num,
                    'hook_rule':
                    hook_rule,
                    'task_id':
                    current_task.id,
                    "task_access_key":
                    current_task.access_key,
                    'task_status':
                    current_task.task_status,
                    'user_name':
                    current_user_name
                })
            return response_data
    except Exception as e:
        if isinstance(e, IndexError):
            return jsonify(status=400,
                           message="获取失败",
                           data={"extra_info": "后台无正在运行任务,请登录后台并创建任务"})
        logger.exception("show_current_task rasie error")
        return jsonify(status=500,
                       message="获取失败",
                       data={"extra_info": "未知异常,可以联系管理员到后台查看"})