use of io.cdap.cdap.proto.RunCountResult in project cdap by caskdata.
the class DefaultStoreTest method testProgramRunCount.
@Test
public void testProgramRunCount() {
ApplicationSpecification spec = Specifications.from(new AllProgramsApp());
ApplicationId appId = NamespaceId.DEFAULT.app(spec.getName());
ArtifactId testArtifact = NamespaceId.DEFAULT.artifact("testArtifact", "1.0").toApiArtifactId();
ProgramId workflowId = appId.workflow(AllProgramsApp.NoOpWorkflow.NAME);
ProgramId serviceId = appId.service(AllProgramsApp.NoOpService.NAME);
ProgramId nonExistingAppProgramId = NamespaceId.DEFAULT.app("nonExisting").workflow("test");
ProgramId nonExistingProgramId = appId.workflow("nonExisting");
// add the application
store.addApplication(appId, spec);
// add some run records to workflow and service
for (int i = 0; i < 5; i++) {
setStart(workflowId.run(RunIds.generate()), Collections.emptyMap(), Collections.emptyMap(), testArtifact);
setStart(serviceId.run(RunIds.generate()), Collections.emptyMap(), Collections.emptyMap(), testArtifact);
}
List<RunCountResult> result = store.getProgramRunCounts(ImmutableList.of(workflowId, serviceId, nonExistingAppProgramId, nonExistingProgramId));
// compare the result
Assert.assertEquals(4, result.size());
for (RunCountResult runCountResult : result) {
ProgramId programId = runCountResult.getProgramId();
Long count = runCountResult.getCount();
if (programId.equals(nonExistingAppProgramId) || programId.equals(nonExistingProgramId)) {
Assert.assertNull(count);
Assert.assertTrue(runCountResult.getException() instanceof NotFoundException);
} else {
Assert.assertNotNull(count);
Assert.assertEquals(5L, count.longValue());
}
}
// remove the app should remove all run count
store.removeApplication(appId);
for (RunCountResult runCountResult : store.getProgramRunCounts(ImmutableList.of(workflowId, serviceId))) {
Assert.assertNull(runCountResult.getCount());
Assert.assertTrue(runCountResult.getException() instanceof NotFoundException);
}
}
use of io.cdap.cdap.proto.RunCountResult in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getRunCounts.
/**
* Returns the run counts for all program runnables that are passed into the data. The data is an array of
* Json objects where each object must contain the following three elements: appId, programType, and programId.
* The max number of programs in the request is 100.
* <p>
* Example input:
* <pre><code>
* [{"appId": "App1", "programType": "Service", "programId": "Service1"},
* {"appId": "App1", "programType": "Workflow", "programId": "testWorkflow"},
* {"appId": "App2", "programType": "Workflow", "programId": "DataPipelineWorkflow"}]
* </code></pre>
* </p><p>
* </p><p>
* The response will be an array of JsonObjects each of which will contain the three input parameters
* as well as 2 fields, "runCount" which maps to the count of the program and "statusCode" which maps to the
* status code for the data in that JsonObjects.
* </p><p>
* If an error occurs in the input (for the example above, workflow in app1 does not exist),
* then all JsonObjects for which the parameters have a valid status will have the count field but all JsonObjects
* for which the parameters do not have a valid status will have an error message and statusCode.
* </p><p>
* For example, if there is no workflow in App1 in the data above, then the response would be 200 OK with following
* possible data:
* </p>
* <pre><code>
* [{"appId": "App1", "programType": "Service", "programId": "Service1",
* "statusCode": 200, "runCount": 20},
* {"appId": "App1", "programType": "Workflow", "programId": "testWorkflow", "statusCode": 404,
* "error": "Program 'testWorkflow' is not found"},
* {"appId": "App2", "programType": "Workflow", "programId": "DataPipelineWorkflow",
* "statusCode": 200, "runCount": 300}]
* </code></pre>
*/
@POST
@Path("/runcount")
public void getRunCounts(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
List<BatchProgram> programs = validateAndGetBatchInput(request, BATCH_PROGRAMS_TYPE);
if (programs.size() > 100) {
throw new BadRequestException(String.format("%d programs found in the request, the maximum number " + "supported is 100", programs.size()));
}
List<ProgramId> programIds = programs.stream().map(batchProgram -> new ProgramId(namespaceId, batchProgram.getAppId(), batchProgram.getProgramType(), batchProgram.getProgramId())).collect(Collectors.toList());
List<BatchProgramCount> counts = new ArrayList<>(programs.size());
for (RunCountResult runCountResult : lifecycleService.getProgramRunCounts(programIds)) {
ProgramId programId = runCountResult.getProgramId();
Exception exception = runCountResult.getException();
if (exception == null) {
counts.add(new BatchProgramCount(programId, HttpResponseStatus.OK.code(), null, runCountResult.getCount()));
} else if (exception instanceof NotFoundException) {
counts.add(new BatchProgramCount(programId, HttpResponseStatus.NOT_FOUND.code(), exception.getMessage(), null));
} else if (exception instanceof UnauthorizedException) {
counts.add(new BatchProgramCount(programId, HttpResponseStatus.FORBIDDEN.code(), exception.getMessage(), null));
} else {
counts.add(new BatchProgramCount(programId, HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), exception.getMessage(), null));
}
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(counts));
}
use of io.cdap.cdap.proto.RunCountResult in project cdap by caskdata.
the class ProgramLifecycleService method getProgramRunCounts.
/**
* Returns the program run count of the given program id list.
*
* @param programIds the list of program ids to get the count
* @return the counts of given program ids
*/
public List<RunCountResult> getProgramRunCounts(List<ProgramId> programIds) throws Exception {
// filter the result
Principal principal = authenticationContext.getPrincipal();
Set<? extends EntityId> visibleEntities = accessEnforcer.isVisible(new HashSet<>(programIds), principal);
Set<ProgramId> filteredIds = programIds.stream().filter(visibleEntities::contains).collect(Collectors.toSet());
Map<ProgramId, RunCountResult> programCounts = store.getProgramRunCounts(filteredIds).stream().collect(Collectors.toMap(RunCountResult::getProgramId, c -> c));
List<RunCountResult> result = new ArrayList<>();
for (ProgramId programId : programIds) {
if (!visibleEntities.contains(programId)) {
result.add(new RunCountResult(programId, null, new UnauthorizedException(principal, programId)));
} else {
RunCountResult count = programCounts.get(programId);
if (count != null) {
result.add(count);
} else {
result.add(new RunCountResult(programId, 0L, null));
}
}
}
return result;
}
use of io.cdap.cdap.proto.RunCountResult in project cdap by caskdata.
the class DefaultStore method getProgramRunCounts.
@Override
public List<RunCountResult> getProgramRunCounts(Collection<ProgramId> programIds) {
return TransactionRunners.run(transactionRunner, context -> {
List<RunCountResult> result = new ArrayList<>();
AppMetadataStore appMetadataStore = getAppMetadataStore(context);
Map<ProgramId, Long> runCounts = appMetadataStore.getProgramRunCounts(appMetadataStore.filterProgramsExistence(programIds));
for (ProgramId programId : programIds) {
Long count = runCounts.get(programId);
if (count == null) {
result.add(new RunCountResult(programId, null, new NotFoundException(programId)));
} else {
result.add(new RunCountResult(programId, count, null));
}
}
return result;
});
}
Aggregations