Exemple #1
0
def create_pipeline(name, processors=None, encoder=None, description=None):
    try:
        p = MongoIns.search_by_name(PIPELINE_COLLECTION_NAME, name)
        if p:
            raise ExistError(f"pipeline <{name}> already exists", "")
        pro = []
        encoder_res = {}
        processor_res = {}
        for processor in processors:
            pr = operator_detail(processor["name"])
            processor_res["operator"] = pr.to_dict()
            processor_res["instance"] = pr.inspect_instance(
                processor["instance"])
            pro.append(processor_res)
        encoder_info = operator_detail(encoder["name"])
        encoder_res["operator"] = encoder_info.to_dict()
        encoder_res["instance"] = encoder_info.inspect_instance(
            encoder["instance"])
        pipe = Pipeline(name, description, pro, encoder_res)
        pipe.metadata = pipe._metadata()
        if pipeline_illegal(pipe):
            raise PipelineIllegalError("Pipeline illegal check error", "")
        MongoIns.insert_documents(PIPELINE_COLLECTION_NAME, pipe.to_dict())
        return pipe
    except Exception as e:
        logger.error(e, exc_info=True)
        raise e
Exemple #2
0
def fetch_operators(url, overwrite=True):
    """fetch operators from origin market

    url -- origin url
    overwrite -- Whether to overwrite local information if the same name exists

    """
    origin = []
    try:
        r = requests.get(url)
        if r.headers.get(MARKET_IDENTITY_HEADER) != "0.1.0":
            raise RequestError("Uncertified market", "")
        if r.status_code != 200:
            raise RequestError(r.text, r.status_code)
    except Exception as e:
        raise RequestError(e.args[0], e)
    for op in r.json():
        origin.append(Operator(op['name'], op['addr'], op['author'],
                               op['version'], op['type'], op['description']))
    local_operators = all_operators()
    local_operator_names = [x.name for x in local_operators]
    for x in origin:
        if x.name not in local_operator_names:
            local_operators.append(x)
        else:
            if overwrite:
                for lop in local_operators:
                    if lop.name == x.name:
                        local_operators.remove(lop)
                        local_operators.append(x)
    MongoIns.delete_mongo_collection(OPERATOR_COLLECTION_NAME)
    for x in local_operators:
        MongoIns.insert_documents(OPERATOR_COLLECTION_NAME, x.to_dict())
    return local_operators
def new_application(app_name, fields, s3_bucket):
    ok, message = fields_check(fields)
    if not ok:
        raise ArgsCheckError(message, "")
    try:
        # check application exist
        if MongoIns.search_by_name(APPLICATION_COLLECTION_NAME, app_name):
            raise ExistError(f"application <{app_name}> had exist", "")
    except ExistError:
        raise
    try:
        for _, value in fields.items():
            if value.get("type") == "pipeline":
                pipe = MongoIns.search_by_name(PIPELINE_COLLECTION_NAME,
                                               value.get("value"))[0]
                ei = identity(
                    pipe.get("encoder").get("instance").get("endpoint"))
                name = f"{app_name}_{pipe.get('encoder').get('instance').get('name').replace('phantoscope_', '')}"
                MilvusIns.new_milvus_collection(name, int(ei["dimension"]),
                                                1024, "l2")
        # create a application entity collection
        MongoIns.new_mongo_collection(f"{app_name}_entity")
        S3Ins.new_s3_buckets(s3_bucket)
        # create milvus collections
        app = Application(name=app_name, fields=fields, bucket=s3_bucket)
        app.metadata = app._metadata()
        MongoIns.insert_documents(APPLICATION_COLLECTION_NAME, app.to_dict())
        return app
    except Exception as e:
        logger.error("error happen during create app: %s",
                     str(e),
                     exc_info=True)
        raise e
Exemple #4
0
def register_operators(name, addr, author, version, type, description):
    try:
        op = Operator(name, addr, author, version, type, description)
        op.metadata = op._metadata()
        if MongoIns.search_by_name(OPERATOR_COLLECTION_NAME, name):
            raise ExistError(f"operator {name} had exist", "")
        MongoIns.insert_documents(OPERATOR_COLLECTION_NAME, op.to_dict())
        return op.to_dict()
    except Exception as e:
        logger.error(f"Unexpected error happen during register operator, {str(e)}", exc_info=True)
        raise e
