1+ var parser = require ( 'mongodb-query-parser' ) ;
2+ var escapeStringRegexp = require ( 'escape-string-regexp' ) ;
13var express = require ( 'express' ) ;
24var bodyParser = require ( 'body-parser' ) ;
35var _ = require ( 'lodash' ) ;
@@ -128,8 +130,8 @@ app.all('/query', function(req, res, next)
128130 setCORSHeaders ( res ) ;
129131
130132 // Parse query string in target
131- substitutions = { "$from" : new Date ( req . body . range . from ) ,
132- "$to" : new Date ( req . body . range . to ) ,
133+ substitutions = { "$from" : req . body . range . from ,
134+ "$to" : req . body . range . to ,
133135 "$dateBucketCount" : getBucketCount ( req . body . range . from , req . body . range . to , req . body . intervalMs )
134136 }
135137
@@ -172,7 +174,7 @@ app.use(function(error, req, res, next)
172174// Get config from server/default.json
173175var serverConfig = config . get ( 'server' ) ;
174176
175- app . listen ( serverConfig . port ) ;
177+ app . listen ( serverConfig . port , serverConfig . host ) ;
176178
177179console . log ( "Server is listening on port " + serverConfig . port ) ;
178180
@@ -232,7 +234,7 @@ function parseQuery(query, substitutions)
232234 }
233235
234236 // Args is the rest up to the last bracket
235- var closeBracketIndex = query . indexOf ( ')' , openBracketIndex )
237+ var closeBracketIndex = query . lastIndexOf ( ')' )
236238 if ( closeBracketIndex == - 1 )
237239 {
238240 queryErrors . push ( "Can't find last bracket" )
@@ -244,7 +246,13 @@ function parseQuery(query, substitutions)
244246 {
245247 // Wrap args in array syntax so we can check for optional options arg
246248 args = '[' + args + ']'
247- docs = JSON . parse ( args )
249+ //docs = JSON.parse(args)
250+ // Replace with substitutions
251+ for ( var key in substitutions ) {
252+ var regex = new RegExp ( escapeStringRegexp ( key ) , 'g' )
253+ args = args . replace ( regex , substitutions [ key ] )
254+ }
255+ docs = parser ( args )
248256 // First Arg is pipeline
249257 doc . pipeline = docs [ 0 ]
250258 // If we have 2 top level args, second is agg options
@@ -253,7 +261,7 @@ function parseQuery(query, substitutions)
253261 doc . agg_options = docs [ 1 ]
254262 }
255263 // Replace with substitutions
256- for ( var i = 0 ; i < doc . pipeline . length ; i ++ )
264+ /* for ( var i = 0; i < doc.pipeline.length; i++)
257265 {
258266 var stage = doc.pipeline[i]
259267 forIn(stage, function (obj, key, value)
@@ -266,7 +274,7 @@ function parseQuery(query, substitutions)
266274 }
267275 }
268276 })
269- }
277+ }*/
270278 }
271279 else
272280 {
@@ -525,4 +533,4 @@ function getBucketCount(from, to, intervalMs)
525533 }
526534
527535 return count
528- }
536+ }
0 commit comments