def create_label(self, label_name, label_alias): label_id = make_uuid("labels") create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') label = Labels(label_id=label_id, label_name=label_name, label_alias=label_alias, create_time=create_time) label.save() return label
def add_label(self, label_alias): if not label_alias: raise ParamsError("标签名不能为空") labels = Labels.objects.filter(label_alias=label_alias) if labels: raise LabelExistError("标签名{0}已存在".format(label_alias)) label_name = self.chinese2pinyin(label_alias) while Labels.objects.filter(label_name=label_name).count() > 0: label_name = label_name + make_uuid(label_name)[-3:] label_id = make_uuid(label_name) label = Labels(label_id=label_id, label_name=label_name, label_alias=label_alias) label.save() return label
def _sync_labels(labels): label_names = [ label.label_name for label in label_repo.get_all_labels() ] new_labels = [] for label_name in labels: if label_name in label_names: continue new_labels.append( Labels( label_id=make_uuid(), label_name=label_name, label_alias=label_name, create_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), )) label_repo.bulk_create(new_labels)
def get(self, request, *args, **kwargs): """ 添加特性获取可用标签 :param request: :param args: :param kwargs: :return: """ try: # 节点添加的标签和数据中心查询回来的标签才可被组件使用 node_labels = node_label_repo.get_all_labels() labels_list = list() labels_name_list = list() if node_labels: node_labels_id_list = [label.label_id for label in node_labels] label_obj_list = label_repo.get_labels_by_label_ids( node_labels_id_list) for label_obj in label_obj_list: labels_name_list.append(label_obj.label_name) # 查询数据中心可使用的标签 try: code, msg, data_list = label_service.get_region_labels( self.tenant, self.service) if code == 200: if data_list: label_name_list = [] labels = label_repo.get_all_labels() if labels: for label in labels: label_name_list.append(label.label_name) for label_name in data_list: if label_name not in label_name_list: label_id = make_uuid("labels") create_time = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') label = Labels(label_id=label_id, label_name=label_name, label_alias=label_name, create_time=create_time) label.save() labels_name_list.append(label_name) except Exception as e: logger.exception(e) pass # 去除该组件已绑定的标签 service_labels = service_label_repo.get_service_labels( self.service.service_id) if service_labels: service_labels_id_list = [l.label_id for l in service_labels] label_obj_list = label_repo.get_labels_by_label_ids( service_labels_id_list) service_labels_name_list = [ l.label_name for l in label_obj_list ] for service_labels_name in service_labels_name_list: if service_labels_name in labels_name_list: labels_name_list.remove(service_labels_name) for labels_name in labels_name_list: label_dict = dict() label_oj = label_repo.get_labels_by_label_name(labels_name) label_dict["label_id"] = label_oj.label_id label_dict["label_alias"] = label_oj.label_alias labels_list.append(label_dict) result = general_message(200, "success", "查询成功", list=labels_list) except Exception as e: logger.exception(e) result = error_message(e.message) return Response(result, status=result["code"])
def get(self, request, *args, **kwargs): """ 添加特性获取可用标签 :param request: :param args: :param kwargs: :return: """ try: # 判断表中是否有windows标签 labels = label_repo.get_all_labels() if labels: labels_name_list = [label.label_name for label in labels] if "windows" not in labels_name_list: label_id = make_uuid("windows") create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') label = Labels(label_id=label_id, label_name="windows", label_alias="windows", create_time=create_time) label.save() else: label_id = make_uuid("windows") create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') label = Labels(label_id=label_id, label_name="windows", label_alias="windows", create_time=create_time) label.save() # 节点添加的标签和windows标签才可被服务使用 node_labels = node_label_repo.get_all_labels() labels_list = list() if node_labels: node_labels_id_list = [label.label_id for label in node_labels] label_obj_list = label_repo.get_labels_by_label_ids(node_labels_id_list) labels_name_list = [label.label_name for label in label_obj_list] # 如果节点添加的标签中没有windows标签,就添加进去 if "windows" not in labels_name_list: labels_name_list.append("windows") # 去除该服务已绑定的标签 service_labels = service_label_repo.get_service_labels(self.service.service_id) if service_labels: service_labels_id_list = [label.label_id for label in service_labels] label_obj_list = label_repo.get_labels_by_label_ids(service_labels_id_list) service_labels_name_list = [label.label_name for label in label_obj_list] for service_labels_name in service_labels_name_list: if service_labels_name in labels_name_list: labels_name_list.remove(service_labels_name) for labels_name in labels_name_list: label_dict = dict() label_oj = label_repo.get_labels_by_label_name(labels_name) label_dict["label_id"] = label_oj.label_id label_dict["label_alias"] = label_oj.label_alias labels_list.append(label_dict) # 暂时只支持除节点标签外的windows标签 else: label_name = "windows" label_dict = dict() win_label = label_repo.get_labels_by_label_name(label_name) label_dict["label_id"] = win_label.label_id label_dict["label_alias"] = win_label.label_alias labels_list.append(label_dict) result = general_message(200, "success", "查询成功", list=labels_list) except Exception as e: logger.exception(e) result = error_message(e.message) return Response(result, status=result["code"])