Browse files

Implement sort order for forms and responses

Sort GET results for forms and responses by creation date, newest first.
Make sure we index the relevant fields for those queries.
  • Loading branch information...
1 parent 7a7ccf5 commit 4e355151ccc25b79ccb559a8d08bd4bd3ee2ac51 @prashtx prashtx committed Aug 18, 2012
Showing with 45 additions and 8 deletions.
  1. +8 −2 forms.js
  2. +12 −3 responses.js
  3. +25 −3 web.js
View
10 forms.js
@@ -85,11 +85,14 @@ app.get('/api/surveys/:surveyid/forms/:formid', function(req, response) {
});
// Get all the forms for a survey.
+// Sort by creation date, newest first.
// GET http://localhost:3000/api/surveys/{SURVEY ID}/forms
app.get('/api/surveys/:sid/forms', function(req, response) {
console.log('Returning all forms for survey ' + req.params.sid);
db.collection(FORMS, function(err, collection) {
- collection.find({'survey': req.params.sid}, function(err, cursor) {
+ collection.find({'survey': req.params.sid},
+ {'sort': [['created', 'desc']]},
+ function(err, cursor) {
if (err) {
console.log('Error finding forms for survey ' + req.params.sid + ': ' + err);
response.send();
@@ -206,6 +209,7 @@ app.del('/api/surveys/:sid/forms/:id', function(req, response) {
});
// Get all forms that reference the specified parcel ID
+// Sort by creation date, newest first.
// GET http://localhost:3000/api/surveys/{SURVEY ID}/parcels/{PARCEL ID}/forms
app.get('/api/surveys/:sid/parcels/:pid/forms', function(req, response) {
var handleError = util.makeErrorHandler(response);
@@ -214,7 +218,9 @@ app.get('/api/surveys/:sid/parcels/:pid/forms', function(req, response) {
console.log('Getting forms for survey ' + sid + ' that reference parcel ' + pid);
getCollection(function(err, collection) {
if (handleError(err)) { return; }
- collection.find({survey: sid, 'parcels.parcel_id': pid}, function(err, cursor) {
+ collection.find({survey: sid, 'parcels.parcel_id': pid},
+ {'sort': [['created', 'desc']]},
+ function(err, cursor) {
if (handleError(err)) { return; }
cursor.toArray(function(err, items) {
if (handleError(err)) { return; }
View
15 responses.js
@@ -250,12 +250,15 @@ function setup(app, db, idgen, collectionName) {
}
// Get all responses for a survey.
+ // Sort by creation date, newest first.
// GET http://localhost:3000/api/surveys/{SURVEY ID}/responses
// GET http://localhost:3000/api/surveys/1/responses
app.get('/api/surveys/:sid/responses', function(req, response) {
var surveyid = req.params.sid;
getCollection(function(err, collection) {
- collection.find({'survey': surveyid}, function(err, cursor) {
+ collection.find({'survey': surveyid},
+ {'sort': [['created', 'desc']]},
+ function(err, cursor) {
if (err != null) {
console.log('Error retrieving responses for survey ' + surveyid + ': ' + err.message);
response.send();
@@ -269,14 +272,17 @@ function setup(app, db, idgen, collectionName) {
});
// Get all responses for a specific parcel.
+ // Sort by creation date, newest first.
// TODO: At some point, parcel should become a generic geographic object ID.
// GET http://localhost:3000/api/surveys/{SURVEY ID}/parcels/{PARCEL ID}/responses
// GET http://localhost:3000/api/surveys/1/parcels/3728048/responses
app.get('/api/surveys/:sid/parcels/:parcel_id/responses', function(req, response) {
var surveyid = req.params.sid;
var parcel_id = req.params.parcel_id;
getCollection(function(err, collection) {
- collection.find({'survey': surveyid, 'parcel_id': parcel_id}, function(err, cursor) {
+ collection.find({'survey': surveyid, 'parcel_id': parcel_id},
+ {'sort': [['created', 'desc']]},
+ function(err, cursor) {
if (err != null) {
console.log('Error retrieving responses for survey ' + surveyid + ': ' + err.message);
response.send();
@@ -406,6 +412,7 @@ function setup(app, db, idgen, collectionName) {
// Get all responses in a bounding box
+ // Sort by creation date, newest first.
// GET http://localhost:3000/api/surveys/{SURVEY ID}/reponses/in/lower-left lat,lower-left lng, upper-right lat, upper-right lng
// GET http://localhost:3000/api/surveys/{SURVEY ID}/reponses/in/1,2,3,4
app.get('/api/surveys/:sid/responses/in/:bounds', function(req, response) {
@@ -427,7 +434,9 @@ function setup(app, db, idgen, collectionName) {
console.log(query['geo_info.centroid']['$within']["$box"]);
getCollection(function(err, collection) {
- collection.find(query, function(err, cursor) {
+ collection.find(query,
+ {'sort': [['created', 'desc']]},
+ function(err, cursor) {
if (handleError(err, response)) return;
cursor.toArray(function(err, items) {
View
28 web.js
@@ -205,10 +205,32 @@ function ensureStructure(db, callback) {
function ensureResponses(done) {
db.collection(RESPONSES, function (error, collection) {
- if (error) { throw error; }
+ if (error) { return done(error); }
// Ensure we have a geo index on the centroid field.
collection.ensureIndex({'geo_info.centroid': '2d'}, function (error) {
- done(error);
+ if (error) { return done(error); }
+ // Index the creation date, which we use to sort
+ collection.ensureIndex('created', function (error, index) {
+ if (error) { return done(error); }
+ // Index the parcel ID
+ collection.ensureIndex('parcel_id', function (error, index) {
+ done(error);
+ });
+ });
+ });
+ });
+ }
+
+ function ensureForms(done) {
+ db.collection(FORMS, function (error, collection) {
+ if (error) { return done(error); }
+ // Index the creation date, which we use to sort
+ collection.ensureIndex('created', function (error, index) {
+ if (error) { return done(error); }
+ // Index the parcel IDs, used by paper forms
+ collection.ensureIndex('parcels.parcel_id', function (error, index) {
+ done(error);
+ });
});
});
}
@@ -260,7 +282,7 @@ function ensureStructure(db, callback) {
});
}
- chain([ensureSlugs, ensureSurveys, ensureResponses])();
+ chain([ensureSlugs, ensureSurveys, ensureForms, ensureResponses])();
}
function startServer(port, cb) {

0 comments on commit 4e35515

Please sign in to comment.