<commit_before>/* globals Panels, $, SocReport, socSortCorrelation */

var Correlations = (function() {
    var loaded = null;

    function urlMaker(version, os) {
        var url = SocReport.base ;
        url += '?product=' + SocReport.product;
        url += '&version=' + version;
        url += '&platform=' + os;
        url += '&signature=' + SocReport.signature;
        return function makeUrl(type) {
            return url + '&correlation_report_type=' + type;
        };
    }

    // Load correlation data for various types.
    // @types CPU, Add-On, Module
    function loadCorrelationTabData(version, os, deferred) {
        var makeUrl = urlMaker(version, os);
        var types = ['core-counts', 'interesting-addons', 'interesting-modules'];

        function loadByType(type) {
            $.getJSON(makeUrl(type), function(data) {
                if(data) {
                    $('#' + type + '_correlation').html('<h3>' + data.reason +
                        '</h3><pre>'+ data.load + '</pre>');
                    socSortCorrelation('#' + type + '_correlation');
                } else {
                    $('#' + type + '_correlation').text('No correlation data found.');
                }
            });
        }

        while (true) {
            var type = types.shift();
            if (!type) {
                // all types done
                deferred.resolve();
                break;
            }
            loadByType(type);
        }
    }


    return {
       activate: function() {
           if (loaded) return;
           var deferred = $.Deferred();
           var $panel = $('#correlations');
           var url = $panel.data('partial-url');
           var qs = location.href.split('?')[1];
           url += '?' + qs;
           var req = $.ajax({
               url: url
           });
           req.done(function(response) {
               $('.loading-placeholder', $panel).hide();
               $('.inner', $panel).html(response);
               var $wrapper = $('.correlations-wrapper', $panel);
               var version = $wrapper.data('correlation_version');
               var os = $wrapper.data('correlation_os');

               $('button.load-version-data').click(function () {
                   var type = $(this).attr('name');
                   var makeUrl = urlMaker(version, os);
                   $.getJSON(makeUrl(type), function(data) {
                       $('#' + type + '-panel').html('<h3>' + data.reason + '</h3><pre>' +
                                                     data.load + '</pre>');
                   });
               });
               if (version && os) {
                   loadCorrelationTabData(version, os, deferred);
               }
           });
           req.fail(function(data, textStatus, errorThrown) {
               $('.loading-placeholder', $panel).hide();
               $('.loading-failed', $panel).show();
               deferred.reject(data, textStatus, errorThrown);
           });
           loaded = true;
           return deferred.promise();
       }
    };
})();

Panels.register('correlations', function() {
    return Correlations.activate();<commit_msg>Fix Bug 943847, handle cases where data is null and add spinner on click<commit_after>/* globals Panels, $, SocReport, socSortCorrelation */

var Correlations = (function() {
    var loaded = null;

    function urlMaker(version, os) {
        var url = SocReport.base ;
        url += '?product=' + SocReport.product;
        url += '&version=' + version;
        url += '&platform=' + os;
        url += '&signature=' + SocReport.signature;
        return function makeUrl(type) {
            return url + '&correlation_report_type=' + type;
        };
    }

    // Load correlation data for various types.
    // @types CPU, Add-On, Module
    function loadCorrelationTabData(version, os, deferred) {
        var makeUrl = urlMaker(version, os);
        var types = ['core-counts', 'interesting-addons', 'interesting-modules'];

        function loadByType(type) {
            $.getJSON(makeUrl(type), function(data) {
                if(data) {
                    $('#' + type + '_correlation').html('<h3>' + data.reason +
                        '</h3><pre>'+ data.load + '</pre>');
                    socSortCorrelation('#' + type + '_correlation');
                } else {
                    $('#' + type + '_correlation').text('No correlation data found.');
                }
            });
        }

        while (true) {
            var type = types.shift();
            if (!type) {
                // all types done
                deferred.resolve();
                break;
            }
            loadByType(type);
        }
    }


    return {
       activate: function() {
           if (loaded) return;
           var deferred = $.Deferred();
           var $panel = $('#correlations');
           var url = $panel.data('partial-url');
           var qs = location.href.split('?')[1];
           url += '?' + qs;
           var req = $.ajax({
               url: url
           });
           req.done(function(response) {
               $('.loading-placeholder', $panel).hide();
               $('.inner', $panel).html(response);
               var $wrapper = $('.correlations-wrapper', $panel);
               var version = $wrapper.data('correlation_version');
               var os = $wrapper.data('correlation_os');

               $('button.load-version-data').click(function () {
                   var type = $(this).attr('name');
                   var makeUrl = urlMaker(version, os);
                   var spinner = $('<img />', {
                      id: 'loading-spinner',
                      src: '/static/img/loading.png',
                      width: '16',
                      height: '17',
                      alt: 'loading spinner'
                   });
                   var contentPanel = $('#' + type + '-panel');

                   contentPanel.empty().append(['Loading ', spinner]);

                   $.getJSON(makeUrl(type), function(data) {
                       if (data) {
                         contentPanel.html('<h3>' + data.reason + '</h3><pre>' +
                                                       data.load + '</pre>');
                       } else {
                          contentPanel.text('No correlation data found.');
                      }
                   });
               });
               if (version && os) {
                   loadCorrelationTabData(version, os, deferred);
               }
           });
           req.fail(function(data, textStatus, errorThrown) {
               $('.loading-placeholder', $panel).hide();
               $('.loading-failed', $panel).show();
               deferred.reject(data, textStatus, errorThrown);
           });
           loaded = true;
           return deferred.promise();
       }
    };
})();

Panels.register('correlations', function() {
    return Correlations.activate();
{'fraction_non_alphanumeric': 0.15054223744292236, 'fraction_numerical': 0.0028538812785388126, 'mean_word_length': 1.4135674931129476, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4943077', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1743, 'n_words': 382}