示例#1
0
def crud_search_result(date, user):
    conn = OperatorMysql()
    res = None
    if date is not None and user is not None:
        sql = " select task_name, sys_name, sys_module, sys_api, task_stat, status, response, result, " \
              "create_time, operator " \
          "from task_result " \
          "where is_delete=0 and date(create_time) = %s and operator=%s " \
          "order by create_time desc"
        res = conn.search(sql, (date, user))
    elif date is not None and user is None:
        sql = " select task_name, sys_name, sys_module, sys_api, task_stat, status, response, result, " \
              "create_time, operator " \
          "from task_result " \
          "where is_delete=0 and date(create_time)= %s " \
          "order by create_time desc"
        res = conn.search(sql, (date, ))
    elif date is None and user is not None:
        sql = " select task_name, sys_name, sys_module, sys_api, task_stat, status, response, result, " \
              "create_time, operator " \
              "from task_result " \
              "where is_delete=0 and operator=%s " \
              "order by create_time desc"
        res = conn.search(sql, (user, ))
    return res
示例#2
0
def crud_get_task_by_sys_total(id: int):
    conn = OperatorMysql()
    sql = "select count(sa.id) " \
          "from " \
          "(sys_name sn left join sys_module sm on sn.id = sm.sys_key) " \
          "left join sys_api sa on sa.module_key=sm.id " \
          "where sn.is_delete=0 and sm.is_delete=0 and sa.is_delete=0 and sn.id=%s "
    res = conn.search(sql, (id, ))
    return res
示例#3
0
def crud_get_task_by_sys(id: int, page_num: int, page_size: int):
    conn = OperatorMysql()
    sql = "select sa.id, sn.name sys_name, sn.host, sa.path, sm.name mod_name, sa.name api_name, " \
          "sa.method, sa.variable, sa.headers, sa.params, sa.form_data " \
          "from " \
          "(sys_name sn left join sys_module sm on sn.id = sm.sys_key) " \
          "left join sys_api sa on sa.module_key=sm.id " \
          "where sn.is_delete=0 and sm.is_delete=0 and sa.is_delete=0 and sn.id=%s order by sa.id"
    res = conn.search(sql, (id, ))
    return res
示例#4
0
def crud_return_set_case_total(query: int):
    conn = OperatorMysql()
    sql = "select " \
          "count(as.name) " \
          "from " \
          "(api_case `a` left join api_step `as` on `a`.id = `as`.api_case_key) " \
          "left join assert_data `ad` on `as`.id=`ad`.api_step_key where a.is_delete=0 and `as`.is_delete=0 " \
          "and ad.is_delete=0 and a.case_set_key=%s"
    res = conn.search(sql, (query, ))
    return res
示例#5
0
def crud_task_result_page():
    conn = OperatorMysql()
    sql = " select task_name, sys_name, sys_module, sys_api, task_stat, status, response, result, " \
          "create_time, operator " \
          "from task_result " \
          "where is_delete=0 " \
          "order by create_time desc limit 100 "
    res = conn.search(sql)
    sql1 = "select name from user_control where is_active=1"
    res2 = conn.search(sql1)
    return res, res2
示例#6
0
def crud_return_set_case(query: int, page_num: int, page_size: int):
    conn = OperatorMysql()
    sql = "select " \
          "`a`.id case_id, `a`.name case_name, `a`.`desc` case_desc, `as`.step step_num , `as`.name step_name, " \
          " `as`.desc step_desc, `as`.set_up, `as`.tear_down, `as`.url, `as`.method, `as`.variable, `as`.headers, " \
          "`as`.params, `as`.form_data, `as`.json_data, `as`.need_assert, ad.assert_method, ad.exp_status, " \
          "ad.exp_extract " \
          "from " \
          "(api_case `a` left join api_step `as` on `a`.id = `as`.api_case_key) " \
          "left join assert_data `ad` on `as`.id=`ad`.api_step_key where a.is_delete=0 and `as`.is_delete=0 " \
          "and ad.is_delete=0 and a.case_set_key=%s order by a.id  limit %s, %s"
    res = conn.search(sql, (query, (page_num - 1) * page_size, page_size))
    return res
