Skip to content
Navigation Menu
{{ message }}
forked from tobami/codespeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
478 lines (384 loc) · 17 KB
/
Copy pathapi.py
File metadata and controls
478 lines (384 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# -*- coding: utf-8 -*-
"""RESTful API implementation
Example:
GET Environment() data:
curl -H "Accept: application/json" \
http://127.0.0.1:8000/api/v1/environment/1/
POST Environment() data:
curl --dump-header - -H "Content-Type: application/json" -X POST \
--data '{"name": "Single Core"}' \
http://127.0.0.1:8000/api/v1/environment/
PUT Environment() data:
curl --dump-header - -H "Content-Type: application/json" -X PUT \
--data '{"name": "Quad Core"}' \
http://127.0.0.1:8000/api/v1/environment/2/
DELETE Environment() data:
curl --dump-header - -H "Content-Type: application/json" -X DELETE \
http://127.0.0.1:8000/api/v1/environment/2/
PUT a full result:
curl --dump-header - -H "Content-Type: application/json" -X POST \
--data '{"commitid": "4", "branch": "default", \
"project": "MyProject", "executable": "myexe O3 64bits", \
"benchmark": "float", "environment": "Quad Core", \
"result_value": 4000, "result": "4000"}' \
http://127.0.0.1:8000/api/v1/benchmark-result/
See http://django-tastypie.readthedocs.org/en/latest/interacting.html
"""
import logging
from datetime import datetime
from django.contrib.auth.models import User
from django.db import models
from django.http import Http404
from django.shortcuts import get_object_or_404
from tastypie.bundle import Bundle
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.http import HttpBadRequest, HttpCreated, HttpNotImplemented
from tastypie.resources import ModelResource, Resource
from tastypie import fields
from tastypie.authorization import Authorization, DjangoAuthorization
from tastypie.authentication import (Authentication, ApiKeyAuthentication,
MultiAuthentication)
from tastypie.models import create_api_key
from tastypie.utils.dict import dict_strip_unicode_keys
from codespeed.models import (Environment, Project, Result, Branch, Revision,
Executable, Benchmark, Report)
models.signals.post_save.connect(create_api_key, sender=User)
class UserResource(ModelResource):
"""Resource for Django User()"""
class Meta:
queryset = User.objects.filter(is_active=True)
resource_name = 'user'
fields = ['username', 'first_name', 'last_name', 'email']
allowed_methods = ['get']
#excludes = ['email', 'password', 'is_superuser']
# Add it here.
authorization = DjangoAuthorization()
authentication = ApiKeyAuthentication()
class ProjectResource(ModelResource):
"""Resource for Project()"""
class Meta:
queryset = Project.objects.all()
authorization = DjangoAuthorization()
# Note, the order for MultiAuthentication matters!
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
class BranchResource(ModelResource):
"""Resource for Branch()"""
project = fields.ToOneField(ProjectResource, 'project')
class Meta:
queryset = Branch.objects.all()
authorization = DjangoAuthorization()
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
class RevisionResource(ModelResource):
"""Resource for Revision()"""
project = fields.ToOneField(ProjectResource, 'project')
branch = fields.ToOneField(BranchResource, 'branch')
class Meta:
queryset = Revision.objects.all()
authorization = DjangoAuthorization()
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
class ExecutableResource(ModelResource):
"""Resource for Executable()"""
project = fields.ToOneField(ProjectResource, 'project')
class Meta:
queryset = Executable.objects.all()
authorization = DjangoAuthorization()
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
class BenchmarkResource(ModelResource):
"""Resource for Benchmark()"""
class Meta:
queryset = Benchmark.objects.all()
authorization = DjangoAuthorization()
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
class EnvironmentResource(ModelResource):
"""Resource for Enviroment()"""
class Meta:
queryset = Environment.objects.all()
resource_name = 'environment'
authorization = DjangoAuthorization()
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
class ResultResource(ModelResource):
"""Resource for Result()"""
revision = fields.ToOneField(RevisionResource, 'revision')
executable = fields.ToOneField(ExecutableResource, 'executable')
benchmark = fields.ToOneField(BenchmarkResource, 'benchmark')
environment = fields.ToOneField(EnvironmentResource, 'environment')
class Meta:
queryset = Result.objects.all()
authorization = DjangoAuthorization()
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
class ReportResource(ModelResource):
"""Resource for Report()"""
revision = fields.ToOneField(RevisionResource, 'revision')
environment = fields.ToOneField(EnvironmentResource, 'environment')
executable = fields.ToOneField(ExecutableResource, 'executable')
class Meta:
queryset = Report.objects.all()
allowed_methods = ['get']
authorization = DjangoAuthorization()
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
class ResultBundle(Bundle):
"""tastypie.api.Bundle class to deal with submitted results.
Note, to populate the Bundle.obj with data .save()
has to be called first.
FIXME (a8): add models.Data if they do not exist in DB
"""
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
# order of mandatory_keys must not be changed
# FIXME (a8): refactor result_value to just value
mandatory_keys = (
'revision',
'project',
'executable',
'benchmark',
'environment',
'branch',
'result_value',
)
# Note, views.add_result() expects result_date. Here it's the
# same as the Result() attribute
optional_keys = (
'std_dev',
'val_min',
'val_max',
'date',
)
def __init__(self, obj=None, request=None, **kwargs):
self.data = kwargs
self.request = request
if isinstance(obj, Result):
self.obj = obj
self._populate_by_obj()
elif obj is None:
self.obj = Result()
else:
raise ValueError("obj has to be an instance of models.Result")
if self.data:
self._check_data()
self.__data_validated = False # not used for now
super(ResultBundle, self).__init__(data=self.data, obj=self.obj,
request=self.request)
def _populate_obj_by_data(self):
"""Database lookup
get everything except the result, 2nd try reverse lookup
"""
def populate(key):
return {'project': lambda: ProjectResource().get_via_uri(
self.data['project'], request=self.request),
'executable': lambda: ExecutableResource().get_via_uri(
self.data['executable'], request=self.request),
'benchmark': lambda: BenchmarkResource().get_via_uri(
self.data['benchmark'], request=self.request),
'environment': lambda: EnvironmentResource().get_via_uri(
self.data['environment'], request=self.request),
'branch': lambda: BranchResource().get_via_uri(
self.data['branch'], request=self.request),
'revision': lambda: RevisionResource().get_via_uri(
self.data['commitid'],
request=self.request)}.get(key, None)()
try:
self.obj.value = float(self.data['result_value'])
except ValueError, error:
logging.error(
"Result value: {0} cannot be converted to float. {1}".format(
self.data['result_value'], error
))
raise ImmediateHttpResponse(
response=HttpBadRequest(u"Value needs to be a number"))
for key in [k for k in self.mandatory_keys
if k not in ('result_value',)]:
try:
#populate
item = populate(key)
setattr(self.obj, key, item)
except Exception, error:
logging.error("Data for field %s: %s not found. %s" % (
key, self.data[key], error))
raise ImmediateHttpResponse(
response=HttpBadRequest(u"Error finding: {0}={1}".format(
key, self.data[key]
)))
# populate optional data
for key in [k for k in self.optional_keys
if k not in ('date')]:
if key in self.data.keys():
setattr(self.obj, key, self.data[key])
if 'date' in self.data.keys():
self.obj.date = self.data['date']
else:
self.obj.date = datetime.now()
def _populate_by_obj(self):
"""set attributes to make obj match ResultBundleResource
FIXME: That looks very wrong here.
"""
self.obj.project = self.obj.executable.project
self.obj.branch = self.obj.revision.branch
#self.obj.result = self.obj
setattr(self.obj, 'result', self.obj)
def _check_data(self):
"""See if all mandatory data is there"""
# check if all mandatory keys are there
for key in [k for k in self.mandatory_keys
if k not in ('revision')]:
if not key in self.data.keys():
error_text = u"You need to provide key: {0}".format(key)
logging.error(error_text)
raise ImmediateHttpResponse(
response=HttpBadRequest(error_text))
# check for data
elif not self.data[key]:
error_text = 'Value for key {0} is empty.'.format(key)
logging.error(error_text)
raise ImmediateHttpResponse(
response=HttpBadRequest(error_text))
# Check that the Environment exists
try:
self.obj.environment = EnvironmentResource().get_via_uri(
self.data['environment'], request=self.request)
except Environment.DoesNotExist:
error_text = 'Environment: {0} not found in database.'.format(
self.data['environment'])
logging.error(error_text)
raise ImmediateHttpResponse(
response=HttpBadRequest(
error_text
))
except Exception as e:
error_text = ('Error while looking up Environment: '
'{0}, {1}.'.format(self.data['environment'], e))
logging.error(error_text)
raise ImmediateHttpResponse(response=HttpBadRequest(error_text))
# check optional data
for key in [k for k in self.optional_keys
if k not in ('date',)]:
if key in self.data.keys():
try:
self.data[key] = float(self.data[key])
except ValueError:
error_text = u"{0} cannot be casted to float.".format(
self.data[key])
logging.error(error_text)
raise ImmediateHttpResponse(
response=HttpBadRequest(error_text))
if 'date' in self.data.keys():
#FIXME (a8): make that more robust for different json date formats
try:
self.data['date'] = datetime.strptime(self.data['date'],
self.DATETIME_FORMAT)
except ValueError:
error_text = u"Cannot convert date {0} into datetime.".format(
self.data['date'])
logging.error(error_text)
raise ImmediateHttpResponse(
response=HttpBadRequest(error_text))
def hydrate_and_save(self):
"""Save self.obj which is an instance of Result()
First populate the Result() instance with self.data
"""
self._populate_obj_by_data()
self.obj.save()
class ResultBundleResource(Resource):
"""Resource for all the data of a benchmark result.
Primarily used to submit benchmark results
mandatory data
'commitid',
'branch',
'project',
'executable',
'benchmark',
'environment',
'result_value',
not mandatory data
'notify' - Send notification to registered user if result varies
from previous results, currently not implemented
"""
revision = fields.ToOneField(RevisionResource, 'revision')
branch = fields.ToOneField(BranchResource, 'branch')
project = fields.ToOneField(ProjectResource, 'project')
executable = fields.ToOneField(ExecutableResource, 'executable')
benchmark = fields.ToOneField(BenchmarkResource, 'benchmark')
environment = fields.ToOneField(EnvironmentResource, 'environment')
result = fields.ToOneField(ResultResource, 'result')
class Meta:
resource_name = 'benchmark-result'
object_class = Result
authorization = DjangoAuthorization()
authentication = MultiAuthentication(ApiKeyAuthentication(),
Authentication())
allowed_methods = ['get', 'post', 'put', 'delete']
def get_resource_uri(self, bundle_or_obj):
kwargs = {
'resource_name': self._meta.resource_name,
}
if isinstance(bundle_or_obj, Bundle):
kwargs['pk'] = bundle_or_obj.obj.pk
else:
kwargs['pk'] = bundle_or_obj.pk
if self._meta.api_name is not None:
kwargs['api_name'] = self._meta.api_name
return self._build_reverse_url("api_dispatch_detail", kwargs=kwargs)
def get_object_list(self, request):
results = Result.objects.all()
return [ResultBundle(obj=r, request=request).obj for r in results]
def obj_get_list(self, request=None, **kwargs):
"""Return all benchmark results ever"""
return self.get_object_list(request)
def obj_get(self, request=None, **kwargs):
"""get the ResultBundle with the result_id as the primary key"""
pk = kwargs['pk']
result = Result.objects.get(pk=pk)
result.project = result.executable.project
result.branch = result.revision.branch
setattr(result, 'result', result)
return result
def obj_create(self, bundle, request=None, **kwargs):
# not calling hydrate here since bundle.save() has that functionality
# self.full_hydrate(bundle) will try to hydrate result which is not
# there yet
#bundle = self.full_hydrate(bundle)
bundle.hydrate_and_save()
return bundle
def obj_update(self, bundle, request=None, **kwargs):
return self.obj_create(bundle, request, **kwargs)
def post_list(self, request, **kwargs):
"""
Creates a new resource/object with the provided data.
Calls ``obj_create`` with the provided data and returns a response
with the new resource's location.
If a new resource is created, return ``HttpCreated`` (201 Created).
"""
deserialized = self.deserialize(
request, request.raw_post_data,
format=request.META.get('CONTENT_TYPE', 'application/json')
)
deserialized = self.alter_deserialized_list_data(request, deserialized)
bundle = ResultBundle(request=request,
**dict_strip_unicode_keys(deserialized))
self.is_valid(bundle)
updated_bundle = self.obj_create(bundle, request=request)
return HttpCreated(location=self.get_resource_uri(updated_bundle))
def post_detail(self, request, **kwargs):
"""
Creates a new subcollection of the resource under a resource.
This is not implemented by default because most people's data models
aren't self-referential.
If a new resource is created, return ``HttpCreated`` (201 Created).
"""
return HttpNotImplemented()
def obj_delete_list(self, request=None, **kwargs):
return HttpNotImplemented()
def obj_delete(self, request=None, **kwargs):
#obj = Result.objects.get(pk=kwargs['pk'])
#obj.delete()
return HttpNotImplemented()
def rollback(self, bundles):
pass
def detail_uri_kwargs(self):
pass
You can’t perform that action at this time.
