~larry-e-works/uci-engine/amqp-to-kombu

« back to all changes in this revision

Viewing changes to nf-stats-service/nfss/api/v1.py

  • Committer: Larry Works
  • Date: 2014-09-30 18:08:31 UTC
  • mfrom: (762.1.55 uci-engine)
  • Revision ID: larry.works@canonical.com-20140930180831-cpztlnifhc47ah69
Massive merge from trunk, no conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
"""
22
22
from datetime import (
23
23
    datetime,
24
 
    timedelta,
25
24
    timezone,
26
25
)
27
26
import json
28
 
from pyramid.view import view_config
29
27
from pyramid.httpexceptions import HTTPBadRequest
30
28
 
31
29
from nfss import database
32
30
from nfss.auth import oauth_protected
33
31
 
34
32
 
35
 
UTC = timezone(timedelta(0))
36
 
 
37
 
 
38
33
def configure_routes(config):
39
34
    """Add url routes to 'config' object."""
40
35
    config.add_route('v1.root', '/')
42
37
    config.add_route('v1.test', '/{project}/{test}')
43
38
 
44
39
 
45
 
@view_config(route_name='v1.root', renderer='json', request_method='GET')
 
40
def configure_views(config):
 
41
    """Add views to 'config' object."""
 
42
    config.add_view(root, route_name='v1.root', renderer='json',
 
43
                    request_method='GET')
 
44
    config.add_view(project, route_name='v1.project', renderer='json',
 
45
                    request_method='GET')
 
46
    config.add_view(test, route_name='v1.test', renderer='json',
 
47
                    request_method='GET')
 
48
    config.add_view(test_add, route_name='v1.test', renderer='json',
 
49
                    request_method='POST')
 
50
 
 
51
 
46
52
def root(request):
47
53
    """Return summary data about the projects and tests."""
48
54
    data = database.get_details_for_all_projects(request.database())
56
62
    )
57
63
 
58
64
 
59
 
@view_config(route_name='v1.project', renderer='json', request_method='GET')
60
65
def project(request):
61
66
    """Return detailed information about this project's tests."""
62
67
    project_name = request.matchdict['project']
74
79
    return dict(project_name=project_name, tests=project_data)
75
80
 
76
81
 
77
 
@view_config(route_name='v1.test', renderer='json', request_method='GET')
78
82
def test(request):
79
83
    """Return data points for this particular test.
80
84
 
108
112
def try_parse_date(date_param):
109
113
    if date_param is not None:
110
114
        try:
111
 
            return datetime.fromtimestamp(float(date_param), UTC)
 
115
            return datetime.fromtimestamp(float(date_param), timezone.utc)
112
116
        except:
113
117
            return None
114
118
    return None
115
119
 
116
120
 
117
 
@view_config(route_name='v1.test', renderer='json', request_method='POST')
 
121
def get_timestamp_from_request_params(parameters):
 
122
    timestamp = parameters.get('timestamp', None)
 
123
    if timestamp:
 
124
        try:
 
125
            return datetime.fromtimestamp(timestamp, timezone.utc)
 
126
        except:
 
127
            raise HTTPBadRequest(
 
128
                'Invalid time zone format; '
 
129
                'Should be epoch seconds in UTC, microseconds optional')
 
130
    return datetime.now(timezone.utc)
 
131
 
 
132
 
 
133
def raise_if_timestamp_invalid(timestamp):
 
134
    time_delta = (timestamp - datetime.now(tz=timezone.utc))
 
135
    if time_delta.days > 0 or (time_delta.days == 0 and
 
136
                               time_delta.seconds > 1800):
 
137
        raise HTTPBadRequest(
 
138
            'Timestamp %s is more than 30 minutes in the future; '
 
139
            'Please check your system clock' % timestamp)
 
140
    if timestamp.year < 2004:
 
141
        raise HTTPBadRequest(
 
142
            'Timestamp %s is older than 2004; '
 
143
            'Please make sure this is a valid Ubuntu benchmark' % timestamp)
 
144
 
 
145
 
118
146
@oauth_protected()
119
147
def test_add(request, oauth_request):
120
148
    """Add data points to this test.
130
158
    except KeyError as e:
131
159
        raise HTTPBadRequest("Missing data in POST request: %s" % e)
132
160
    else:
 
161
        timestamp = get_timestamp_from_request_params(request.params)
 
162
        raise_if_timestamp_invalid(timestamp)
133
163
        created_id = database.insert_test_data(
134
164
            request.database(),
135
165
            project_name,
136
166
            test_name,
137
167
            data,
138
 
            oauth_request.client_key
 
168
            oauth_request.client_key,
 
169
            timestamp,
139
170
        )
140
171
        return dict(created_id=created_id)