<commit_before>/*global define*/

define(['jquery', 'underscore', 'backbone', 'models/usage-model', 'models/health-model', 'models/status-model', 'marionette'], function($, _, Backbone, UsageModel, HealthModel, StatusModel) {
    'use strict';

    function newPoller(eventPrefix) {
        var fnName = eventPrefix + 'Poller',
            timerName = eventPrefix + 'Timer',
            modelName = eventPrefix + 'Model';
        return function() {
            var self = this;
            var App = this.App;
            _.each(['request', 'sync', 'error'], function(event) {
                this.listenTo(this[modelName], event, function() {
                    App.vent.trigger(eventPrefix + ':' + event);
                });
            }, this);
            var delay = this[timerName] === null ? 0 : this.delay;
            this[timerName] = setTimeout(function() {
                self[modelName].fetch({
                    success: function(model /*, response, options*/ ) {
                        App.vent.trigger(eventPrefix + ':update', model);
                        self[timerName] = self[fnName].apply(self);
                    },
                    error: function(model, response) {
                        console.log(response);
                        self[timerName] = self[fnName].apply(self);
                    }
                });
            }, delay);
            return this[timerName];
        };
    }

    function newEventEmitter(fnName, timerName, eventName) {
        return function() {
            var self = this;
            var delay = this[timerName] === null ? 0 : this.delay;
            this[timerName] = setTimeout(function() {
                self.App.vent.trigger(eventName);
                self[timerName] = self[fnName].apply(self);
            }, delay);
            return this[timerName];
        };
    }
    return Backbone.Marionette.ItemView.extend({
        usageTimer: null,
        usageModel: null,
        healthTimer: null,
        healthModel: null,
        statusModel: null,
        statusTimer: null,
        updateTimer: null,
        delay: 20000,
        initialize: function() {
            this.App = Backbone.Marionette.getOption(this, 'App');
            this.cluster = Backbone.Marionette.getOption(this, 'cluster');
            this.healthModel = new HealthModel({
                cluster: this.cluster
            });
            this.usageModel = new UsageModel({
                cluster: this.cluster
            });
            this.statusModel = new StatusModel({
                cluster: this.cluster
            });

            this.healthPoller = newPoller('health');
            this.usagePoller = newPoller('usage');
            this.statusPoller = newPoller('status');
            this.updateEvent = newEventEmitter('updateEvent', 'updateTimer', 'osd:update');
            this.listenTo(this.App.vent, 'cluster:update', this.updateModels);
            _.bindAll(this, 'stop', 'updateModels', 'start');
            this.models = ['health', 'usage', 'status'];
            this.timers = ['health', 'usage', 'status', 'update'];
            this.pollers = ['healthPoller', 'usagePoller', 'statusPoller', 'updateEvent'];
        },
        // Cluster ID has changed. Update pollers.
        updateModels: function(cluster) {
            _.each(this.models, function(model) {
                this[model + 'Model'].set('cluster', cluster.id);
            }, this);
            this.stop();
            this.start();
        },
        // Restart Poller functions
        start: function() {
            _.each(this.pollers, function(poller) {
                this[poller].call(this);
            }, this);
        },
        // Stop and Remove All Currently running Pollers
        stop: function() {
            _.each(this.timers, function(timer) {<commit_msg>Avoid re-registering listeners on ever poll!<commit_after>/*global define*/

define(['jquery', 'underscore', 'backbone', 'models/usage-model', 'models/health-model', 'models/status-model', 'marionette'], function($, _, Backbone, UsageModel, HealthModel, StatusModel) {
    'use strict';

    function newPoller(eventPrefix, context) {
        var fnName = eventPrefix + 'Poller',
            timerName = eventPrefix + 'Timer',
            modelName = eventPrefix + 'Model';
        var self = context;
        var App = context.App;
        _.each(['request', 'sync', 'error'], function(event) {
            this.listenTo(this[modelName], event, function() {
                App.vent.trigger(eventPrefix + ':' + event);
            });
        }, context);
        return function() {
            var delay = this[timerName] === null ? 0 : this.delay;
            this[timerName] = setTimeout(function() {
                self[modelName].fetch({
                    success: function(model /*, response, options*/ ) {
                        App.vent.trigger(eventPrefix + ':update', model);
                        self[timerName] = self[fnName].apply(self);
                    },
                    error: function(model, response) {
                        console.log(response);
                        self[timerName] = self[fnName].apply(self);
                    }
                });
            }, delay);
            return this[timerName];
        };
    }

    function newEventEmitter(fnName, timerName, eventName) {
        return function() {
            var self = this;
            var delay = this[timerName] === null ? 0 : this.delay;
            this[timerName] = setTimeout(function() {
                self.App.vent.trigger(eventName);
                self[timerName] = self[fnName].apply(self);
            }, delay);
            return this[timerName];
        };
    }
    return Backbone.Marionette.ItemView.extend({
        usageTimer: null,
        usageModel: null,
        healthTimer: null,
        healthModel: null,
        statusModel: null,
        statusTimer: null,
        updateTimer: null,
        delay: 20000,
        initialize: function() {
            this.App = Backbone.Marionette.getOption(this, 'App');
            this.cluster = Backbone.Marionette.getOption(this, 'cluster');
            this.healthModel = new HealthModel({
                cluster: this.cluster
            });
            this.usageModel = new UsageModel({
                cluster: this.cluster
            });
            this.statusModel = new StatusModel({
                cluster: this.cluster
            });

            this.healthPoller = newPoller('health', this);
            this.usagePoller = newPoller('usage', this);
            this.statusPoller = newPoller('status', this);
            this.updateEvent = newEventEmitter('updateEvent', 'updateTimer', 'osd:update');
            this.listenTo(this.App.vent, 'cluster:update', this.updateModels);
            _.bindAll(this, 'stop', 'updateModels', 'start');
            this.models = ['health', 'usage', 'status'];
            this.timers = ['health', 'usage', 'status', 'update'];
            this.pollers = ['healthPoller', 'usagePoller', 'statusPoller', 'updateEvent'];
        },
        // Cluster ID has changed. Update pollers.
        updateModels: function(cluster) {
            _.each(this.models, function(model) {
                this[model + 'Model'].set('cluster', cluster.id);
            }, this);
            this.stop();
            this.start();
        },
        // Restart Poller functions
        start: function() {
            _.each(this.pollers, function(poller) {
                this[poller].call(this);
            }, this);
        },
        // Stop and Remove All Currently running Pollers
        stop: function() {
            _.each(this.timers, function(timer) {
{'fraction_non_alphanumeric': 0.13338503295851106, 'fraction_numerical': 0.0018094868812201111, 'mean_word_length': 1.7606136282554405, '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': '644283', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1976, 'n_words': 417}