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 | |
| 15 | import datetime |
| 16 | |
| 17 | from google.appengine.ext import db |
| 18 | |
| 19 | import oauth_post_request_handler |
| 20 | |
| 21 | class CoverageData(db.Model): |
| 22 | """This represents one coverage report from the build bot.""" |
| 23 | date = db.DateTimeProperty(required=True) |
| 24 | line_coverage = db.FloatProperty(required=True) |
| 25 | function_coverage = db.FloatProperty(required=True) |
| 26 | |
| 27 | |
| 28 | def _parse_percentage(string_value): |
| 29 | percentage = float(string_value) |
| 30 | if percentage < 0.0 or percentage > 100.0: |
| 31 | raise ValueError('%s is not a valid percentage.' % string_value) |
| 32 | return percentage |
| 33 | |
| 34 | |
| 35 | class AddCoverageData(oauth_post_request_handler.OAuthPostRequestHandler): |
| 36 | """Used to report coverage data. |
| 37 | |
| 38 | Coverage data is reported as a POST request and should contain, aside from |
| 39 | the regular oauth_* parameters, these values: |
| 40 | |
| 41 | date: The POSIX timestamp for when the coverage observation was made. |
| 42 | line_coverage: A float percentage in the interval 0-100.0. |
| 43 | function_coverage: A float percentage in the interval 0-100.0. |
| 44 | """ |
| 45 | |
| 46 | def post(self): |
| 47 | try: |
| 48 | posix_time = int(self.request.get('date')) |
| 49 | parsed_date = datetime.datetime.fromtimestamp(posix_time) |
| 50 | |
| 51 | line_coverage_string = self.request.get('line_coverage') |
| 52 | line_coverage = _parse_percentage(line_coverage_string) |
| 53 | function_coverage_string = self.request.get('function_coverage') |
| 54 | function_coverage = _parse_percentage(function_coverage_string) |
| 55 | |
| 56 | except ValueError as exception: |
| 57 | self._show_error_page('Invalid parameter in request. Details: %s' % |
| 58 | exception) |
| 59 | return |
| 60 | |
| 61 | item = CoverageData(date=parsed_date, |
| 62 | line_coverage=line_coverage, |
| 63 | function_coverage=function_coverage) |
| 64 | item.put() |
| 65 | |