phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | #-*- coding: utf-8 -*- |
| 3 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
| 10 | |
| 11 | """Implements a handler for adding coverage data.""" |
| 12 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 13 | from datetime import datetime |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame] | 14 | import logging |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 15 | |
| 16 | from google.appengine.ext import db |
| 17 | |
| 18 | import oauth_post_request_handler |
| 19 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 20 | REPORT_CATEGORIES = ('small_medium_tests', 'large_tests') |
| 21 | |
| 22 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 23 | class CoverageData(db.Model): |
| 24 | """This represents one coverage report from the build bot.""" |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 25 | # The date the report was made. |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 26 | date = db.DateTimeProperty(required=True) |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 27 | |
| 28 | # Coverage percentages. |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 29 | line_coverage = db.FloatProperty(required=True) |
| 30 | function_coverage = db.FloatProperty(required=True) |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 31 | branch_coverage = db.FloatProperty() |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 32 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 33 | # The report category must be one of the REPORT_CATEGORIES. |
| 34 | report_category = db.CategoryProperty() |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | class AddCoverageData(oauth_post_request_handler.OAuthPostRequestHandler): |
| 38 | """Used to report coverage data. |
| 39 | |
| 40 | Coverage data is reported as a POST request and should contain, aside from |
| 41 | the regular oauth_* parameters, these values: |
| 42 | |
| 43 | date: The POSIX timestamp for when the coverage observation was made. |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 44 | report_category: A value in REPORT_CATEGORIES which characterizes the |
| 45 | coverage information (e.g. is the coverage from small / medium tests |
| 46 | or large tests?) |
| 47 | |
| 48 | line_coverage: Line coverage percentage. |
| 49 | function_coverage: Function coverage percentage. |
| 50 | branch_coverage: Branch coverage percentage. |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 51 | """ |
phoglund@webrtc.org | 86ce46d | 2012-02-06 10:55:12 +0000 | [diff] [blame] | 52 | def _parse_and_store_data(self): |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 53 | try: |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 54 | request_posix_timestamp = float(self.request.get('oauth_timestamp')) |
| 55 | parsed_date = datetime.fromtimestamp(request_posix_timestamp) |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 56 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 57 | line_coverage = self._parse_percentage('line_coverage') |
| 58 | function_coverage = self._parse_percentage('function_coverage') |
| 59 | branch_coverage = self._parse_percentage('branch_coverage') |
| 60 | report_category = self._parse_category('report_category') |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 61 | |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame] | 62 | except ValueError as error: |
phoglund@webrtc.org | 0f1a96a | 2012-03-01 15:50:30 +0000 | [diff] [blame] | 63 | logging.warn('Invalid parameter in request: %s.' % error) |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame] | 64 | self.response.set_status(400) |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 65 | return |
| 66 | |
| 67 | item = CoverageData(date=parsed_date, |
| 68 | line_coverage=line_coverage, |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 69 | function_coverage=function_coverage, |
| 70 | branch_coverage=branch_coverage, |
| 71 | report_category=report_category) |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 72 | item.put() |
| 73 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 74 | def _parse_percentage(self, key): |
| 75 | """Parses out a percentage value from the request.""" |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 76 | string_value = self.request.get(key) |
| 77 | percentage = float(string_value) |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame] | 78 | if percentage < 0.0 or percentage > 100.0: |
| 79 | raise ValueError('%s is not a valid percentage.' % string_value) |
| 80 | return percentage |
| 81 | |
| 82 | def _parse_category(self, key): |
| 83 | value = self.request.get(key) |
| 84 | if value in REPORT_CATEGORIES: |
| 85 | return value |
| 86 | else: |
| 87 | raise ValueError("Invalid category %s." % value) |