blob: ed3abad70bdb9305f2b3b7477588da70e5a3d6ea [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
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000013from datetime import datetime
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000014import logging
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000015
16from google.appengine.ext import db
17
18import oauth_post_request_handler
19
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000020REPORT_CATEGORIES = ('small_medium_tests', 'large_tests')
21
22
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000023class CoverageData(db.Model):
24 """This represents one coverage report from the build bot."""
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000025 # The date the report was made.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000026 date = db.DateTimeProperty(required=True)
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000027
28 # Coverage percentages.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000029 line_coverage = db.FloatProperty(required=True)
30 function_coverage = db.FloatProperty(required=True)
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000031 branch_coverage = db.FloatProperty()
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000032
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000033 # The report category must be one of the REPORT_CATEGORIES.
34 report_category = db.CategoryProperty()
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000035
36
37class AddCoverageData(oauth_post_request_handler.OAuthPostRequestHandler):
38 """Used to report coverage data.
39
40 Coverage data is reported as a POST request and should contain, aside from
41 the regular oauth_* parameters, these values:
42
43 date: The POSIX timestamp for when the coverage observation was made.
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000044 report_category: A value in REPORT_CATEGORIES which characterizes the
45 coverage information (e.g. is the coverage from small / medium tests
46 or large tests?)
47
48 line_coverage: Line coverage percentage.
49 function_coverage: Function coverage percentage.
50 branch_coverage: Branch coverage percentage.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000051 """
phoglund@webrtc.org86ce46d2012-02-06 10:55:12 +000052 def _parse_and_store_data(self):
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000053 try:
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000054 request_posix_timestamp = float(self.request.get('oauth_timestamp'))
55 parsed_date = datetime.fromtimestamp(request_posix_timestamp)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000056
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000057 line_coverage = self._parse_percentage('line_coverage')
58 function_coverage = self._parse_percentage('function_coverage')
59 branch_coverage = self._parse_percentage('branch_coverage')
60 report_category = self._parse_category('report_category')
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000061
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000062 except ValueError as error:
phoglund@webrtc.org0f1a96a2012-03-01 15:50:30 +000063 logging.warn('Invalid parameter in request: %s.' % error)
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000064 self.response.set_status(400)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000065 return
66
67 item = CoverageData(date=parsed_date,
68 line_coverage=line_coverage,
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000069 function_coverage=function_coverage,
70 branch_coverage=branch_coverage,
71 report_category=report_category)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000072 item.put()
73
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000074 def _parse_percentage(self, key):
75 """Parses out a percentage value from the request."""
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +000076 string_value = self.request.get(key)
77 percentage = float(string_value)
phoglund@webrtc.orgfc402762012-03-12 09:12:32 +000078 if percentage < 0.0 or percentage > 100.0:
79 raise ValueError('%s is not a valid percentage.' % string_value)
80 return percentage
81
82 def _parse_category(self, key):
83 value = self.request.get(key)
84 if value in REPORT_CATEGORIES:
85 return value
86 else:
87 raise ValueError("Invalid category %s." % value)