Пример #1
0
def home_index():
    date = DateHelper.getCurrentTime(fmt="%Y-%m-%d")
    job_count = JobList.query.filter_by(is_del=CommonConstant.default_status_false).count()
    server_count = JobServer.query.filter_by(status=CommonConstant.default_status_true).count()
    alert_count = JobAlertList.query.filter(JobAlertList.created_time.between(date, date + " 23:59:59")).count()

    cate_map = ModelHelper.getDictFilterField( JobCategory )
    cat_job_map = {}
    cat_job_list = JobList.query.with_entities(JobList.cate_id ,func.count( JobList.id) )\
        .filter_by(is_del=CommonConstant.default_status_false)\
        .group_by(JobList.cate_id).all()
    if cat_job_list:
        for _item in cat_job_list:
            cat_job_map[ _item[0] ] = _item[1]

    type_job_map = {}
    job_type_map = CommonConstant.job_type_map
    type_job_list = JobList.query.with_entities(JobList.job_type, func.count(JobList.id)) \
        .filter_by(is_del=CommonConstant.default_status_false) \
        .group_by(JobList.job_type).all()
    if type_job_list:
        for _item in type_job_list:
            type_job_map[ _item[0] ] = _item[1]

    return UtilHelper.renderView("home/index/index.html", {
        "job_count": job_count,
        "server_count": server_count,
        "alert_count": alert_count,
        'cate_map' : cate_map,
        'cat_job_map' : cat_job_map,
        'job_type_map' : job_type_map,
        'type_job_map' : type_job_map,
    })
Пример #2
0
def job_info():
    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    info = JobList.query.filter_by(id=id).first()
    if not info:
        return redirect(GlobalUrlService.buildHomeUrl("/job/index/index"))

    info = ModelHelper.model2Dict(info)

    server_info = JobServer.query.filter_by(id=info['server_id']).first()
    cate_info = JobCategory.query.filter_by(id=info['cate_id']).first()
    server_env_map = CommonConstant.server_env_map
    run_status_map = CommonConstant.run_status_map

    info['next_run_time'] = DateHelper.getDateOnTimestamps(
        info['next_run_time'], '%Y-%m-%d %H:%M')
    info['env_name'] = server_env_map.get(info['env_id'])
    info['run_status_desc'] = run_status_map.get(info['run_status'])
    info['job_status_desc'] = job_status_map.get(info['status'])
    info['server_name'] = server_info.name
    info['cate_name'] = cate_info.name if cate_info else ''
    info['run_interval_desc'] = DateHelper.formatBeautyTime(
        info['run_interval'] * 60)

    user_map = ModelHelper.getDictFilterField(
        User,
        select_field=User.id,
        id_list=[info['owner_uid'], info['relate_uid']])

    ##获取最近5天运行记录
    log_list = JobRunLog.query.filter_by(job_id=id).order_by(
        JobRunLog.id.desc())[0:5]
    log_data = []
    if log_list:
        for item in log_list:
            tmp_data = ModelHelper.model2Dict(item)
            tmp_data['status_desc'] = CommonConstant.job_log_status_map[
                tmp_data['status']]
            tmp_data['duration'] = ""
            if DateHelper.getCurrentTime(date=tmp_data['end_time']
                                         ) == CommonConstant.DEFAULT_DATETIME:
                tmp_data['end_time'] = "未知"
                tmp_data['duration'] = time.time() - time.mktime(
                    tmp_data['start_time'].timetuple())
            else:
                tmp_data['duration'] = tmp_data['end_time'].timestamp(
                ) - tmp_data['start_time'].timestamp()
            tmp_data['duration'] = DateHelper.formatBeautyTime(
                tmp_data['duration'])
            log_data.append(tmp_data)

    return UtilHelper.renderView(
        "home/job/index/info.html", {
            "info": info,
            "log_list": log_data,
            "user_map": user_map,
            "job_level_map": CommonConstant.job_level_map,
        })
