{"action":"created","comment":{"url":"https://api.github.com/repos/caskdata/cdap/pulls/comments/37385084","id":37385084,"diff_hunk":"@@ -0,0 +1,300 @@\n+/*\n+ * Copyright © 2015 Cask Data, Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n+ * use this file except in compliance with the License. You may obtain a copy of\n+ * the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+ * License for the specific language governing permissions and limitations under\n+ * the License.\n+ */\n+\n+package co.cask.cdap.internal.app.store;\n+\n+import co.cask.cdap.api.common.Bytes;\n+import co.cask.cdap.api.dataset.lib.AbstractDataset;\n+import co.cask.cdap.api.dataset.table.Row;\n+import co.cask.cdap.api.dataset.table.Scan;\n+import co.cask.cdap.api.dataset.table.Scanner;\n+import co.cask.cdap.api.dataset.table.Table;\n+import co.cask.cdap.data2.dataset2.lib.table.MDSKey;\n+import co.cask.cdap.proto.Id;\n+import co.cask.cdap.proto.PercentileInformation;\n+import co.cask.cdap.proto.ProgramType;\n+import co.cask.cdap.proto.WorkflowStatistics;\n+import com.google.common.primitives.Longs;\n+import com.google.gson.Gson;\n+import com.google.gson.reflect.TypeToken;\n+\n+import java.lang.reflect.Type;\n+import java.util.ArrayList;\n+import java.util.Collections;\n+import java.util.Comparator;\n+import java.util.HashMap;\n+import java.util.List;\n+import java.util.Map;\n+import java.util.Set;\n+import javax.annotation.Nullable;\n+\n+/**\n+ * Dataset for Completed Workflows and their associated actions\n+ */\n+public class WorkflowDataset extends AbstractDataset {\n+\n+  private static final Gson GSON = new Gson();\n+  private static final byte[] RUNID = Bytes.toBytes(\"r\");\n+  private static final byte[] TIME_TAKEN = Bytes.toBytes(\"t\");\n+  private static final byte[] NODES = Bytes.toBytes(\"n\");\n+  private static final Type ACTION_RUNS_TYPE = new TypeToken<List<ProgramRun>>() { }.getType();\n+\n+  private final Table table;\n+\n+  WorkflowDataset(Table table) {\n+    super(\"workflow.statistics\", table);\n+    this.table = table;\n+  }\n+\n+  void write(Id.Workflow id, RunRecordMeta runRecordMeta, List<ProgramRun> actionRunList) {\n+    long start = runRecordMeta.getStartTs();\n+\n+    MDSKey mdsKey = new MDSKey.Builder().add(id.getApplication().getNamespaceId())\n+      .add(id.getApplicationId()).add(id.getId()).add(start).build();\n+    byte[] rowKey = mdsKey.getKey();\n+    long timeTaken = runRecordMeta.getStopTs() - start;\n+\n+    String value = GSON.toJson(actionRunList, ACTION_RUNS_TYPE);\n+\n+    table.put(rowKey, RUNID, Bytes.toBytes(runRecordMeta.getPid()));\n+    table.put(rowKey, TIME_TAKEN, Bytes.toBytes(timeTaken));\n+    table.put(rowKey, NODES, Bytes.toBytes(value));\n+  }\n+\n+  /**\n+   * This function scans the workflow.stats dataset for a list of workflow runs in a time range.\n+   *\n+   * @param id The workflow id\n+   * @param timeRangeStart Start of the time range that the scan should begin from\n+   * @param timeRangeEnd End of the time range that the scan should end at\n+   * @return List of WorkflowRunRecords\n+   * @throws Exception\n+   */\n+  public List<WorkflowRunRecord> scan(Id.Workflow id, long timeRangeStart, long timeRangeEnd) throws Exception {\n+    byte[] startRowKey = new MDSKey.Builder().add(id.getApplication().getNamespaceId()).add(id.getApplicationId()).\n+      add(id.getId()).add(timeRangeStart).build().getKey();\n+    byte[] endRowKey = new MDSKey.Builder().add(id.getApplication().getNamespaceId()).add(id.getApplicationId()).\n+      add(id.getId()).add(timeRangeEnd).build().getKey();\n+    Scan scan = new Scan(startRowKey, endRowKey);\n+\n+    Scanner scanner = table.scan(scan);\n+    Row indexRow;\n+    List<WorkflowRunRecord> workflowRunRecordList = new ArrayList<>();\n+    while ((indexRow = scanner.next()) != null) {\n+      Map<byte[], byte[]> columns = indexRow.getColumns();\n+      String workflowRunId = Bytes.toString(columns.get(RUNID));\n+      long timeTaken = Bytes.toLong(columns.get(TIME_TAKEN));\n+\n+      List<ProgramRun> actionRunList = GSON.fromJson(Bytes.toString(columns.get(NODES)), ACTION_RUNS_TYPE);\n+      WorkflowRunRecord workflowRunRecord = new WorkflowRunRecord(workflowRunId, timeTaken, actionRunList);\n+      workflowRunRecordList.add(workflowRunRecord);\n+    }\n+    return workflowRunRecordList;\n+  }\n+\n+  /**\n+   * This method returns the statistics for a corresponding workflow. The user has to\n+   * provide a time interval and a list of percentiles that are required.\n+   *\n+   * @param id The workflow id\n+   * @param startTime The start of the time range from where the user wants the statistics\n+   * @param endTime The end of the time range till where the user wants the statistics\n+   * @param percentiles The list of percentiles that the user wants information on\n+   * @return A statistics object that provides information about the workflow or null if there are no runs associated\n+   * with the workflow\n+   * @throws Exception\n+   */\n+  @Nullable\n+  public WorkflowStatistics getStatistics(Id.Workflow id, long startTime,\n+                                          long endTime, List<Double> percentiles) throws Exception {\n+    List<WorkflowRunRecord> workflowRunRecords = scan(id, startTime, endTime);\n+    int runs = workflowRunRecords.size();\n+\n+    if (runs == 0) {\n+      return null;\n+    }\n+\n+    double avgRunTime = 0.0;\n+    for (WorkflowDataset.WorkflowRunRecord workflowRunRecord : workflowRunRecords) {\n+      avgRunTime += workflowRunRecord.getTimeTaken();\n+    }\n+    avgRunTime /= runs;\n+\n+    workflowRunRecords = sort(workflowRunRecords);\n+\n+    List<PercentileInformation> percentileInformationList = getPercentiles(workflowRunRecords, percentiles);\n+\n+    Map<String, ProgramRunDetails> actionToRunRecord = getActionRuns(workflowRunRecords);\n+\n+    Map<String, Map<String, String>> actionToStatistic = new HashMap<>();","path":"cdap-app-fabric/src/main/java/co/cask/cdap/internal/app/store/WorkflowDataset.java","position":142,"original_position":142,"commit_id":"1882774606a6b098c8fc951adad05a60a55dcba2","original_commit_id":"1882774606a6b098c8fc951adad05a60a55dcba2","user":{"login":"gsps1","id":1328150,"avatar_url":"https://avatars.githubusercontent.com/u/1328150?v=3","gravatar_id":"","url":"https://api.github.com/users/gsps1","html_url":"https://github.com/gsps1","followers_url":"https://api.github.com/users/gsps1/followers","following_url":"https://api.github.com/users/gsps1/following{/other_user}","gists_url":"https://api.github.com/users/gsps1/gists{/gist_id}","starred_url":"https://api.github.com/users/gsps1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsps1/subscriptions","organizations_url":"https://api.github.com/users/gsps1/orgs","repos_url":"https://api.github.com/users/gsps1/repos","events_url":"https://api.github.com/users/gsps1/events{/privacy}","received_events_url":"https://api.github.com/users/gsps1/received_events","type":"User","site_admin":false},"body":"can you make sure you use program instead of action everywhere. ","created_at":"2015-08-19T07:05:57Z","updated_at":"2015-08-19T07:05:57Z","html_url":"https://github.com/caskdata/cdap/pull/3582#discussion_r37385084","pull_request_url":"https://api.github.com/repos/caskdata/cdap/pulls/3582","_links":{"self":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/comments/37385084"},"html":{"href":"https://github.com/caskdata/cdap/pull/3582#discussion_r37385084"},"pull_request":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/3582"}}},"pull_request":{"url":"https://api.github.com/repos/caskdata/cdap/pulls/3582","id":42319129,"html_url":"https://github.com/caskdata/cdap/pull/3582","diff_url":"https://github.com/caskdata/cdap/pull/3582.diff","patch_url":"https://github.com/caskdata/cdap/pull/3582.patch","issue_url":"https://api.github.com/repos/caskdata/cdap/issues/3582","number":3582,"state":"open","locked":false,"title":"Workflow Stats SLA's","user":{"login":"rohanahata","id":2381977,"avatar_url":"https://avatars.githubusercontent.com/u/2381977?v=3","gravatar_id":"","url":"https://api.github.com/users/rohanahata","html_url":"https://github.com/rohanahata","followers_url":"https://api.github.com/users/rohanahata/followers","following_url":"https://api.github.com/users/rohanahata/following{/other_user}","gists_url":"https://api.github.com/users/rohanahata/gists{/gist_id}","starred_url":"https://api.github.com/users/rohanahata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rohanahata/subscriptions","organizations_url":"https://api.github.com/users/rohanahata/orgs","repos_url":"https://api.github.com/users/rohanahata/repos","events_url":"https://api.github.com/users/rohanahata/events{/privacy}","received_events_url":"https://api.github.com/users/rohanahata/received_events","type":"User","site_admin":false},"body":"This PR is a WIP. It just has the implementation for the first endpoint, which is the /statistics endpoint. \r\nhttps://wiki.cask.co/display/CE/Workflow+Diagnostics+Tool\r\nhttp://builds.cask.co/browse/CDAP-DUT2494\r\nhttps://issues.cask.co/browse/CDAP-3371","created_at":"2015-08-13T06:37:29Z","updated_at":"2015-08-19T07:05:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8eb05367ee3eff718050158f738258d7116080b4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/caskdata/cdap/pulls/3582/commits","review_comments_url":"https://api.github.com/repos/caskdata/cdap/pulls/3582/comments","review_comment_url":"https://api.github.com/repos/caskdata/cdap/pulls/comments{/number}","comments_url":"https://api.github.com/repos/caskdata/cdap/issues/3582/comments","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/1882774606a6b098c8fc951adad05a60a55dcba2","head":{"label":"caskdata:feature/workflow-stats-sla","ref":"feature/workflow-stats-sla","sha":"1882774606a6b098c8fc951adad05a60a55dcba2","user":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"repo":{"id":22542759,"name":"cdap","full_name":"caskdata/cdap","owner":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/caskdata/cdap","description":"Cask Data Application Platform (CDAP)","fork":false,"url":"https://api.github.com/repos/caskdata/cdap","forks_url":"https://api.github.com/repos/caskdata/cdap/forks","keys_url":"https://api.github.com/repos/caskdata/cdap/keys{/key_id}","collaborators_url":"https://api.github.com/repos/caskdata/cdap/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/caskdata/cdap/teams","hooks_url":"https://api.github.com/repos/caskdata/cdap/hooks","issue_events_url":"https://api.github.com/repos/caskdata/cdap/issues/events{/number}","events_url":"https://api.github.com/repos/caskdata/cdap/events","assignees_url":"https://api.github.com/repos/caskdata/cdap/assignees{/user}","branches_url":"https://api.github.com/repos/caskdata/cdap/branches{/branch}","tags_url":"https://api.github.com/repos/caskdata/cdap/tags","blobs_url":"https://api.github.com/repos/caskdata/cdap/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/caskdata/cdap/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/caskdata/cdap/git/refs{/sha}","trees_url":"https://api.github.com/repos/caskdata/cdap/git/trees{/sha}","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/{sha}","languages_url":"https://api.github.com/repos/caskdata/cdap/languages","stargazers_url":"https://api.github.com/repos/caskdata/cdap/stargazers","contributors_url":"https://api.github.com/repos/caskdata/cdap/contributors","subscribers_url":"https://api.github.com/repos/caskdata/cdap/subscribers","subscription_url":"https://api.github.com/repos/caskdata/cdap/subscription","commits_url":"https://api.github.com/repos/caskdata/cdap/commits{/sha}","git_commits_url":"https://api.github.com/repos/caskdata/cdap/git/commits{/sha}","comments_url":"https://api.github.com/repos/caskdata/cdap/comments{/number}","issue_comment_url":"https://api.github.com/repos/caskdata/cdap/issues/comments{/number}","contents_url":"https://api.github.com/repos/caskdata/cdap/contents/{+path}","compare_url":"https://api.github.com/repos/caskdata/cdap/compare/{base}...{head}","merges_url":"https://api.github.com/repos/caskdata/cdap/merges","archive_url":"https://api.github.com/repos/caskdata/cdap/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/caskdata/cdap/downloads","issues_url":"https://api.github.com/repos/caskdata/cdap/issues{/number}","pulls_url":"https://api.github.com/repos/caskdata/cdap/pulls{/number}","milestones_url":"https://api.github.com/repos/caskdata/cdap/milestones{/number}","notifications_url":"https://api.github.com/repos/caskdata/cdap/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/caskdata/cdap/labels{/name}","releases_url":"https://api.github.com/repos/caskdata/cdap/releases{/id}","created_at":"2014-08-02T09:00:10Z","updated_at":"2015-08-10T06:00:52Z","pushed_at":"2015-08-19T03:03:23Z","git_url":"git://github.com/caskdata/cdap.git","ssh_url":"git@github.com:caskdata/cdap.git","clone_url":"https://github.com/caskdata/cdap.git","svn_url":"https://github.com/caskdata/cdap","homepage":"http://cdap.io","size":851498,"stargazers_count":60,"watchers_count":60,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":19,"mirror_url":null,"open_issues_count":17,"forks":19,"open_issues":17,"watchers":60,"default_branch":"develop"}},"base":{"label":"caskdata:develop","ref":"develop","sha":"216b084716056c5761c1bca71583967d457a3dee","user":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"repo":{"id":22542759,"name":"cdap","full_name":"caskdata/cdap","owner":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/caskdata/cdap","description":"Cask Data Application Platform (CDAP)","fork":false,"url":"https://api.github.com/repos/caskdata/cdap","forks_url":"https://api.github.com/repos/caskdata/cdap/forks","keys_url":"https://api.github.com/repos/caskdata/cdap/keys{/key_id}","collaborators_url":"https://api.github.com/repos/caskdata/cdap/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/caskdata/cdap/teams","hooks_url":"https://api.github.com/repos/caskdata/cdap/hooks","issue_events_url":"https://api.github.com/repos/caskdata/cdap/issues/events{/number}","events_url":"https://api.github.com/repos/caskdata/cdap/events","assignees_url":"https://api.github.com/repos/caskdata/cdap/assignees{/user}","branches_url":"https://api.github.com/repos/caskdata/cdap/branches{/branch}","tags_url":"https://api.github.com/repos/caskdata/cdap/tags","blobs_url":"https://api.github.com/repos/caskdata/cdap/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/caskdata/cdap/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/caskdata/cdap/git/refs{/sha}","trees_url":"https://api.github.com/repos/caskdata/cdap/git/trees{/sha}","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/{sha}","languages_url":"https://api.github.com/repos/caskdata/cdap/languages","stargazers_url":"https://api.github.com/repos/caskdata/cdap/stargazers","contributors_url":"https://api.github.com/repos/caskdata/cdap/contributors","subscribers_url":"https://api.github.com/repos/caskdata/cdap/subscribers","subscription_url":"https://api.github.com/repos/caskdata/cdap/subscription","commits_url":"https://api.github.com/repos/caskdata/cdap/commits{/sha}","git_commits_url":"https://api.github.com/repos/caskdata/cdap/git/commits{/sha}","comments_url":"https://api.github.com/repos/caskdata/cdap/comments{/number}","issue_comment_url":"https://api.github.com/repos/caskdata/cdap/issues/comments{/number}","contents_url":"https://api.github.com/repos/caskdata/cdap/contents/{+path}","compare_url":"https://api.github.com/repos/caskdata/cdap/compare/{base}...{head}","merges_url":"https://api.github.com/repos/caskdata/cdap/merges","archive_url":"https://api.github.com/repos/caskdata/cdap/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/caskdata/cdap/downloads","issues_url":"https://api.github.com/repos/caskdata/cdap/issues{/number}","pulls_url":"https://api.github.com/repos/caskdata/cdap/pulls{/number}","milestones_url":"https://api.github.com/repos/caskdata/cdap/milestones{/number}","notifications_url":"https://api.github.com/repos/caskdata/cdap/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/caskdata/cdap/labels{/name}","releases_url":"https://api.github.com/repos/caskdata/cdap/releases{/id}","created_at":"2014-08-02T09:00:10Z","updated_at":"2015-08-10T06:00:52Z","pushed_at":"2015-08-19T03:03:23Z","git_url":"git://github.com/caskdata/cdap.git","ssh_url":"git@github.com:caskdata/cdap.git","clone_url":"https://github.com/caskdata/cdap.git","svn_url":"https://github.com/caskdata/cdap","homepage":"http://cdap.io","size":851498,"stargazers_count":60,"watchers_count":60,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":19,"mirror_url":null,"open_issues_count":17,"forks":19,"open_issues":17,"watchers":60,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/3582"},"html":{"href":"https://github.com/caskdata/cdap/pull/3582"},"issue":{"href":"https://api.github.com/repos/caskdata/cdap/issues/3582"},"comments":{"href":"https://api.github.com/repos/caskdata/cdap/issues/3582/comments"},"review_comments":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/3582/comments"},"review_comment":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/3582/commits"},"statuses":{"href":"https://api.github.com/repos/caskdata/cdap/statuses/1882774606a6b098c8fc951adad05a60a55dcba2"}}}}