<commit_before>
define([
    'flight/lib/component',
    'hbs!./template',
    './handlers',
    'd3',
    'configuration/plugins/activity/plugin',
    'util/formatters',
    'util/withCollapsibleSections',
    'service/longRunningProcess'
], function(
    defineComponent,
    template,
    builtinHandlers,
    d3,
    ActivityHandlers,
    F,
    withCollapsibleSections,
    LongRunningProcessService) {
    'use strict';

    var AUTO_UPDATE_INTERVAL_SECONDS = 60,
        longRunningProcessService = new LongRunningProcessService();

    return defineComponent(Activity, withCollapsibleSections);

    function Activity() {

        this.defaultAttrs({
            typesSelector: '.types',
            deleteButtonSelector: 'button.delete',
            cancelButtonSelector: 'button.cancel',
            noActivitySelector: '.no-activity'
        })

        this.before('teardown', function() {
            clearInterval(this.autoUpdateTimer);
        });

        this.after('initialize', function() {
            ActivityHandlers.registerActivityHandlers(builtinHandlers);

            this.removedTasks = {};
            this.$node.html(template({}));

            this.tasks = window.currentUser && window.currentUser.longRunningProcesses || [];
            this.tasksById = _.indexBy(this.tasks, 'id');

            this.on(document, 'menubarToggleDisplay', this.onToggleDisplay);
            this.on(document, 'socketMessage', this.onSocketMessage);

            this.on('click', {
                deleteButtonSelector: this.onDelete,
                cancelButtonSelector: this.onCancel
            })
        });

        this.onCancel = function(event) {
            this.callServiceMethodRemove(event, 'cancel');
        };

        this.onDelete = function(event) {
            this.callServiceMethodRemove(event, 'delete');
        };

        this.callServiceMethodRemove = function(event, name) {
            var self = this,
                $button = $(event.target),
                processId = $button.closest('li').data('processId');

            if (processId) {
                $button.addClass('loading');
                longRunningProcessService[name](processId)
                    .always(function() {
                        $button.removeClass('loading');
                    })
                    .done(function() {
                        self.removedTasks[processId] = true;
                        var task = self.tasksById[processId];
                        delete self.tasksById[processId];
                        self.tasks.splice(self.tasks.indexOf(task), 1);
                        self.update();
                    });
            }
        };

        this.onSocketMessage = function(event, message) {
            if (message && message.type === 'longRunningProcessChange') {
                var task = message.data,
                    existingTask = this.tasksById[task.id];

                if (this.removedTasks[task.id]) {
                    return;
                }

                if (existingTask) {
                    this.tasks.splice(this.tasks.indexOf(existingTask), 1, task);
                } else {
                    this.tasks.splice(0, 0, task);
                }

                this.tasksById[task.id] = task;

                this.update();
            }
        };

        this.onToggleDisplay = function(event, data) {
            var openingActivity = data.name === 'activity' && this.$node.closest('.visible').length;

            this.isOpen = openingActivity;

            if (openingActivity) {
                this.update();
                this.startAutoUpdate();
            } else {
                this.pauseAutoUpdate();
            }
        };

        this.pauseAutoUpdate = function() {
            clearInterval(this.autoUpdateTimer);
        };

        this.startAutoUpdate = function() {
            this.autoUpdateTimer = setInterval(this.update.bind(this), AUTO_UPDATE_INTERVAL_SECONDS * 1000)
        };

        this.update = function() {
            if (!this.isOpen) {
                return;
            }

            var data = _.chain(this.tasks)
                    .filter(function(p) {
                        return !p.canceled;
                    })
                    .groupBy('type')
                    .pairs()
                    .sortBy(function(pair) {
                        return pair[0].toLowerCase();
                    })
                    .value(),
                uniqueTypes = _.chain(data)
                    .map(function(p) {
                        return p[0];
                    })
                    .unique()<commit_msg>Sort activity items by order queued (desc)<commit_after>
define([
    'flight/lib/component',
    'hbs!./template',
    './handlers',
    'd3',
    'configuration/plugins/activity/plugin',
    'util/formatters',
    'util/withCollapsibleSections',
    'service/longRunningProcess'
], function(
    defineComponent,
    template,
    builtinHandlers,
    d3,
    ActivityHandlers,
    F,
    withCollapsibleSections,
    LongRunningProcessService) {
    'use strict';

    var AUTO_UPDATE_INTERVAL_SECONDS = 60,
        longRunningProcessService = new LongRunningProcessService();

    return defineComponent(Activity, withCollapsibleSections);

    function Activity() {

        this.defaultAttrs({
            typesSelector: '.types',
            deleteButtonSelector: 'button.delete',
            cancelButtonSelector: 'button.cancel',
            noActivitySelector: '.no-activity'
        })

        this.before('teardown', function() {
            clearInterval(this.autoUpdateTimer);
        });

        this.after('initialize', function() {
            ActivityHandlers.registerActivityHandlers(builtinHandlers);

            this.removedTasks = {};
            this.$node.html(template({}));

            this.tasks = window.currentUser && window.currentUser.longRunningProcesses || [];
            this.tasksById = _.indexBy(this.tasks, 'id');

            this.on(document, 'menubarToggleDisplay', this.onToggleDisplay);
            this.on(document, 'socketMessage', this.onSocketMessage);

            this.on('click', {
                deleteButtonSelector: this.onDelete,
                cancelButtonSelector: this.onCancel
            })
        });

        this.onCancel = function(event) {
            this.callServiceMethodRemove(event, 'cancel');
        };

        this.onDelete = function(event) {
            this.callServiceMethodRemove(event, 'delete');
        };

        this.callServiceMethodRemove = function(event, name) {
            var self = this,
                $button = $(event.target),
                processId = $button.closest('li').data('processId');

            if (processId) {
                $button.addClass('loading');
                longRunningProcessService[name](processId)
                    .always(function() {
                        $button.removeClass('loading');
                    })
                    .done(function() {
                        self.removedTasks[processId] = true;
                        var task = self.tasksById[processId];
                        delete self.tasksById[processId];
                        self.tasks.splice(self.tasks.indexOf(task), 1);
                        self.update();
                    });
            }
        };

        this.onSocketMessage = function(event, message) {
            if (message && message.type === 'longRunningProcessChange') {
                var task = message.data,
                    existingTask = this.tasksById[task.id];

                if (this.removedTasks[task.id]) {
                    return;
                }

                if (existingTask) {
                    this.tasks.splice(this.tasks.indexOf(existingTask), 1, task);
                } else {
                    this.tasks.splice(0, 0, task);
                }

                this.tasksById[task.id] = task;

                this.update();
            }
        };

        this.onToggleDisplay = function(event, data) {
            var openingActivity = data.name === 'activity' && this.$node.closest('.visible').length;

            this.isOpen = openingActivity;

            if (openingActivity) {
                this.update();
                this.startAutoUpdate();
            } else {
                this.pauseAutoUpdate();
            }
        };

        this.pauseAutoUpdate = function() {
            clearInterval(this.autoUpdateTimer);
        };

        this.startAutoUpdate = function() {
            this.autoUpdateTimer = setInterval(this.update.bind(this), AUTO_UPDATE_INTERVAL_SECONDS * 1000)
        };

        this.update = function() {
            if (!this.isOpen) {
                return;
            }

            var data = _.chain(this.tasks)
                    .filter(function(p) {
                        return !p.canceled;
                    })
                    .sortBy(function(p) {
                        return p.enqueueTime * -1;
                    })
                    .groupBy('type')
                    .pairs()
                    .sortBy(function(pair) {
                        return pair[0].toLowerCase();
                    })
                    .value(),
                uniqueTypes = _.chain(data)
                    .map(function(p) {
                        return p[0];
                    })
                    .unique()
{'fraction_non_alphanumeric': 0.11364355808800253, 'fraction_numerical': 0.003060040097077134, 'mean_word_length': 1.4421540839989693, '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': '7241899', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2294, 'n_words': 361}