def get_model_configuration(job_id, role, party_id): models = MLModel.select(MLModel.f_dsl, MLModel.f_runtime_conf, MLModel.f_train_runtime_conf).where(MLModel.f_job_id == job_id, MLModel.f_role == role, MLModel.f_party_id == party_id) if models: model = models[0] return model.f_dsl, model.f_runtime_conf, model.f_train_runtime_conf else: return {}, {}, {}
def models_group_by_party_model_id_and_model_version(): args = [ MLModel.f_role, MLModel.f_party_id, MLModel.f_model_id, MLModel.f_model_version, ] models = MLModel.select(*args).group_by(*args) for model in models: model.f_party_model_id = gen_party_model_id(role=model.f_role, party_id=model.f_party_id, model_id=model.f_model_id) return models
def query_model_info_from_db(model_version, role=None, party_id=None, model_id=None, query_filters=None, **kwargs): conditions = [] filters = [] aruments = locals() cond_attrs = [ attr for attr in ['model_version', 'model_id', 'role', 'party_id'] if aruments[attr] ] for f_n in cond_attrs: conditions.append( operator.attrgetter('f_%s' % f_n)(MLModel) == aruments[f_n]) for f_n in kwargs: if hasattr(MLModel, 'f_%s' % f_n): conditions.append(operator.attrgetter('f_%s' % f_n)(MLModel)) if query_filters and isinstance(query_filters, list): for attr in query_filters: attr_name = 'f_%s' % attr if hasattr(MLModel, attr_name): filters.append(operator.attrgetter(attr_name)(MLModel)) if filters: models = MLModel.select(*filters).where(*conditions) else: models = MLModel.select().where(*conditions) if models: return 0, 'Query model info from db success.', [ model.to_json() for model in models ] else: return 100, 'Query model info failed, cannot find model from db. ', []
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))