Skip to content

Instantly share code, notes, and snippets.

@cphoover
Created February 23, 2018 20:03
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/56624e00f12c0343f160bd7676558497 to your computer and use it in GitHub Desktop.
Save cphoover/56624e00f12c0343f160bd7676558497 to your computer and use it in GitHub Desktop.
const haversine = require("haversine")
const moment = require('moment');
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,
deptTime: 1457200800000
},
{
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,
deptTime: 1456596000000
},
{
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,
deptTime: 1455991200000
},
{
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,
deptTime: 1452967200000
}
];
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,
deptTime: 1457805600000
};
// randomized value : 5%;
const attachDepartureDeltas = (pastTrips, currentTrip) => {
return pastTrips.map((trip, i) => {
return Object.assign(trip, {
daysTillNext : moment((pastTrips[i - 1] ? pastTrips[i - 1] : currentTrip).deptTime).diff(moment(trip.deptTime), 'days')
});
});
};
const attachHaversineDelta = (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 attachDayOfWeekDelta = (pastTrip, currentTrip) => {
pastTrip.departureDayDelta = Math.abs(currentTrip.departureDay - pastTrip.departureDay)
return pastTrip;
};
const calculateDeltas = (pastTrips, currentTrip) => pastTrips.map(trip => {
return attachHaversineDelta(attachDayOfWeekDelta(trip, currentTrip), currentTrip);
})
const withDepartureDeltas = attachDepartureDeltas(twoMadridTripsWithinDayRange, currentTrip);
const pastTripsWithDeltas = calculateDeltas(withDepartureDeltas, 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
// randomize by 5%
const thresholds = {
origAir: 50,
destAir: 50,
hotel: 50,
deptDay: 2,
repetition: 2,
cadence: 2, // the cadence between the filteredTrips
// must not differ outside of a threshold of X days
};
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);
const filteredWithCadence = filteredTrips
.filter((trip, i) => {
return filteredTrips[i - 1] ? Math.abs(trip.daysTillNext - filteredTrips[i - 1].daysTillNext) <= thresholds.cadence : true;
});
console.log('unfilteredTrips', pastTripsWithDeltas);
console.log('filteredTrips', filteredTrips);
console.log('filteredWithCadence', filteredWithCadence);
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