def new_application(app_name, fields, s3_buckets): ok, message = fields_check(fields) if not ok: raise ArgsCheckError(message, "") try: # check application exist if search_application(app_name): raise ExistError(f"application <{app_name}> had exist", "") # insert fields to metadata fieldsdb = [] for name, field in fields.items(): fieldsdb.append( FieldsDB(name=name, type=field.get('type'), value=field.get('value'), app=app_name)) ids = insert_fields(fieldsdb) # create a application entity collection MongoIns.new_mongo_collection(f"{app_name}_entity") app = Application(name=app_name, fields=ids, buckets=s3_buckets) # create milvus collections create_milvus_collections_by_fields(app) # insert application to metadata app.save() app.fields = fields2dict(search_fields(ids)) return app except Exception as e: logger.error("error happen during create app: %s", str(e), exc_info=True) raise e
def application_detail(name): try: x = search_application(name) if not x: raise NotExistError(f"application {name} not exist", "") fields = search_fields(json.loads(x.fields)) app = Application(name=x.name, fields=fields2dict(fields), buckets=x.s3_buckets) return app except Exception as e: logger.error(e) raise e
def all_applications(): res = [] try: apps = search_application() for x in apps: fields = search_fields(json.loads(x.Application.fields)) app = Application(name=x.Application.name, fields=fields2dict(fields), buckets=x.Application.s3_buckets) res.append(app) logger.info("get all application") return res except Exception as e: logger.error(e) raise e
def delete_application(name): try: if len(entities_list(name, 100, 0)): raise RequestError( "Prevent to delete application with entity not deleted", "") # TODO rewrite clean all resource before change metadata x = del_application(name) if not x: raise NotExistError(f"application {name} not exist", "") x = x[0] fields = search_fields(json.loads(x.fields)) app = Application(name=x.name, fields=fields2dict(fields), buckets=x.s3_buckets) delete_milvus_collections_by_fields(app) delete_fields(json.loads(x.fields)) S3Ins.del_s3_buckets(x.s3_buckets.split(",")) MongoIns.delete_mongo_collection(f"{name}_entity") logger.info("delete application %s", name) return app except Exception as e: logger.error(e) raise e