slashroots / harvest-v2

The Harvest API is an open data platform for the publishing and sharing of agricultural sector data across government and with the public.
http://data.abisjamaica.com.jm
4 stars 3 forks source link

New Aggregate Functions #114

Closed nickwleo closed 7 years ago

nickwleo commented 7 years ago

This pull request implements two new aggregate functions :

1) The 'sum' function that was here has been replaced. Please see additional comment(s) below for details.

2) A 'date_range' parameter that allows the user to specify a field to be compared with a date range by also specifying either a start_date, an end_date, or both. If only one is specified, it will return all the results with a date after the start date or before the end date accordingly.

Example query : http://localhost:3000/api/crops?end_date=2012-08-31&start_date=2012-08-01&date_range=Plant_Date&District=Ticky%20Ticky - This will return all the crops planted in August of 2008 in the Ticky Ticky district.

@thinkking Please note these features and update the documentation accordingly on a separate branch.

nickwleo commented 7 years ago

http://localhost:3000/api/livestock?sum=Livestock_Count&Livestock_Name=Goats&date_range=Update_Date&start_date=2008-01-01&end_date=2008-12-31&District=Ticky%20Ticky - This will return the total number of goats known to be on farms in Ticky Ticky District that were updated sometime in 2008.

nickwleo commented 7 years ago

@matjames007 The changes discussed at last night's meeting have been made so this pull request can be reviewed.

@thinkking You could probably start working on the documentation for these changes in the meantime.

nickwleo commented 7 years ago

@matjames007 Please review this pull request again when you can. I've made all the proposed changes with the exception of implementing the other possible functions the library provides. I will create an issue for that now and I've already given @paradoxxjm a list of functions we could easily implement using the library for him to look over and for possible team discussion tomorrow evening.

matjames007 commented 7 years ago

We need to specify the appropriate error code. In this case most of them are bad formatting or 400 errors.

nickwleo commented 7 years ago

I have implemented error code handling as per @matjames007 's recommendations.

I have also implemented the aggregate functions differently and they now work as follows :

6 aggregate functions are currently supported and they all carry out a type of operation on a specific field of the result set. They are :

(i) sum - This computes the total of all the values from a specific field in the result set. (ii) avg - This computes the average of all the values from a specific field in the result set. (iii) min - This returns the lowest of all the values from a specific field in the result set. (iv) max - This returns the highest of all the values from a specific field in the result set. (v) var - This returns the statistical variance of all the values from a specific field in the result set. (vi) stdev - This returns the standard deviation of all the values from a specific field in the result set.

An example of a query would be as follows :

http://localhost:3000/api/properties?District=Ticky%20Ticky&aggregate_function=avg&field=Property_Size_Ha - This query would give us the average property size in hectares of farms in Ticky Ticky district.

We specify the aggregate function we would like to carry out using the "aggregate_function" parameter and we specify the field we would like to target using the "field" parameter. All other supported query parameters can be used in conjunction with these to enhance specificity.

nickwleo commented 7 years ago

@matjames007 Please review when you can.

@thinkking Please update the documentation accordingly.

matjames007 commented 7 years ago

@nick22891 there are several functions that are shared across all the /api resources. If you can combine the features and have the end result be one function that constructs the parameters object based on the limit and offset values as well as the date ranges supplied and any aggregation information. This will make for better reuse and a centralized area to maintain all the changes.

nickwleo commented 7 years ago

I've moved all the duplicate code from each of the /api endpoints to common-util.js and implemented the resulting function in each of the resource endpoints.

@matjames007 Please review again when you can.

nickwleo commented 7 years ago

Oh you just did... Lol.