def get_template_info(request, template_id, bk_biz_id): try: tmpl = TaskTemplate.objects.select_related('pipeline_template', 'business')\ .get(id=template_id, business__cc_id=bk_biz_id, is_deleted=False) except TaskTemplate.DoesNotExist: result = { 'result': False, 'message': 'template: %s of business:%s does not exist' % (template_id, bk_biz_id) } return JsonResponse(result) pipeline_tree = tmpl.pipeline_tree pipeline_tree.pop('line') pipeline_tree.pop('location') data = { 'id': tmpl.id, 'name': tmpl.pipeline_template.name, 'creator': tmpl.pipeline_template.creator, 'create_time': strftime_with_timezone(tmpl.pipeline_template.create_time), 'editor': tmpl.pipeline_template.editor, 'edit_time': strftime_with_timezone(tmpl.pipeline_template.edit_time), 'category': tmpl.category, 'bk_biz_id': bk_biz_id, 'bk_biz_name': tmpl.business.cc_name, 'pipeline_tree': pipeline_tree } return JsonResponse({'result': True, 'data': data})
def get_template_list(request, bk_biz_id): biz = Business.objects.get(cc_id=bk_biz_id) templates = TaskTemplate.objects.select_related( 'pipeline_template').filter(business=biz, is_deleted=False) data = [{ 'id': tmpl.id, 'name': tmpl.pipeline_template.name, 'creator': tmpl.pipeline_template.creator, 'create_time': strftime_with_timezone(tmpl.pipeline_template.create_time), 'editor': tmpl.pipeline_template.editor, 'edit_time': strftime_with_timezone(tmpl.pipeline_template.edit_time), 'category': tmpl.category, 'bk_biz_id': bk_biz_id, 'bk_biz_name': biz.cc_name } for tmpl in templates] return JsonResponse({'result': True, 'data': data})
def format_pipeline_status(status_tree): """ @summary: 转换通过 pipeline api 获取的任务状态格式 @return: """ status_tree.setdefault('children', {}) status_tree.pop('created_time', '') status_tree['start_time'] = strftime_with_timezone( status_tree.pop('started_time')) status_tree['finish_time'] = strftime_with_timezone( status_tree.pop('archived_time')) child_status = [] for identifier_code, child_tree in status_tree['children'].iteritems(): TaskFlowInstance.format_pipeline_status(child_tree) child_status.append(child_tree['state']) if status_tree['state'] == states.BLOCKED: if states.RUNNING in child_status: status_tree['state'] = states.RUNNING elif states.FAILED in child_status: status_tree['state'] = states.FAILED elif states.SUSPENDED in child_status or 'NODE_SUSPENDED' in child_status: status_tree['state'] = 'NODE_SUSPENDED' # 子流程 BLOCKED 状态表示子节点失败 elif not child_status: status_tree['state'] = states.FAILED
def format_template_data(template, biz=None): pipeline_tree = template.pipeline_tree pipeline_tree.pop('line') pipeline_tree.pop('location') data = { 'id': template.id, 'name': template.pipeline_template.name, 'creator': template.pipeline_template.creator, 'create_time': strftime_with_timezone(template.pipeline_template.create_time), 'editor': template.pipeline_template.editor, 'edit_time': strftime_with_timezone(template.pipeline_template.edit_time), 'category': template.category, 'pipeline_tree': pipeline_tree } if biz: data.update({'bk_biz_id': biz.cc_id, 'bk_biz_name': biz.cc_name}) return data
def format_template_list_data(templates, biz=None): data = [] for tmpl in templates: item = { 'id': tmpl.id, 'name': tmpl.pipeline_template.name, 'creator': tmpl.pipeline_template.creator, 'create_time': strftime_with_timezone(tmpl.pipeline_template.create_time), 'editor': tmpl.pipeline_template.editor, 'edit_time': strftime_with_timezone(tmpl.pipeline_template.edit_time), 'category': tmpl.category, } if biz: item.update({'bk_biz_id': biz.cc_id, 'bk_biz_name': biz.cc_name}) data.append(item) return data
def test_get_template_list__for_common_template(self): pt1 = MockPipelineTemplate(id=1, name='pt1') pt2 = MockPipelineTemplate(id=2, name='pt2') task_tmpl1 = MockCommonTemplate(id=1, pipeline_template=pt1) task_tmpl2 = MockCommonTemplate(id=2, pipeline_template=pt2) task_templates = [task_tmpl1, task_tmpl2] with mock.patch( COMMONTEMPLATE_SELECT_RELATE, MagicMock(return_value=MockQuerySet( filter_result=task_templates))): assert_data = [{ 'id': tmpl.id, 'name': tmpl.pipeline_template.name, 'creator': tmpl.pipeline_template.creator, 'create_time': strftime_with_timezone(tmpl.pipeline_template.create_time), 'editor': tmpl.pipeline_template.editor, 'edit_time': strftime_with_timezone(tmpl.pipeline_template.edit_time), 'category': tmpl.category, 'bk_biz_id': TEST_BIZ_CC_ID, 'bk_biz_name': TEST_BIZ_CC_NAME } for tmpl in task_templates] response = self.client.get(path=self.GET_TEMPLATE_LIST_URL.format( biz_cc_id=TEST_BIZ_CC_ID), data={'template_source': 'common'}) self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertTrue(data['result']) self.assertEqual(data['data'], assert_data) with mock.patch( COMMONTEMPLATE_SELECT_RELATE, MagicMock(return_value=MockQuerySet(filter_result=[]))): assert_data = [] response = self.client.get(path=self.GET_TEMPLATE_LIST_URL.format( biz_cc_id=TEST_BIZ_CC_ID), data={'template_source': 'common'}) data = json.loads(response.content) self.assertTrue(data['result']) self.assertEqual(data['data'], assert_data)
def get_template_info(request, template_id, bk_biz_id): biz = Business.objects.get(cc_id=bk_biz_id) template_source = request.GET.get('template_source', 'business') if template_source == 'business': try: tmpl = TaskTemplate.objects.select_related( 'pipeline_template').get(id=template_id, business__cc_id=bk_biz_id, is_deleted=False) except TaskTemplate.DoesNotExist: result = { 'result': False, 'message': 'template[id={template_id}] of business[bk_biz_id={bk_biz_id}] does not exist' .format(template_id=template_id, bk_biz_id=bk_biz_id) } return JsonResponse(result) else: try: tmpl = CommonTemplate.objects.select_related( 'pipeline_template').get(id=template_id, is_deleted=False) except CommonTemplate.DoesNotExist: result = { 'result': False, 'message': 'common template[id={template_id}] does not exist'.format( template_id=template_id) } return JsonResponse(result) pipeline_tree = tmpl.pipeline_tree pipeline_tree.pop('line') pipeline_tree.pop('location') data = { 'id': tmpl.id, 'name': tmpl.pipeline_template.name, 'creator': tmpl.pipeline_template.creator, 'create_time': strftime_with_timezone(tmpl.pipeline_template.create_time), 'editor': tmpl.pipeline_template.editor, 'edit_time': strftime_with_timezone(tmpl.pipeline_template.edit_time), 'category': tmpl.category, 'bk_biz_id': bk_biz_id, 'bk_biz_name': biz.cc_name, 'pipeline_tree': pipeline_tree } return JsonResponse({'result': True, 'data': data})
def test_get_periodic_task_list(self): pt1 = MockPeriodicTask(id='1') pt2 = MockPeriodicTask(id='2') pt3 = MockPeriodicTask(id='3') periodic_tasks = [pt1, pt2, pt3] assert_data = [{ 'id': task.id, 'name': task.name, 'template_id': task.template_id, 'creator': task.creator, 'cron': task.cron, 'enabled': task.enabled, 'last_run_at': strftime_with_timezone(task.last_run_at), 'total_run_count': task.total_run_count, } for task in periodic_tasks] with mock.patch(PERIODIC_TASK_FILTER, MagicMock(return_value=periodic_tasks)): response = self.client.get( path=self.GET_PERIODIC_TASK_LIST_URL.format( bk_biz_id=TEST_BIZ_CC_ID)) data = json.loads(response.content) self.assertTrue(data['result']) self.assertEqual(data['data'], assert_data)
def test_get_template_info__for_common_template(self): pt1 = MockPipelineTemplate(id=1, name='pt1') tmpl = MockCommonTemplate(id=1, pipeline_template=pt1) with mock.patch(COMMONTEMPLATE_SELECT_RELATE, MagicMock(return_value=MockQuerySet(get_result=tmpl))): pipeline_tree = copy.deepcopy(tmpl.pipeline_tree) pipeline_tree.pop('line') pipeline_tree.pop('location') assert_data = { 'id': tmpl.id, 'name': tmpl.pipeline_template.name, 'creator': tmpl.pipeline_template.creator, 'create_time': strftime_with_timezone(tmpl.pipeline_template.create_time), 'editor': tmpl.pipeline_template.editor, 'edit_time': strftime_with_timezone(tmpl.pipeline_template.edit_time), 'category': tmpl.category, 'bk_biz_id': TEST_BIZ_CC_ID, 'bk_biz_name': TEST_BIZ_CC_NAME, 'pipeline_tree': pipeline_tree } response = self.client.get(path=self.GET_TEMPLATE_INFO_URL.format( template_id=TEST_TEMPLATE_ID, bk_biz_id=TEST_BIZ_CC_ID), data={'template_source': 'common'}) data = json.loads(response.content) self.assertTrue(data['result']) self.assertEqual(assert_data, data['data'])
def test_create_periodic_task__success(self): task = MockPeriodicTask() assert_data = { 'id': task.id, 'name': task.name, 'template_id': task.template_id, 'creator': task.creator, 'cron': task.cron, 'enabled': task.enabled, 'last_run_at': strftime_with_timezone(task.last_run_at), 'total_run_count': task.total_run_count, 'form': task.form, 'pipeline_tree': task.pipeline_tree } biz = MockBusiness(cc_id=TEST_BIZ_CC_ID, cc_name=TEST_BIZ_CC_NAME) template = MockTaskTemplate() with mock.patch(TASKTEMPLATE_GET, MagicMock(return_value=template)): with mock.patch(BUSINESS_GET, MagicMock(return_value=biz)): with mock.patch(PERIODIC_TASK_CREATE, MagicMock(return_value=task)): response = self.client.post( path=self.CREATE_PERIODIC_TASK_URL.format( template_id=TEST_TEMPLATE_ID, bk_biz_id=TEST_BIZ_CC_ID), data=json.dumps({ 'name': task.name, 'cron': task.cron, 'exclude_task_nodes_id': 'exclude_task_nodes_id' }), content_type='application/json') TaskFlowInstance.objects.preview_pipeline_tree_exclude_task_nodes.assert_called_with( template.pipeline_tree, 'exclude_task_nodes_id') PeriodicTask.objects.create.assert_called_once_with( business=biz, template=template, name=task.name, cron=task.cron, pipeline_tree=template.pipeline_tree, creator='') data = json.loads(response.content) self.assertTrue(data['result']) self.assertEqual(data['data'], assert_data)
def info_data_from_period_task(task, detail=True): info = { 'id': task.id, 'name': task.name, 'template_id': task.template_id, 'creator': task.creator, 'cron': task.cron, 'enabled': task.enabled, 'last_run_at': strftime_with_timezone(task.last_run_at), 'total_run_count': task.total_run_count, } if detail: info['form'] = task.form info['pipeline_tree'] = task.pipeline_tree return info
def test_get_periodic_task_info__success(self): task = MockPeriodicTask() assert_data = { 'id': task.id, 'name': task.name, 'template_id': task.template_id, 'creator': task.creator, 'cron': task.cron, 'enabled': task.enabled, 'last_run_at': strftime_with_timezone(task.last_run_at), 'total_run_count': task.total_run_count, 'form': task.form, 'pipeline_tree': task.pipeline_tree } with mock.patch(PERIODIC_TASK_GET, MagicMock(return_value=task)): response = self.client.get( path=self.GET_PERIODIC_TASK_INFO_URL.format( task_id=TEST_PERIODIC_TASK_ID, bk_biz_id=TEST_BIZ_CC_ID)) data = json.loads(response.content) self.assertTrue(data['result']) self.assertEqual(data['data'], assert_data)