where Clauses for analytics APIThe current API where clause is limited in terms of functionality and the set of operations it can support. This document represents a survey of the possible alternatives.
The requirements of any alternative is that it supports (either partially or fully) the following list of features:
* An extensible array of operators, namely:
* ==, != Equality and inequality (both point query and ‘in’ query) operator
* <, <=, > and >= Range query opeators
* &&, || and ! Logical operators
* (, ) grouping operators
The alternatives below are evaluated on three axes: * Ease of use * A new user should find it easy to read the documentation/examples and not find something in the API very difficult to use. This covers the syntax as well as the readability of the syntax. This requirement also ties into how much effort is needed to on-board a new user in terms of reading the documentation, examples and being able to get started. The effort needed to get from zero to understanding a sizable chuck of the functionality so that the developer may feel productive. * Extensibility * This requirement is more forward facing and intends to capture how easy is it to extend the functionality provided by an option. The developer who creates a new piece of functionality should be able to add that feature without disturbing the ease of use argument.
This option suggests that the URI query string be used like a regular string and we construct queries based on a set of developer defined operators. This means that we could potentially specify a query string that is very readable but not necessarily similar to the key-value syntax of most URI query strings.
In this example we use ;, , to denote logical and and or operations.
https://api.brightcove.com/v1/accounts/1234567890/report?from=2014-01-04&to=now&offset=200&limit=100&dimensions=video&where=(video_duration%3E=300;video==1,2,3,4;video_view%3C=100),(video_view%3E=9000)
(video_duration>=300;video==1,2,3,4;video_view<=100),(video_view>=9000)
The where clause if fairly easy to understand (once decoded) the user knows what the different operators mean but it would still require some getting used to.
This option proposes the use of a grammar that represents a conjunctive query with negation. Parse.com does this form of api. As an example consider:
bash
curl -X GET \
-H "X-Parse-Application-Id: ${APPLICATION_ID}" \
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
-G \
--data-urlencode 'where={"score":{"$gte":1000,"$lte":3000}}' \
https://api.parse.com/1/classes/GameScore
As is evident, here the syntax of the where parameter is a json document that will be url encoded which increases the readability but also makes the user think in a prefix (conjunctive query like) syntax for the expressions.
Another way to represent may use a more familiar syntax by not using json and replacing it with “regular looking” function calls. Eg:
and(gte(video_duration, 200), or(eq(video, 1), eq(video, 2), eq(video, 3), eq(video_name, foo%2Cbar))
function name and the corresponding logic needed to generate the database query.