Exemple #5
0
def upload(name, **kwargs):
    try:
        app = application_detail(name)
        if not app:
            raise NotExistError("application not exist", "application %s not exist" % name)
        bucket_name = app.buckets.split(",")[0]
        accept_fields = [x for x, y in app.fields.items() if y.get('type') != "pipeline"]
        pipeline_fields = {x: y['value'] for x, y in app.fields.items() if y.get('type') == "pipeline"}
        new_fields = app.fields.copy()
        for k, v in kwargs.items():
            if k in accept_fields:
                new_fields[k]['value'] = v
        res = []
        for k, _ in kwargs.get('fields').items():
            if k not in accept_fields and k not in pipeline_fields:
                raise RequestError(f"fields {k} not in application", "")

        docs = {}
        valid_field_flag = False
        for n, p in pipeline_fields.items():
            pipe = pipeline_detail(p)
            if not pipe:
                raise NotExistError("pipeline not exist", "pipeline %s not exist" % p)
            value = kwargs['fields'].get(n)
            if not value:
                continue
            valid_field_flag = True
            file_data = value.get('data')
            url = value.get('url')
            if not file_data and not url:
                raise RequestError("can't find data or url from request", "")
            file_name = "{}-{}".format(name, uuid.uuid4().hex)
            file_path = save_tmp_file(file_name, file_data, url)

            S3Ins.upload2bucket(bucket_name, file_path, file_name)

            vectors = run_pipeline(pipe, data=file_data, url=url)
            if not vectors:
                raise NoneVectorError("can't encode data by encoder, check input or encoder", "")

            milvus_collection_name = f"{app.name}_{pipe.encoder['name']}_{pipe.encoder['instance']}"
            vids = MilvusIns.insert_vectors(milvus_collection_name, vectors)

            docs[n] = {"ids": vids, "url": gen_url(bucket_name, file_name)}
            doc_id = MongoIns.insert_documents(f"{app.name}_entity", docs)
            res.append(new_mapping_ins(docs))
        if not valid_field_flag:
            raise RequestError("none valid field exist", "")
        return res
    except Exception as e:
        err_msg = f"Unexpected error happen when upload: {str(e)}"
        logger.error(err_msg, exc_info=True)
        raise UnexpectedError(err_msg, e)
Exemple #6
0
def upload(name, **kwargs):
    try:
        app = application_detail(name)
        if not app:
            raise NotExistError("application not exist", "application %s not exist" % name)
        bucket_name = app.buckets.split(",")[0]
        accept_fields = [x for x, y in app.fields.items() if y.get('type') != "pipeline"]
        pipeline_fields = {x: y['value'] for x, y in app.fields.items() if y.get('type') == "pipeline"}
        new_fields = app.fields.copy()
        for k, v in kwargs.items():
            if k in accept_fields:
                new_fields[k]['value'] = v
        res = []
        for k, _ in kwargs.get('fields').items():
            if k not in accept_fields and k not in pipeline_fields:
                raise RequestError(f"fields {k} not in application", "")
        docs = {}
        for n, p in pipeline_fields.items():
            pipe = pipeline_detail(p)
            if not pipe:
                raise NotExistError("pipeline not exist", "pipeline %s not exist" % p)
            value = kwargs['fields'].get(n)
            file_data = value.get('data')
            url = value.get('url')
            if not file_data and not url:
                raise RequestError("can't find data or url from request", "")
            file_name = "{}-{}".format(name, uuid.uuid4().hex)
            file_path = save_tmp_file(file_name, file_data, url)

            S3Ins.upload2bucket(bucket_name, file_path, file_name)

            vectors = run_pipeline(pipe, data=file_data, url=url)

            milvus_collection_name = f"{app.name}_{pipe.encoder['name']}_{pipe.encoder['instance']}"
            vids = MilvusIns.insert_vectors(milvus_collection_name, vectors)
            docs[n] = {"ids": vids, "url": gen_url(bucket_name, file_name)}
            doc_id = MongoIns.insert_documents(f"{app.name}_entity", docs)
            res.append(new_mapping_ins(docs))
                         fields=new_fields))
        return res