Search in sources :

Example 1 with ExperimentsEndpointDTO

use of io.hops.hopsworks.api.experiments.dto.ExperimentsEndpointDTO in project hopsworks by logicalclocks.

the class ExperimentsBuilder method build.

// Build specific
public ExperimentDTO build(UriInfo uriInfo, ResourceRequest resourceRequest, Project project, Users user, Map<Long, ExperimentsEndpointDTO> endpoints, ProvStateDTO fileProvenanceHit) throws ExperimentsException, DatasetException, ProvenanceException, MetadataException, GenericException {
    ExperimentDTO experimentDTO = new ExperimentDTO();
    ExperimentsEndpointDTO endpoint = endpoints.get(fileProvenanceHit.getProjectInodeId());
    if (endpoint == null) {
        // no endpoint - no results
        return null;
    }
    uri(experimentDTO, uriInfo, project, endpoint, fileProvenanceHit);
    if (expand(experimentDTO, resourceRequest).isExpand()) {
        if (fileProvenanceHit.getXattrs() != null && fileProvenanceHit.getXattrs().containsKey(EXPERIMENT_SUMMARY_XATTR_NAME)) {
            JSONObject summary = new JSONObject(fileProvenanceHit.getXattrs().get(EXPERIMENT_SUMMARY_XATTR_NAME));
            ExperimentDTO experimentSummary = experimentConverter.unmarshalDescription(summary.toString());
            experimentDTO.setStarted(fileProvenanceHit.getCreateTime());
            boolean updateNeeded = false;
            // update experiment that failed to set experiment status
            if ((experimentSummary.getState() == null || runningState(experimentSummary)) && Provenance.AppState.valueOf(fileProvenanceHit.getAppState().getCurrentState().name()).isFinalState()) {
                updateNeeded = true;
                experimentSummary.setState(fileProvenanceHit.getAppState().getCurrentState().name());
                experimentSummary.setFinished(fileProvenanceHit.getAppState().getFinishTime());
            }
            experimentDTO.setState(experimentSummary.getState());
            experimentDTO.setFinished(experimentSummary.getFinished());
            if (updateNeeded) {
                experimentsController.attachExperiment(user, project, fileProvenanceHit.getMlId(), experimentSummary);
            }
            if (fileProvenanceHit.getXattrs().containsKey(EXPERIMENT_MODEL_XATTR_NAME)) {
                ModelXAttr model = experimentConverter.unmarshal(fileProvenanceHit.getXattrs().get(EXPERIMENT_MODEL_XATTR_NAME), ModelXAttr.class);
                experimentDTO.setModel(model.getId());
                experimentDTO.setModelProjectName(model.getProjectName());
            }
            experimentDTO.setId(experimentSummary.getId());
            experimentDTO.setName(experimentSummary.getName());
            experimentDTO.setUserFullName(experimentSummary.getUserFullName());
            experimentDTO.setMetric(experimentSummary.getMetric());
            experimentDTO.setDescription(experimentSummary.getDescription());
            experimentDTO.setExperimentType(experimentSummary.getExperimentType());
            experimentDTO.setFunction(experimentSummary.getFunction());
            experimentDTO.setDirection(experimentSummary.getDirection());
            experimentDTO.setOptimizationKey(experimentSummary.getOptimizationKey());
            experimentDTO.setJobName(experimentSummary.getJobName());
            experimentDTO.setAppId(experimentSummary.getAppId());
            experimentDTO.setBestDir(experimentSummary.getBestDir());
            experimentDTO.setEnvironment(experimentSummary.getEnvironment());
            experimentDTO.setProgram(experimentSummary.getProgram());
            experimentDTO.setTensorboard(tensorBoardBuilder.build(uriInfo, resourceRequest.get(ResourceRequest.Name.TENSORBOARD), project, fileProvenanceHit.getMlId()));
            experimentDTO.setResults(experimentResultsBuilder.build(uriInfo, resourceRequest.get(ResourceRequest.Name.RESULTS), project, fileProvenanceHit.getMlId()));
        } else {
            return null;
        }
    }
    return experimentDTO;
}
Also used : JSONObject(org.json.JSONObject) ExperimentDTO(io.hops.hopsworks.api.experiments.dto.ExperimentDTO) ExperimentsEndpointDTO(io.hops.hopsworks.api.experiments.dto.ExperimentsEndpointDTO)

Example 2 with ExperimentsEndpointDTO

use of io.hops.hopsworks.api.experiments.dto.ExperimentsEndpointDTO in project hopsworks by logicalclocks.