示例#7
0
def platform_creat(data: RunSystemTaskSchema):
    conn = OperatorMysql()
    temp_list = []
    get_task_name = data.run_data.task_name + str(round(time.time() * 1000))
    get_operator = data.operator
    for item in data.api_list:
        today = datetime.datetime.today()
        timestamp = int(round(time.time() * 1000))
        sql = "insert into task_result " \
              "(task_name, sys_name, sys_module, sys_api, task_stat, operator, create_time, is_delete, timestamp) " \
              "values (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
        conn.insert(sql,
                    (get_task_name, item.sys_name, item.mod_name,
                     item.api_name, '创建', get_operator, today, 0, timestamp))
        temp_list.append([item.api_name, timestamp])
    return temp_list
示例#8
0
def crud_get_task_page():
    conn = OperatorMysql()
    sql = "select id, name from sys_name where is_delete=0"
    res = conn.search(sql)
    return res
示例#9
0
def crud_get_module_by_sys(id: int):
    conn = OperatorMysql()
    sql2 = "select id, name from sys_module where is_delete=0 and sys_key=%s"
    mod_list = conn.search(sql2, (id, ))
    return mod_list
示例#10
0
def crud_query_case_set():
    conn = OperatorMysql()
    sql = "select id, name from case_set where is_delete=0 "
    res = conn.search(sql)
    return res
示例#11
0
def platform_runner(data: RunSystemTaskSchema):
    conn = OperatorMysql()

    get_token = platform_login(data.run_data.username, data.run_data.host)
    if get_token[0] is True:
        today = datetime.datetime.today()
        search_day = str(datetime.date.today())
        temp_list = platform_creat(data=data)
        replace_dict = {
            'START-DATE': data.run_data.start_date[0:10],
            'END-DATE': data.run_data.end_date[0:10],
            'ONE-DAY': data.run_data.one_day[0:10],
            'TODAY': search_day
        }
        for item, item_list in zip(data.api_list, temp_list):
            msg_id = str(uuid.uuid4()).replace('-', '')
            log_id = str(uuid.uuid4()).replace('-', '')
            trns_id = str(uuid.uuid4()).replace('-', '')
            sql = "update task_result set task_stat='运行' where sys_api=%s and timestamp=%s"
            conn.update(sql, (item_list[0], item_list[1]))
            temp_form_data = item.form_data
            temp_headers = eval(item.headers)
            temp_headers['Cookie'] = get_token[1]
            if item.path:
                url = data.run_data.host + str(item.path).strip()
            else:
                url = data.run_data.host
            if item.params is not None:
                pass
            if item.form_data is not None:
                for key, value in replace_dict.items():
                    temp_form_data = temp_form_data.replace(key, value)
            if item.method == 'GET':
                pass
            elif item.method == 'POST':
                # pattern = re.compile(r'\$[a-zA-Z0-9]\w+\$')
                # result1 = pattern.findall('$asd$ $asdv$ $12$ $a123$ $12aaaa$')
                data_body = eval(temp_form_data)
                data_body['header']['msg_id'] = msg_id
                data_body['header']['log_id'] = log_id
                data_body['header']['send_time'] = today
                data_body['bodys'][0]['trns_id'] = trns_id
                data_body['bodys'][0]['crt_time'] = today
                if temp_form_data.find('page_size') != -1:
                    data_body['bodys'][0]['exts']['query_paged_cond'][
                        'page_size'] = data.run_data.page_size
                response = requests.post(url=url,
                                         data=data_body,
                                         headers=temp_headers)
                if response.status_code == 200:
                    sql = "update task_result set task_stat='完成', status=%s, response=%s " \
                          "where sys_api=%s and timestamp=%s"
                    conn.update(sql, (response.status_code, response.content,
                                      item_list[0], item_list[1]))
                else:
                    sql = "update task_result set task_stat='失败', status=%s, response=%s " \
                          "where sys_api=%s and timestamp=%s"
                    conn.update(sql, (response.status_code, response.content,
                                      item_list[0], item_list[1]))
        return {'result': '任务完成', 'task_name': data.run_data.task_name}
    else:
        return {
            'result': '获取token失败,任务未执行',
            'task_name': data.run_data.task_name
        }