@Test public void testListJobsWithHostNameFilter() throws Exception { final String namePattern = "foo"; when(model.listHosts(namePattern)).thenReturn(ImmutableList.of("foobar.example.net")); final JobId jobId1 = JobId.parse("foobar:1"); final JobId jobId2 = JobId.parse("foobat:2"); final Job job1 = Job.newBuilder().build(); final Job job2 = Job.newBuilder().build(); final HostStatus hostStatus = mockHostStatus(ImmutableMap.of( jobId1, Deployment.of(jobId1, Goal.START), jobId2, Deployment.of(jobId2, Goal.START) )); when(model.getHostStatus("foobar.example.net")).thenReturn(hostStatus); when(model.getJob(jobId1)).thenReturn(job1); when(model.getJob(jobId2)).thenReturn(job2); assertThat(resource.list("", namePattern), is( ImmutableMap.of( jobId1, job1, jobId2, job2 ) )); }
@GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered public Map<JobId, Job> list( @QueryParam("q") @DefaultValue("") final String query, @QueryParam("hostPattern") @DefaultValue("") final String hostPattern) { final Map<JobId, Job> allJobs; if (!hostPattern.isEmpty()) { allJobs = model.listHosts(hostPattern).stream() .map(model::getHostStatus) .flatMap(hostStatus -> hostStatus.getJobs().keySet().stream()) .distinct() .collect(Collectors.toMap(Function.identity(), model::getJob)); } else { allJobs = model.getJobs(); } if (query.isEmpty()) { metrics.jobsInJobList(allJobs.size()); return allJobs; } final Map<JobId, Job> filteredJobs = Maps.newHashMap(); for (final Map.Entry<JobId, Job> entry : allJobs.entrySet()) { if (entry.getKey().toString().contains(query)) { filteredJobs.put(entry.getKey(), entry.getValue()); } } metrics.jobsInJobList(filteredJobs.size()); return filteredJobs; }
JobsResource { @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered public Map<JobId, Job> list( @QueryParam("q") @DefaultValue("") final String query, @QueryParam("hostPattern") @DefaultValue("") final String hostPattern) { final Map<JobId, Job> allJobs; if (!hostPattern.isEmpty()) { allJobs = model.listHosts(hostPattern).stream() .map(model::getHostStatus) .flatMap(hostStatus -> hostStatus.getJobs().keySet().stream()) .distinct() .collect(Collectors.toMap(Function.identity(), model::getJob)); } else { allJobs = model.getJobs(); } if (query.isEmpty()) { metrics.jobsInJobList(allJobs.size()); return allJobs; } final Map<JobId, Job> filteredJobs = Maps.newHashMap(); for (final Map.Entry<JobId, Job> entry : allJobs.entrySet()) { if (entry.getKey().toString().contains(query)) { filteredJobs.put(entry.getKey(), entry.getValue()); } } metrics.jobsInJobList(filteredJobs.size()); return filteredJobs; } }
JobsResource { @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered public Map<JobId, Job> list( @QueryParam("q") @DefaultValue("") final String query, @QueryParam("hostPattern") @DefaultValue("") final String hostPattern) { final Map<JobId, Job> allJobs; if (!hostPattern.isEmpty()) { allJobs = model.listHosts(hostPattern).stream() .map(model::getHostStatus) .flatMap(hostStatus -> hostStatus.getJobs().keySet().stream()) .distinct() .collect(Collectors.toMap(Function.identity(), model::getJob)); } else { allJobs = model.getJobs(); } if (query.isEmpty()) { metrics.jobsInJobList(allJobs.size()); return allJobs; } final Map<JobId, Job> filteredJobs = Maps.newHashMap(); for (final Map.Entry<JobId, Job> entry : allJobs.entrySet()) { if (entry.getKey().toString().contains(query)) { filteredJobs.put(entry.getKey(), entry.getValue()); } } metrics.jobsInJobList(filteredJobs.size()); return filteredJobs; } JobsResource(final MasterModel model, final MasterMetrics metrics,
                      final Set<String> whitelistedCapabilities); @VisibleForTesting  JobsResource(final MasterModel model, final MasterMetrics metrics,
               final Set<String> whitelistedCapabilities, final Clock clock); }