Пример #3
0
def tools_alert():
    req = request.values
    page = int(req.get("p", 1))
    date_from = req.get("date_from", DateHelper.getCurrentTime(fmt="%Y-%m-%d"))
    date_to = req.get("date_to", DateHelper.getCurrentTime(fmt="%Y-%m-%d"))
    status = int(req.get("status", CommonConstant.default_status_neg_99))

    query = JobAlertList.query.filter(
        JobAlertList.created_time.between(date_from, date_to + " 23:59:59"))
    if status > CommonConstant.default_status_neg_99:
        query = query.filter_by(status=status)

    page_params = {
        "total": query.count(),
        "page_size": CommonConstant.PAGE_SIZE,
        "page": page,
        "display": CommonConstant.PAGE_DISPLAY,
    }

    pages = UtilHelper.iPagination(page_params)
    offset = (page - 1) * CommonConstant.PAGE_SIZE
    limit = CommonConstant.PAGE_SIZE * page
    list = query.order_by(JobAlertList.id.desc())[offset:limit]
    data = []
    if list:
        job_ids = ModelHelper.getFieldList(list, "job_id")
        job_map = ModelHelper.getDictFilterField(JobList,
                                                 select_field=JobList.id,
                                                 id_list=job_ids.sort())
        for item in list:
            tmp_data = ModelHelper.model2Dict(item)
            tmp_job_info = ModelHelper.model2Dict(
                job_map.get(tmp_data['job_id']))
            tmp_data['status_desc'] = CommonConstant.common_status_map4[
                tmp_data['status']]
            tmp_data['job_name'] = tmp_job_info['name']

            data.append(tmp_data)

    sc = {'date_from': date_from, 'date_to': date_to, 'status': status}

    return UtilHelper.renderView(
        "home/job/tools/alert.html", {
            "list": data,
            "pages": pages,
            "sc": sc,
            "status_map": CommonConstant.common_status_map4,
            "current": "alert"
        })
Пример #4
0
def staff_index():
    req = request.values
    kw = req.get("kw", "").strip()
    page = int(req.get("p", 1))

    query = User.query
    if kw:
        query = query.filter( or_( User.name.ilike( '%{}%'.format(kw) ) ,User.email.ilike( '%{}%'.format(kw) ) ))

    page_params = {
        "total": query.count(),
        "page_size": CommonConstant.PAGE_SIZE,
        "page": page,
        "display": CommonConstant.PAGE_DISPLAY,
    }

    pages = UtilHelper.iPagination(page_params)
    offset = (page - 1) * CommonConstant.PAGE_SIZE
    limit = CommonConstant.PAGE_SIZE * page
    list = query.order_by( User.id.desc() )[offset:limit]

    dept_map = ModelHelper.getDictFilterField( Role )
    sc = {
        'kw': kw
    }

    set_flag = RBACService.checkPageRelatePrivilege("set")
    ops_flag = RBACService.checkPageRelatePrivilege("ops")

    return UtilHelper.renderView( "home/rbac/staff/index.html",   {
        "list": list,
        "pages": pages,
        "dept_map" : dept_map,
        "sc": sc,
        "set_flag": set_flag,
        "ops_flag": ops_flag,
    })
Пример #5
0
    def run(self, params):
        list = JobAlertList.query.filter_by(status=CommonConstant.default_status_neg_2)\
            .order_by( JobAlertList.id.asc() ).limit(10).all()

        if not list:
            app.logger.info("没有报警异常数据需要处理")
            return self.exitOK()

        job_ids = ModelHelper.getFieldList(list, "job_id")
        job_map = ModelHelper.getDictFilterField(JobList, select_field=JobList.id, id_list= job_ids )

        staff_ids = []
        for _key,_item in job_map.items():
            staff_ids.append( _item.owner_uid )
            staff_ids.append( _item.relate_uid )

        staff_map = ModelHelper.getDictFilterField(User, select_field= User.id, id_list= staff_ids)
        done_ids = []
        alert_content = [
            "Job异常报警"
        ]

        cate_map = ModelHelper.getDictFilterField(JobCategory)
        for item in list:

            tmp_data = ModelHelper.model2Dict( item )
            tmp_job_info = ModelHelper.model2Dict(job_map.get(tmp_data['job_id']))

            #负责人和相关人用户信息
            tmp_job_owner_info =  ModelHelper.model2Dict( staff_map.get( tmp_job_info['owner_uid'] ) )
            tmp_job_relate_info =  ModelHelper.model2Dict( staff_map.get( tmp_job_info['relate_uid'] ) )

            #分类
            tmp_cate_info = ModelHelper.model2Dict( cate_map.get( tmp_job_info['cate_id'] ) )

            done_ids.append(tmp_data['id'])
            tmp_msg = "Job Id: {0},名称:[{1}]{2},负责人:{3},相关人:{4},重要级别:{5},类型:{6},报警内容:{7}"\
                .format( tmp_job_info['id'],tmp_cate_info['name'],tmp_job_info['name']
                         ,tmp_job_owner_info['name'],tmp_job_relate_info['name']
                         ,CommonConstant.job_level_map[ tmp_job_info['job_level'] ]
                         ,CommonConstant.job_type_map[ tmp_job_info['job_type'] ]
                         , tmp_data['content']
                         )

            if 'Job平台标识没有运行'  in tmp_msg or 'Job平台标识正在运行'  in tmp_msg:
                continue

            self.handleItem(tmp_data, tmp_job_info)
            alert_content.append(tmp_msg)


        if done_ids:
            '''
            synchronize_session用于query在进行delete or update操作时,对session的同步策略。
            False - 不对session进行同步,直接进行delete or update操作
            '''
            JobAlertList.query.filter( JobAlertList.id.in_( done_ids ) )\
                .update( dict( status =  CommonConstant.default_status_true  ) ,synchronize_session=False )
            db.session.commit()


        if len( alert_content ) > 1:
            alert_content = "\r\n".join( alert_content )
            self.dingdingAlert( alert_content )
            self.wechatworkAlert( alert_content )

        return self.exitOK()
