Esempio n. 1
0
def tag_model(operation):
    if operation not in ['retrieve', 'create', 'remove']:
        return get_json_result(
            100, "'{}' is not currently supported.".format(operation))

    request_data = request.json
    model = MLModel.get_or_none(
        MLModel.f_model_version == request_data.get("job_id"))
    if not model:
        raise Exception("Can not found model by job id: '{}'.".format(
            request_data.get("job_id")))

    if operation == 'retrieve':
        res = {'tags': []}
        tags = (Tag.select().join(
            ModelTag, on=ModelTag.f_t_id == Tag.f_id).where(
                ModelTag.f_m_id == model.f_model_version))
        for tag in tags:
            res['tags'].append({'name': tag.f_name, 'description': tag.f_desc})
        res['count'] = tags.count()
        return get_json_result(data=res)
    elif operation == 'remove':
        tag = Tag.get_or_none(Tag.f_name == request_data.get('tag_name'))
        if not tag:
            raise Exception("Can not found '{}' tag.".format(
                request_data.get('tag_name')))
        tags = (Tag.select().join(
            ModelTag, on=ModelTag.f_t_id == Tag.f_id).where(
                ModelTag.f_m_id == model.f_model_version))
        if tag.f_name not in [t.f_name for t in tags]:
            raise Exception("Model {} {} does not have tag '{}'.".format(
                model.f_model_id, model.f_model_version, tag.f_name))
        delete_query = ModelTag.delete().where(
            ModelTag.f_m_id == model.f_model_version,
            ModelTag.f_t_id == tag.f_id)
        delete_query.execute()
        return get_json_result(
            retmsg="'{}' tag has been removed from tag list of model {} {}.".
            format(request_data.get('tag_name'), model.f_model_id,
                   model.f_model_version))
    else:
        if not str(request_data.get('tag_name')):
            raise Exception("Tag name should not be an empty string.")
        tag = Tag.get_or_none(Tag.f_name == request_data.get('tag_name'))
        if not tag:
            tag = Tag()
            tag.f_name = request_data.get('tag_name')
            tag.save(force_insert=True)
        else:
            tags = (Tag.select().join(
                ModelTag, on=ModelTag.f_t_id == Tag.f_id).where(
                    ModelTag.f_m_id == model.f_model_version))
            if tag.f_name in [t.f_name for t in tags]:
                raise Exception(
                    "Model {} {} already been tagged as tag '{}'.".format(
                        model.f_model_id, model.f_model_version, tag.f_name))
        ModelTag.create(f_t_id=tag.f_id, f_m_id=model.f_model_version)
        return get_json_result(
            retmsg="Adding {} tag for model with job id: {} successfully.".
            format(request_data.get('tag_name'), request_data.get('job_id')))
Esempio n. 2
0
def operate_tag(tag_operation):
    request_data = request.json
    if tag_operation not in [
            TagOperation.CREATE, TagOperation.RETRIEVE, TagOperation.UPDATE,
            TagOperation.DESTROY, TagOperation.LIST
    ]:
        raise Exception('The {} operation is not currently supported.'.format(
            tag_operation))

    tag_name = request_data.get('tag_name')
    tag_desc = request_data.get('tag_desc')
    if tag_operation == TagOperation.CREATE:
        try:
            if not tag_name:
                return get_json_result(
                    100,
                    "'{}' tag created failed. Please input a valid tag name.".
                    format(tag_name))
            else:
                Tag.create(f_name=tag_name, f_desc=tag_desc)
        except peewee.IntegrityError:
            raise Exception(
                "'{}' has already exists in database.".format(tag_name))
        else:
            return get_json_result(
                "'{}' tag has been created successfully.".format(tag_name))

    elif tag_operation == TagOperation.LIST:
        tags = Tag.select()
        limit = request_data.get('limit')
        res = {"tags": []}

        if limit > len(tags):
            count = len(tags)
        else:
            count = limit
        for tag in tags[:count]:
            res['tags'].append({
                'name':
                tag.f_name,
                'description':
                tag.f_desc,
                'model_count':
                ModelTag.filter(ModelTag.f_t_id == tag.f_id).count()
            })
        return get_json_result(data=res)

    else:
        if not (tag_operation == TagOperation.RETRIEVE
                and not request_data.get('with_model')):
            try:
                tag = Tag.get(Tag.f_name == tag_name)
            except peewee.DoesNotExist:
                raise Exception("Can not found '{}' tag.".format(tag_name))

        if tag_operation == TagOperation.RETRIEVE:
            if request_data.get('with_model', False):
                res = {'models': []}
                models = (MLModel.select().join(
                    ModelTag,
                    on=ModelTag.f_m_id == MLModel.f_model_version).where(
                        ModelTag.f_t_id == tag.f_id))
                for model in models:
                    res["models"].append({
                        "model_id": model.f_model_id,
                        "model_version": model.f_model_version,
                        "model_size": model.f_size,
                        "role": model.f_role,
                        "party_id": model.f_party_id
                    })
                res["count"] = models.count()
                return get_json_result(data=res)
            else:
                tags = Tag.filter(Tag.f_name.contains(tag_name))
                if not tags:
                    return get_json_result(100, retmsg="No tags found.")
                res = {'tags': []}
                for tag in tags:
                    res['tags'].append({
                        'name': tag.f_name,
                        'description': tag.f_desc
                    })
                return get_json_result(data=res)

        elif tag_operation == TagOperation.UPDATE:
            new_tag_name = request_data.get('new_tag_name', None)
            new_tag_desc = request_data.get('new_tag_desc', None)
            if (tag.f_name == new_tag_name) and (tag.f_desc == new_tag_desc):
                return get_json_result(100, "Nothing to be updated.")
            else:
                if request_data.get('new_tag_name'):
                    if not Tag.get_or_none(Tag.f_name == new_tag_name):
                        tag.f_name = new_tag_name
                    else:
                        return get_json_result(
                            100,
                            retmsg="'{}' tag already exists.".format(
                                new_tag_name))

                tag.f_desc = new_tag_desc
                tag.save()
                return get_json_result(
                    retmsg=
                    "Infomation of '{}' tag has been updated successfully.".
                    format(tag_name))

        else:
            delete_query = ModelTag.delete().where(ModelTag.f_t_id == tag.f_id)
            delete_query.execute()
            Tag.delete_instance(tag)
            return get_json_result(
                retmsg="'{}' tag has been deleted successfully.".format(
                    tag_name))