Exemple #1
0
    def testGetTasksUrlNum(self):
        """
        测试执行SQL
        :return: 
        """
        from model.task import TaskService
        import logging
        logger = logging.getLogger('peewee')
        logger.addHandler(logging.StreamHandler())
        logger.setLevel(logging.DEBUG)

        TaskService.get_tasks_url_num(task_id=1, task_status=3)
def list_tasks():
    """
    显示所有任务列表,方便管理任务
    :return: 
    """
    try:
        task_id = request.args.get("task_id")
        task_status = request.args.get('status')
        # 构造条件查询元组
        task_info_list = list()
        tasks = TaskService.get_tasks_url_num(task_id=task_id,
                                              task_status=task_status)
        for task in tasks:
            hook_rule = task.hook_rule
            # RedisService.get_task(task.id)["hook_rule"]
            unscaned_urls_num = task.unscaned_urls_num
            scaned_urls_num = task.scaned_urls_num
            total_url_num = unscaned_urls_num + scaned_urls_num

            if task.task_status in [TaskStatus.KILLED, TaskStatus.DONE]:
                percent = 100
            else:
                percent = 0 if total_url_num == 0 else int(
                    (scaned_urls_num / total_url_num) * 100)

            task_info_list.append({
                'receiver_emails':
                task.receivers_email,
                'task_name':
                task.task_name,
                'create_time':
                task.created_time.strftime("%Y-%m-%d %H:%M"),
                'percent':
                percent,
                'unscaned_url_num':
                unscaned_urls_num,
                'scaned_url_num':
                scaned_urls_num,
                'total_url_num':
                total_url_num,
                'hook_rule':
                hook_rule,
                'task_id':
                task.id,
                'task_access_key':
                task.access_key,
                'task_status':
                task.task_status,
                "create_user_name":
                task.create_user_name
            })
        task_info_list.reverse()
        response = jsonify(status=200, message="查询成功", data=task_info_list)
        return response

    except Exception as e:
        logger.exception("show_current_tasks rasie error")
        if isinstance(e, BaseHunterException):
            return jsonify(status=400,
                           message=str(e),
                           data={"extra_info": "查询任务时传入非法的task_id"})
        return jsonify(status=500,
                       message="未知异常",
                       data={"extra_info": "查询任务时出现未知异常,请联系管理员查看异常日志"})