Пример #6
0
def tools_log():
    status_map = CommonConstant.job_log_status_map

    req = request.values
    page = int(req.get("p", 1))
    date_from = req.get("date_from", DateHelper.getCurrentTime(fmt="%Y-%m-%d"))
    date_to = req.get("date_to", DateHelper.getCurrentTime(fmt="%Y-%m-%d"))
    status = int(req.get("status", CommonConstant.default_status_neg_99))
    job_id = int(req.get("job_id", CommonConstant.default_status_false))

    query = JobRunLog.query.filter(
        JobRunLog.created_time.between(date_from, date_to + " 23:59:59"))

    if job_id:
        query = query.filter_by(job_id=job_id)

    if status > CommonConstant.default_status_neg_99:
        query = query.filter_by(status=status)

    page_params = {
        "total": query.count(),
        "page_size": CommonConstant.PAGE_SIZE,
        "page": page,
        "display": CommonConstant.PAGE_DISPLAY,
    }

    pages = UtilHelper.iPagination(page_params)
    offset = (page - 1) * CommonConstant.PAGE_SIZE
    limit = CommonConstant.PAGE_SIZE * page
    list = query.order_by(JobRunLog.id.desc())[offset:limit]
    data = []
    if list:
        job_ids = ModelHelper.getFieldList(list, "job_id")
        job_map = ModelHelper.getDictFilterField(JobList,
                                                 select_field=JobList.id,
                                                 id_list=job_ids.sort())
        for item in list:
            tmp_data = ModelHelper.model2Dict(item)
            tmp_job_info = ModelHelper.model2Dict(
                job_map.get(tmp_data['job_id']))
            tmp_data['status_desc'] = status_map[tmp_data['status']]
            tmp_data['job_name'] = tmp_job_info['name']
            tmp_data['duration'] = ""
            if DateHelper.getCurrentTime(date=tmp_data['end_time']
                                         ) == CommonConstant.DEFAULT_DATETIME:
                tmp_data['end_time'] = "未知"
                tmp_data['duration'] = time.time() - time.mktime(
                    tmp_data['start_time'].timetuple())
            else:
                tmp_data['duration'] = (tmp_data['end_time'].timestamp() -
                                        tmp_data['start_time'].timestamp())
            tmp_data['duration'] = DateHelper.formatBeautyTime(
                tmp_data['duration'])

            data.append(tmp_data)

    sc = {
        'date_from': date_from,
        'date_to': date_to,
        'status': status,
        'job_id': job_id
    }

    return UtilHelper.renderView(
        "home/job/tools/log.html", {
            "list": data,
            "pages": pages,
            "sc": sc,
            "status_map": status_map,
            "current": "log"
        })
