From 14925e8c79caa10a5fe393826dd6f0935d6af77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AD=A6=E5=B3=B0?= Date: Thu, 29 Sep 2022 14:41:09 +0800 Subject: [PATCH] feature: machine json for result;func result add current and except --- models/job_model.py | 2 ++ services/plan_service.py | 4 ++- services/tone_job_service.py | 61 ++++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/models/job_model.py b/models/job_model.py index a805829..5b04aab 100644 --- a/models/job_model.py +++ b/models/job_model.py @@ -33,6 +33,8 @@ class FuncResult(CommonModel): tone_case_id = Column(Integer(), nullable=False, default=-1, index=True, comment='tone case id') tone_job_id = Column(Integer(), nullable=False, default=-1, index=True, comment='tone job id') task_id = Column(Integer(), nullable=False, default=-1, index=True, comment='测试方案id') + current = Column(JSON(), nullable=True, default=[], comment='存储tone job的current信息') + expect = Column(JSON(), nullable=True, default=[], comment='存储tone job的expect信息') class PerfResult(CommonModel): diff --git a/services/plan_service.py b/services/plan_service.py index ec564af..7d8e912 100644 --- a/services/plan_service.py +++ b/services/plan_service.py @@ -355,7 +355,9 @@ async def export_plan(plan_id): "suite_name": case['suite_name'], "tone_suite_name": case['tone_suite_name'], "tone_case_name": case['tone_case_name'], - "status": func_case.sub_case_result.value + "status": func_case.sub_case_result.value, + "current": func_case.current, + "expect": func_case.expect } __update_func_result_count(auto_case, result) return result diff --git a/services/tone_job_service.py b/services/tone_job_service.py index 371387a..476cb61 100644 --- a/services/tone_job_service.py +++ b/services/tone_job_service.py @@ -1,36 +1,27 @@ -import json import time +import json +import uuid from app.conf import conf from app.log import log -from common.enums import Tone_Job_State, Track_Result, Case_Result, Status_EN, Test_Type -from common.tone.api import TONE_JOB_QUERY, TONE_CREATE_JOB -from common.tone.tone_request import get_res_from_tone +from common.enums import Tone_Job_State, Track_Result, Case_Result, Status_EN from models.job_model import ToneJob, PerfResult, FuncResult -from models.plan_model import update_plan_status from models.task_model import Task, update_task_status +from models.plan_model import update_plan_status from services.const import ERROR_UN_EXISTED_TONE_JOB +from common.tone.tone_request import get_res_from_tone +from common.tone.api import TONE_JOB_QUERY, TONE_CREATE_JOB +from app.oss import alg_oss async def create_job(data, task_id): - tone_job = { - 'name': data.get('name'), - 'state': Tone_Job_State.PENDING.value, - 'test_type': __transfer_job_type(data.get('test_type')), - 'tone_job_id': data.get('id'), - 'task_id': task_id - } + tone_job = {'name': data.get('job_name'), 'state': Tone_Job_State.PENDING.value, + 'test_type': data.get('test_type'), 'tone_job_id': data.get('job_id'), 'task_id': task_id} result = await ToneJob().save(tone_job) await update_task_status(Status_EN.RUNNING.value, task_id) return result.to_dict(), True -def __transfer_job_type(test_type): - if test_type == '性能测试' or test_type == Test_Type.PERFORMANCE.value: - return Test_Type.PERFORMANCE.value - return Test_Type.FUNCTIONAL.value - - async def update_job(data, is_finished): tone_job = await ToneJob.query_obj_one(ToneJob.tone_job_id == data.get('job_id')) if not tone_job: @@ -60,7 +51,11 @@ async def save_job_result(data): tone_job = await ToneJob.query_obj_one(ToneJob.tone_job_id == data.get('job_id')) if not tone_job: return ERROR_UN_EXISTED_TONE_JOB, False - tone_job.server_info = data.get('server_info', list()) + machine_info = await parse_result_file(data.get('machine_file'), data.get('job_id')) + if machine_info: + tone_job.server_info = machine_info + else: + tone_job.server_info = data.get('server_info', list()) await tone_job.update() if test_type == 'functional': await save_func_detail(tone_job.task_id, data) @@ -74,7 +69,7 @@ async def save_func_detail(task_id, data): func_results = await FuncResult.query_obj_all(FuncResult.tone_job_id == tone_job_id) if len(func_results) > 0: for result in func_results: - result.remove() + await result.remove() task = await Task.query_obj_one(Task.id == task_id) if task: run_result = task.run_result @@ -100,7 +95,14 @@ async def save_func_detail(task_id, data): 'task_id': task_id }) continue + statistic_json = await parse_result_file(func_result.get('statistic_file'), tone_job_id) for case_result in func_result.get('case_result'): + current, expect = [], [] + if statistic_json: + results = statistic_json.get('results') + for res in results: + if res['testcase'] == case_result.get('sub_case_name'): + current, expect = res.get('current', []), res.get('expect',[]) if case_result.get('sub_case_result') == 2: case_state = Case_Result.FAIL.value elif case_result.get('sub_case_result') == 1: @@ -113,13 +115,27 @@ async def save_func_detail(task_id, data): 'tone_suite_id': test_suite_id, 'tone_case_id': test_case_id, 'tone_job_id': tone_job_id, - 'task_id': task_id + 'task_id': task_id, + 'current': current, + 'expect': expect }) func_result_list.append(func_result_obj) await FuncResult().batch_add(func_result_list) await task.update() +async def parse_result_file(res_file, job_id): + if res_file: + index = res_file.find('/' + str(job_id)) + oss_file = res_file[index + 1:] + filepath = 'common/static/' + str(uuid.uuid4()) + '.json' + if alg_oss.file_exists(oss_file): + alg_oss.get_object_to_file(oss_file, filepath) + with open(filepath, 'r') as f: + return json.load(f) + return None + + async def save_perf_detail(task_id, data): tone_job_id = data.get('job_id') perf_results = await PerfResult.query_obj_all(PerfResult.tone_job_id == tone_job_id) @@ -168,10 +184,9 @@ async def create_tone_job(data): 'test_config': data.get('test_config'), 'callback_api': conf.config['MAIN_DOMAIN'] + 'api/tone/callback' } + log.logger.info(f"tone job params:{req_data}") status, result = await get_res_from_tone('post', TONE_CREATE_JOB, req_data) - log.logger.info(f"Create tone job params:{req_data}") if status == 200: - log.logger.debug(result) if result['success']: await create_job(result['data'], data.get('task_id')) return None, True -- Gitee