the class ExperimentsBuilder method buildFilter.

private Pair<ProvStateParamBuilder, Map<Long, ExperimentsEndpointDTO>> buildFilter(Project project, Set<? extends AbstractFacade.FilterBy> filters) throws ProvenanceException, GenericException, DatasetException {
    ProvStateParamBuilder provFilesParamBuilder = new ProvStateParamBuilder();
    Map<Long, ExperimentsEndpointDTO> selectedEndpoints = new HashMap<>();
    if (filters != null) {
        Users filterUser = null;
        Project filterUserProject = project;
        for (AbstractFacade.FilterBy filterBy : filters) {
            if (filterBy.getParam().compareToIgnoreCase(Filters.ENDPOINT_ID.name()) == 0) {
                ExperimentsEndpointDTO endpoint = verifyExperimentsEndpoint(project, filterBy.getValue());
                selectedEndpoints.put(endpoint.getParentProject().getInode().getId(), endpoint);
            } else if (filterBy.getParam().compareToIgnoreCase(Filters.NAME_LIKE.name()) == 0) {
                provFilesParamBuilder.filterLikeXAttr(EXPERIMENT_SUMMARY_XATTR_NAME + ".name", filterBy.getValue());
            } else if (filterBy.getParam().compareToIgnoreCase(Filters.NAME_EQ.name()) == 0) {
                provFilesParamBuilder.filterByXAttr(EXPERIMENT_SUMMARY_XATTR_NAME + ".name", filterBy.getValue());
            } else if (filterBy.getParam().compareToIgnoreCase(Filters.DATE_START_LT.name()) == 0) {
                Long timestamp = getDate(filterBy.getField(), filterBy.getValue()).getTime();
                provFilesParamBuilder.filterByField(ProvStateParser.FieldsPF.CREATE_TIMESTAMP_LT, timestamp);
            } else if (filterBy.getParam().compareToIgnoreCase(Filters.DATE_START_GT.name()) == 0) {
                Long timestamp = getDate(filterBy.getField(), filterBy.getValue()).getTime();
                provFilesParamBuilder.filterByField(ProvStateParser.FieldsPF.CREATE_TIMESTAMP_GT, timestamp);
            } else if (filterBy.getParam().compareToIgnoreCase(Filters.USER.name()) == 0) {
                try {
                    filterUser = userFacade.find(Integer.parseInt(filterBy.getValue()));
                } catch (NumberFormatException e) {
                    throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "expected int user id, found: " + filterBy.getValue());
                }
            } else if (filterBy.getParam().compareToIgnoreCase(Filters.USER_PROJECT.name()) == 0) {
                try {
                    filterUserProject = projectFacade.find(Integer.parseInt(filterBy.getValue()));
                } catch (NumberFormatException e) {
                    throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "expected int user project id, found: " + filterBy.getValue());
                }
            } else if (filterBy.getParam().compareToIgnoreCase(Filters.STATE.name()) == 0) {
                provFilesParamBuilder.filterLikeXAttr(EXPERIMENT_SUMMARY_XATTR_NAME + ".state", filterBy.getValue());
            } else if (filterBy.getParam().compareToIgnoreCase(Filters.ID_EQ.name()) == 0) {
                provFilesParamBuilder.filterByXAttr(EXPERIMENT_SUMMARY_XATTR_NAME + ".id", filterBy.getValue());
            } else {
                throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "Filter by - found: " + filterBy.getParam() + " expected:" + EnumSet.allOf(Filters.class));
            }
        }
        if (filterUser != null) {
            ProjectTeam member = projectTeamFacade.findByPrimaryKey(filterUserProject, filterUser);
            if (member == null) {
                throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "Selected user: " + filterUser.getUid() + " is not part of project:" + filterUserProject.getId());
            }
            String hdfsUserStr = hdfsUsersController.getHdfsUserName(filterUserProject, filterUser);
            HdfsUsers hdfsUsers = hdfsUsersFacade.findByName(hdfsUserStr);
            provFilesParamBuilder.filterByField(ProvStateParser.FieldsP.USER_ID, hdfsUsers.getId().toString());
        }
    }
    // an endpoint always has to be selected, if none provided, then all accessible endpoints are used
    if (selectedEndpoints.isEmpty()) {
        for (ExperimentsEndpointDTO endpoint : experimentsController.getExperimentsEndpoints(project)) {
            selectedEndpoints.put(endpoint.getParentProject().getInode().getId(), endpoint);
        }
    }
    for (ExperimentsEndpointDTO endpoint : selectedEndpoints.values()) {
        provFilesParamBuilder.filterByField(ProvStateParser.FieldsP.PROJECT_I_ID, endpoint.getParentProject().getInode().getId()).filterByField(ProvStateParser.FieldsP.DATASET_I_ID, endpoint.getDatasetInodeId());
    }
    return Pair.with(provFilesParamBuilder, selectedEndpoints);
}
Also used : HashMap(java.util.HashMap) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers) Users(io.hops.hopsworks.persistence.entity.user.Users) GenericException(io.hops.hopsworks.exceptions.GenericException) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers) ProvStateParamBuilder(io.hops.hopsworks.common.provenance.state.ProvStateParamBuilder) Project(io.hops.hopsworks.persistence.entity.project.Project) ProjectTeam(io.hops.hopsworks.persistence.entity.project.team.ProjectTeam) AbstractFacade(io.hops.hopsworks.common.dao.AbstractFacade) ExperimentsEndpointDTO(io.hops.hopsworks.api.experiments.dto.ExperimentsEndpointDTO)

Example 3 with ExperimentsEndpointDTO

use of io.hops.hopsworks.api.experiments.dto.ExperimentsEndpointDTO in project hopsworks by logicalclocks.

the class ExperimentsResource method get.

@ApiOperation(value = "Get an experiment", response = ExperimentDTO.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" })
public Response get(@PathParam("id") String id, @BeanParam ExpansionBeanParam expansions, @Context UriInfo uriInfo, @Context SecurityContext sc) throws ExperimentsException, DatasetException, ProvenanceException, MetadataException, GenericException {
    ResourceRequest resourceRequest = new ResourceRequest(ResourceRequest.Name.EXPERIMENTS);
    resourceRequest.setExpansions(expansions.getResources());
    ProvStateDTO fileState = experimentsController.getExperiment(project, id);
    Users user = jwtHelper.getUserPrincipal(sc);
    if (fileState != null) {
        Map<Long, ExperimentsEndpointDTO> endpoints = new HashMap<>();
        endpoints.put(project.getInode().getId(), experimentsController.getExperimentsEndpoint(project));
        ExperimentDTO dto = experimentsBuilder.build(uriInfo, resourceRequest, project, user, endpoints, fileState);
        if (dto == null) {
            throw new GenericException(RESTCodes.GenericErrorCode.NOT_AUTHORIZED_TO_ACCESS, Level.FINE);
        } else {
            return Response.ok().entity(dto).build();
        }
    } else {
        throw new ExperimentsException(RESTCodes.ExperimentsErrorCode.EXPERIMENT_NOT_FOUND, Level.FINE);
    }
}
Also used : ExperimentsException(io.hops.hopsworks.exceptions.ExperimentsException) HashMap(java.util.HashMap) ExperimentDTO(io.hops.hopsworks.api.experiments.dto.ExperimentDTO) ExperimentsEndpointDTO(io.hops.hopsworks.api.experiments.dto.ExperimentsEndpointDTO) ResourceRequest(io.hops.hopsworks.common.api.ResourceRequest) Users(io.hops.hopsworks.persistence.entity.user.Users) ProvStateDTO(io.hops.hopsworks.common.provenance.state.dto.ProvStateDTO) GenericException(io.hops.hopsworks.exceptions.GenericException) 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) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Aggregations

ExperimentsEndpointDTO (io.hops.hopsworks.api.experiments.dto.ExperimentsEndpointDTO)3 ExperimentDTO (io.hops.hopsworks.api.experiments.dto.ExperimentDTO)2 GenericException (io.hops.hopsworks.exceptions.GenericException)2 Users (io.hops.hopsworks.persistence.entity.user.Users)2 HashMap (java.util.HashMap)2 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)1 ResourceRequest (io.hops.hopsworks.common.api.ResourceRequest)1 AbstractFacade (io.hops.hopsworks.common.dao.AbstractFacade)1 ProvStateParamBuilder (io.hops.hopsworks.common.provenance.state.ProvStateParamBuilder)1 ProvStateDTO (io.hops.hopsworks.common.provenance.state.dto.ProvStateDTO)1 ExperimentsException (io.hops.hopsworks.exceptions.ExperimentsException)1 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)1 HdfsUsers (io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers)1 Project (io.hops.hopsworks.persistence.entity.project.Project)1 ProjectTeam (io.hops.hopsworks.persistence.entity.project.team.ProjectTeam)1 ApiOperation (io.swagger.annotations.ApiOperation)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 JSONObject (org.json.JSONObject)1