Search in sources :

Example 1 with ModelRegistryException

use of io.hops.hopsworks.exceptions.ModelRegistryException in project hopsworks by logicalclocks.

the class ModelsResource method get.

@ApiOperation(value = "Get a model", response = ModelDTO.class)
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
@JWTRequired(acceptedTokens = { Audience.API, Audience.JOB }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiKeyRequired(acceptedScopes = { ApiScope.MODELREGISTRY }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
public Response get(@PathParam("id") String id, @BeanParam ModelsBeanParam modelsBeanParam, @Context UriInfo uriInfo, @Context SecurityContext sc) throws ProvenanceException, ModelRegistryException, DatasetException, GenericException, SchematizedTagException, MetadataException {
    Users user = jwtHelper.getUserPrincipal(sc);
    ResourceRequest resourceRequest = new ResourceRequest(ResourceRequest.Name.MODELS);
    resourceRequest.setExpansions(modelsBeanParam.getExpansions().getResources());
    ProvStateDTO fileState = modelsController.getModel(modelRegistryProject, id);
    if (fileState != null) {
        ModelDTO dto = modelsBuilder.build(uriInfo, resourceRequest, user, userProject, modelRegistryProject, fileState, modelUtils.getModelsDatasetPath(userProject, modelRegistryProject));
        if (dto == null) {
            throw new GenericException(RESTCodes.GenericErrorCode.NOT_AUTHORIZED_TO_ACCESS, Level.FINE);
        } else {
            return Response.ok().entity(dto).build();
        }
    } else {
        throw new ModelRegistryException(RESTCodes.ModelRegistryErrorCode.MODEL_NOT_FOUND, Level.FINE);
    }
}
Also used : ModelDTO(io.hops.hopsworks.api.modelregistry.models.dto.ModelDTO) Users(io.hops.hopsworks.persistence.entity.user.Users) ResourceRequest(io.hops.hopsworks.common.api.ResourceRequest) ProvStateDTO(io.hops.hopsworks.common.provenance.state.dto.ProvStateDTO) GenericException(io.hops.hopsworks.exceptions.GenericException) ModelRegistryException(io.hops.hopsworks.exceptions.ModelRegistryException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) ApiKeyRequired(io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 2 with ModelRegistryException

use of io.hops.hopsworks.exceptions.ModelRegistryException in project hopsworks by logicalclocks.

the class ModelRegistryBuilder method build.

// Build collection
public ModelRegistryDTO build(UriInfo uriInfo, ResourceRequest resourceRequest, Users user, Project project) throws GenericException, ModelRegistryException, SchematizedTagException, MetadataException {
    ModelRegistryDTO dto = new ModelRegistryDTO();
    uri(dto, uriInfo, project);
    expand(dto, resourceRequest);
    Collection<Dataset> dsInProject = project.getDatasetCollection();
    // Add all datasets shared with the project
    dsInProject.addAll(project.getDatasetSharedWithCollection().stream().filter(DatasetSharedWith::getAccepted).map(DatasetSharedWith::getDataset).collect(Collectors.toList()));
    Collection<Dataset> modelsDatasets = dsInProject.stream().filter(ds -> ds.getName().equals(Settings.HOPS_MODELS_DATASET)).collect(Collectors.toList());
    dto.setCount((long) modelsDatasets.size());
    for (Dataset ds : modelsDatasets) {
        ModelRegistryDTO modelRegistryDTO = build(uriInfo, resourceRequest, user, project, ds.getProject());
        if (modelRegistryDTO != null) {
            dto.addItem(modelRegistryDTO);
        }
    }
    return dto;
}
Also used : Stateless(javax.ejb.Stateless) ModelRegistryDTO(io.hops.hopsworks.api.modelregistry.dto.ModelRegistryDTO) ModelsBuilder(io.hops.hopsworks.api.modelregistry.models.ModelsBuilder) Collection(java.util.Collection) ModelRegistryException(io.hops.hopsworks.exceptions.ModelRegistryException) Collectors(java.util.stream.Collectors) Project(io.hops.hopsworks.persistence.entity.project.Project) Settings(io.hops.hopsworks.common.util.Settings) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) TransactionAttributeType(javax.ejb.TransactionAttributeType) GenericException(io.hops.hopsworks.exceptions.GenericException) DatasetSharedWith(io.hops.hopsworks.persistence.entity.dataset.DatasetSharedWith) MetadataException(io.hops.hopsworks.exceptions.MetadataException) TransactionAttribute(javax.ejb.TransactionAttribute) ResourceRequest(io.hops.hopsworks.common.api.ResourceRequest) UriInfo(javax.ws.rs.core.UriInfo) Users(io.hops.hopsworks.persistence.entity.user.Users) EJB(javax.ejb.EJB) SchematizedTagException(io.hops.hopsworks.exceptions.SchematizedTagException) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) DatasetSharedWith(io.hops.hopsworks.persistence.entity.dataset.DatasetSharedWith) ModelRegistryDTO(io.hops.hopsworks.api.modelregistry.dto.ModelRegistryDTO)

Example 3 with ModelRegistryException

use of io.hops.hopsworks.exceptions.ModelRegistryException in project hopsworks by logicalclocks.

the class ModelConverter method castMetricsToDouble.

private String castMetricsToDouble(String modelSummaryStr) throws ModelRegistryException {
    JSONObject modelSummary = new JSONObject(modelSummaryStr);
    if (modelSummary.has("metrics")) {
        JSONObject metrics = modelSummary.getJSONObject("metrics");
        for (Object metric : metrics.keySet()) {
            String metricKey = null;
            try {
                metricKey = (String) metric;
            } catch (Exception e) {
                throw new ModelRegistryException(RESTCodes.ModelRegistryErrorCode.KEY_NOT_STRING, Level.FINE, "keys in metrics dict must be string", e.getMessage(), e);
            }
            try {
                metrics.put(metricKey, Double.valueOf(metrics.getString(metricKey)));
            } catch (Exception e) {
                throw new ModelRegistryException(RESTCodes.ModelRegistryErrorCode.METRIC_NOT_NUMBER, Level.FINE, "Provided value for metric " + metricKey + " is not a number", e.getMessage(), e);
            }
        }
        modelSummary.put("metrics", metrics);
    }
    return modelSummary.toString();
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) IOException(java.io.IOException) ModelRegistryException(io.hops.hopsworks.exceptions.ModelRegistryException) JAXBException(javax.xml.bind.JAXBException) ModelRegistryException(io.hops.hopsworks.exceptions.ModelRegistryException)

Example 4 with ModelRegistryException

use of io.hops.hopsworks.exceptions.ModelRegistryException in project hopsworks by logicalclocks.

the class ModelsBuilder method build.

// Build collection
public ModelDTO build(UriInfo uriInfo, ResourceRequest resourceRequest, Users user, Project userProject, Project modelRegistryProject) throws ModelRegistryException, GenericException, SchematizedTagException, MetadataException {
    ModelDTO dto = new ModelDTO();
    uri(dto, uriInfo, userProject, modelRegistryProject);
    expand(dto, resourceRequest);
    dto.setCount(0l);
    if (dto.isExpand()) {
        validatePagination(resourceRequest);
        ProvStateDTO fileState;
        try {
            Pair<ProvStateParamBuilder, ModelRegistryDTO> provFilesParamBuilder = buildModelProvenanceParams(userProject, modelRegistryProject, resourceRequest);
            if (provFilesParamBuilder.getValue1() == null) {
                // no endpoint - no results
                return dto;
            }
            fileState = provenanceController.provFileStateList(provFilesParamBuilder.getValue1().getParentProject(), provFilesParamBuilder.getValue0());
            List<ProvStateDTO> models = new LinkedList<>(fileState.getItems());
            dto.setCount(fileState.getCount());
            String modelsDatasetPath = modelUtils.getModelsDatasetPath(userProject, modelRegistryProject);
            for (ProvStateDTO fileProvStateHit : models) {
                ModelDTO modelDTO = build(uriInfo, resourceRequest, user, userProject, modelRegistryProject, fileProvStateHit, modelsDatasetPath);
                if (modelDTO != null) {
                    dto.addItem(modelDTO);
                }
            }
        } catch (ProvenanceException e) {
            if (ProvHelper.missingMappingForField(e)) {
                LOGGER.log(Level.WARNING, "Could not find elastic mapping for experiments query", e);
                return dto;
            } else {
                throw new ModelRegistryException(RESTCodes.ModelRegistryErrorCode.MODEL_LIST_FAILED, Level.FINE, "Unable to list models for project " + modelRegistryProject.getName(), e.getMessage(), e);
            }
        } catch (DatasetException e) {
            throw new ModelRegistryException(RESTCodes.ModelRegistryErrorCode.MODEL_LIST_FAILED, Level.FINE, "Unable to list models for project " + modelRegistryProject.getName(), e.getMessage(), e);
        }
    }
    return dto;
}
Also used : ProvStateParamBuilder(io.hops.hopsworks.common.provenance.state.ProvStateParamBuilder) ModelDTO(io.hops.hopsworks.api.modelregistry.models.dto.ModelDTO) ProvenanceException(io.hops.hopsworks.exceptions.ProvenanceException) ModelRegistryDTO(io.hops.hopsworks.api.modelregistry.dto.ModelRegistryDTO) ProvStateDTO(io.hops.hopsworks.common.provenance.state.dto.ProvStateDTO) LinkedList(java.util.LinkedList) ModelRegistryException(io.hops.hopsworks.exceptions.ModelRegistryException) DatasetException(io.hops.hopsworks.exceptions.DatasetException)

Example 5 with ModelRegistryException

use of io.hops.hopsworks.exceptions.ModelRegistryException in project hopsworks by logicalclocks.

the class ModelConverter method createUnmarshaller.

private Unmarshaller createUnmarshaller() throws ModelRegistryException {
    try {
        Unmarshaller unmarshaller = jaxbModelsContext.createUnmarshaller();
        unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
        return unmarshaller;
    } catch (JAXBException e) {
        throw new ModelRegistryException(RESTCodes.ModelRegistryErrorCode.MODEL_MARSHALLING_FAILED, Level.INFO, "Failed to unmarshal", "Error occurred during unmarshalling setup", e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) Unmarshaller(javax.xml.bind.Unmarshaller) ModelRegistryException(io.hops.hopsworks.exceptions.ModelRegistryException)

Aggregations

ModelRegistryException (io.hops.hopsworks.exceptions.ModelRegistryException)7 JAXBException (javax.xml.bind.JAXBException)3 ModelRegistryDTO (io.hops.hopsworks.api.modelregistry.dto.ModelRegistryDTO)2 ModelDTO (io.hops.hopsworks.api.modelregistry.models.dto.ModelDTO)2 ResourceRequest (io.hops.hopsworks.common.api.ResourceRequest)2 ProvStateDTO (io.hops.hopsworks.common.provenance.state.dto.ProvStateDTO)2 DatasetException (io.hops.hopsworks.exceptions.DatasetException)2 GenericException (io.hops.hopsworks.exceptions.GenericException)2 Dataset (io.hops.hopsworks.persistence.entity.dataset.Dataset)2 Project (io.hops.hopsworks.persistence.entity.project.Project)2 Users (io.hops.hopsworks.persistence.entity.user.Users)2 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)1 ApiKeyRequired (io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired)1 ModelsBuilder (io.hops.hopsworks.api.modelregistry.models.ModelsBuilder)1 ProvStateParamBuilder (io.hops.hopsworks.common.provenance.state.ProvStateParamBuilder)1 Settings (io.hops.hopsworks.common.util.Settings)1 MetadataException (io.hops.hopsworks.exceptions.MetadataException)1 ProvenanceException (io.hops.hopsworks.exceptions.ProvenanceException)1 SchematizedTagException (io.hops.hopsworks.exceptions.SchematizedTagException)1 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)1