blob: 52a388adc791065fcd000426b24c0ff1e6da60c2 [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
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000016import logging
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000017
18from google.appengine.ext import db
19
20import oauth_post_request_handler
21
22class 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
29def _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
36class 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.org86ce46d2012-02-06 10:55:12 +000047 def _parse_and_store_data(self):
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000048 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.org914ef272012-02-27 15:42:25 +000057 except ValueError as error:
phoglund@webrtc.org0f1a96a2012-03-01 15:50:30 +000058 logging.warn('Invalid parameter in request: %s.' % error)
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000059 self.response.set_status(400)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000060 return
61
62 item = CoverageData(date=parsed_date,
63 line_coverage=line_coverage,
64 function_coverage=function_coverage)
65 item.put()
66