2012-02-01 11:59:23 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#-*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license
|
|
|
|
# that can be found in the LICENSE file in the root of the source
|
|
|
|
# tree. An additional intellectual property rights grant can be found
|
|
|
|
# in the file PATENTS. All contributing project authors may
|
|
|
|
# be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
|
|
|
"""Implements a handler for adding coverage data."""
|
|
|
|
|
|
|
|
__author__ = 'phoglund@webrtc.org (Patrik Höglund)'
|
|
|
|
|
2012-03-12 10:12:32 +01:00
|
|
|
from datetime import datetime
|
2012-02-27 16:42:25 +01:00
|
|
|
import logging
|
2012-02-01 11:59:23 +01:00
|
|
|
|
|
|
|
from google.appengine.ext import db
|
|
|
|
|
|
|
|
import oauth_post_request_handler
|
|
|
|
|
2012-03-12 10:12:32 +01:00
|
|
|
REPORT_CATEGORIES = ('small_medium_tests', 'large_tests')
|
|
|
|
|
|
|
|
|
2012-02-01 11:59:23 +01:00
|
|
|
class CoverageData(db.Model):
|
|
|
|
"""This represents one coverage report from the build bot."""
|
2012-03-12 10:12:32 +01:00
|
|
|
|
|
|
|
# The date the report was made.
|
2012-02-01 11:59:23 +01:00
|
|
|
date = db.DateTimeProperty(required=True)
|
2012-03-12 10:12:32 +01:00
|
|
|
|
|
|
|
# Coverage percentages.
|
2012-02-01 11:59:23 +01:00
|
|
|
line_coverage = db.FloatProperty(required=True)
|
|
|
|
function_coverage = db.FloatProperty(required=True)
|
2012-03-12 10:12:32 +01:00
|
|
|
branch_coverage = db.FloatProperty()
|
2012-02-01 11:59:23 +01:00
|
|
|
|
2012-03-12 10:12:32 +01:00
|
|
|
# The report category must be one of the REPORT_CATEGORIES.
|
|
|
|
report_category = db.CategoryProperty()
|
2012-02-01 11:59:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AddCoverageData(oauth_post_request_handler.OAuthPostRequestHandler):
|
|
|
|
"""Used to report coverage data.
|
|
|
|
|
|
|
|
Coverage data is reported as a POST request and should contain, aside from
|
|
|
|
the regular oauth_* parameters, these values:
|
|
|
|
|
|
|
|
date: The POSIX timestamp for when the coverage observation was made.
|
2012-03-12 10:12:32 +01:00
|
|
|
report_category: A value in REPORT_CATEGORIES which characterizes the
|
|
|
|
coverage information (e.g. is the coverage from small / medium tests
|
|
|
|
or large tests?)
|
|
|
|
|
|
|
|
line_coverage: Line coverage percentage.
|
|
|
|
function_coverage: Function coverage percentage.
|
|
|
|
branch_coverage: Branch coverage percentage.
|
2012-02-01 11:59:23 +01:00
|
|
|
"""
|
|
|
|
|
2012-02-06 11:55:12 +01:00
|
|
|
def _parse_and_store_data(self):
|
2012-02-01 11:59:23 +01:00
|
|
|
try:
|
2012-03-12 10:12:32 +01:00
|
|
|
request_posix_timestamp = float(self.request.get('oauth_timestamp'))
|
|
|
|
parsed_date = datetime.fromtimestamp(request_posix_timestamp)
|
2012-02-01 11:59:23 +01:00
|
|
|
|
2012-03-12 10:12:32 +01:00
|
|
|
line_coverage = self._parse_percentage('line_coverage')
|
|
|
|
function_coverage = self._parse_percentage('function_coverage')
|
|
|
|
branch_coverage = self._parse_percentage('branch_coverage')
|
|
|
|
report_category = self._parse_category('report_category')
|
2012-02-01 11:59:23 +01:00
|
|
|
|
2012-02-27 16:42:25 +01:00
|
|
|
except ValueError as error:
|
2012-03-01 16:50:30 +01:00
|
|
|
logging.warn('Invalid parameter in request: %s.' % error)
|
2012-02-27 16:42:25 +01:00
|
|
|
self.response.set_status(400)
|
2012-02-01 11:59:23 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
item = CoverageData(date=parsed_date,
|
|
|
|
line_coverage=line_coverage,
|
2012-03-12 10:12:32 +01:00
|
|
|
function_coverage=function_coverage,
|
|
|
|
branch_coverage=branch_coverage,
|
|
|
|
report_category=report_category)
|
2012-02-01 11:59:23 +01:00
|
|
|
item.put()
|
|
|
|
|
2012-03-12 10:12:32 +01:00
|
|
|
def _parse_percentage(self, key):
|
|
|
|
"""Parses out a percentage value from the request."""
|
2012-04-17 10:49:10 +02:00
|
|
|
percentage = float(self.request.get(key))
|
2012-03-12 10:12:32 +01:00
|
|
|
if percentage < 0.0 or percentage > 100.0:
|
|
|
|
raise ValueError('%s is not a valid percentage.' % string_value)
|
|
|
|
return percentage
|
|
|
|
|
|
|
|
def _parse_category(self, key):
|
|
|
|
value = self.request.get(key)
|
|
|
|
if value in REPORT_CATEGORIES:
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
raise ValueError("Invalid category %s." % value)
|