Search in sources :

Example 1 with WorkflowStatsComparison

use of co.cask.cdap.proto.WorkflowStatsComparison in project cdap by caskdata.

the class WorkflowStatsSLAHttpHandler method workflowRunDetail.

/**
   * The endpoint returns a list of workflow metrics based on the workflow run and a surrounding number of runs
   * of the workflow that are spaced apart by a time interval from each other.
   *
   * @param request The request
   * @param responder The responder
   * @param namespaceId The namespace the application is in
   * @param appId The application the workflow is in
   * @param workflowId The workflow that needs to have it stats shown
   * @param runId The run id of the Workflow that the user wants to see
   * @param limit The number of the records that the user wants to compare against on either side of the run
   * @param interval The timeInterval with which the user wants to space out the runs
   */
@GET
@Path("apps/{app-id}/workflows/{workflow-id}/runs/{run-id}/statistics")
public void workflowRunDetail(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("workflow-id") String workflowId, @PathParam("run-id") String runId, @QueryParam("limit") @DefaultValue("10") int limit, @QueryParam("interval") @DefaultValue("10s") String interval) throws Exception {
    if (limit <= 0) {
        throw new BadRequestException("Limit has to be greater than 0. Entered value was : " + limit);
    }
    long timeInterval;
    try {
        timeInterval = TimeMathParser.resolutionInSeconds(interval);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Interval is specified with invalid time unit. It should be specified with one" + " of the 'ms', 's', 'm', 'h', 'd' units. Entered value was : " + interval);
    }
    if (timeInterval <= 0) {
        throw new BadRequestException("Interval should be greater than 0 and should be specified with one of the 'ms'," + " 's', 'm', 'h', 'd' units. Entered value was : " + interval);
    }
    WorkflowId workflow = new WorkflowId(namespaceId, appId, workflowId);
    Collection<WorkflowDataset.WorkflowRunRecord> workflowRunRecords = store.retrieveSpacedRecords(workflow, runId, limit, timeInterval);
    List<WorkflowRunMetrics> workflowRunMetricsList = new ArrayList<>();
    Map<String, Long> startTimes = new HashMap<>();
    for (WorkflowDataset.WorkflowRunRecord workflowRunRecord : workflowRunRecords) {
        workflowRunMetricsList.add(getDetailedRecord(workflow, workflowRunRecord.getWorkflowRunId()));
        startTimes.put(workflowRunRecord.getWorkflowRunId(), RunIds.getTime(RunIds.fromString(workflowRunRecord.getWorkflowRunId()), TimeUnit.SECONDS));
    }
    Collection<WorkflowStatsComparison.ProgramNodes> formattedStatisticsMap = format(workflowRunMetricsList);
    responder.sendJson(HttpResponseStatus.OK, new WorkflowStatsComparison(startTimes, formattedStatisticsMap));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WorkflowId(co.cask.cdap.proto.id.WorkflowId) WorkflowStatsComparison(co.cask.cdap.proto.WorkflowStatsComparison) BadRequestException(co.cask.cdap.common.BadRequestException) WorkflowDataset(co.cask.cdap.internal.app.store.WorkflowDataset) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with WorkflowStatsComparison

use of co.cask.cdap.proto.WorkflowStatsComparison in project cdap by caskdata.

the class WorkflowStatsSLAHttpHandlerTest method testDetails.

@Test
public void testDetails() throws Exception {
    deploy(WorkflowApp.class);
    String workflowName = "FunWorkflow";
    String mapreduceName = "ClassicWordCount";
    String sparkName = "SparkWorkflowTest";
    WorkflowId workflowProgram = WORKFLOW_APP.workflow(workflowName);
    ProgramId mapreduceProgram = WORKFLOW_APP.mr(mapreduceName);
    ProgramId sparkProgram = WORKFLOW_APP.spark(sparkName);
    List<RunId> runIdList = setupRuns(workflowProgram, mapreduceProgram, sparkProgram, store, 13);
    String request = String.format("%s/namespaces/%s/apps/%s/workflows/%s/runs/%s/statistics?limit=%s&interval=%s", Constants.Gateway.API_VERSION_3, Id.Namespace.DEFAULT.getId(), WorkflowApp.class.getSimpleName(), workflowProgram.getProgram(), runIdList.get(6).getId(), "3", "10m");
    HttpResponse response = doGet(request);
    WorkflowStatsComparison workflowStatistics = readResponse(response, new TypeToken<WorkflowStatsComparison>() {
    }.getType());
    Assert.assertEquals(7, workflowStatistics.getProgramNodesList().iterator().next().getWorkflowProgramDetailsList().size());
    request = String.format("%s/namespaces/%s/apps/%s/workflows/%s/runs/%s/statistics?limit=0", Constants.Gateway.API_VERSION_3, Id.Namespace.DEFAULT.getId(), WorkflowApp.class.getSimpleName(), workflowProgram.getProgram(), runIdList.get(6).getId());
    response = doGet(request);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), response.getStatusLine().getStatusCode());
    request = String.format("%s/namespaces/%s/apps/%s/workflows/%s/runs/%s/statistics?limit=10&interval=10", Constants.Gateway.API_VERSION_3, Id.Namespace.DEFAULT.getId(), WorkflowApp.class.getSimpleName(), workflowProgram.getProgram(), runIdList.get(6).getId());
    response = doGet(request);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), response.getStatusLine().getStatusCode());
    request = String.format("%s/namespaces/%s/apps/%s/workflows/%s/runs/%s/statistics?limit=10&interval=10P", Constants.Gateway.API_VERSION_3, Id.Namespace.DEFAULT.getId(), WorkflowApp.class.getSimpleName(), workflowProgram.getProgram(), runIdList.get(6).getId());
    response = doGet(request);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), response.getStatusLine().getStatusCode());
    request = String.format("%s/namespaces/%s/apps/%s/workflows/%s/runs/%s/statistics?limit=20&interval=0d", Constants.Gateway.API_VERSION_3, Id.Namespace.DEFAULT.getId(), WorkflowApp.class.getSimpleName(), workflowProgram.getProgram(), runIdList.get(6).getId());
    response = doGet(request);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), response.getStatusLine().getStatusCode());
}
Also used : WorkflowApp(co.cask.cdap.WorkflowApp) WorkflowStatsComparison(co.cask.cdap.proto.WorkflowStatsComparison) TypeToken(com.google.gson.reflect.TypeToken) HttpResponse(org.apache.http.HttpResponse) WorkflowId(co.cask.cdap.proto.id.WorkflowId) ProgramId(co.cask.cdap.proto.id.ProgramId) RunId(org.apache.twill.api.RunId) Test(org.junit.Test)

Aggregations

WorkflowStatsComparison (co.cask.cdap.proto.WorkflowStatsComparison)2 WorkflowId (co.cask.cdap.proto.id.WorkflowId)2 WorkflowApp (co.cask.cdap.WorkflowApp)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 WorkflowDataset (co.cask.cdap.internal.app.store.WorkflowDataset)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 TypeToken (com.google.gson.reflect.TypeToken)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 HttpResponse (org.apache.http.HttpResponse)1 RunId (org.apache.twill.api.RunId)1 Test (org.junit.Test)1