summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/job/VtsCoverageAlertJobServlet.java
blob: a11249621f0f166a62e2011ffae4452296581395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (c) 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.job;

import static com.googlecode.objectify.ObjectifyService.ofy;

import com.android.vts.entity.DeviceInfoEntity;
import com.android.vts.entity.TestCoverageStatusEntity;
import com.android.vts.entity.TestRunEntity;
import com.android.vts.util.DatastoreHelper;
import com.android.vts.util.EmailHelper;
import com.google.appengine.api.datastore.DatastoreFailureException;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreTimeoutException;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;

/**
 * Coverage notification job.
 */
public class VtsCoverageAlertJobServlet extends HttpServlet {

  private static final String COVERAGE_ALERT_URL = "/task/vts_coverage_job";
  protected static final Logger logger =
      Logger.getLogger(VtsCoverageAlertJobServlet.class.getName());
  protected static final double CHANGE_ALERT_THRESHOLD = 0.05;
  protected static final double GOOD_THRESHOLD = 0.7;
  protected static final double BAD_THRESHOLD = 0.3;

  protected static final DecimalFormat FORMATTER;

  /** Initialize the decimal formatter. */
  static {
    FORMATTER = new DecimalFormat("#.#");
    FORMATTER.setRoundingMode(RoundingMode.HALF_UP);
  }

  /**
   * Gets a new coverage status and adds notification emails to the messages list.
   *
   * Send an email to notify subscribers in the event that a test goes up or down by more than 5%,
   * becomes higher or lower than 70%, or becomes higher or lower than 30%.
   *
   * @param status The TestCoverageStatusEntity object for the test.
   * @param testRunKey The key for TestRunEntity whose data to process and reflect in the state.
   * @param link The string URL linking to the test's status table.
   * @param emailAddresses The list of email addresses to send notifications to.
   * @param messages The email Message queue.
   * @returns TestCoverageStatusEntity or null if no update is available.
   */
  public static TestCoverageStatusEntity getTestCoverageStatus(
      TestCoverageStatusEntity status,
      Key testRunKey,
      String link,
      List<String> emailAddresses,
      List<Message> messages)
      throws IOException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    String testName = status.getTestName();

    double previousPct;
    double coveragePct;
    if (status == null || status.getTotalLineCount() <= 0 || status.getCoveredLineCount() < 0) {
      previousPct = 0;
    } else {
      previousPct = ((double) status.getCoveredLineCount()) / status.getTotalLineCount();
    }

    Entity testRun;
    try {
      testRun = datastore.get(testRunKey);
    } catch (EntityNotFoundException e) {
      logger.log(Level.WARNING, "Test run not found: " + testRunKey);
      return null;
    }

    TestRunEntity testRunEntity = TestRunEntity.fromEntity(testRun);
    if (testRunEntity == null || !testRunEntity.isHasCoverage()) {
      return null;
    }
    if (testRunEntity.getTotalLineCount() <= 0 || testRunEntity.getCoveredLineCount() < 0) {
      coveragePct = 0;
    } else {
      coveragePct =
          ((double) testRunEntity.getCoveredLineCount()) / testRunEntity.getTotalLineCount();
    }

    Set<String> buildIdList = new HashSet<>();
    Query deviceQuery = new Query(DeviceInfoEntity.KIND).setAncestor(testRun.getKey());
    List<DeviceInfoEntity> devices = new ArrayList<>();
    for (Entity device : datastore.prepare(deviceQuery).asIterable()) {
      DeviceInfoEntity deviceEntity = DeviceInfoEntity.fromEntity(device);
      if (deviceEntity == null) {
        continue;
      }
      devices.add(deviceEntity);
      buildIdList.add(deviceEntity.buildId);
    }
    String deviceBuild = StringUtils.join(buildIdList, ", ");
    String footer = EmailHelper.getEmailFooter(testRunEntity, devices, link);