Пример #7
0
def job_index():
    req = request.values
    cate_id = int(req.get("cate_id", CommonConstant.default_status_false))
    owner_uid = int(req.get("owner_uid", CurrentUserService.getUid()))
    env_id = int(req.get("env_id", CommonConstant.default_status_false))
    server_id = int(req.get("server_id", CommonConstant.default_status_false))
    status = int(req.get("status", CommonConstant.default_status_neg_99))
    display_status = int(
        req.get("display_status", CommonConstant.default_status_neg_99))
    job_type = int(req.get("job_type", CommonConstant.default_status_neg_99))
    job_level = int(req.get("job_level", CommonConstant.default_status_neg_99))

    kw = req.get("kw", "").strip()
    page = int(req.get("p", 1))

    query = JobList.query

    if RBACService.checkDataPrivilege("all"):
        pass
    else:
        query = query.filter_by(owner_uid=CurrentUserService.getUid())

    if cate_id:
        query = query.filter_by(cate_id=cate_id)

    if env_id:
        query = query.filter_by(env_id=env_id)

    if owner_uid:
        query = query.filter_by(owner_uid=owner_uid)

    if server_id:
        query = query.filter_by(server_id=server_id)

    if status > CommonConstant.default_status_neg_99:
        query = query.filter_by(status=status)

    if display_status > CommonConstant.default_status_neg_99:
        query = query.filter_by(is_del=display_status)

    if job_type > CommonConstant.default_status_neg_99:
        query = query.filter_by(job_type=job_type)

    if job_level > CommonConstant.default_status_neg_99:
        query = query.filter_by(job_level=job_level)

    if kw:
        if kw.isdigit():
            query = query.filter_by(id=int(kw))
        else:
            query = query.filter(
                or_(JobList.name.ilike('%{}%'.format(kw)),
                    JobList.command.ilike('%{}%'.format(kw))))

    page_params = {
        "total": query.count(),
        "page_size": CommonConstant.PAGE_SIZE,
        "page": page,
        "display": CommonConstant.PAGE_DISPLAY
    }

    pages = UtilHelper.iPagination(page_params)
    offset = (page - 1) * CommonConstant.PAGE_SIZE
    limit = CommonConstant.PAGE_SIZE * page
    list = query.order_by(JobList.id.desc())[offset:limit]
    data = []

    server_map = ModelHelper.getDictFilterField(JobServer)
    cate_map = ModelHelper.getDictFilterField(JobCategory)
    staff_map = ModelHelper.getDictFilterField(User)
    server_env_map = CommonConstant.server_env_map
    run_status_map = CommonConstant.run_status_map
    job_type_map = CommonConstant.job_type_map
    job_level_map = CommonConstant.job_level_map
    if list:
        for item in list:
            tmp_data = ModelHelper.model2Dict(item)
            tmp_server_info = ModelHelper.model2Dict(
                server_map.get(tmp_data['server_id']))
            tmp_cate_info = ModelHelper.model2Dict(
                cate_map.get(tmp_data['cate_id']))
            tmp_owner_staff_info = ModelHelper.model2Dict(
                staff_map.get(tmp_data['owner_uid']))
            tmp_relate_staff_info = ModelHelper.model2Dict(
                staff_map.get(tmp_data['relate_uid']))
            tmp_data['next_run_time'] = DateHelper.getDateOnTimestamps(
                tmp_data['next_run_time'], '%Y-%m-%d %H:%M')
            tmp_data['env_name'] = server_env_map.get(tmp_data['env_id'])
            tmp_data['run_status_desc'] = run_status_map.get(
                tmp_data['run_status'])
            tmp_data['job_status_desc'] = job_status_map.get(
                tmp_data['status'])
            tmp_data['server_name'] = tmp_server_info.get("name")
            tmp_data['cate_name'] = tmp_cate_info.get("name", '')
            tmp_data['owner_name'] = tmp_owner_staff_info.get("name", '')
            tmp_data['relate_name'] = tmp_relate_staff_info.get("name", '')
            tmp_data['run_interval_desc'] = DateHelper.formatBeautyTime(
                tmp_data['run_interval'] * 60)
            data.append(tmp_data)
    sc = {
        'kw': kw,
        'cate_id': cate_id,
        'owner_uid': owner_uid,
        'env_id': env_id,
        'server_id': server_id,
        'status': status,
        'display_status': display_status,
        'job_type': job_type,
        'job_level': job_level,
    }

    set_flag = RBACService.checkPageRelatePrivilege("set")
    ops_flag = RBACService.checkPageRelatePrivilege("ops")
    return UtilHelper.renderView(
        "home/job/index/index.html", {
            "list": data,
            "pages": pages,
            "job_status_map": job_status_map,
            "server_env_map": server_env_map,
            "server_map": server_map,
            "staff_map": staff_map,
            "cate_map": cate_map,
            "display_status_map": display_status_map,
            "job_type_map": job_type_map,
            "job_level_map": job_level_map,
            "sc": sc,
            "set_flag": set_flag,
            "ops_flag": ops_flag,
        })