Skip to content

Commit

Permalink
Add GET /api/v1/jobs
Browse files Browse the repository at this point in the history
Signed-off-by: Willy Lulciuc <willy.lulciuc@gmail.com>
  • Loading branch information
wslulciuc committed Oct 17, 2024
1 parent 90a2f65 commit 0042457
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
22 changes: 18 additions & 4 deletions api/src/main/java/marquez/api/JobResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -159,6 +160,19 @@ public Response listJobVersions(
return Response.ok(new JobVersions(jobVersions)).build();
}

@Timed
@ResponseMetered
@ExceptionMetered
@GET
@Path("/jobs")
@Produces(APPLICATION_JSON)
public Response list(
@QueryParam("lastRunStates") List<RunState> lastRunStates,
@QueryParam("limit") @DefaultValue("100") @Min(value = 0) int limit,
@QueryParam("offset") @DefaultValue("0") @Min(value = 0) int offset) {
return list(null, lastRunStates, limit, offset);
}

@Timed
@ResponseMetered
@ExceptionMetered
Expand All @@ -170,17 +184,17 @@ public Response list(
@QueryParam("lastRunStates") List<RunState> lastRunStates,
@QueryParam("limit") @DefaultValue("100") @Min(value = 0) int limit,
@QueryParam("offset") @DefaultValue("0") @Min(value = 0) int offset) {
throwIfNotExists(namespaceName);
final Optional<NamespaceName> namespaceOrNull = Optional.ofNullable(namespaceName);
final String namespace = namespaceOrNull.map(NamespaceName::getValue).orElse(null);

// default to all run states if not specified
if (lastRunStates.isEmpty()) {
lastRunStates = new ArrayList<>();
Collections.addAll(lastRunStates, RunState.values());
}

final List<Job> jobs =
jobService.findAllWithRun(namespaceName.getValue(), lastRunStates, limit, offset);
final int totalCount = jobService.countFor(namespaceName.getValue());
final List<Job> jobs = jobService.findAllWithRun(namespace, lastRunStates, limit, offset);
final int totalCount = jobService.countFor(namespace);
return Response.ok(new ResultsPage<>("jobs", jobs, totalCount)).build();
}

Expand Down
14 changes: 8 additions & 6 deletions api/src/main/java/marquez/db/JobDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ default Optional<Job> findWithDatasetsAndRun(String namespaceName, String jobNam
Optional<Job> job = findJobByName(namespaceName, jobName);
job.ifPresent(
j -> {
List<Run> runs = createRunDao().findByLatestJob(namespaceName, jobName, 1, 0);
List<Run> runs = createRunDao().findByLatestJob(namespaceName, jobName, 10, 0);
this.setJobData(runs, j);
this.setJobDataset(
createJobVersionDao().findCurrentInputOutputDatasetsFor(namespaceName, jobName), j);
Expand Down Expand Up @@ -179,15 +179,15 @@ default Optional<Job> findWithDatasetsAndRun(String namespaceName, String jobNam
FROM
jobs_view AS j
WHERE
j.namespace_name = :namespaceName
(:namespaceName IS NULL OR j.namespace_name = :namespaceName)
),
job_versions_temp AS (
SELECT
*
FROM
job_versions AS j
WHERE
j.namespace_name = :namespaceName
(:namespaceName IS NULL OR j.namespace_name = :namespaceName)
),
facets_temp AS (
SELECT
Expand Down Expand Up @@ -219,7 +219,7 @@ job_tags as (
ON
jtm.job_uuid = j.uuid
AND
j.namespace_name = :namespaceName
(:namespaceName IS NULL OR j.namespace_name = :namespaceName)
INNER JOIN
tags t
ON
Expand Down Expand Up @@ -271,7 +271,7 @@ List<Job> findAll(
int countJobRuns(String namespaceName, String job);

@SqlQuery(
"SELECT count(*) FROM jobs_view AS j WHERE j.namespace_name = :namespaceName\n"
"SELECT count(*) FROM jobs_view AS j WHERE (:namespaceName IS NULL OR j.namespace_name = :namespaceName)\n"
+ "AND symlink_target_uuid IS NULL")
int countFor(String namespaceName);

Expand All @@ -281,7 +281,9 @@ default List<Job> findAllWithRun(
return findAll(namespaceName, lastRunStates, limit, offset).stream()
.peek(
j -> {
List<Run> runs = runDao.findByLatestJob(namespaceName, j.getName().getValue(), 10, 0);
List<Run> runs =
runDao.findByLatestJob(
j.getNamespace().getValue(), j.getName().getValue(), 10, 0);
this.setJobData(runs, j);
})
.toList();
Expand Down
29 changes: 29 additions & 0 deletions api/src/test/java/marquez/db/JobDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

package marquez.db;

import static marquez.common.models.CommonModelGenerator.newJobName;
import static marquez.db.DbTestUtils.createJobWithSymlinkTarget;
import static marquez.db.DbTestUtils.createJobWithoutSymlinkTarget;
import static marquez.db.DbTestUtils.newJob;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -22,6 +24,7 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import marquez.common.models.JobName;
import marquez.common.models.JobType;
import marquez.common.models.RunState;
import marquez.db.models.DbModelGenerator;
Expand Down Expand Up @@ -124,6 +127,32 @@ public void testFindAll() {
DbModelGenerator.jobIdFor(namespace.getName(), anotherJobSameNamespace.getName()));
}

@Test
public void testFindAllWithNoNamespace() {
String nullNamespace = null;

List<RunState> runStates = new ArrayList<>();
Collections.addAll(runStates, RunState.values());

final List<Job> jobsWillBeEmpty = jobDao.findAll(nullNamespace, runStates, 10, 0);
assertThat(jobsWillBeEmpty).isEmpty();

JobName jobName0 = newJobName();
JobName jobName1 = newJobName();
JobName jobName2 = newJobName();

JobRow job0 = newJob(jdbi, jobName0.getValue());
JobRow job1 = newJob(jdbi, jobName1.getValue());
JobRow job2 = newJob(jdbi, jobName2.getValue());

final List<Job> jobsWillNotBeEmpty = jobDao.findAll(nullNamespace, runStates, 10, 0);
assertThat(jobsWillNotBeEmpty)
.isNotEmpty()
.hasSize(3)
.extracting(Job::getName)
.containsExactlyInAnyOrder(jobName0, jobName1, jobName2);
}

@Test
public void testCountFor() {
JobRow targetJob =
Expand Down

0 comments on commit 0042457

Please sign in to comment.