<commit_before>define([
    'underscore',
    'knockout',
    'moment',
    'views/search/base-filter',
    'arches',
    'bindings/datepicker',
    'bindings/chosen',
    'bindings/time-wheel'
],
function(_, ko, moment, BaseFilter, arches) {
    return BaseFilter.extend({
        initialize: function(options) {
            var self = this;
            this.name = 'Time Filter';
            this.filter = {
                fromDate: ko.observable(null),
                toDate: ko.observable(null),
                dateNodeId: ko.observable(null),
                inverted: ko.observable(false)
            }

            this.dateRangeType = ko.observable('custom');
            this.format = 'YYYY-MM-DD';
            this.showWheel = ko.observable(false);
            this.breadCrumb = ko.observable();
            this.selectedPeriod = ko.observable();
            this.wheelConfig = ko.observable();
            this.getTimeWheelConfig();
            this.selectedPeriod.subscribe(function (d) {
                if (d) {
                    var start = moment(0, 'YYYY').add(d.start, 'years').format(this.format);
                    var end = moment(0, 'YYYY').add(d.end, 'years').format(this.format);
                    self.dateRangeType('custom');
                    self.filter.fromDate(end);
                    self.filter.toDate(end);
                    self.filter.fromDate(start);
                }
            })

            this.dateRangeType.subscribe(function(value) {
                var today = moment();
                var from = today.format(this.format);
                var to = today.format(this.format);
                switch (value) {
                    case "today":
                        break;
                    case "last-7":
                        from = today.subtract(7, 'days').format(this.format);
                        break;
                    case "last-30":
                        from = today.subtract(30, 'days').format(this.format);
                        break;
                    case "this-week":
                        from = today.day(0).format(this.format);
                        to = today.day(6).format(this.format);
                        break;
                    case "this-month":
                        from = today.date(1).format(this.format);
                        to = moment().month(today.month()+1).date(0).format(this.format);
                        break;
                    case "this-quarter":
                        from = moment().date(1).quarter(today.quarter()).format(this.format);
                        to = moment().date(1).quarter(today.quarter()+1).format(this.format);
                        break;
                    case "this-year":
                        var first = today.dayOfYear(1);
                        from = first.format(this.format);
                        to = first.add(1, 'years').subtract(1, 'days').format(this.format);
                        break;
                    default:
                        return;
                }
                this.filter.toDate(to);
                this.filter.fromDate(from);
            }, this);

            this.filterChanged = ko.computed(function(){
                if(!!this.filter.fromDate() || !!this.filter.toDate()){
                    this.termFilter.addTag(this.name, this.name, this.filter.inverted);
                }
                return ko.toJSON(this.filter);
            }, this).extend({ deferred: true });

            BaseFilter.prototype.initialize.call(this, options);
        },

        getTimeWheelConfig: function(){
            var self = this;
            $.ajax({
                type: "GET",
                url: arches.urls.time_wheel_config,
                success: function(response) {
                    self.wheelConfig(response);
                },
                error: function(response) {
                    self.breadCrumb(response.responseText);
                }
            });
        },

        appendFilters: function(filterParams) {
            var filters_applied = !!this.filter.fromDate() || !!this.filter.toDate();
            if(filters_applied){
                filterParams.temporalFilter = ko.toJSON(this.filter);
            }
            return filters_applied;
        },

        restoreState: function(query) {
            var doQuery = false;
            if ('temporalFilter' in query) {
                query.temporalFilter = JSON.parse(query.temporalFilter);
                this.filter.inverted(!!query.temporalFilter.inverted);
                this.termFilter.addTag(this.name, this.name, this.filter.inverted);
                ['fromDate', 'toDate', 'dateNodeId'].forEach(function(key) {
                    if (key in query.temporalFilter) {
                        this.filter[key](query.temporalFilter[key]);
                    }
                }, this);
                doQuery = true;
            }
            return doQuery;
        },

        clear: function() {
            this.filter.fromDate(null);
            this.filter.toDate(null);
            this.filter.dateNodeId(null);
            this.filter.inverted(false);
            this.dateRangeType('custom');
            this.termFilter.removeTag(this.name);
            this.selectedPeriod(null);
            return;
        }
    });
});
<commit_msg>keeping date ranges sensible re: #1791<commit_after>define([
    'underscore',
    'knockout',
    'moment',
    'views/search/base-filter',
    'arches',
    'bindings/datepicker',
    'bindings/chosen',
    'bindings/time-wheel'
],
function(_, ko, moment, BaseFilter, arches) {
    return BaseFilter.extend({
        initialize: function(options) {
            var self = this;
            this.name = 'Time Filter';
            this.filter = {
                fromDate: ko.observable(null),
                toDate: ko.observable(null),
                dateNodeId: ko.observable(null),
                inverted: ko.observable(false)
            }
            this.filter.fromDate.subscribe(function (fromDate) {
                var toDate = self.filter.toDate();
                if (fromDate && toDate && toDate < fromDate) {
                    self.filter.toDate(fromDate);
                }
            });
            this.filter.toDate.subscribe(function (toDate) {
                var fromDate = self.filter.fromDate();
                if (fromDate && toDate && fromDate > toDate) {
                    self.filter.fromDate(toDate);
                }
            })
            this.dateRangeType = ko.observable('custom');
            this.format = 'YYYY-MM-DD';
            this.showWheel = ko.observable(false);
            this.breadCrumb = ko.observable();
            this.selectedPeriod = ko.observable();
            this.wheelConfig = ko.observable();
            this.getTimeWheelConfig();
            this.selectedPeriod.subscribe(function (d) {
                if (d) {
                    var start = moment(0, 'YYYY').add(d.start, 'years').format(this.format);
                    var end = moment(0, 'YYYY').add(d.end, 'years').format(this.format);
                    self.dateRangeType('custom');
                    self.filter.fromDate(end);
                    self.filter.toDate(end);
                    self.filter.fromDate(start);
                }
            })

            this.dateRangeType.subscribe(function(value) {
                var today = moment();
                var from = today.format(this.format);
                var to = today.format(this.format);
                switch (value) {
                    case "today":
                        break;
                    case "last-7":
                        from = today.subtract(7, 'days').format(this.format);
                        break;
                    case "last-30":
                        from = today.subtract(30, 'days').format(this.format);
                        break;
                    case "this-week":
                        from = today.day(0).format(this.format);
                        to = today.day(6).format(this.format);
                        break;
                    case "this-month":
                        from = today.date(1).format(this.format);
                        to = moment().month(today.month()+1).date(0).format(this.format);
                        break;
                    case "this-quarter":
                        from = moment().date(1).quarter(today.quarter()).format(this.format);
                        to = moment().date(1).quarter(today.quarter()+1).format(this.format);
                        break;
                    case "this-year":
                        var first = today.dayOfYear(1);
                        from = first.format(this.format);
                        to = first.add(1, 'years').subtract(1, 'days').format(this.format);
                        break;
                    default:
                        return;
                }
                this.filter.toDate(to);
                this.filter.fromDate(from);
            }, this);

            this.filterChanged = ko.computed(function(){
                if(!!this.filter.fromDate() || !!this.filter.toDate()){
                    this.termFilter.addTag(this.name, this.name, this.filter.inverted);
                }
                return ko.toJSON(this.filter);
            }, this).extend({ deferred: true });

            BaseFilter.prototype.initialize.call(this, options);
        },

        getTimeWheelConfig: function(){
            var self = this;
            $.ajax({
                type: "GET",
                url: arches.urls.time_wheel_config,
                success: function(response) {
                    self.wheelConfig(response);
                },
                error: function(response) {
                    self.breadCrumb(response.responseText);
                }
            });
        },

        appendFilters: function(filterParams) {
            var filters_applied = !!this.filter.fromDate() || !!this.filter.toDate();
            if(filters_applied){
                filterParams.temporalFilter = ko.toJSON(this.filter);
            }
            return filters_applied;
        },

        restoreState: function(query) {
            var doQuery = false;
            if ('temporalFilter' in query) {
                query.temporalFilter = JSON.parse(query.temporalFilter);
                this.filter.inverted(!!query.temporalFilter.inverted);
                this.termFilter.addTag(this.name, this.name, this.filter.inverted);
                ['fromDate', 'toDate', 'dateNodeId'].forEach(function(key) {
                    if (key in query.temporalFilter) {
                        this.filter[key](query.temporalFilter[key]);
                    }
                }, this);
                doQuery = true;
            }
            return doQuery;
        },

        clear: function() {
            this.filter.fromDate(null);
            this.filter.toDate(null);
            this.filter.dateNodeId(null);
            this.filter.inverted(false);
            this.dateRangeType('custom');
            this.termFilter.removeTag(this.name);
            this.selectedPeriod(null);
            return;
        }
    });
});

{'fraction_non_alphanumeric': 0.1230837394771821, 'fraction_numerical': 0.0037217545414266725, 'mean_word_length': 1.2981062919975566, 'pattern_counts': {'":': 14, '<': 4, '<?xml version=': 0, '>': 4, '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': '3623509', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2782, 'n_words': 443}