<commit_before>(function() {
    "use strict";

    // sdk
    var data = require('sdk/self').data;
    var storage = require('sdk/simple-storage').storage;
    var timers = require('sdk/timers');

    // 1st party
    var api = require('../api');
    var main = require('../main');
    var timer = require('../timer');

    var name = 'queues',
        port,
        queuecallgroups = [],
        sizes = {};

    exports.contentScripts = [
        data.url('widgets/assets/js/queues.js'),
    ];

    exports.init = function(_port) {
        port = _port;

        // keep track of selected queue
        port.on('queue.select', function(id) {
            storage.widgets.queues.selected = id;
            if(id) {
                var size = NaN;
                if(sizes && sizes.hasOwnProperty(id)) {
                    size = sizes[id];
                }
                main.setIcon(getIconForSize(size));
            } else {
                // restore availability icon
                main.setIcon(storage.widgets.availability.icon);
            }

            timer.update('queue.size');
        });
    };

    exports.load = function() {
        api.asyncRequest(
            api.getUrl('queuecallgroup'),
            null,
            'get',
            {
                onComplete: function() {
                    port.emit('widget.indicator.stop', name);
                },
                onOk: function(response) {
                    storage.widgets.availability.unauthorized = false;

                    // find the id's for queuecallgroups
                    var queues = response.json.objects;
                    if(queues.length){
                        queuecallgroups = [];

                        queues.forEach(function(queue) {
                            queuecallgroups.push(queue.id);
                        });

                        port.emit('queue.reset');
                        port.emit('queue.fill', queues, storage.widgets.queues.selected);

                        // reset storage
                        sizes = {};
                        queues.forEach(function(queue) {
                            sizes[queue.id] = queue.queue_size;
                        });
                    } else {
                        port.emit('queue.empty');
                    }

                    setQueueSizesTimer();
                },
                onUnauthorized: function() {
                    console.info('widget.unauthorized: ' + name);

                    // update authorization status
                    storage.widgets.queues.unauthorized = true;

                    // display an icon explaining the user lacks permissions to use
                    // this feature of the plugin
                    port.emit('widget.unauthorized', name);
                },
            }
        );
    };

    exports.reset = function() {
        timer.stopTimer('queue.size');
        timer.unregisterTimer('queue.size');
        port.emit('queue.reset');
        port.emit('queue.empty');
    };

    function getIconForSize(size) {
        var icon = 'widgets/assets/img/queue/queue.png';
        if(!isNaN(size)) {
            if(size < 10) {
                icon = 'widgets/assets/img/queue/queue' + size + '.png';
            } else {
                icon = 'widgets/assets/img/queue/queue10.png';
            }
        }
        return icon;
    }

    function setQueueSizesTimer() {
        function timerFunction() {
            if(queuecallgroups.length) {
                queuecallgroups.forEach(function(id) {
                    api.asyncRequest(
                        // FIXME: the current limitation on the server of 20r/m will always reject some requests (status 503) in case of more than one queuecallgroup
                        api.getUrl('queuecallgroup') + id + '/',
                        null,
                        'get',
                        {
                            onOk: function(response) {
                                var size = parseInt(response.json.queue_size, 10);
                                if(isNaN(size)) {
                                    size = '?';  // queue size is not available
                                }

                                // update icon for toolbarbutton if this queuecallgroup was selected earlier
                                if(response.json.id == storage.widgets.queues.selected) {
                                    main.setIcon(getIconForSize(size));
                                }

                                sizes[response.json.id] = size;
                                port.emit('queue.size', response.json.id, size);
                            }
                        }
                    );
                });
            }
        }

        // check for queue sizes on a variable timeout
        function timerTimeout() {
            var timeout = 0;

            // only when authenticated
            if(storage.user) {
                // at least every 20s when a queue is selected
                if(storage.widgets.queues.selected) {
                    timeout = 20000;
                }

                // quicker if the panel is visible and the queues widget is open<commit_msg>Add onUnauthorized callback for the queue size timer.<commit_after>(function() {
    "use strict";

    // sdk
    var data = require('sdk/self').data;
    var storage = require('sdk/simple-storage').storage;
    var timers = require('sdk/timers');

    // 1st party
    var api = require('../api');
    var main = require('../main');
    var timer = require('../timer');

    var name = 'queues',
        port,
        queuecallgroups = [],
        sizes = {};

    exports.contentScripts = [
        data.url('widgets/assets/js/queues.js'),
    ];

    exports.init = function(_port) {
        port = _port;

        // keep track of selected queue
        port.on('queue.select', function(id) {
            storage.widgets.queues.selected = id;
            if(id) {
                var size = NaN;
                if(sizes && sizes.hasOwnProperty(id)) {
                    size = sizes[id];
                }
                main.setIcon(getIconForSize(size));
            } else {
                // restore availability icon
                main.setIcon(storage.widgets.availability.icon);
            }

            timer.update('queue.size');
        });
    };

    exports.load = function() {
        api.asyncRequest(
            api.getUrl('queuecallgroup'),
            null,
            'get',
            {
                onComplete: function() {
                    port.emit('widget.indicator.stop', name);
                },
                onOk: function(response) {
                    storage.widgets.availability.unauthorized = false;

                    // find the id's for queuecallgroups
                    var queues = response.json.objects;
                    if(queues.length){
                        queuecallgroups = [];

                        queues.forEach(function(queue) {
                            queuecallgroups.push(queue.id);
                        });

                        port.emit('queue.reset');
                        port.emit('queue.fill', queues, storage.widgets.queues.selected);

                        // reset storage
                        sizes = {};
                        queues.forEach(function(queue) {
                            sizes[queue.id] = queue.queue_size;
                        });
                    } else {
                        port.emit('queue.empty');
                    }

                    setQueueSizesTimer();
                },
                onUnauthorized: function() {
                    console.info('widget.unauthorized: ' + name);

                    // update authorization status
                    storage.widgets.queues.unauthorized = true;

                    // display an icon explaining the user lacks permissions to use
                    // this feature of the plugin
                    port.emit('widget.unauthorized', name);
                },
            }
        );
    };

    exports.reset = function() {
        timer.stopTimer('queue.size');
        timer.unregisterTimer('queue.size');
        port.emit('queue.reset');
        port.emit('queue.empty');
    };

    function getIconForSize(size) {
        var icon = 'widgets/assets/img/queue/queue.png';
        if(!isNaN(size)) {
            if(size < 10) {
                icon = 'widgets/assets/img/queue/queue' + size + '.png';
            } else {
                icon = 'widgets/assets/img/queue/queue10.png';
            }
        }
        return icon;
    }

    function setQueueSizesTimer() {
        function timerFunction() {
            if(queuecallgroups.length) {
                queuecallgroups.forEach(function(id) {
                    api.asyncRequest(
                        // FIXME: the current limitation on the server of 20r/m will always reject some requests (status 503) in case of more than one queuecallgroup
                        api.getUrl('queuecallgroup') + id + '/',
                        null,
                        'get',
                        {
                            onOk: function(response) {
                                var size = parseInt(response.json.queue_size, 10);
                                if(isNaN(size)) {
                                    size = '?';  // queue size is not available
                                }

                                // update icon for toolbarbutton if this queuecallgroup was selected earlier
                                if(response.json.id == storage.widgets.queues.selected) {
                                    main.setIcon(getIconForSize(size));
                                }

                                sizes[response.json.id] = size;
                                port.emit('queue.size', response.json.id, size);
                            },
                            onUnauthorized: function() {
                                console.info('widget.unauthorized: ' + name);

                                // update authorization status
                                storage.widgets.queues.unauthorized = true;

                                // display an icon explaining the user lacks permissions to use
                                // this feature of the plugin
                                port.emit('widget.unauthorized', name);
                            },
                        }
                    );
                });
            }
        }

        // check for queue sizes on a variable timeout
        function timerTimeout() {
            var timeout = 0;

            // only when authenticated
            if(storage.user) {
                // at least every 20s when a queue is selected
                if(storage.widgets.queues.selected) {
                    timeout = 20000;
                }

                // quicker if the panel is visible and the queues widget is open
{'fraction_non_alphanumeric': 0.10162162162162162, 'fraction_numerical': 0.0036036036036036037, 'mean_word_length': 1.069537658463833, 'pattern_counts': {'":': 0, '<': 5, '<?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': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6472529', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2484, 'n_words': 540}