use of io.hops.hopsworks.api.provenance.ops.dto.ProvArtifactUsageParentDTO in project hopsworks by logicalclocks.
the class ProjectProvenanceResource method usage.
@GET
@Path("usage")
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_SCIENTIST, AllowedProjectRoles.DATA_OWNER })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiKeyRequired(acceptedScopes = { ApiScope.PROJECT }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiOperation(value = "Artifact usage", response = ProvArtifactUsageParentDTO.class)
public Response usage(@QueryParam("artifact_id") String artifactId, @QueryParam("endpoint_id") Integer endpointId, @QueryParam("artifact_type") DatasetAccessType accessType, @BeanParam ProvUsageBeanParams params, @Context UriInfo uriInfo, @Context SecurityContext sc) throws ProvenanceException, GenericException, DatasetException, MetadataException, SchematizedTagException {
Users user = jWTHelper.getUserPrincipal(sc);
if (artifactId == null) {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "artifactId id cannot be null");
}
Project targetProject = project;
if (endpointId != null) {
targetProject = projectFacade.findById(endpointId).orElseThrow(() -> new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "target project not found"));
}
Dataset targetEndpoint;
if (accessType != null) {
try {
switch(accessType) {
case FEATUREGROUPS:
targetEndpoint = fsCtrl.getProjectFeaturestoreDataset(targetProject);
break;
case TRAININGDATASETS:
String tdName = project.getName() + "_" + Settings.ServiceDataset.TRAININGDATASETS.getName();
targetEndpoint = datasetCtrl.getByName(targetProject, tdName);
break;
case MODELS:
targetEndpoint = datasetCtrl.getByName(targetProject, Settings.HOPS_MODELS_DATASET);
break;
case EXPERIMENTS:
targetEndpoint = datasetCtrl.getByName(targetProject, Settings.HOPS_EXPERIMENTS_DATASET);
break;
default:
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "access type not supports:" + accessType);
}
} catch (FeaturestoreException | DatasetException e) {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_STATE, Level.FINE, "cannot access the dataset of the artifact");
}
} else {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_STATE, Level.FINE, "access type not defined");
}
DatasetPath targetEndpointPath = datasetHelper.getTopLevelDatasetPath(project, targetEndpoint);
ProvArtifactUsageParentDTO status = usageBuilder.buildAccessible(uriInfo, user, targetEndpointPath, artifactId, params.getUsageType());
return Response.ok().entity(status).build();
}
use of io.hops.hopsworks.api.provenance.ops.dto.ProvArtifactUsageParentDTO in project hopsworks by logicalclocks.
the class ProvArtifactResource method status.
@GET
@Path("usage")
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_SCIENTIST, AllowedProjectRoles.DATA_OWNER })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiOperation(value = "Artifact usage", response = ProvArtifactUsageParentDTO.class)
public Response status(@BeanParam ProvUsageBeanParams params, @Context UriInfo uriInfo, @Context SecurityContext sc) throws ProvenanceException, GenericException, DatasetException, MetadataException, SchematizedTagException {
Users user = jWTHelper.getUserPrincipal(sc);
ProvArtifactUsageParentDTO status = usageBuilder.buildAccessible(uriInfo, user, targetEndpoint, artifactId, params.getUsageType());
return Response.ok().entity(status).build();
}
use of io.hops.hopsworks.api.provenance.ops.dto.ProvArtifactUsageParentDTO in project hopsworks by logicalclocks.
the class ProvUsageBuilder method buildAccessible.
public ProvArtifactUsageParentDTO buildAccessible(UriInfo uriInfo, Users user, DatasetPath targetEndpoint, String artifactId, Set<ProvUsageType> type) throws ProvenanceException, GenericException, DatasetException, MetadataException, SchematizedTagException {
if (!accessController.hasAccess(targetEndpoint.getAccessProject(), targetEndpoint.getDataset())) {
throw new GenericException(RESTCodes.GenericErrorCode.NOT_AUTHORIZED_TO_ACCESS, Level.FINE);
}
ProvArtifactUsageParentDTO usage = new ProvArtifactUsageParentDTO();
usage.setArtifactId(artifactId);
DatasetDTO datasetDTO = datasetBuilder.build(uriInfo, new ResourceRequest(ResourceRequest.Name.DATASET), user, targetEndpoint);
usage.setDataset(datasetDTO);
usage.setProjectId(targetEndpoint.getDataset().getProject().getId());
usage.setProjectName(targetEndpoint.getDataset().getProject().getName());
ProvOpsParamBuilder params = getBasicUsageOpsParams(targetEndpoint.getDataset(), artifactId);
ProvOpsDTO ops = opsBuilder.build(targetEndpoint.getDataset().getProject(), params, ProvOpsReturnType.AGGREGATIONS);
Optional<ProvOpsDTO> aggregation = ops.getItems().stream().filter(agg -> agg.getAggregation() != null && agg.getAggregation().equals(ProvOpsAggregations.APP_USAGE.toString())).findFirst();
if (!aggregation.isPresent()) {
return usage;
}
Optional<ProvOpsDTO> artifact = aggregation.get().getItems().stream().filter(art -> art.getMlId().equals(artifactId)).findFirst();
if (!artifact.isPresent()) {
return usage;
}
for (ProvUsageType t : type) {
switch(t) {
case READ_CURRENT:
usage.setReadCurrent(usage(uriInfo, artifact.get(), Provenance.FileOps.ACCESS_DATA, true));
break;
case WRITE_CURRENT:
usage.setWriteCurrent(usage(uriInfo, artifact.get(), Provenance.FileOps.MODIFY_DATA, true));
break;
case READ_LAST:
lastUsage(uriInfo, artifact.get(), Provenance.FileOps.ACCESS_DATA).ifPresent(usage::setReadLast);
break;
case WRITE_LAST:
lastUsage(uriInfo, artifact.get(), Provenance.FileOps.MODIFY_DATA).ifPresent(usage::setWriteLast);
break;
case READ_HISTORY:
usage.setReadHistory(usage(uriInfo, artifact.get(), Provenance.FileOps.ACCESS_DATA, false));
break;
case WRITE_HISTORY:
usage.setWriteHistory(usage(uriInfo, artifact.get(), Provenance.FileOps.MODIFY_DATA, false));
break;
}
}
return usage;
}
Aggregations