def update_platform(cls, _id, p_name, p_logo, p_type, user_id, **kargs): """ 更新应用 """ try: # 除当前修改的名称以外,是否还存在要修改的名称 data = platform_col.find({"p_name": p_name, "_id": {"$ne": _id}}) if data.count() != 0: return -1001 else: data = platform_col.update({"_id": _id}, { "$set": { "p_name": p_name, "p_logo": p_logo, "p_type": p_type, "update_time": Util.timeFormat() } }) if data["ok"] == 1: log_str = "应用名称:{};应用ID:{};应用logo:{};应用类型:{}".format( p_name, _id, p_logo, p_type) log_result = LogModel.add_log("修改应用", log_str, user_id, "update") return 1 else: return 0 except Exception as e: current_app.logger.error(e) return 0
def insert_platform(cls, p_name, p_logo, p_type, user_id, **kargs): """ 添加应用 """ try: data = platform_col.find({"p_name": p_name}) if data.count() != 0: return -1001 else: p_id = str(uuid.uuid1()) platform_col.insert({ "_id": p_id, "p_name": p_name, "p_logo": p_logo, "p_type": p_type, "create_time": Util.timeFormat(), "deleted": 0, "creator_id": user_id }) log_str = "应用名称:{};应用ID:{};应用logo:{};应用类型:{}".format( p_name, p_id, p_logo, p_type) log_result = LogModel.add_log("添加应用", log_str, user_id, "insert") return 1 except Exception as e: current_app.logger.error(e) return 0
def update_test_info(cls, p_id, t_id, t_name, t_str, t_desc, user_id): """ 修改实验信息 """ try: data = test_col.find({ "p_id": p_id, "t_name": t_name, }) test_col.update({ "p_id": p_id, "_id": t_id, "t_id": { "$ne": t_id } }, { "$set": { "t_name": t_name, "t_str": t_str, "t_desc": t_desc, "update_time": Util.timeFormat() } }) log_str = "实验名称:{};实验ID:{};实验描述:{};加盐字符:{}".format( t_name, t_id, t_desc, t_str) log_result = LogModel.add_log("修改实验", log_str, user_id, "update") return 1 except Exception as e: current_app.logger.error(e) return 0
def add_test(cls, p_id, t_name, t_str, t_desc, user_id): """ 创建实验 """ try: data = test_col.find({"p_id": p_id, "t_name": t_name}) data_len = len(list(data)) if data_len != 0: return -2001 else: t_id = str(uuid.uuid1()) test_col.insert({ "_id": t_id, "p_id": p_id, "t_name": t_name, "t_str": t_str, "t_desc": t_desc, "t_status": 1, "create_time": Util.timeFormat(), "creator_id": user_id }) log_str = "实验名称:{};实验ID:{};实验描述:{};加盐字符:{}".format( t_name, t_id, t_desc, t_str) log_result = LogModel.add_log("创建实验", log_str, user_id, "insert") return 1 except Exception as e: current_app.logger.error(e) return 0
def delete(cls, s_id, b_id, user_id): """ 删除分桶 """ try: bucket_col.remove({"_id": b_id}) log_str = "策略ID:{};分桶ID:{}".format(s_id, b_id) log_result = LogModel.add_log("删除分桶", log_str, user_id, "remove") if bucket_col.find({"s_id": s_id}).count() == 0: stragegy_col.remove({"s_id": s_id}) return 1 except Exception as e: current_app.logger.error(e) return 0
def delete_platform(cls, p_id, user_id): """ 软删除应用 """ try: data = platform_col.remove({"_id": p_id}) test_data = test_col.remove({"p_id": p_id}) if data["ok"] == 1 and test_data["ok"] == 1: log_str = "应用ID:{};".format(p_id) log_result = LogModel.add_log("删除应用", log_str, user_id, "delete") return 1 else: return 0 except Exception as e: current_app.logger.error(e) return -1
def toggle_status(cls, t_id, status, user_id): """ 改变实验状态 """ try: data = test_col.update({"_id": t_id}, { "$set": { "t_status": int(status), "update_time": Util.timeFormat() } }) if data["ok"] == 1: test_status = "启用" if int(status) == 1 else "停用" log_str = "实验ID:{};状态:{}".format(t_id, test_status) log_result = LogModel.add_log("启用/停用实验", log_str, user_id, "update") return 1 else: return 0 except Exception as e: current_app.logger.error(e) return 0
def insert_stragegy(cls, t_id, s_name, s_desc, section_min, section_max, user_id): """ 新增策略 """ try: # 分桶区间逻辑判 # 查询实验的所有策略 data = stragegy_col.aggregate([{ "$match": { "t_id": t_id } }, { "$lookup": { "from": "buckets", "localField": "s_id", "foreignField": "s_id", "as": "buckets" } }, { "$unwind": { "path": "$buckets", "preserveNullAndEmptyArrays": True } }]) # 如果有策略则进行判断(区间是否有重合) data_list = list(data) data_len = len(data_list) s_id = "" if data_len > 0: for item in range(data_len): temp_min = int( data_list[item].get("buckets").get("section_min")) temp_max = int( data_list[item].get("buckets").get("section_max")) if max(temp_min, int(section_min)) <= min( temp_max, int(section_max)): return -3002 if s_name == data_list[item].get("s_name"): s_id = data_list[item].get("_id") if s_id == "": # 如果没有策略则直接插入 _id = str(uuid.uuid1()) s_id = stragegy_col.insert({ "_id": _id, "s_id": _id, "t_id": t_id, "s_name": s_name, "create_time": Util.timeFormat() }) bucket_id = str(uuid.uuid1()) bucket_col.insert({ "_id": bucket_id, "s_id": s_id, "t_id": t_id, "s_desc": s_desc, "section_min": section_min, "section_max": section_max, "create_time": Util.timeFormat(), "creator_id": user_id }) log_str = "策略名称:{};策略ID:{};策略描述:{};分桶ID:{};分桶区间:{}".format( s_name, s_id, s_desc, bucket_id, str(section_min) + " ~ " + str(section_max)) log_result = LogModel.add_log("创建策略", log_str, user_id, "insert") return 1 except Exception as e: current_app.logger.error(e) return 0
def update_stragegy(cls, t_id, s_id, b_id, s_name, s_desc, section_min, section_max, user_id): """ 更新策略 """ try: # 更新策略描述 stragegy_col.update({ "s_id": s_id, "s_desc": { "$ne": s_desc } }, {"$set": { "s_desc": s_desc, "update_time": Util.timeFormat() }}) # 更新分桶 bucket_data = bucket_col.find({"t_id": t_id, "_id": {"$ne": b_id}}) for item in bucket_data: temp_min = int(item.get("section_min")) temp_max = int(item.get("section_max")) if max(temp_min, int(section_min)) <= min( temp_max, section_max): return -3002 data = bucket_col.update( { "s_id": s_id, "_id": b_id, "$or": [{ "section_min": { "$ne": section_min } }, { "section_max": { "$ne": section_max } }, { "s_desc": { "$ne": s_desc } }] }, { "$set": { "s_desc": s_desc, "section_min": section_min, "section_max": section_max, "update_time": Util.timeFormat() } }) if data.get("ok") == 1: log_str = "策略ID:{};策略描述:{};分桶ID:{};分桶区间:{}".format( s_id, s_desc, b_id, str(section_min) + " ~ " + str(section_max)) log_result = LogModel.add_log("修改策略", log_str, user_id, "update") return 1 else: return 0 except Exception as e: current_app.logger.error(e) return 0