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 | |
| 13 | __author__ = 'phoglund@webrtc.org (Patrik Höglund)' |
| 14 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 15 | from datetime import datetime |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame] | 16 | import logging |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 17 | |
| 18 | from google.appengine.ext import db |
| 19 | |
| 20 | import oauth_post_request_handler |
| 21 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 22 | REPORT_CATEGORIES = ('small_medium_tests', 'large_tests') |
| 23 | |
| 24 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 25 | class CoverageData(db.Model): |
| 26 | """This represents one coverage report from the build bot.""" |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 27 | |
| 28 | # The date the report was made. |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 29 | date = db.DateTimeProperty(required=True) |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 30 | |
| 31 | # Coverage percentages. |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 32 | line_coverage = db.FloatProperty(required=True) |
| 33 | function_coverage = db.FloatProperty(required=True) |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 34 | branch_coverage = db.FloatProperty() |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 35 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 36 | # The report category must be one of the REPORT_CATEGORIES. |
| 37 | report_category = db.CategoryProperty() |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | class AddCoverageData(oauth_post_request_handler.OAuthPostRequestHandler): |
| 41 | """Used to report coverage data. |
| 42 | |
| 43 | Coverage data is reported as a POST request and should contain, aside from |
| 44 | the regular oauth_* parameters, these values: |
| 45 | |
| 46 | date: The POSIX timestamp for when the coverage observation was made. |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 47 | report_category: A value in REPORT_CATEGORIES which characterizes the |
| 48 | coverage information (e.g. is the coverage from small / medium tests |
| 49 | or large tests?) |
| 50 | |
| 51 | line_coverage: Line coverage percentage. |
| 52 | function_coverage: Function coverage percentage. |
| 53 | branch_coverage: Branch coverage percentage. |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 54 | """ |
| 55 | |
phoglund@webrtc.org | 86ce46d | 2012-02-06 10:55:12 +0000 | [diff] [blame] | 56 | def _parse_and_store_data(self): |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 57 | try: |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 58 | request_posix_timestamp = float(self.request.get('oauth_timestamp')) |
| 59 | parsed_date = datetime.fromtimestamp(request_posix_timestamp) |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 60 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 61 | line_coverage = self._parse_percentage('line_coverage') |
| 62 | function_coverage = self._parse_percentage('function_coverage') |
| 63 | branch_coverage = self._parse_percentage('branch_coverage') |
| 64 | report_category = self._parse_category('report_category') |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 65 | |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame] | 66 | except ValueError as error: |
phoglund@webrtc.org | 0f1a96a | 2012-03-01 15:50:30 +0000 | [diff] [blame] | 67 | logging.warn('Invalid parameter in request: %s.' % error) |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame] | 68 | self.response.set_status(400) |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 69 | return |
| 70 | |
| 71 | item = CoverageData(date=parsed_date, |
| 72 | line_coverage=line_coverage, |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 73 | function_coverage=function_coverage, |
| 74 | branch_coverage=branch_coverage, |
| 75 | report_category=report_category) |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 76 | item.put() |
| 77 | |
phoglund@webrtc.org | fc40276 | 2012-03-12 09:12:32 +0000 | [diff] [blame^] | 78 | def _parse_percentage(self, key): |
| 79 | """Parses out a percentage value from the request.""" |
| 80 | value = float(self.request.get(key)) |
| 81 | if percentage < 0.0 or percentage > 100.0: |
| 82 | raise ValueError('%s is not a valid percentage.' % string_value) |
| 83 | return percentage |
| 84 | |
| 85 | def _parse_category(self, key): |
| 86 | value = self.request.get(key) |
| 87 | if value in REPORT_CATEGORIES: |
| 88 | return value |
| 89 | else: |
| 90 | raise ValueError("Invalid category %s." % value) |