From 5c8f39344b70118df71b55f04f4bf232ab7e3f49 Mon Sep 17 00:00:00 2001 From: Gilles De Mey Date: Sun, 21 Dec 2014 20:58:11 +0100 Subject: [PATCH] Levenshtein distance lookup for routes --- routes/routes.js | 73 ++++++++++++++++++++++++++++------- routes/stations.js | 94 +++++++++++++++++++++++----------------------- 2 files changed, 106 insertions(+), 61 deletions(-) diff --git a/routes/routes.js b/routes/routes.js index b99c8bf..db15961 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -1,6 +1,8 @@ 'use strict'; var express = require('express'), + async = require('async'), + db = require('../db/stations'), request = require('request'), router = express.Router(), datetime = require('../utils/datetime'), @@ -11,9 +13,6 @@ var express = require('express'), /* GET status */ router.get('/', function(req, res, next) { - var from = parseInt(req.query.from) || 0; - var to = parseInt(req.query.to) || 0; - var searchType = 1; var dateTime = datetime.toCET().format('YYYY-MM-DD HH:mm:ss'); @@ -27,30 +26,76 @@ router.get('/', function(req, res, next) { var params = { resultCountBefore : 0, - departureStationId : from, dateTime : dateTime, - arrivalStationId : to, searchType : 1, minTransferTime : 0, resultCountAfter : 3 }; - request.get(new OAuth('RetrieveRoutes', params), function(err, resp, body) { + async.parallel({ + + from: function(callback) { + + if (isFinite(req.query.from)) + return callback(null, parseInt(req.query.from)); + + db.levenLookup(req.query.from, function(err, result) { + params.stationID = result.id; + + if (isFinite(result.score)) + callback(null, result.id); + else + return res.json(404, new ex.StationNotFoundException()); + }); + + }, + + to: function(callback) { + + if (isFinite(req.query.to)) + return callback(null, parseInt(req.query.to)); + + db.levenLookup(req.query.to, function(err, result) { + params.stationID = result.id; + + if (isFinite(result.score)) + callback(null, result.id); + else + return res.json(404, new ex.StationNotFoundException()); + }); - if (err || resp.statusCode !== 200) { - return res.json(500, new ex.RailtimeException('Could not resolve route')); } - maps.routes.route(body, req.locale, function(err, directions) { - if (err) - res.send(resp.statusCode || 500, { error: err }); - else - res.json(directions); - }); + }, function(err, results) { + + if (err) + return res.json(500, err); + params.departureStationId = results.from; + params.arrivalStationId = results.to; + makeRequest(params); }); + function makeRequest(params) { + + request.get(new OAuth('RetrieveRoutes', params), function(err, resp, body) { + + if (err || resp.statusCode !== 200) { + return res.json(500, new ex.RailtimeException('Could not resolve route')); + } + + maps.routes.route(body, req.locale, function(err, directions) { + if (err) + res.send(resp.statusCode || 500, { error: err }); + else + res.json(directions); + }); + + }); + + } + }); module.exports = router; diff --git a/routes/stations.js b/routes/stations.js index fd525c8..daf4b0b 100644 --- a/routes/stations.js +++ b/routes/stations.js @@ -11,50 +11,6 @@ var express = require('express'), db = require('../db/stations'), OAuth = require('../utils/oauth'); -function makeRequest(req, res, next, params) { - - request.get(new OAuth('RetrieveStationSchedule', params), function(err, resp, body) { - - if (err || resp.statusCode !== 200) - return res.send(resp.statusCode || 500, { error: '¯\\_(ツ)_/¯' }); - - maps.stations.station(body, req.locale, function(err, body) { - - if (err) - return res.send(resp.statusCode || 500, { error: err }); - - var sort, reject; - - if (req.params.subset === 'arrivals') { - - sort = 'arrival'; - reject = function(train) { - return train.arrival === null; - }; - - } else if (req.params.subset === 'departures') { - - sort = 'departure'; - reject = function(train) { - return train.departure === null; - }; - - } else { - sort = 'departure'; - reject = utils.noop; - } - - body.trains = _.chain(body.trains) - .reject(reject) - .sortBy(sort) - .value(); - - res.json(body); - }); - }); - -} - /* GET status */ router.get('/:station/:subset(arrivals|departures)?', function(req, res, next) { @@ -72,21 +28,65 @@ router.get('/:station/:subset(arrivals|departures)?', function(req, res, next) { if (isFinite(req.params.station)) { params.stationID = req.params.station; - makeRequest(req, res, next, params); + makeRequest(params); } - else if (isNaN(req.params.station)) + else db.levenLookup(req.params.station, function(err, result) { params.stationID = result.id; // no results from lookup returns Infinity if (isFinite(result.score)) - makeRequest(req, res, next, params); + makeRequest(params); else return res.json(404, new ex.StationNotFoundException()); }); + function makeRequest(params) { + + request.get(new OAuth('RetrieveStationSchedule', params), function(err, resp, body) { + + if (err || resp.statusCode !== 200) + return res.send(resp.statusCode || 500, { error: '¯\\_(ツ)_/¯' }); + + maps.stations.station(body, req.locale, function(err, body) { + + if (err) + return res.send(resp.statusCode || 500, { error: err }); + + var sort, reject; + + if (req.params.subset === 'arrivals') { + + sort = 'arrival'; + reject = function(train) { + return train.arrival === null; + }; + + } else if (req.params.subset === 'departures') { + + sort = 'departure'; + reject = function(train) { + return train.departure === null; + }; + + } else { + sort = 'departure'; + reject = utils.noop; + } + + body.trains = _.chain(body.trains) + .reject(reject) + .sortBy(sort) + .value(); + + res.json(body); + }); + }); + + } + }); /* GET index */