findAnnotations: include duration alongside result count

fixes #2921
This commit is contained in:
Will Thompson 2016-05-04 16:58:57 +00:00
parent 41cc8e3573
commit 05e6118a88
No known key found for this signature in database
GPG key ID: 3422DC0D7AD482A7

View file

@ -3,6 +3,8 @@
from __future__ import division from __future__ import division
from django.conf import settings from django.conf import settings
from django.db.models import Count, Sum, F, Value
from django.db.models.functions import Coalesce
import ox import ox
from ox.utils import json from ox.utils import json
@ -85,12 +87,21 @@ def findAnnotations(request, data):
range: [int, int], // items to return, per current sort order, see `find` range: [int, int], // items to return, per current sort order, see `find`
sort: [] // list of sort object, see `find` sort: [] // list of sort object, see `find`
} }
returns { returns { // if `keys` is present
annotations: [{ annotations: [{
id: string, // annotation id id: string, // annotation id
... // more annotation properties ... // more annotation properties
}] }]
} or { // if `keys` is missing
items: int, // total number of matching annotations
duration: float // total duration of matching annotations, in seconds
} or { // if `positions` is present
positions: { // returns positions of given annotations
id: position, // position of the annotation, per current sort order
... // more id/position pairs
} }
}
see: addAnnotation, addAnnotations, editAnnotation, find, getAnnotation, see: addAnnotation, addAnnotations, editAnnotation, find, getAnnotation,
removeAnnotation removeAnnotation
''' '''
@ -120,7 +131,10 @@ def findAnnotations(request, data):
ids = [i.public_id for i in qs] ids = [i.public_id for i in qs]
response['data']['positions'] = utils.get_positions(ids, data['positions']) response['data']['positions'] = utils.get_positions(ids, data['positions'])
else: else:
response['data']['items'] = qs.count() response['data'] = qs.aggregate(
items=Count('*'),
duration=Coalesce(Sum(F('end') - F('start')), Value(0)),
)
return render_to_json_response(response) return render_to_json_response(response)
actions.register(findAnnotations) actions.register(findAnnotations)