Skip to content

Instantly share code, notes, and snippets.

@cphoover
Created February 23, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cphoover/bf7cf4bc577547e1e203295a860f2d68 to your computer and use it in GitHub Desktop.
Save cphoover/bf7cf4bc577547e1e203295a860f2d68 to your computer and use it in GitHub Desktop.
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);
// 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
const thresholds = {
origAir: 50,
destAir : 50,
hotel: 50,
deptDay : 2,
repetition: 2
};
const filteredTrips = pastTripsWithDeltas
.filter(trip => trip.departureDayDelta <= thresholds.deptDay)
.filter(trip => trip.origAirDistance <= thresholds.origAir)
.filter(trip => trip.destAirDistance <= thresholds.destAir)
.filter(trip => trip.hotelDistance <= thresholds.hotel);
console.log('filteredTrips', filteredTrips);
console.log('is repeated trip', filteredTrips.length >= thresholds.repetition);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment