<commit_before>define(['angular', 'lodash'], function(angular, _) {
    'use strict';

    angular.module('superdesk.services')
    .factory('DataAdapter', ['$rootScope', '$location', 'em', function($rootScope, $location, em) {

        /**
         * $location state adapter
         */
        function LocationState(loc) {

            this.get = function(key) {
                return arguments.length ? loc.search()[key] : loc.search();
            }

            this.set = function(key, val) {
                return loc.search(key, val);
            }
        }

        // @todo implement storage state provider
        var stateProviders = {
            location: new LocationState($location)
        };

        /**
         * Data Provider for given resource
         */
        return function DataAdapter(resource, params) {
            var _this = this;
            var state = stateProviders.location;
            var defaultParams = angular.extend({max_results: 25, page: 1, where: {}, sort: [], filters: []}, params);

            /**
             * Get query criteria - extend default params with current search
             */
            function getQueryCriteria() {
                var criteria = angular.extend({}, defaultParams, state.get());
                angular.extend(criteria.where, _.pick(state.get(), defaultParams.filters));

                if (criteria.hasOwnProperty('_id')) {
                    // prevent reload on preview
                    delete criteria['_id'];
                }

                return criteria;
            }

            /**
             * Log slow query into console
             *
             * @param {integer} start
             */
            function slowQueryLog(start) {
                var took = Date.now() - start;
                if (took > 500) {
                    console.log('Slow query: /' + resource + ' ' + took + 'ms');
                }
            }

            /**
             * Execute query
             *
             * @param {Object} criteria
             */
            function query(criteria) {
                var start = Date.now();
                _this._items = null;
                return em.getRepository(resource).matching(criteria).then(function(data) {
                    slowQueryLog(start);
                    angular.extend(_this, data);
                });
            }

            /**
             * Get/set current page
             *
             * @param {integer} page
             */
            this.page = function(page) {
                switch(arguments.length) {
                    case 0:
                    return state.get(page) || defaultParams.page;
                    break;

                    case 1:
                    if (this._items) {
                        state.set('page', page !== defaultParams.page ? page : null);
                    }
                    break;
                }
            };

            /**
             * Get/set current search query
             *
             * @param {string} q
             * @param {string} df
             */
            this.search = function(q, df) {
                switch(arguments.length) {
                    case 0:
                    return state.get('q');<commit_msg>Fix data adapter page handling.<commit_after>define(['angular', 'lodash'], function(angular, _) {
    'use strict';

    angular.module('superdesk.services')
    .factory('DataAdapter', ['$rootScope', '$location', 'em', function($rootScope, $location, em) {

        /**
         * $location state adapter
         */
        function LocationState(loc) {

            this.get = function(key) {
                return arguments.length ? loc.search()[key] : loc.search();
            }

            this.set = function(key, val) {
                return loc.search(key, val);
            }
        }

        // @todo implement storage state provider
        var stateProviders = {
            location: new LocationState($location)
        };

        /**
         * Data Provider for given resource
         */
        return function DataAdapter(resource, params) {
            var _this = this;
            var state = stateProviders.location;
            var defaultParams = angular.extend({max_results: 25, page: 1, where: {}, sort: [], filters: []}, params);

            /**
             * Get query criteria - extend default params with current search
             */
            function getQueryCriteria() {
                var criteria = angular.extend({}, defaultParams, state.get());
                angular.extend(criteria.where, _.pick(state.get(), defaultParams.filters));

                if (criteria.hasOwnProperty('_id')) {
                    // prevent reload on preview
                    delete criteria['_id'];
                }

                return criteria;
            }

            /**
             * Log slow query into console
             *
             * @param {integer} start
             */
            function slowQueryLog(start) {
                var took = Date.now() - start;
                if (took > 500) {
                    console.log('Slow query: /' + resource + ' ' + took + 'ms');
                }
            }

            /**
             * Execute query
             *
             * @param {Object} criteria
             */
            function query(criteria) {
                var start = Date.now();
                _this._items = null;
                return em.getRepository(resource).matching(criteria).then(function(data) {
                    slowQueryLog(start);
                    angular.extend(_this, data);
                });
            }

            /**
             * Get/set current page
             *
             * @param {integer} page
             */
            this.page = function(page) {
                switch(arguments.length) {
                    case 0:
                    return state.get('page') || defaultParams.page;
                    break;

                    case 1:
                    if (this._items) {
                        state.set('page', page !== defaultParams.page ? page : null);
                    }
                    break;
                }
            };

            /**
             * Get/set current search query
             *
             * @param {string} q
             * @param {string} df
             */
            this.search = function(q, df) {
                switch(arguments.length) {
                    case 0:
                    return state.get('q');
{'fraction_non_alphanumeric': 0.11177007299270073, 'fraction_numerical': 0.002737226277372263, 'mean_word_length': 1.1271021992238033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4808271', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1486, 'n_words': 355}