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 |
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 | |
| 22 | class CoverageData(db.Model): |
| 23 | """This represents one coverage report from the build bot.""" |
| 24 | date = db.DateTimeProperty(required=True) |
| 25 | line_coverage = db.FloatProperty(required=True) |
| 26 | function_coverage = db.FloatProperty(required=True) |
| 27 | |
| 28 | |
| 29 | def _parse_percentage(string_value): |
| 30 | percentage = float(string_value) |
| 31 | if percentage < 0.0 or percentage > 100.0: |
| 32 | raise ValueError('%s is not a valid percentage.' % string_value) |
| 33 | return percentage |
| 34 | |
| 35 | |
| 36 | class AddCoverageData(oauth_post_request_handler.OAuthPostRequestHandler): |
| 37 | """Used to report coverage data. |
| 38 | |
| 39 | Coverage data is reported as a POST request and should contain, aside from |
| 40 | the regular oauth_* parameters, these values: |
| 41 | |
| 42 | date: The POSIX timestamp for when the coverage observation was made. |
| 43 | line_coverage: A float percentage in the interval 0-100.0. |
| 44 | function_coverage: A float percentage in the interval 0-100.0. |
| 45 | """ |
| 46 | |
phoglund@webrtc.org | 86ce46d | 2012-02-06 10:55:12 +0000 | [diff] [blame] | 47 | def _parse_and_store_data(self): |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 48 | try: |
| 49 | posix_time = int(self.request.get('date')) |
| 50 | parsed_date = datetime.datetime.fromtimestamp(posix_time) |
| 51 | |
| 52 | line_coverage_string = self.request.get('line_coverage') |
| 53 | line_coverage = _parse_percentage(line_coverage_string) |
| 54 | function_coverage_string = self.request.get('function_coverage') |
| 55 | function_coverage = _parse_percentage(function_coverage_string) |
| 56 | |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame] | 57 | except ValueError as error: |
phoglund@webrtc.org | 0f1a96a | 2012-03-01 15:50:30 +0000 | [diff] [blame^] | 58 | logging.warn('Invalid parameter in request: %s.' % error) |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame] | 59 | self.response.set_status(400) |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 60 | return |
| 61 | |
| 62 | item = CoverageData(date=parsed_date, |
| 63 | line_coverage=line_coverage, |
| 64 | function_coverage=function_coverage) |
| 65 | item.put() |
| 66 | |