Created
February 23, 2018 17:02
-
-
Save cphoover/8fce375a71ed8320624f019e498dc64e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var haversine = require("haversine") | |
const twoMadridTripsWithinDayRange = [{ | |
id: 'TEST-1234', | |
origAir: { // chicago | |
lat: 41.878114, | |
lon: -87.629798 | |
}, | |
destAir: { // madrid | |
lat: 40.416775, | |
lon: -3.703790 | |
}, | |
hotel: { //somewhere in madrid | |
lat: 40.413861, | |
lon: -3.704992 | |
}, | |
departureDay: 3 | |
}, | |
{ | |
id: 'TEST-2312', | |
origAir: { // chicago | |
lat: 41.803635, | |
lon: -87.798026 | |
}, | |
destAir: { // madrid | |
lat: 40.434675, | |
lon: -3.717351 | |
}, | |
hotel: { //somewhere in madrid | |
lat: 40.421108, | |
lon: -3.704420 | |
}, | |
departureDay: 4 | |
}, | |
{ | |
id: 'TEST-333', | |
origAir: { // baltimore | |
lat: 39.290385, | |
lon: -76.612189 | |
}, | |
destAir: { // los angeles | |
lat: 34.052234, | |
lon: -118.243685 | |
}, | |
hotel: { //long beach | |
lat: 33.770050, | |
lon: -118.193739 | |
}, | |
departureDay: 5 | |
} | |
]; | |
var currentTrip = { | |
origAir: { // somewhere in chicago | |
lat: 41.927186, | |
lon: -87.740917 | |
}, | |
destAir: { | |
lat: 40.412712, | |
lon: -3.707027 | |
}, | |
hotel: { | |
lat: 40.412712, | |
lon: -3.707027 | |
}, | |
departureDay: 3 | |
}; | |
// randomized value : 5%; | |
const attachHaversineDeltas = (pastTrip, currentTrip) => { | |
pastTrip.origAirDistance = haversine(pastTrip.origAir, currentTrip.origAir, { | |
format: '{lon,lat}' | |
}); | |
pastTrip.destAirDistance = haversine(pastTrip.destAir, currentTrip.destAir, { | |
format: '{lon,lat}' | |
}); | |
pastTrip.hotelDistance = haversine(pastTrip.hotel, currentTrip.hotel, { | |
format: '{lon,lat}' | |
}); | |
return pastTrip; | |
}; | |
const attachDayOfWeekDeltas = (pastTrip, currentTrip) => { | |
pastTrip.departureDayDelta = Math.abs(currentTrip.departureDay - pastTrip.departureDay) | |
return pastTrip; | |
}; | |
const calculateDeltas = (pastTrips, currentTrip) => pastTrips.map(trip => { | |
return attachHaversineDeltas(attachDayOfWeekDeltas(trip, currentTrip), currentTrip);; | |
}) | |
const pastTripsWithDeltas = calculateDeltas(twoMadridTripsWithinDayRange, currentTrip); | |
const thresholds = { | |
origAir: 50, | |
destAir : 50, | |
hotel: 50, | |
deptDay : 2, | |
repetition: 2 | |
}; | |
const filteredTrips = pastTripsWithDeltas | |
.filter(trip => trip.departureDayDelta < thresholds.deptDay); | |
console.log('is repeated trip', filteredTrips.length >= thresholds.repetition); | |
// if they hit rebook | |
// we get the distrance deltas and avg them into the thresholds | |
// then with new thresholds we get number of repetitions and avg them | |
// into the repetition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment