@Test public void query() throws Exception { environmentVariables.set("ELASTICSEARCH_HOST", "localhost"); environmentVariables.set("ELASTICSEARCH_HTTP_PORT", "9200"); environmentVariables.set("HOBBIT_RABBIT_HOST", "localhost"); }
@GET @Path("query") @Produces(MediaType.APPLICATION_JSON) public Response query(@QueryParam("id") String idsCommaSep, @QueryParam("benchmark-id") String benchmarkId, @QueryParam("challenge-task-id") String challengeTaskId, @Context SecurityContext sc) { List<ExperimentBean> results = null; String[] ids = null; if (idsCommaSep != null) { ids = idsCommaSep.split(","); } Set<String> userOwnedSystemIds = null; if (Application.isUsingDevDb()) { return Response.ok(getDevDb().queryExperiments(ids, challengeTaskId)).build(); } else { if (ids != null) { LOGGER.debug("Querying experiment results for " + Arrays.toString(ids)); results = new ArrayList<>(ids.length); for (String id : ids) { Resource experiment = HobbitExperiments.getExperiment(id); String query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the public graph.", id); results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } else { LOGGER.trace("Got no result for {} from the public graph. Trying the private graph.", id); query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the private graph.", id); Resource system = RdfHelper.getObjectResource(model, experiment, HOBBIT.involvesSystemInstance); if (system != null) { LOGGER.trace("Check visibility of system {}.", system.getURI()); String systemURI = system.getURI(); userOwnedSystemIds = InternalResources.getUserSystemIds(sc); if (userOwnedSystemIds.contains(systemURI)) { results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } } } else { ExperimentBean exp = new ExperimentBean(); exp.setId(id); exp.setError(UNKNOWN_EXP_ERROR_MSG); ConfiguredBenchmarkBean benchmark = new ConfiguredBenchmarkBean(); benchmark.setConfigurationParamValues(new ArrayList<>()); exp.setBenchmark(benchmark); exp.setKpis(new ArrayList<>()); results.add(exp); } } } if (results.size() > 0) { String esHost = System.getenv("ELASTICSEARCH_HOST"); if(esHost == null) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); } } else { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (userOwnedSystemIds == null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { if(e.getError() != null && e.getError().equals(UNKNOWN_EXP_ERROR_MSG)) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); continue; } if (userInfo.hasRole("system-provider") || userInfo.hasRole("challenge-organiser")) { e.setBenchmarkLogAvailable(true); } else { e.setBenchmarkLogAvailable(false); } NamedEntityBean system = e.getSystem(); if(system != null) { String systemId = system.getId(); e.setSystemLogAvailable(userOwnedSystemIds.contains(systemId)); } else { e.setSystemLogAvailable(false); } } } } } else if (benchmarkId != null) { LOGGER.debug("Querying experiment results for benchmark " + benchmarkId); String query = SparqlQueries.getExperimentGraphOfBenchmarkQuery(benchmarkId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { LOGGER.info("Couldn't find experiments for benchmark {}. Returning empty list.", benchmarkId); results = new ArrayList<>(0); } } else if (challengeTaskId != null) { LOGGER.debug("Querying experiment results for challenge task " + challengeTaskId); String query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { boolean challengeOwner = true; query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { Resource challengeTask = model.getResource(challengeTaskId); String challengeQuery = SparqlQueries.getChallengeTaskOrganizer(challengeTaskId, null); Model challengeModel = StorageServiceClientSingleton.getInstance() .sendConstructQuery(challengeQuery); Resource challenge = RdfHelper.getObjectResource(challengeModel, challengeTask, HOBBIT.isTaskOf); if (challenge != null) { String organizer = RdfHelper.getStringValue(challengeModel, challenge, HOBBIT.organizer); UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (organizer != null) { if (organizer.equals(userInfo.getPreferredUsername())) { results = RdfModelHelper.createExperimentBeans(model); challengeOwner = true; } } else { LOGGER.error( "Couldn't get organizer for task {}. Falling back to retrieving experiments that can be seen by the user in his role as system owner.", challengeTaskId); } if (results == null) { List<ExperimentBean> experiments = RdfModelHelper.createExperimentBeans(model); if (experiments != null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); final Set<String> filter = userOwnedSystemIds; results = experiments.stream() .filter(exp -> filter.contains(exp.getSystem().getId())) .collect(Collectors.toList()); } } } else { LOGGER.error("Couldn't find the challenge of challenge task {}.", challengeTaskId); } } else { LOGGER.info("Couldn't find experiments for task {}. Returning empty list.", challengeTaskId); results = new ArrayList<>(0); } if ((results != null) && (results.size() > 0)) { if (challengeOwner) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(true); } } else { if (userOwnedSystemIds == null) { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(userOwnedSystemIds.contains(e.getSystem().getId())); } } } } } else { results = RdfModelHelper.createExperimentBeans(StorageServiceClientSingleton.getInstance() .sendConstructQuery(SparqlQueries.getShallowExperimentGraphQuery(null, null))); } } if (results == null) results = new ArrayList<>(0); return Response.ok(new GenericEntity<List<ExperimentBean>>(results) { }).build(); }
ExperimentsResources { @GET @Path("query") @Produces(MediaType.APPLICATION_JSON) public Response query(@QueryParam("id") String idsCommaSep, @QueryParam("benchmark-id") String benchmarkId, @QueryParam("challenge-task-id") String challengeTaskId, @Context SecurityContext sc) { List<ExperimentBean> results = null; String[] ids = null; if (idsCommaSep != null) { ids = idsCommaSep.split(","); } Set<String> userOwnedSystemIds = null; if (Application.isUsingDevDb()) { return Response.ok(getDevDb().queryExperiments(ids, challengeTaskId)).build(); } else { if (ids != null) { LOGGER.debug("Querying experiment results for " + Arrays.toString(ids)); results = new ArrayList<>(ids.length); for (String id : ids) { Resource experiment = HobbitExperiments.getExperiment(id); String query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the public graph.", id); results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } else { LOGGER.trace("Got no result for {} from the public graph. Trying the private graph.", id); query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the private graph.", id); Resource system = RdfHelper.getObjectResource(model, experiment, HOBBIT.involvesSystemInstance); if (system != null) { LOGGER.trace("Check visibility of system {}.", system.getURI()); String systemURI = system.getURI(); userOwnedSystemIds = InternalResources.getUserSystemIds(sc); if (userOwnedSystemIds.contains(systemURI)) { results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } } } else { ExperimentBean exp = new ExperimentBean(); exp.setId(id); exp.setError(UNKNOWN_EXP_ERROR_MSG); ConfiguredBenchmarkBean benchmark = new ConfiguredBenchmarkBean(); benchmark.setConfigurationParamValues(new ArrayList<>()); exp.setBenchmark(benchmark); exp.setKpis(new ArrayList<>()); results.add(exp); } } } if (results.size() > 0) { String esHost = System.getenv("ELASTICSEARCH_HOST"); if(esHost == null) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); } } else { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (userOwnedSystemIds == null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { if(e.getError() != null && e.getError().equals(UNKNOWN_EXP_ERROR_MSG)) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); continue; } if (userInfo.hasRole("system-provider") || userInfo.hasRole("challenge-organiser")) { e.setBenchmarkLogAvailable(true); } else { e.setBenchmarkLogAvailable(false); } NamedEntityBean system = e.getSystem(); if(system != null) { String systemId = system.getId(); e.setSystemLogAvailable(userOwnedSystemIds.contains(systemId)); } else { e.setSystemLogAvailable(false); } } } } } else if (benchmarkId != null) { LOGGER.debug("Querying experiment results for benchmark " + benchmarkId); String query = SparqlQueries.getExperimentGraphOfBenchmarkQuery(benchmarkId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { LOGGER.info("Couldn't find experiments for benchmark {}. Returning empty list.", benchmarkId); results = new ArrayList<>(0); } } else if (challengeTaskId != null) { LOGGER.debug("Querying experiment results for challenge task " + challengeTaskId); String query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { boolean challengeOwner = true; query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { Resource challengeTask = model.getResource(challengeTaskId); String challengeQuery = SparqlQueries.getChallengeTaskOrganizer(challengeTaskId, null); Model challengeModel = StorageServiceClientSingleton.getInstance() .sendConstructQuery(challengeQuery); Resource challenge = RdfHelper.getObjectResource(challengeModel, challengeTask, HOBBIT.isTaskOf); if (challenge != null) { String organizer = RdfHelper.getStringValue(challengeModel, challenge, HOBBIT.organizer); UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (organizer != null) { if (organizer.equals(userInfo.getPreferredUsername())) { results = RdfModelHelper.createExperimentBeans(model); challengeOwner = true; } } else { LOGGER.error( "Couldn't get organizer for task {}. Falling back to retrieving experiments that can be seen by the user in his role as system owner.", challengeTaskId); } if (results == null) { List<ExperimentBean> experiments = RdfModelHelper.createExperimentBeans(model); if (experiments != null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); final Set<String> filter = userOwnedSystemIds; results = experiments.stream() .filter(exp -> filter.contains(exp.getSystem().getId())) .collect(Collectors.toList()); } } } else { LOGGER.error("Couldn't find the challenge of challenge task {}.", challengeTaskId); } } else { LOGGER.info("Couldn't find experiments for task {}. Returning empty list.", challengeTaskId); results = new ArrayList<>(0); } if ((results != null) && (results.size() > 0)) { if (challengeOwner) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(true); } } else { if (userOwnedSystemIds == null) { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(userOwnedSystemIds.contains(e.getSystem().getId())); } } } } } else { results = RdfModelHelper.createExperimentBeans(StorageServiceClientSingleton.getInstance() .sendConstructQuery(SparqlQueries.getShallowExperimentGraphQuery(null, null))); } } if (results == null) results = new ArrayList<>(0); return Response.ok(new GenericEntity<List<ExperimentBean>>(results) { }).build(); } }
ExperimentsResources { @GET @Path("query") @Produces(MediaType.APPLICATION_JSON) public Response query(@QueryParam("id") String idsCommaSep, @QueryParam("benchmark-id") String benchmarkId, @QueryParam("challenge-task-id") String challengeTaskId, @Context SecurityContext sc) { List<ExperimentBean> results = null; String[] ids = null; if (idsCommaSep != null) { ids = idsCommaSep.split(","); } Set<String> userOwnedSystemIds = null; if (Application.isUsingDevDb()) { return Response.ok(getDevDb().queryExperiments(ids, challengeTaskId)).build(); } else { if (ids != null) { LOGGER.debug("Querying experiment results for " + Arrays.toString(ids)); results = new ArrayList<>(ids.length); for (String id : ids) { Resource experiment = HobbitExperiments.getExperiment(id); String query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the public graph.", id); results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } else { LOGGER.trace("Got no result for {} from the public graph. Trying the private graph.", id); query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the private graph.", id); Resource system = RdfHelper.getObjectResource(model, experiment, HOBBIT.involvesSystemInstance); if (system != null) { LOGGER.trace("Check visibility of system {}.", system.getURI()); String systemURI = system.getURI(); userOwnedSystemIds = InternalResources.getUserSystemIds(sc); if (userOwnedSystemIds.contains(systemURI)) { results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } } } else { ExperimentBean exp = new ExperimentBean(); exp.setId(id); exp.setError(UNKNOWN_EXP_ERROR_MSG); ConfiguredBenchmarkBean benchmark = new ConfiguredBenchmarkBean(); benchmark.setConfigurationParamValues(new ArrayList<>()); exp.setBenchmark(benchmark); exp.setKpis(new ArrayList<>()); results.add(exp); } } } if (results.size() > 0) { String esHost = System.getenv("ELASTICSEARCH_HOST"); if(esHost == null) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); } } else { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (userOwnedSystemIds == null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { if(e.getError() != null && e.getError().equals(UNKNOWN_EXP_ERROR_MSG)) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); continue; } if (userInfo.hasRole("system-provider") || userInfo.hasRole("challenge-organiser")) { e.setBenchmarkLogAvailable(true); } else { e.setBenchmarkLogAvailable(false); } NamedEntityBean system = e.getSystem(); if(system != null) { String systemId = system.getId(); e.setSystemLogAvailable(userOwnedSystemIds.contains(systemId)); } else { e.setSystemLogAvailable(false); } } } } } else if (benchmarkId != null) { LOGGER.debug("Querying experiment results for benchmark " + benchmarkId); String query = SparqlQueries.getExperimentGraphOfBenchmarkQuery(benchmarkId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { LOGGER.info("Couldn't find experiments for benchmark {}. Returning empty list.", benchmarkId); results = new ArrayList<>(0); } } else if (challengeTaskId != null) { LOGGER.debug("Querying experiment results for challenge task " + challengeTaskId); String query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { boolean challengeOwner = true; query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { Resource challengeTask = model.getResource(challengeTaskId); String challengeQuery = SparqlQueries.getChallengeTaskOrganizer(challengeTaskId, null); Model challengeModel = StorageServiceClientSingleton.getInstance() .sendConstructQuery(challengeQuery); Resource challenge = RdfHelper.getObjectResource(challengeModel, challengeTask, HOBBIT.isTaskOf); if (challenge != null) { String organizer = RdfHelper.getStringValue(challengeModel, challenge, HOBBIT.organizer); UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (organizer != null) { if (organizer.equals(userInfo.getPreferredUsername())) { results = RdfModelHelper.createExperimentBeans(model); challengeOwner = true; } } else { LOGGER.error( "Couldn't get organizer for task {}. Falling back to retrieving experiments that can be seen by the user in his role as system owner.", challengeTaskId); } if (results == null) { List<ExperimentBean> experiments = RdfModelHelper.createExperimentBeans(model); if (experiments != null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); final Set<String> filter = userOwnedSystemIds; results = experiments.stream() .filter(exp -> filter.contains(exp.getSystem().getId())) .collect(Collectors.toList()); } } } else { LOGGER.error("Couldn't find the challenge of challenge task {}.", challengeTaskId); } } else { LOGGER.info("Couldn't find experiments for task {}. Returning empty list.", challengeTaskId); results = new ArrayList<>(0); } if ((results != null) && (results.size() > 0)) { if (challengeOwner) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(true); } } else { if (userOwnedSystemIds == null) { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(userOwnedSystemIds.contains(e.getSystem().getId())); } } } } } else { results = RdfModelHelper.createExperimentBeans(StorageServiceClientSingleton.getInstance() .sendConstructQuery(SparqlQueries.getShallowExperimentGraphQuery(null, null))); } } if (results == null) results = new ArrayList<>(0); return Response.ok(new GenericEntity<List<ExperimentBean>>(results) { }).build(); }  }
ExperimentsResources { @GET @Path("query") @Produces(MediaType.APPLICATION_JSON) public Response query(@QueryParam("id") String idsCommaSep, @QueryParam("benchmark-id") String benchmarkId, @QueryParam("challenge-task-id") String challengeTaskId, @Context SecurityContext sc) { List<ExperimentBean> results = null; String[] ids = null; if (idsCommaSep != null) { ids = idsCommaSep.split(","); } Set<String> userOwnedSystemIds = null; if (Application.isUsingDevDb()) { return Response.ok(getDevDb().queryExperiments(ids, challengeTaskId)).build(); } else { if (ids != null) { LOGGER.debug("Querying experiment results for " + Arrays.toString(ids)); results = new ArrayList<>(ids.length); for (String id : ids) { Resource experiment = HobbitExperiments.getExperiment(id); String query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the public graph.", id); results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } else { LOGGER.trace("Got no result for {} from the public graph. Trying the private graph.", id); query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the private graph.", id); Resource system = RdfHelper.getObjectResource(model, experiment, HOBBIT.involvesSystemInstance); if (system != null) { LOGGER.trace("Check visibility of system {}.", system.getURI()); String systemURI = system.getURI(); userOwnedSystemIds = InternalResources.getUserSystemIds(sc); if (userOwnedSystemIds.contains(systemURI)) { results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } } } else { ExperimentBean exp = new ExperimentBean(); exp.setId(id); exp.setError(UNKNOWN_EXP_ERROR_MSG); ConfiguredBenchmarkBean benchmark = new ConfiguredBenchmarkBean(); benchmark.setConfigurationParamValues(new ArrayList<>()); exp.setBenchmark(benchmark); exp.setKpis(new ArrayList<>()); results.add(exp); } } } if (results.size() > 0) { String esHost = System.getenv("ELASTICSEARCH_HOST"); if(esHost == null) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); } } else { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (userOwnedSystemIds == null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { if(e.getError() != null && e.getError().equals(UNKNOWN_EXP_ERROR_MSG)) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); continue; } if (userInfo.hasRole("system-provider") || userInfo.hasRole("challenge-organiser")) { e.setBenchmarkLogAvailable(true); } else { e.setBenchmarkLogAvailable(false); } NamedEntityBean system = e.getSystem(); if(system != null) { String systemId = system.getId(); e.setSystemLogAvailable(userOwnedSystemIds.contains(systemId)); } else { e.setSystemLogAvailable(false); } } } } } else if (benchmarkId != null) { LOGGER.debug("Querying experiment results for benchmark " + benchmarkId); String query = SparqlQueries.getExperimentGraphOfBenchmarkQuery(benchmarkId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { LOGGER.info("Couldn't find experiments for benchmark {}. Returning empty list.", benchmarkId); results = new ArrayList<>(0); } } else if (challengeTaskId != null) { LOGGER.debug("Querying experiment results for challenge task " + challengeTaskId); String query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { boolean challengeOwner = true; query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { Resource challengeTask = model.getResource(challengeTaskId); String challengeQuery = SparqlQueries.getChallengeTaskOrganizer(challengeTaskId, null); Model challengeModel = StorageServiceClientSingleton.getInstance() .sendConstructQuery(challengeQuery); Resource challenge = RdfHelper.getObjectResource(challengeModel, challengeTask, HOBBIT.isTaskOf); if (challenge != null) { String organizer = RdfHelper.getStringValue(challengeModel, challenge, HOBBIT.organizer); UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (organizer != null) { if (organizer.equals(userInfo.getPreferredUsername())) { results = RdfModelHelper.createExperimentBeans(model); challengeOwner = true; } } else { LOGGER.error( "Couldn't get organizer for task {}. Falling back to retrieving experiments that can be seen by the user in his role as system owner.", challengeTaskId); } if (results == null) { List<ExperimentBean> experiments = RdfModelHelper.createExperimentBeans(model); if (experiments != null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); final Set<String> filter = userOwnedSystemIds; results = experiments.stream() .filter(exp -> filter.contains(exp.getSystem().getId())) .collect(Collectors.toList()); } } } else { LOGGER.error("Couldn't find the challenge of challenge task {}.", challengeTaskId); } } else { LOGGER.info("Couldn't find experiments for task {}. Returning empty list.", challengeTaskId); results = new ArrayList<>(0); } if ((results != null) && (results.size() > 0)) { if (challengeOwner) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(true); } } else { if (userOwnedSystemIds == null) { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(userOwnedSystemIds.contains(e.getSystem().getId())); } } } } } else { results = RdfModelHelper.createExperimentBeans(StorageServiceClientSingleton.getInstance() .sendConstructQuery(SparqlQueries.getShallowExperimentGraphQuery(null, null))); } } if (results == null) results = new ArrayList<>(0); return Response.ok(new GenericEntity<List<ExperimentBean>>(results) { }).build(); }  @GET @Path("query") @Produces(MediaType.APPLICATION_JSON) Response query(@QueryParam("id") String idsCommaSep, @QueryParam("benchmark-id") String benchmarkId, @QueryParam("challenge-task-id") String challengeTaskId,
            @Context SecurityContext sc); @GET @Path("count-by-challenge/{id}") @Produces(MediaType.APPLICATION_JSON) Response countByChallengeTaskIds(@PathParam("id") String challengeId); @GET @Path("terminate/{id}") Response terminateExperiment(@Context SecurityContext sc, @PathParam("id") String experimentId); }
ExperimentsResources { @GET @Path("query") @Produces(MediaType.APPLICATION_JSON) public Response query(@QueryParam("id") String idsCommaSep, @QueryParam("benchmark-id") String benchmarkId, @QueryParam("challenge-task-id") String challengeTaskId, @Context SecurityContext sc) { List<ExperimentBean> results = null; String[] ids = null; if (idsCommaSep != null) { ids = idsCommaSep.split(","); } Set<String> userOwnedSystemIds = null; if (Application.isUsingDevDb()) { return Response.ok(getDevDb().queryExperiments(ids, challengeTaskId)).build(); } else { if (ids != null) { LOGGER.debug("Querying experiment results for " + Arrays.toString(ids)); results = new ArrayList<>(ids.length); for (String id : ids) { Resource experiment = HobbitExperiments.getExperiment(id); String query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the public graph.", id); results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } else { LOGGER.trace("Got no result for {} from the public graph. Trying the private graph.", id); query = SparqlQueries.getExperimentGraphQuery(experiment.getURI(), Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { LOGGER.trace("Got result for {} from the private graph.", id); Resource system = RdfHelper.getObjectResource(model, experiment, HOBBIT.involvesSystemInstance); if (system != null) { LOGGER.trace("Check visibility of system {}.", system.getURI()); String systemURI = system.getURI(); userOwnedSystemIds = InternalResources.getUserSystemIds(sc); if (userOwnedSystemIds.contains(systemURI)) { results.add(RdfModelHelper.createExperimentBean(model, experiment)); LOGGER.trace("Added result bean of {} to the list of results.", id); } } } else { ExperimentBean exp = new ExperimentBean(); exp.setId(id); exp.setError(UNKNOWN_EXP_ERROR_MSG); ConfiguredBenchmarkBean benchmark = new ConfiguredBenchmarkBean(); benchmark.setConfigurationParamValues(new ArrayList<>()); exp.setBenchmark(benchmark); exp.setKpis(new ArrayList<>()); results.add(exp); } } } if (results.size() > 0) { String esHost = System.getenv("ELASTICSEARCH_HOST"); if(esHost == null) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); } } else { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (userOwnedSystemIds == null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { if(e.getError() != null && e.getError().equals(UNKNOWN_EXP_ERROR_MSG)) { e.setBenchmarkLogAvailable(false); e.setSystemLogAvailable(false); continue; } if (userInfo.hasRole("system-provider") || userInfo.hasRole("challenge-organiser")) { e.setBenchmarkLogAvailable(true); } else { e.setBenchmarkLogAvailable(false); } NamedEntityBean system = e.getSystem(); if(system != null) { String systemId = system.getId(); e.setSystemLogAvailable(userOwnedSystemIds.contains(systemId)); } else { e.setSystemLogAvailable(false); } } } } } else if (benchmarkId != null) { LOGGER.debug("Querying experiment results for benchmark " + benchmarkId); String query = SparqlQueries.getExperimentGraphOfBenchmarkQuery(benchmarkId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { LOGGER.info("Couldn't find experiments for benchmark {}. Returning empty list.", benchmarkId); results = new ArrayList<>(0); } } else if (challengeTaskId != null) { LOGGER.debug("Querying experiment results for challenge task " + challengeTaskId); String query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PUBLIC_RESULT_GRAPH_URI); Model model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { results = RdfModelHelper.createExperimentBeans(model); } else { boolean challengeOwner = true; query = SparqlQueries.getExperimentOfTaskQuery(null, challengeTaskId, Constants.PRIVATE_RESULT_GRAPH_URI); model = StorageServiceClientSingleton.getInstance().sendConstructQuery(query); if (model != null && model.size() > 0) { Resource challengeTask = model.getResource(challengeTaskId); String challengeQuery = SparqlQueries.getChallengeTaskOrganizer(challengeTaskId, null); Model challengeModel = StorageServiceClientSingleton.getInstance() .sendConstructQuery(challengeQuery); Resource challenge = RdfHelper.getObjectResource(challengeModel, challengeTask, HOBBIT.isTaskOf); if (challenge != null) { String organizer = RdfHelper.getStringValue(challengeModel, challenge, HOBBIT.organizer); UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); if (organizer != null) { if (organizer.equals(userInfo.getPreferredUsername())) { results = RdfModelHelper.createExperimentBeans(model); challengeOwner = true; } } else { LOGGER.error( "Couldn't get organizer for task {}. Falling back to retrieving experiments that can be seen by the user in his role as system owner.", challengeTaskId); } if (results == null) { List<ExperimentBean> experiments = RdfModelHelper.createExperimentBeans(model); if (experiments != null) { userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); final Set<String> filter = userOwnedSystemIds; results = experiments.stream() .filter(exp -> filter.contains(exp.getSystem().getId())) .collect(Collectors.toList()); } } } else { LOGGER.error("Couldn't find the challenge of challenge task {}.", challengeTaskId); } } else { LOGGER.info("Couldn't find experiments for task {}. Returning empty list.", challengeTaskId); results = new ArrayList<>(0); } if ((results != null) && (results.size() > 0)) { if (challengeOwner) { for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(true); } } else { if (userOwnedSystemIds == null) { UserInfoBean userInfo = InternalResources.getUserInfoBean(sc); userOwnedSystemIds = InternalResources.getUserSystemIds(userInfo); } for (ExperimentBean e : results) { e.setBenchmarkLogAvailable(true); e.setSystemLogAvailable(userOwnedSystemIds.contains(e.getSystem().getId())); } } } } } else { results = RdfModelHelper.createExperimentBeans(StorageServiceClientSingleton.getInstance() .sendConstructQuery(SparqlQueries.getShallowExperimentGraphQuery(null, null))); } } if (results == null) results = new ArrayList<>(0); return Response.ok(new GenericEntity<List<ExperimentBean>>(results) { }).build(); }  @GET @Path("query") @Produces(MediaType.APPLICATION_JSON) Response query(@QueryParam("id") String idsCommaSep, @QueryParam("benchmark-id") String benchmarkId, @QueryParam("challenge-task-id") String challengeTaskId,
            @Context SecurityContext sc); @GET @Path("count-by-challenge/{id}") @Produces(MediaType.APPLICATION_JSON) Response countByChallengeTaskIds(@PathParam("id") String challengeId); @GET @Path("terminate/{id}") Response terminateExperiment(@Context SecurityContext sc, @PathParam("id") String experimentId);  }