<commit_before>var MongoClient = require('mongodb').MongoClient;
var mongoUtils = require('../../utils/mongoUtils');
var arrayUtils = require('../../utils/arrayUtils');

/**
 * Query atbats for a batter or a pitcher. If there isn't a "batter" or
 * "pitcher" query parameter than this method will throw a 500.
 * 
 * @param req -
 *            the express request
 * @param res -
 *            the express response
 */
exports.query = function(req, res) {
    var playerId = parseInt(req.params.playerId, 10);
    var query = {
        '$and' : [],
    };
    if (req.params.type === 'batter') {
        query['atbat.batter'] = playerId;
    } else {
        query['atbat.pitcher'] = playerId;
    }

    var atbatFilter = JSON.parse(req.query.atbatFilter);
    adjustQueryByAtBatFilter(query, atbatFilter);

    console.log(JSON.stringify(query, null, 4));

    MongoClient.connect("mongodb://localhost:27017/mlbatbat", function(err, db) {
        db.collection('pitches').find(query).toArray(function(err, docs) {
            res.json(docs);
            db.close();
        });
    });
};

/**
 * Adjusts the mongoDB query using the filter provided from the API call
 * 
 * @param query
 * @param filter
 */
function adjustQueryByAtBatFilter(query, filter) {

    var topLevelFilters = [];
    if (filter.pitcherHand) {
        topLevelFilters.push({
            'atbat.p_throws' : filter.pitcherHand
        });
    }
    if (filter.batterHand) {
        topLevelFilters.push({
            'atbat.stand' : filter.batterHand
        });
    }

    arrayUtils.pushIfExists(topLevelFilters, mongoUtils.buildInFilter(filter.outs, 'atbat.o_start'));
    arrayUtils.pushIfExists(topLevelFilters, mongoUtils.buildInFilter(filter.balls, 'atbat.b'));<commit_msg>limit the pitch document so the payload isn't so large<commit_after>var MongoClient = require('mongodb').MongoClient;
var mongoUtils = require('../../utils/mongoUtils');
var arrayUtils = require('../../utils/arrayUtils');

/**
 * Explains the document of properties that we're interested in. This will make
 * the wire object much lighter than returning everything from the Pitch collection.
 */
var pitchIncludes = {
    'atbat.des' : 1,
    'hip.des' : 1,
    'pitch_type' : 1,
    'type' : 1,
    'des' : 1,
    'px' : 1,
    'pz' : 1,
    'start_speed' : 1,
    '_id' : 0
};

/**
 * Query atbats for a batter or a pitcher. If there isn't a "batter" or
 * "pitcher" query parameter than this method will throw a 500.
 * 
 * @param req -
 *            the express request
 * @param res -
 *            the express response
 */
exports.query = function(req, res) {
    var playerId = parseInt(req.params.playerId, 10);
    var query = {
        '$and' : [],
    };
    if (req.params.type === 'batter') {
        query['atbat.batter'] = playerId;
    } else {
        query['atbat.pitcher'] = playerId;
    }

    var atbatFilter = JSON.parse(req.query.atbatFilter);
    adjustQueryByAtBatFilter(query, atbatFilter);

    console.log(JSON.stringify(query, null, 4));

    MongoClient.connect("mongodb://localhost:27017/mlbatbat", function(err, db) {
        db.collection('pitches').find(query, pitchIncludes).toArray(function(err, docs) {
            res.json(docs);
            db.close();
        });
    });
};

/**
 * Adjusts the mongoDB query using the filter provided from the API call
 * 
 * @param query
 * @param filter
 */
function adjustQueryByAtBatFilter(query, filter) {

    var topLevelFilters = [];
    if (filter.pitcherHand) {
        topLevelFilters.push({
            'atbat.p_throws' : filter.pitcherHand
        });
    }
    if (filter.batterHand) {
        topLevelFilters.push({
            'atbat.stand' : filter.batterHand
        });
    }

    arrayUtils.pushIfExists(topLevelFilters, mongoUtils.buildInFilter(filter.outs, 'atbat.o_start'));
    arrayUtils.pushIfExists(topLevelFilters, mongoUtils.buildInFilter(filter.balls, 'atbat.b'));
{'fraction_non_alphanumeric': 0.14140127388535031, 'fraction_numerical': 0.007898089171974522, 'mean_word_length': 2.785920925747348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2745513', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1229, 'n_words': 279}