diff --git a/appStore/testCase/views.py b/appStore/testCase/views.py index 069a96f1aae640c3c23904cf79c4dd9d4272998a..c55f738d1df98081b68ebf8a665902bf643f2a0e 100644 --- a/appStore/testCase/views.py +++ b/appStore/testCase/views.py @@ -11,8 +11,7 @@ import shutil import subprocess import time -import yaml -from django.http import HttpResponse, FileResponse +from django.http import HttpResponse, FileResponse, HttpRequest from appStore.testCase.models import TestCase from appStore.testCase.serializers import TestCaseSerializer @@ -96,7 +95,6 @@ class TestCaseViewSet(CusModelViewSet): configfile.write('password={}\n'.format(request.data.get('user_password'))) # 将配置数据写入YAML文件 - # 第一代的版本就先只支持迭代次数 if int(data_test_case['stream']): stream_yaml = request.data.get('yaml')['stream'].replace('maxiterations: 1', 'maxiterations: %d' % (int(data_test_case['stream']))) with open(user_config_path + '/yaml-base/stream-base.yaml', 'w', encoding='UTF-8') as fp: @@ -132,7 +130,7 @@ class TestCaseViewSet(CusModelViewSet): with open(user_config_path + '/yaml-base/cpu2006-base.yaml', 'w', encoding='UTF-8') as fp: fp.write(cpu2006_yaml) cpu2006_loongarch64_yaml = request.data.get('yaml')['cpu2006_loongarch64'].replace('maxiterations: 1', 'maxiterations: %d' % ( - int(data_test_case['cpu2006_loongarch64']))) + int(data_test_case['cpu2006']))) with open(user_config_path + '/yaml-base/cpu2006-loongarch64-base.yaml', 'w', encoding='UTF-8') as fp: fp.write(cpu2006_loongarch64_yaml) if int(data_test_case['cpu2017']): @@ -148,6 +146,16 @@ class TestCaseViewSet(CusModelViewSet): test_case_id = serializer_test_case.data['id'] else: return json_response(serializer_test_case.errors, status.HTTP_400_BAD_REQUEST, get_error_message(serializer_test_case)) + + """保存至配置管理数据库""" + from appStore.userConfig.views import UserConfigViewSet + request_user_config = HttpRequest() + request_user_config.method = 'POST' + request.data['is_send_config'] = True + request_user_config = request + UserConfigViewSet = UserConfigViewSet() + UserConfigViewSet.create(request=request_user_config, *args, **kwargs) + # 运行测试 return_result = test_case(data_test_case['ip'], 'root', test_password, test_case_names, user_config_path, data_test_case['result_log_name']) if return_result.stderr and return_result.stderr != '\nAuthorized users only. All activities may be monitored and reported.\n': @@ -157,7 +165,7 @@ class TestCaseViewSet(CusModelViewSet): TestCase.objects.filter(id=test_case_id).update(test_result='测试完成') return json_response('', status.HTTP_200_OK, '测试完成') - def delete(self, request): + def delete(self, request, *args, **kwargs): id = request.data.get('id', None) if not id or not TestCase.objects.filter(id=id): return json_response({}, status.HTTP_205_RESET_CONTENT, '请传递正确的测试id') @@ -166,17 +174,22 @@ class TestCaseViewSet(CusModelViewSet): test_case_data = TestCase.objects.filter(id=id).first() if not test_case_data: return json_response({}, status.HTTP_205_RESET_CONTENT, '没有该数据') + # todo 判断这个日志文件是否需要被删除 验证是否正确 + if not TestCase.objects.get(id=id).is_error: + subprocess.run("rm -rf " + str(TestCase.objects.filter(id=id).first().result_log_name) + '.tar', + shell=True) # 删除数据 TestCase.objects.filter(id=id).delete() # 删除日志文件 - subprocess.run("rm -rf " + str(TestCase.objects.filter(id=id).first().result_log_name) + '.tar', shell=True) + return json_response({}, status.HTTP_200_OK, '删除成功') else: return json_response({}, status.HTTP_205_RESET_CONTENT, '此用户不允许删除该数据') def down_message(self, request, *args, **kwargs): - test_case_id = request.GET.get('id') - result_log_name = TestCase.objects.filter(id=test_case_id).first().result_log_name + # test_case_id = request.GET.get('id') + # result_log_name = TestCase.objects.filter(id=test_case_id).first().result_log_name + result_log_name = request.GET.get('result_log_name') # 检查文件是否存在 log_file_path = result_log_name+'.tar' if not os.path.exists(log_file_path): diff --git a/appStore/userConfig/models.py b/appStore/userConfig/models.py index d241fa2b903e304656ac0267874a2a989e01c4a2..a977a802e8ae14936f8eedea06a1ca5316b1f75b 100644 --- a/appStore/userConfig/models.py +++ b/appStore/userConfig/models.py @@ -25,7 +25,7 @@ class UserConfig(models.Model): cpu2017_number = models.CharField(max_length=100, verbose_name='cpu2017测试迭代次数') stream_config = models.TextField(verbose_name='stream测试配置文件') lmbench_config = models.TextField(verbose_name='lmbench测试配置文件') - unixbbench_config = models.TextField(verbose_name='unixbbench测试配置文件') + unixbench_config = models.TextField(verbose_name='unixbbench测试配置文件') fio_config = models.TextField(verbose_name='fio测试配置文件') iozone_config = models.TextField(verbose_name='iozone测试配置文件') jvm2008_config = models.TextField(verbose_name='jvm2008测试配置文件') @@ -33,6 +33,7 @@ class UserConfig(models.Model): cpu2006_loongarch64_config = models.TextField(verbose_name='cpu2006_loongarch64测试配置文件') cpu2017_config = models.TextField(verbose_name='cpu2017测试配置文件') message = models.CharField(max_length=200,verbose_name='描述信息', null=True, blank=True) + is_send_config = models.BooleanField(default=False,verbose_name="是否是发起测试而保存的数据") class Meta: db_table = 'userConfig' \ No newline at end of file diff --git a/appStore/userConfig/serializers.py b/appStore/userConfig/serializers.py index 498e8d9ee8f2ad3002c67e34ca9a7f1302dc0514..aeaa8f9124bcb7b9ebba0f45d69257f216e69aed 100644 --- a/appStore/userConfig/serializers.py +++ b/appStore/userConfig/serializers.py @@ -11,7 +11,7 @@ from appStore.userConfig.models import UserConfig class UserConfigSerializer(serializers.ModelSerializer): """ - stream数据序列化 + userConfig数据序列化 """ class Meta: diff --git a/appStore/userConfig/views.py b/appStore/userConfig/views.py index 46a4ab741d36ab92d3f615dcabeeab8e0f02deaa..60d661ae80ac7e6264ff38f2963129fafe50b533 100644 --- a/appStore/userConfig/views.py +++ b/appStore/userConfig/views.py @@ -13,15 +13,21 @@ from appStore.utils.customer_view import CusModelViewSet class UserConfigViewSet(CusModelViewSet): """ - 测试机器数据管理 + 用户配置数据管理 """ queryset = UserConfig.objects.all().order_by('-id') serializer_class = UserConfigSerializer def list(self, request, *args, **kwargs): queryset = UserConfig.objects.filter(user_name=request.user.username).all().order_by('-id') + id = request.GET.get('configID') if not queryset: return json_response({}, status.HTTP_200_OK, '列表') + if id: + if id == '0': # 获取最后一条数据 + queryset = [queryset.first()] + else: + queryset=queryset.filter(id=id) serializer = self.get_serializer(queryset, many=True) return json_response(serializer.data, status.HTTP_200_OK, '测试完成') @@ -42,20 +48,61 @@ class UserConfigViewSet(CusModelViewSet): user_config_data['cpu2017_number'] = request.data.get('cpu2017') user_config_data['stream_config'] = request.data.get('yaml')['stream'] user_config_data['lmbench_config'] = request.data.get('yaml')['lmbench'] - user_config_data['unixbbench_config'] = request.data.get('yaml')['unixbench'] + user_config_data['unixbench_config'] = request.data.get('yaml')['unixbench'] user_config_data['fio_config'] = request.data.get('yaml')['fio'] user_config_data['iozone_config'] = request.data.get('yaml')['iozone'] user_config_data['jvm2008_config'] = request.data.get('yaml')['jvm2008'] user_config_data['cpu2006_config'] = request.data.get('yaml')['cpu2006'] user_config_data['cpu2006_loongarch64_config'] = request.data.get('yaml')['cpu2006_loongarch64'] user_config_data['cpu2017_config'] = request.data.get('yaml')['cpu2017'] + user_config_data['message'] = request.data.get('message') + if request.data.get('is_send_config'): + user_config_data['is_send_config'] = request.data.get('is_send_config') + # 删除旧数据 + UserConfig.objects.filter(is_send_config=True).delete() config_serializer = UserConfigSerializer(data=user_config_data) if config_serializer.is_valid(): self.perform_create(config_serializer) return json_response(config_serializer.data, status.HTTP_200_OK, '创建成功!') return json_response({}, status.HTTP_400_BAD_REQUEST,config_serializer.errors) - def delete(self, request): + def put(self, request, *args, **kwargs): + id = request.data.get('id') + if not id or not UserConfig.objects.filter(id=id): + return json_response({}, status.HTTP_205_RESET_CONTENT, '请传递正确的测试id') + user_name = UserConfig.objects.filter(id=id).first().user_name + if request.user.is_superuser or request.user.username == user_name: + config_data = UserConfig.objects.get(id=id) #get=filter.first() + if not config_data: + return json_response({}, status.HTTP_205_RESET_CONTENT, '没有该数据') + config_data.user_password = request.data.get('user_password') + config_data.project_name = request.data.get('project_name') + config_data.test_ip = request.data.get('test_ip') + config_data.test_password = request.data.get('test_password') + config_data.stream_number = request.data.get('stream') + config_data.lmbench_number = request.data.get('lmbench') + config_data.unixbench_number = request.data.get('unixbench') + config_data.fio_number = request.data.get('fio') + config_data.iozone_number = request.data.get('iozone') + config_data.jvm2008_number = request.data.get('jvm2008') + config_data.cpu2006_number = request.data.get('cpu2006') + config_data.cpu2017_number = request.data.get('cpu2017') + config_data.stream_config = request.data.get('yaml').get('stream') + config_data.lmbench_config = request.data.get('yaml').get('lmbench') + config_data.unixbench_config = request.data.get('yaml').get('unixbench') + config_data.fio_config = request.data.get('yaml').get('fio') + config_data.iozone_config = request.data.get('yaml').get('iozone') + config_data.jvm2008_config = request.data.get('yaml').get('jvm2008') + config_data.cpu2006_config = request.data.get('yaml').get('cpu2006') + config_data.cpu2006_loongarch64_config = request.data.get('yaml').get('cpu2006_loongarch64') + config_data.cpu2017_config = request.data.get('yaml').get('cpu2017') + config_data.message = request.data.get('message') + config_data.save() + return json_response({}, status.HTTP_200_OK, '修改成功') + else: + return json_response({}, status.HTTP_205_RESET_CONTENT, '此用户不允许修改该数据') + + def delete(self, request, *args, **kwargs): id = request.data.get('id', None) if not id or not UserConfig.objects.filter(id=id): return json_response({}, status.HTTP_205_RESET_CONTENT, '请传递正确的测试id')