    String subject = null;
    String body = null;
    String subjectSuffix = " @ " + deviceBuild;
    if (coveragePct >= GOOD_THRESHOLD && previousPct < GOOD_THRESHOLD) {
      // Coverage entered the good zone
      subject =
          "Congratulations! "
              + testName
              + " has exceeded "
              + FORMATTER.format(GOOD_THRESHOLD * 100)
              + "% coverage"
              + subjectSuffix;
      body =
          "Hello,<br><br>The "
              + testName
              + " has achieved "
              + FORMATTER.format(coveragePct * 100)
              + "% code coverage on device build ID(s): "
              + deviceBuild
              + "."
              + footer;
    } else if (coveragePct < GOOD_THRESHOLD && previousPct >= GOOD_THRESHOLD) {
      // Coverage dropped out of the good zone
      subject =
          "Warning! "
              + testName
              + " has dropped below "
              + FORMATTER.format(GOOD_THRESHOLD * 100)
              + "% coverage"
              + subjectSuffix;
      ;
      body =
          "Hello,<br><br>The test "
              + testName
              + " has dropped to "
              + FORMATTER.format(coveragePct * 100)
              + "% code coverage on device build ID(s): "
              + deviceBuild
              + "."
              + footer;
    } else if (coveragePct <= BAD_THRESHOLD && previousPct > BAD_THRESHOLD) {
      // Coverage entered into the bad zone
      subject =
          "Warning! "
              + testName
              + " has dropped below "
              + FORMATTER.format(BAD_THRESHOLD * 100)
              + "% coverage"
              + subjectSuffix;
      body =
          "Hello,<br><br>The test "
              + testName
              + " has dropped to "
              + FORMATTER.format(coveragePct * 100)
              + "% code coverage on device build ID(s): "
              + deviceBuild
              + "."
              + footer;
    } else if (coveragePct > BAD_THRESHOLD && previousPct <= BAD_THRESHOLD) {
      // Coverage emerged from the bad zone
      subject =
          "Congratulations! "
              + testName
              + " has exceeded "
              + FORMATTER.format(BAD_THRESHOLD * 100)
              + "% coverage"
              + subjectSuffix;
      body =
          "Hello,<br><br>The test "
              + testName
              + " has achived "
              + FORMATTER.format(coveragePct * 100)
              + "% code coverage on device build ID(s): "
              + deviceBuild
              + "."
              + footer;
    } else if (coveragePct - previousPct < -CHANGE_ALERT_THRESHOLD) {
      // Send a coverage drop alert
      subject =
          "Warning! "
              + testName
              + "'s code coverage has decreased by more than "
              + FORMATTER.format(CHANGE_ALERT_THRESHOLD * 100)
              + "%"
              + subjectSuffix;
      body =
          "Hello,<br><br>The test "
              + testName
              + " has dropped from "
              + FORMATTER.format(previousPct * 100)
              + "% code coverage to "
              + FORMATTER.format(coveragePct * 100)
              + "% code coverage on device build ID(s): "
              + deviceBuild
              + "."
              + footer;
    } else if (coveragePct - previousPct > CHANGE_ALERT_THRESHOLD) {
      // Send a coverage improvement alert
      subject =
          testName
              + "'s code coverage has increased by more than "
              + FORMATTER.format(CHANGE_ALERT_THRESHOLD * 100)
              + "%"
              + subjectSuffix;
      body =
          "Hello,<br><br>The test "
              + testName
              + " has increased from "
              + FORMATTER.format(previousPct * 100)
              + "% code coverage to "
              + FORMATTER.format(coveragePct * 100)
              + "% code coverage on device build ID(s): "
              + deviceBuild
              + "."
              + footer;
    }
    if (subject != null && body != null) {
      try {
        messages.add(EmailHelper.composeEmail(emailAddresses, subject, body));
      } catch (MessagingException | UnsupportedEncodingException e) {
        logger.log(Level.WARNING, "Error composing email : ", e);
      }
    }
    return new TestCoverageStatusEntity(
        testName,
        testRunEntity.getStartTimestamp(),
        testRunEntity.getCoveredLineCount(),
        testRunEntity.getTotalLineCount());
  }

  /**
   * Add a task to process coverage data
   *
   * @param testRunKey The key of the test run whose data process.
   */
  public static void addTask(Key testRunKey) {
    Queue queue = QueueFactory.getDefaultQueue();
    String keyString = KeyFactory.keyToString(testRunKey);
    queue.add(
        TaskOptions.Builder.withUrl(COVERAGE_ALERT_URL)
            .param("runKey", keyString)
            .method(TaskOptions.Method.POST));
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    String runKeyString = request.getParameter("runKey");

    Key testRunKey;
    try {
      testRunKey = KeyFactory.stringToKey(runKeyString);
    } catch (IllegalArgumentException e) {
      logger.log(Level.WARNING, "Invalid key specified: " + runKeyString);
      return;
    }
    String testName = testRunKey.getParent().getName();

    TestCoverageStatusEntity status = ofy().load().type(TestCoverageStatusEntity.class).id(testName)
        .now();
    if (status == null) {
      status = new TestCoverageStatusEntity(testName, 0, -1, -1);
    }

    StringBuffer fullUrl = request.getRequestURL();
    String baseUrl = fullUrl.substring(0, fullUrl.indexOf(request.getRequestURI()));
    String link = baseUrl + "/show_tree?testName=" + testName;
    TestCoverageStatusEntity newStatus;
    List<Message> messageQueue = new ArrayList<>();
    try {
      List<String> emails = EmailHelper.getSubscriberEmails(testRunKey.getParent());
      newStatus = getTestCoverageStatus(status, testRunKey, link, emails, messageQueue);
    } catch (IOException e) {
      logger.log(Level.SEVERE, e.toString());
      return;
    }

    if (newStatus == null) {
      return;
    } else {
      if (status == null || status.getUpdatedTimestamp() < newStatus.getUpdatedTimestamp()) {
        newStatus.save();
        EmailHelper.sendAll(messageQueue);
      }
    }
  }
}