<commit_before>"use strict";

const EventEmitter = require('events');
const moment = require('moment');

const mongodb = require('../util/mongodb');

const emitter = new EventEmitter();

function findStops(journey, from, to) {
    const fromStops = journey.stops[from];
    const toStops = journey.stops[to];

    if (fromStops.length === 1 && toStops.length === 1) {
        if (fromStops[0].idx < toStops[0].idx) {
            return {
                from: fromStops[0],
                to: toStops[0]
            };
        }
        return null;
    }

    const closest = fromStops.reduce((closest, fromStop) => {
        return toStops.reduce((closest, toStop) => {
            const distance = toStop.idx - fromStop.idx;
            if (distance > 0 && (!closest.distance || distance < closest.distance)) {
                closest.from = fromStop;
                closest.to = toStop;
                closest.distance = distance;
            }
            return closest;
        }, closest);
    }, {
        from: null,
        to: null,
        distance: 0
    });

    return closest.distance ? closest : null;
}

function stripJourney(journey, from, to) {
    const stops = findStops(journey, from, to);
    if (stops) {
        return {
            uid: journey.uid,
            trainID: journey.trainID,
            ssd: journey.ssd,
            from: stops.from,
            to: stops.to
        };
    }
    
    return null;
}

function constructUserJourneys(results, from, to) {
    return results.reduce((journeys, journey) => {
        const userJourney = stripJourney(journey, from, to);
        if (userJourney) {
            journeys.push(userJourney);
        }
        
        return journeys;
    }, []);
}

function queryForJourneys(from, to, around, db) {
    const query = {},
        projection = {},
        sort = {};

    const mmt = moment(around);

    const lowerBound = moment(mmt).subtract(15, 'minutes');
    const upperBound = moment(mmt).add(45, 'minutes');

    query[`stops.${from}`] = {
        $elemMatch: {
            predictedTime: {
                $gte: lowerBound.toDate(),
                $lte: upperBound.toDate()
            }
        }
    };
    query[`stops.${to}`] = {
        $exists: true
    };

    projection.uid = 1;
    projection.trainID = 1;
    projection.ssd = 1;
    projection[`stops.${from}`] = 1;
    projection[`stops.${to}`] = 1;

    return db.collection('journeys')
        .find(query, projection)
        .sort(sort)
        .toArray()
        .then(results => constructUserJourneys(results, from, to));
}

function find(from, to, around) {
    return mongodb.connect()
        .then(queryForJourneys.bind(undefined, from, to, around));
}
<commit_msg>Wrong way round guys<commit_after>"use strict";

const EventEmitter = require('events');
const moment = require('moment');

const mongodb = require('../util/mongodb');

const emitter = new EventEmitter();

function findStops(journey, from, to) {
    const fromStops = journey.stops[from];
    const toStops = journey.stops[to];

    if (fromStops.length === 1 && toStops.length === 1) {
        if (fromStops[0].idx < toStops[0].idx) {
            return {
                from: fromStops[0],
                to: toStops[0]
            };
        }
        return null;
    }

    const closest = fromStops.reduce((closest, fromStop) => {
        return toStops.reduce((closest, toStop) => {
            const distance = toStop.idx - fromStop.idx;
            if (distance > 0 && (!closest.distance || distance < closest.distance)) {
                closest.from = fromStop;
                closest.to = toStop;
                closest.distance = distance;
            }
            return closest;
        }, closest);
    }, {
        from: null,
        to: null,
        distance: 0
    });

    return closest.distance ? closest : null;
}

function stripJourney(journey, from, to) {
    const stops = findStops(journey, from, to);
    if (stops) {
        return {
            uid: journey.uid,
            trainID: journey.trainID,
            ssd: journey.ssd,
            from: stops.from,
            to: stops.to
        };
    }
    
    return null;
}

function constructUserJourneys(results, from, to) {
    return results.reduce((journeys, journey) => {
        const userJourney = stripJourney(journey, from, to);
        if (userJourney) {
            journeys.push(userJourney);
        }
        
        return journeys;
    }, []);
}

function queryForJourneys(from, to, around, db) {
    const query = {},
        projection = {},
        sort = {};

    const mmt = moment(around);

    const lowerBound = moment(mmt).subtract(45, 'minutes');
    const upperBound = moment(mmt).add(15, 'minutes');

    query[`stops.${from}`] = {
        $elemMatch: {
            predictedTime: {
                $gte: lowerBound.toDate(),
                $lte: upperBound.toDate()
            }
        }
    };
    query[`stops.${to}`] = {
        $exists: true
    };

    projection.uid = 1;
    projection.trainID = 1;
    projection.ssd = 1;
    projection[`stops.${from}`] = 1;
    projection[`stops.${to}`] = 1;

    return db.collection('journeys')
        .find(query, projection)
        .sort(sort)
        .toArray()
        .then(results => constructUserJourneys(results, from, to));
}

function find(from, to, around) {
    return mongodb.connect()
        .then(queryForJourneys.bind(undefined, from, to, around));
}

{'fraction_non_alphanumeric': 0.13854871138731492, 'fraction_numerical': 0.006214585998903308, 'mean_word_length': 1.945102260495156, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7105624', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1698, 'n_words': 357}