1 /* 2 * Copyright (c) 2017 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may 6 * obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 * implied. See the License for the specific language governing 14 * permissions and limitations under the License. 15 */ 16 17 package com.android.vts.servlet; 18 19 import com.android.vts.entity.DeviceInfoEntity; 20 import com.android.vts.entity.TestPlanEntity; 21 import com.android.vts.entity.TestPlanRunEntity; 22 import com.android.vts.entity.TestRunEntity; 23 import com.android.vts.proto.VtsReportMessage.TestCaseResult; 24 import com.android.vts.util.TestRunMetadata; 25 import com.google.appengine.api.datastore.DatastoreService; 26 import com.google.appengine.api.datastore.DatastoreServiceFactory; 27 import com.google.appengine.api.datastore.Entity; 28 import com.google.appengine.api.datastore.EntityNotFoundException; 29 import com.google.appengine.api.datastore.Key; 30 import com.google.appengine.api.datastore.KeyFactory; 31 import com.google.appengine.api.datastore.Query; 32 import com.google.gson.Gson; 33 import com.google.gson.JsonObject; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.List; 37 import java.util.Map; 38 import java.util.logging.Level; 39 import javax.servlet.RequestDispatcher; 40 import javax.servlet.ServletException; 41 import javax.servlet.http.HttpServletRequest; 42 import javax.servlet.http.HttpServletResponse; 43 44 /** Servlet for handling requests to load individual plan runs. */ 45 public class ShowPlanRunServlet extends BaseServlet { 46 private static final String PLAN_RUN_JSP = "WEB-INF/jsp/show_plan_run.jsp"; 47 48 @Override getNavParentType()49 public PageType getNavParentType() { 50 return PageType.RELEASE; 51 } 52 53 @Override getBreadcrumbLinks(HttpServletRequest request)54 public List<Page> getBreadcrumbLinks(HttpServletRequest request) { 55 List<Page> links = new ArrayList<>(); 56 String planName = request.getParameter("plan"); 57 links.add(new Page(PageType.PLAN_RELEASE, planName, "?plan=" + planName)); 58 59 String time = request.getParameter("time"); 60 links.add(new Page(PageType.PLAN_RUN, "?plan=" + planName + "&time=" + time)); 61 return links; 62 } 63 64 @Override doGetHandler(HttpServletRequest request, HttpServletResponse response)65 public void doGetHandler(HttpServletRequest request, HttpServletResponse response) 66 throws IOException { 67 Long time = null; // time in microseconds 68 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 69 RequestDispatcher dispatcher = null; 70 71 String plan = request.getParameter("plan"); 72 73 if (request.getParameter("time") != null) { 74 String timeString = request.getParameter("time"); 75 try { 76 time = Long.parseLong(timeString); 77 time = time > 0 ? time : null; 78 } catch (NumberFormatException e) { 79 time = null; 80 } 81 } 82 83 // Add result names to list 84 List<String> resultNames = new ArrayList<>(); 85 for (TestCaseResult r : TestCaseResult.values()) { 86 resultNames.add(r.name()); 87 } 88 89 List<JsonObject> passingTestObjects = new ArrayList<>(); 90 List<JsonObject> failingTestObjects = new ArrayList<>(); 91 List<JsonObject> testRunObjects = new ArrayList<>(); 92 93 Key planKey = KeyFactory.createKey(TestPlanEntity.KIND, plan); 94 Key planRunKey = KeyFactory.createKey(planKey, TestPlanRunEntity.KIND, time); 95 String testBuildId = ""; 96 int passCount = 0; 97 int failCount = 0; 98 long startTime = 0; 99 long endTime = 0; 100 long moduleCount = 0; 101 long totalApiCount = 0L; 102 long totalCoveredApiCount = 0L; 103 try { 104 Entity testPlanRunEntity = datastore.get(planRunKey); 105 TestPlanRunEntity testPlanRun = TestPlanRunEntity.fromEntity(testPlanRunEntity); 106 Map<Key, Entity> testRuns = datastore.get(testPlanRun.getOldTestRuns()); 107 testBuildId = testPlanRun.getTestBuildId(); 108 passCount = (int) testPlanRun.getPassCount(); 109 failCount = (int) testPlanRun.getFailCount(); 110 totalApiCount = testPlanRun.getTotalApiCount(); 111 logger.log(Level.INFO, "totalApiCount => " + totalApiCount); 112 totalCoveredApiCount = testPlanRun.getCoveredApiCount(); 113 logger.log(Level.INFO, "totalCoveredApiCount => " + totalCoveredApiCount); 114 startTime = testPlanRun.getStartTimestamp(); 115 endTime = testPlanRun.getEndTimestamp(); 116 moduleCount = testPlanRun.getTestRuns().size(); 117 118 for (Key key : testPlanRun.getOldTestRuns()) { 119 if (!testRuns.containsKey(key)) continue; 120 TestRunEntity testRunEntity = TestRunEntity.fromEntity(testRuns.get(key)); 121 if (testRunEntity == null) continue; 122 Query deviceInfoQuery = new Query(DeviceInfoEntity.KIND).setAncestor(key); 123 List<DeviceInfoEntity> devices = new ArrayList<>(); 124 for (Entity device : datastore.prepare(deviceInfoQuery).asIterable()) { 125 DeviceInfoEntity deviceEntity = DeviceInfoEntity.fromEntity(device); 126 if (deviceEntity == null) continue; 127 devices.add(deviceEntity); 128 } 129 TestRunMetadata metadata = 130 new TestRunMetadata(key.getParent().getName(), testRunEntity, devices); 131 if (metadata.testRun.getFailCount() > 0) { 132 failingTestObjects.add(metadata.toJson()); 133 } else { 134 passingTestObjects.add(metadata.toJson()); 135 } 136 } 137 } catch (EntityNotFoundException e) { 138 // Invalid parameters 139 } 140 testRunObjects.addAll(failingTestObjects); 141 testRunObjects.addAll(passingTestObjects); 142 143 int[] topBuildResultCounts = new int[TestCaseResult.values().length]; 144 topBuildResultCounts[TestCaseResult.TEST_CASE_RESULT_PASS.getNumber()] = passCount; 145 topBuildResultCounts[TestCaseResult.TEST_CASE_RESULT_FAIL.getNumber()] = failCount; 146 147 request.setAttribute("plan", request.getParameter("plan")); 148 request.setAttribute("time", request.getParameter("time")); 149 150 request.setAttribute("resultNames", resultNames); 151 request.setAttribute("resultNamesJson", new Gson().toJson(resultNames)); 152 request.setAttribute("testRuns", new Gson().toJson(testRunObjects)); 153 request.setAttribute("testBuildId", new Gson().toJson(testBuildId)); 154 request.setAttribute("startTime", new Gson().toJson(startTime)); 155 request.setAttribute("endTime", new Gson().toJson(endTime)); 156 request.setAttribute("moduleCount", new Gson().toJson(moduleCount)); 157 request.setAttribute("passingTestCaseCount", new Gson().toJson(passCount)); 158 request.setAttribute("failingTestCaseCount", new Gson().toJson(failCount)); 159 request.setAttribute("totalApiCount", totalApiCount); 160 request.setAttribute("totalCoveredApiCount", totalCoveredApiCount); 161 162 // data for pie chart 163 request.setAttribute("topBuildResultCounts", new Gson().toJson(topBuildResultCounts)); 164 165 dispatcher = request.getRequestDispatcher(PLAN_RUN_JSP); 166 try { 167 dispatcher.forward(request, response); 168 } catch (ServletException e) { 169 logger.log(Level.SEVERE, "Servlet Exception caught : " + e.toString()); 170 } 171 } 172 } 173