blob: 7f3a1a54cc8b62b2afa9f0b6e151b9a0f560e892 [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
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000015from datetime import 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
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000022REPORT_CATEGORIES = ('small_medium_tests', 'large_tests')
23
24
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000025class CoverageData(db.Model):
26 """This represents one coverage report from the build bot."""
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000027
28 # The date the report was made.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000029 date = db.DateTimeProperty(required=True)
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000030
31 # Coverage percentages.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000032 line_coverage = db.FloatProperty(required=True)
33 function_coverage = db.FloatProperty(required=True)
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000034 branch_coverage = db.FloatProperty()
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000035
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000036 # The report category must be one of the REPORT_CATEGORIES.
37 report_category = db.CategoryProperty()
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000038
39
40class 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.orgfc402762012-03-12 09:12:32 +000047 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.orgd4f0a0e2012-02-01 10:59:23 +000054 """
55
phoglund@webrtc.org86ce46d2012-02-06 10:55:12 +000056 def _parse_and_store_data(self):
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000057 try:
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000058 request_posix_timestamp = float(self.request.get('oauth_timestamp'))
59 parsed_date = datetime.fromtimestamp(request_posix_timestamp)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000060
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000061 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.orgd4f0a0e2012-02-01 10:59:23 +000065
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000066 except ValueError as error:
phoglund@webrtc.org0f1a96a2012-03-01 15:50:30 +000067 logging.warn('Invalid parameter in request: %s.' % error)
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000068 self.response.set_status(400)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000069 return
70
71 item = CoverageData(date=parsed_date,
72 line_coverage=line_coverage,
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000073 function_coverage=function_coverage,
74 branch_coverage=branch_coverage,
75 report_category=report_category)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000076 item.put()
77
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000078 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)