Skip to content

Package: JobExecutions

JobExecutions

nameinstructionbranchcomplexitylinemethod
JobExecutions()
M: 34 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
count(ScopeId, EntityId, JobExecutionQuery)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
executionsByTarget(ScopeId, EntityId, EntityId, int, int)
M: 36 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
find(ScopeId, EntityId, EntityId)
M: 52 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
query(ScopeId, EntityId, JobExecutionQuery)
M: 28 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
simpleQuery(ScopeId, EntityId, boolean, DateParam, DateParam, String, SortOrder, int, int)
M: 70 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.app.api.resources.v1.resources;
14:
15: import javax.ws.rs.Consumes;
16: import javax.ws.rs.DefaultValue;
17: import javax.ws.rs.GET;
18: import javax.ws.rs.POST;
19: import javax.ws.rs.Path;
20: import javax.ws.rs.PathParam;
21: import javax.ws.rs.Produces;
22: import javax.ws.rs.QueryParam;
23: import javax.ws.rs.core.MediaType;
24:
25: import org.eclipse.kapua.KapuaEntityNotFoundException;
26: import org.eclipse.kapua.KapuaException;
27: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
28: import org.eclipse.kapua.app.api.core.model.CountResult;
29: import org.eclipse.kapua.app.api.core.model.DateParam;
30: import org.eclipse.kapua.app.api.core.model.EntityId;
31: import org.eclipse.kapua.app.api.core.model.ScopeId;
32: import org.eclipse.kapua.locator.KapuaLocator;
33: import org.eclipse.kapua.model.KapuaEntityAttributes;
34: import org.eclipse.kapua.model.id.KapuaId;
35: import org.eclipse.kapua.model.query.SortOrder;
36: import org.eclipse.kapua.model.query.predicate.AndPredicate;
37: import org.eclipse.kapua.model.query.predicate.AttributePredicate;
38: import org.eclipse.kapua.service.KapuaService;
39: import org.eclipse.kapua.service.job.Job;
40: import org.eclipse.kapua.service.job.execution.JobExecution;
41: import org.eclipse.kapua.service.job.execution.JobExecutionAttributes;
42: import org.eclipse.kapua.service.job.execution.JobExecutionFactory;
43: import org.eclipse.kapua.service.job.execution.JobExecutionListResult;
44: import org.eclipse.kapua.service.job.execution.JobExecutionQuery;
45: import org.eclipse.kapua.service.job.execution.JobExecutionService;
46: import org.eclipse.kapua.service.job.targets.JobTargetFactory;
47: import org.eclipse.kapua.service.job.targets.JobTargetListResult;
48: import org.eclipse.kapua.service.job.targets.JobTargetQuery;
49: import org.eclipse.kapua.service.job.targets.JobTargetService;
50:
51: import com.google.common.base.Strings;
52:
53: @Path("{scopeId}/jobs/{jobId}/executions")
54: public class JobExecutions extends AbstractKapuaResource {
55:
56: private final KapuaLocator locator = KapuaLocator.getInstance();
57: private final JobExecutionService jobExecutionService = locator.getService(JobExecutionService.class);
58: private final JobTargetService jobTargetService = locator.getService(JobTargetService.class);
59: private final JobExecutionFactory jobExecutionFactory = locator.getFactory(JobExecutionFactory.class);
60: private final JobTargetFactory jobTargetFactory = locator.getFactory(JobTargetFactory.class);
61:
62: /**
63: * Gets the {@link JobExecution} list for a given {@link Job}.
64: *
65: * @param scopeId The {@link ScopeId} in which to search results.
66: * @param jobId The {@link Job} id to filter results
67: * @param askTotalCount Ask for the total count of the matched entities in the result
68: * @param sortParam The name of the parameter that will be used as a sorting key
69: * @param sortDir The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
70: * @param offset The result set offset.
71: * @param limit The result set limit.
72: * @return The {@link JobExecutionListResult} of all the jobs executions associated to the current selected job.
73: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
74: * @since 1.0.0
75: */
76: @GET
77: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
78: public JobExecutionListResult simpleQuery(
79: @PathParam("scopeId") ScopeId scopeId,
80: @PathParam("jobId") EntityId jobId,
81: @QueryParam("askTotalCount") boolean askTotalCount,
82: @QueryParam("startDate") DateParam startDateParam,
83: @QueryParam("endDate") DateParam endDateParam,
84: @QueryParam("sortParam") String sortParam,
85: @QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
86: @QueryParam("offset") @DefaultValue("0") int offset,
87: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
88: JobExecutionQuery query = jobExecutionFactory.newQuery(scopeId);
89:
90: AndPredicate andPredicate = query.andPredicate(query.attributePredicate(JobExecutionAttributes.JOB_ID, jobId));
91:
92:• if (startDateParam != null) {
93: andPredicate.and(query.attributePredicate(JobExecutionAttributes.STARTED_ON, startDateParam.getDate(), AttributePredicate.Operator.GREATER_THAN_OR_EQUAL));
94: }
95:• if (endDateParam != null) {
96: andPredicate.and(query.attributePredicate(JobExecutionAttributes.ENDED_ON, endDateParam.getDate(), AttributePredicate.Operator.LESS_THAN_OR_EQUAL));
97: }
98:
99: query.setPredicate(andPredicate);
100:
101:• if (!Strings.isNullOrEmpty(sortParam)) {
102: query.setSortCriteria(query.fieldSortCriteria(sortParam, sortDir));
103: }
104:
105: query.setAskTotalCount(askTotalCount);
106: query.setOffset(offset);
107: query.setLimit(limit);
108:
109: return query(scopeId, jobId, query);
110: }
111:
112: /**
113: * Queries the results with the given {@link JobExecutionQuery} parameter.
114: *
115: * @param scopeId The {@link ScopeId} in which to search results.
116: * @param query The {@link JobExecutionQuery} to use to filter results.
117: * @return The {@link JobExecutionListResult} of all the result matching the given {@link JobExecutionQuery} parameter.
118: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
119: * @since 1.0.0
120: */
121: @POST
122: @Path("_query")
123: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
124: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
125: public JobExecutionListResult query(
126: @PathParam("scopeId") ScopeId scopeId,
127: @PathParam("jobId") EntityId jobId,
128: JobExecutionQuery query) throws KapuaException {
129: query.setScopeId(scopeId);
130: final AndPredicate andPredicate = query.andPredicate(
131: query.attributePredicate(JobExecutionAttributes.JOB_ID, jobId),
132: query.getPredicate()
133: );
134: query.setPredicate(andPredicate);
135: return jobExecutionService.query(query);
136: }
137:
138: /**
139: * Counts the results with the given {@link JobExecutionQuery} parameter.
140: *
141: * @param scopeId The {@link ScopeId} in which to search results.
142: * @param query The {@link JobExecutionQuery} to use to filter results.
143: * @return The count of all the result matching the given {@link JobExecutionQuery} parameter.
144: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
145: * @since 1.0.0
146: */
147: @POST
148: @Path("_count")
149: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
150: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
151: public CountResult count(
152: @PathParam("scopeId") ScopeId scopeId,
153: @PathParam("jobId") EntityId jobId,
154: JobExecutionQuery query) throws KapuaException {
155: query.setScopeId(scopeId);
156: query.setPredicate(query.attributePredicate(JobExecutionAttributes.JOB_ID, jobId));
157:
158: return new CountResult(jobExecutionService.count(query));
159: }
160:
161: /**
162: * Returns the Job specified by the "jobId" path parameter.
163: *
164: * @param scopeId The {@link ScopeId} of the requested {@link Job}.
165: * @param jobId The id of the requested Job.
166: * @param executionId The id of the requested JobExecution.
167: * @return The requested Job object.
168: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
169: * @since 1.0.0
170: */
171: @GET
172: @Path("{executionId}")
173: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
174: public JobExecution find(
175: @PathParam("scopeId") ScopeId scopeId,
176: @PathParam("jobId") EntityId jobId,
177: @PathParam("executionId") EntityId executionId) throws KapuaException {
178: JobExecutionQuery jobExecutionQuery = jobExecutionFactory.newQuery(scopeId);
179: jobExecutionQuery.setPredicate(jobExecutionQuery.andPredicate(
180: jobExecutionQuery.attributePredicate(JobExecutionAttributes.JOB_ID, jobId),
181: jobExecutionQuery.attributePredicate(KapuaEntityAttributes.ENTITY_ID, executionId)
182: ));
183: jobExecutionQuery.setOffset(0);
184: jobExecutionQuery.setLimit(1);
185: JobExecutionListResult jobExecutionListResult = jobExecutionService.query(jobExecutionQuery);
186:
187:• if (jobExecutionListResult.isEmpty()) {
188: throw new KapuaEntityNotFoundException(JobExecution.TYPE, executionId);
189: }
190:
191: return jobExecutionListResult.getFirstItem();
192: }
193:
194: @GET
195: @Path("{executionId}/targets")
196: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
197: public JobTargetListResult executionsByTarget(
198: @PathParam("scopeId") ScopeId scopeId,
199: @PathParam("jobId") EntityId jobId,
200: @PathParam("executionId") EntityId executionId,
201: @QueryParam("offset") @DefaultValue("0") int offset,
202: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
203: JobExecution jobExecution = jobExecutionService.find(scopeId, executionId);
204: JobTargetQuery jobTargetQuery = jobTargetFactory.newQuery(scopeId);
205: jobTargetQuery.setPredicate(jobTargetQuery.attributePredicate(KapuaEntityAttributes.ENTITY_ID, jobExecution.getTargetIds().toArray(new KapuaId[0])));
206: jobTargetQuery.setLimit(limit);
207: jobTargetQuery.setOffset(offset);
208:
209: return jobTargetService.query(jobTargetQuery);
210: }
211:
212: }