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);
}
}
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;
}
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();
}
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;
}
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);
}
}
Aggregations