blob: 1ec9f6a1c4884edee42f8fdf40aeea8fdfe125ef [file] [log] [blame]
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +00001#!/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
15import datetime
16
17from google.appengine.ext import db
18
19import oauth_post_request_handler
20
21class 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
28def _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
35class 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
phoglund@webrtc.org86ce46d2012-02-06 10:55:12 +000046 def _parse_and_store_data(self):
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000047 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