<commit_before>var util = require('util'),
	async = require('async');

module.exports = function (model) {

	return function (req, res, next) {

        var records = [],
            filters = {},
            totalRecords = 0,
            pageSize = req.body.pageSize || 20,
            pageIndex = req.body.page || 1;

        // set the model on linz
		req.linz.model = req.linz.get('models')[req.params.model];

		async.series([

			// grab the grid object and append it to the model, i.e. req.linz.model.grid.columns
			function (cb) {

				req.linz.model.getGrid(function (err, grid) {

                    if (!err) {
                        req.linz.model.grid = grid;
                    }

					cb(err);

				});

			},

            // render the filters
            function (cb) {

                async.each(Object.keys(req.linz.model.grid.filters), function (fieldName, filtersDone) {

                    if (req.linz.model.grid.filters[fieldName].formControls) {
                        return filtersDone(null);
                    }

                    // call the filter renderer and update the content with the result
                    req.linz.model.grid.filters[fieldName].filter.renderer(fieldName, function (err, result) {

                        if (!err) {
                            req.linz.model.grid.filters[fieldName].formControls = result;
                        }

                        return filtersDone(err);

                    });

                }, function (err) {

                    return cb(err);

                });

            },

            // render the active filters
            function (cb) {

                req.linz.model.grid.activeFilters = {};

                // check if there are any filters in the form post
                if (!req.body.selectedFilters) {
                    return cb(null);
                }

                async.each(req.body.selectedFilters.split(','), function (fieldName, filtersDone) {

                    // call the filter binder to render active filter form controls with form value added
                    req.linz.model.grid.filters[fieldName].filter.bind(fieldName, req.body, function (err, result) {

                        if (!err) {
                            req.linz.model.grid.activeFilters[fieldName] = {
                                label: req.linz.model.grid.filters[fieldName].label,
                                controls: result
                            }
                        }

                        return filtersDone(err);

                    });

                }, function (err) {

                    return cb(err);

                });

            },

            // get the filters
            function (cb) {

                // check if there are any filters in the form post
                if (!req.body.selectedFilters) {
                    return cb(null);
                }

                async.each(req.body.selectedFilters.split(','), function (fieldName, filtersDone) {

                    // call the filter renderer and update the content with the result
                    req.linz.model.grid.filters[fieldName].filter.filter(fieldName, req.body, function (err, result) {

                        if (!err) {

                            filters = req.linz.model.addSearchFilter(filters, result);

                        }

                        return filtersDone(err);

                    });

                }, function (err) {

                    return cb(err);

                });

            },

            // count the docs
            function (cb) {

                req.linz.model.count(filters).exec(function (err, docs) {

                    if (!err) {
                        totalRecords = docs;
                    }

                    return cb(null);

                });

            },

			// find the docs
			function (cb) {

                // consolidate filters into query
                if (Object.keys(filters).length) {
                    filters = req.linz.model.setFiltersAsQuery(filters);
                }

				var query = req.linz.model.find(filters);

				// sort by the chosen sort field, or use the first sortBy option as the default
				if (!req.body.sort && req.linz.model.grid.sortBy.length) {

					query.sort(req.linz.model.grid.sortBy[0].field);

				} else if (req.body.sort) {

					query.sort(req.body.sort);

				}

                // add in paging skip and limit
                query.skip(pageIndex*pageSize-pageSize).limit(pageSize);

				query.exec(function (err, docs) {

					if (!err) {

                        records = docs;
                    }

					cb(err);

				});

			},

            // create the values for the datagrids for each doc<commit_msg>Fixed an issue with the count functionality.<commit_after>var util = require('util'),
	async = require('async');

module.exports = function (model) {

	return function (req, res, next) {

        var records = [],
            filters = {},
            totalRecords = 0,
            pageSize = req.body.pageSize || 20,
            pageIndex = req.body.page || 1;

        // set the model on linz
		req.linz.model = req.linz.get('models')[req.params.model];

		async.series([

			// grab the grid object and append it to the model, i.e. req.linz.model.grid.columns
			function (cb) {

				req.linz.model.getGrid(function (err, grid) {

                    if (!err) {
                        req.linz.model.grid = grid;
                    }

					cb(err);

				});

			},

            // render the filters
            function (cb) {

                async.each(Object.keys(req.linz.model.grid.filters), function (fieldName, filtersDone) {

                    if (req.linz.model.grid.filters[fieldName].formControls) {
                        return filtersDone(null);
                    }

                    // call the filter renderer and update the content with the result
                    req.linz.model.grid.filters[fieldName].filter.renderer(fieldName, function (err, result) {

                        if (!err) {
                            req.linz.model.grid.filters[fieldName].formControls = result;
                        }

                        return filtersDone(err);

                    });

                }, function (err) {

                    return cb(err);

                });

            },

            // render the active filters
            function (cb) {

                req.linz.model.grid.activeFilters = {};

                // check if there are any filters in the form post
                if (!req.body.selectedFilters) {
                    return cb(null);
                }

                async.each(req.body.selectedFilters.split(','), function (fieldName, filtersDone) {

                    // call the filter binder to render active filter form controls with form value added
                    req.linz.model.grid.filters[fieldName].filter.bind(fieldName, req.body, function (err, result) {

                        if (!err) {
                            req.linz.model.grid.activeFilters[fieldName] = {
                                label: req.linz.model.grid.filters[fieldName].label,
                                controls: result
                            }
                        }

                        return filtersDone(err);

                    });

                }, function (err) {

                    return cb(err);

                });

            },

            // get the filters
            function (cb) {

                // check if there are any filters in the form post
                if (!req.body.selectedFilters) {
                    return cb(null);
                }

                async.each(req.body.selectedFilters.split(','), function (fieldName, filtersDone) {

                    // call the filter renderer and update the content with the result
                    req.linz.model.grid.filters[fieldName].filter.filter(fieldName, req.body, function (err, result) {

                        if (!err) {

                            filters = req.linz.model.addSearchFilter(filters, result);

                        }

                        return filtersDone(err);

                    });

                }, function (err) {

                    return cb(err);

                });

            },

            // count the docs
            function (cb) {

                // consolidate filters into query
                if (Object.keys(filters).length) {
                    filters = req.linz.model.setFiltersAsQuery(filters);
                }

                req.linz.model.count(filters, function (err, docs) {

                    if (!err) {
                        totalRecords = docs;
                    }

                    return cb(null);

                });

            },

			// find the docs
			function (cb) {

				var query = req.linz.model.find(filters);

				// sort by the chosen sort field, or use the first sortBy option as the default
				if (!req.body.sort && req.linz.model.grid.sortBy.length) {

					query.sort(req.linz.model.grid.sortBy[0].field);

				} else if (req.body.sort) {

					query.sort(req.body.sort);

				}

                // add in paging skip and limit
                query.skip(pageIndex*pageSize-pageSize).limit(pageSize);

				query.exec(function (err, docs) {

					if (!err) {

                        records = docs;
                    }

					cb(err);

				});

			},

            // create the values for the datagrids for each doc
{'fraction_non_alphanumeric': 0.10453505304763885, 'fraction_numerical': 0.001040149781568546, 'mean_word_length': 1.1587337224966323, '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': '628606', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2302, 'n_words': 558}