JobsResource { @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered public Map<JobId, Job> list( @QueryParam("q") @DefaultValue("") final String query, @QueryParam("hostPattern") @DefaultValue("") final String hostPattern) { final Map<JobId, Job> allJobs; if (!hostPattern.isEmpty()) { allJobs = model.listHosts(hostPattern).stream() .map(model::getHostStatus) .flatMap(hostStatus -> hostStatus.getJobs().keySet().stream()) .distinct() .collect(Collectors.toMap(Function.identity(), model::getJob)); } else { allJobs = model.getJobs(); } if (query.isEmpty()) { metrics.jobsInJobList(allJobs.size()); return allJobs; } final Map<JobId, Job> filteredJobs = Maps.newHashMap(); for (final Map.Entry<JobId, Job> entry : allJobs.entrySet()) { if (entry.getKey().toString().contains(query)) { filteredJobs.put(entry.getKey(), entry.getValue()); } } metrics.jobsInJobList(filteredJobs.size()); return filteredJobs; } JobsResource(final MasterModel model, final MasterMetrics metrics,
                      final Set<String> whitelistedCapabilities); @VisibleForTesting  JobsResource(final MasterModel model, final MasterMetrics metrics,
               final Set<String> whitelistedCapabilities, final Clock clock); @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered Map<JobId, Job> list(
      @QueryParam("q") @DefaultValue("") final String query,
      @QueryParam("hostPattern") @DefaultValue("") final String hostPattern); @Path("{id}") @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered Optional<Job> get(@InjectParam @PathParam("id") @Valid final JobId id); @POST @Produces(APPLICATION_JSON) @Timed @ExceptionMetered CreateJobResponse post(@Valid final Job job,
                                @RequestUser final String username); @Path("{id}") @DELETE @Produces(APPLICATION_JSON) @Timed @ExceptionMetered JobDeleteResponse delete(@PathParam("id") @Valid final JobId id,
                                  @QueryParam("token") @DefaultValue("") final String token); @Path("{id}/status") @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered Optional<JobStatus> statusGet(@PathParam("id") @Valid final JobId id); @Path("/statuses") @POST @Produces(APPLICATION_JSON) @Timed @ExceptionMetered Map<JobId, JobStatus> jobStatuses(@Valid final Set<JobId> ids); }
JobsResource { @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered public Map<JobId, Job> list( @QueryParam("q") @DefaultValue("") final String query, @QueryParam("hostPattern") @DefaultValue("") final String hostPattern) { final Map<JobId, Job> allJobs; if (!hostPattern.isEmpty()) { allJobs = model.listHosts(hostPattern).stream() .map(model::getHostStatus) .flatMap(hostStatus -> hostStatus.getJobs().keySet().stream()) .distinct() .collect(Collectors.toMap(Function.identity(), model::getJob)); } else { allJobs = model.getJobs(); } if (query.isEmpty()) { metrics.jobsInJobList(allJobs.size()); return allJobs; } final Map<JobId, Job> filteredJobs = Maps.newHashMap(); for (final Map.Entry<JobId, Job> entry : allJobs.entrySet()) { if (entry.getKey().toString().contains(query)) { filteredJobs.put(entry.getKey(), entry.getValue()); } } metrics.jobsInJobList(filteredJobs.size()); return filteredJobs; } JobsResource(final MasterModel model, final MasterMetrics metrics,
                      final Set<String> whitelistedCapabilities); @VisibleForTesting  JobsResource(final MasterModel model, final MasterMetrics metrics,
               final Set<String> whitelistedCapabilities, final Clock clock); @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered Map<JobId, Job> list(
      @QueryParam("q") @DefaultValue("") final String query,
      @QueryParam("hostPattern") @DefaultValue("") final String hostPattern); @Path("{id}") @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered Optional<Job> get(@InjectParam @PathParam("id") @Valid final JobId id); @POST @Produces(APPLICATION_JSON) @Timed @ExceptionMetered CreateJobResponse post(@Valid final Job job,
                                @RequestUser final String username); @Path("{id}") @DELETE @Produces(APPLICATION_JSON) @Timed @ExceptionMetered JobDeleteResponse delete(@PathParam("id") @Valid final JobId id,
                                  @QueryParam("token") @DefaultValue("") final String token); @Path("{id}/status") @GET @Produces(APPLICATION_JSON) @Timed @ExceptionMetered Optional<JobStatus> statusGet(@PathParam("id") @Valid final JobId id); @Path("/statuses") @POST @Produces(APPLICATION_JSON) @Timed @ExceptionMetered Map<JobId, JobStatus> jobStatuses(@Valid final Set<JobId> ids);  }