<commit_before>/*********************************************
 *  Copyright (c) 2014 AnsibleWorks, Inc.
 *
 *  LogViewer.js
 *
 */

'use strict';

angular.module('EventViewerHelper', ['ModalDialog', 'Utilities'])

    .factory('EventViewer', ['$compile', 'CreateDialog', 'GetEvent', 'Wait', 'EventAddTable', 'GetBasePath', 'LookUpName', 'Empty', 'EventAddPreFormattedText',
    function($compile, CreateDialog, GetEvent, Wait, EventAddTable, GetBasePath, LookUpName, Empty, EventAddPreFormattedText) {
        return function(params) {
            var parent_scope = params.scope,
                url = params.url,
                title = params.title, //optional
                scope = parent_scope.$new(true);

            if (scope.removeModalReady) {
                scope.removeModalReady();
            }
            scope.removeModalReady = scope.$on('ModalReady', function() {
                Wait('stop');
                $('#eventviewer-modal-dialog').dialog('open');
            });

            if (scope.removeJobReady) {
                scope.removeJobReady();
            }
            scope.removeEventReady = scope.$on('EventReady', function(e, data) {
                var elem;

                $('#status-form-container').empty();
                $('#stdout-form-container').empty();
                $('#stderr-form-container').empty();
                $('#traceback-form-container').empty();
                $('#eventview-tabs li:eq(1)').hide();
                $('#eventview-tabs li:eq(2)').hide();
                $('#eventview-tabs li:eq(3)').hide();

                EventAddTable({ scope: scope, id: 'status-form-container', event: data });

                if (data.stdout) {
                    $('#eventview-tabs li:eq(1)').show();
                    EventAddPreFormattedText({
                        id: 'stdout-form-container',
                        val: data.stdout
                    });
                }

                if (data.stderr) {
                    $('#eventview-tabs li:eq(2)').show();
                    EventAddPreFormattedText({
                        id: 'stderr-form-container',
                        val: data.stderr
                    });
                }

                if (data.traceback) {
                    $('#eventview-tabs li:eq(3)').show();
                    EventAddPreFormattedText({
                        id: 'traceback-form-container',
                        val: data.traceback
                    });
                }

                elem = angular.element(document.getElementById('eventviewer-modal-dialog'));
                $compile(elem)(scope);

                CreateDialog({
                    scope: scope,
                    width: 675,
                    height: 600,
                    minWidth: 450,
                    callback: 'ModalReady',
                    id: 'eventviewer-modal-dialog',
                    // onResizeStop: resizeText,
                    title: ( (title) ? title : 'Event Details' ),
                    onOpen: function() {
                        $('#eventview-tabs a:first').tab('show');
                        $('#dialog-ok-button').focus();
                    }
                });
            });

            GetEvent({
                url: url,
                scope: scope
            });

            scope.modalOK = function() {
                $('#eventviewer-modal-dialog').dialog('close');
                scope.$destroy();
            };
        };
    }])

    .factory('GetEvent', ['Wait', 'Rest', 'ProcessErrors', function(Wait, Rest, ProcessErrors) {
        return function(params) {
            var url = params.url,
                scope = params.scope;

            function getStatus(data) {
                return (data.results[0].event === "runner_on_unreachable") ? "unreachable" : (data.results[0].event === "runner_on_skipped") ? 'skipped' : (data.results[0].failed) ? 'failed' :
                            (data.results[0].changed) ? 'changed' : 'ok';
            }

            Wait('start');
            Rest.setUrl(url);
            Rest.get()
                .success( function(data) {
                    var key, event_data = {};
                    if (data.results.length > 0 && data.results[0].event_data.res) {
                        for (key in data.results[0].event_data) {
                            if (key !== "res") {
                                data.results[0].event_data.res[key] = data.results[0].event_data[key];
                            }
                        }
                        if (data.results[0].event_data.res.ansible_facts) {
                            // don't show fact gathering results
                            delete data.results[0].event_data.res.ansible_facts;
                        }
                        data.results[0].event_data.res.status = getStatus(data);
                        event_data = data.results[0].event_data.res;
                    }
                    else {
                        data.results[0].event_data.status = getStatus(data);
                        event_data = data.results[0].event_data;
                    }
                    // convert results to stdout
                    if (event_data.results && typeof event_data.results === "object" && Array.isArray(event_data.results)) {
                        event_data.stdout = "";
                        event_data.results.forEach(function(row) {
                            event_data.stdout += row + "\n";
                        });
                        delete event_data.results;
                    }
                    if (event_data.invocation) {
                        for (key in event_data.invocation) {
                            event_data[key] = event_data.invocation[key];
                        }
                        delete event_data.invocation;
                    }
                    event_data.parent = data.results[0].parent;
                    event_data.play = data.results[0].play;
                    event_data.task = data.results[0].task;
                    event_data.created = data.results[0].created;
                    event_data.role = data.results[0].role;
                    event_data.host_id = data.results[0].host;
                    event_data.host_name = data.results[0].host_name;
                    event_data.id = data.results[0].id;
                    event_data.parent = data.results[0].parent;
                    scope.$emit('EventReady', event_data);
                })
                .error(function(data, status) {
                    ProcessErrors(scope, data, status, null, { hdr: 'Error!',
                        msg: 'Failed to get event ' + url + '. GET returned: ' + status });
                });
        };
    }])

    .factory('EventAddTable', ['$compile', '$filter', 'Empty', function($compile, $filter, Empty) {
        return function(params) {
            var scope = params.scope,
                id = params.id,
                event = params.event,
                html = '', e;

            function keyToLabel(key) {
                var label = '';
                switch(key) {
                    case "id":<commit_msg>Job detail page Added event/event_display value to event viewer status info.<commit_after>/*********************************************
 *  Copyright (c) 2014 AnsibleWorks, Inc.
 *
 *  LogViewer.js
 *
 */

'use strict';

angular.module('EventViewerHelper', ['ModalDialog', 'Utilities'])

    .factory('EventViewer', ['$compile', 'CreateDialog', 'GetEvent', 'Wait', 'EventAddTable', 'GetBasePath', 'LookUpName', 'Empty', 'EventAddPreFormattedText',
    function($compile, CreateDialog, GetEvent, Wait, EventAddTable, GetBasePath, LookUpName, Empty, EventAddPreFormattedText) {
        return function(params) {
            var parent_scope = params.scope,
                url = params.url,
                title = params.title, //optional
                scope = parent_scope.$new(true);

            if (scope.removeModalReady) {
                scope.removeModalReady();
            }
            scope.removeModalReady = scope.$on('ModalReady', function() {
                Wait('stop');
                $('#eventviewer-modal-dialog').dialog('open');
            });

            if (scope.removeJobReady) {
                scope.removeJobReady();
            }
            scope.removeEventReady = scope.$on('EventReady', function(e, data) {
                var elem;

                $('#status-form-container').empty();
                $('#stdout-form-container').empty();
                $('#stderr-form-container').empty();
                $('#traceback-form-container').empty();
                $('#eventview-tabs li:eq(1)').hide();
                $('#eventview-tabs li:eq(2)').hide();
                $('#eventview-tabs li:eq(3)').hide();

                EventAddTable({ scope: scope, id: 'status-form-container', event: data });

                if (data.stdout) {
                    $('#eventview-tabs li:eq(1)').show();
                    EventAddPreFormattedText({
                        id: 'stdout-form-container',
                        val: data.stdout
                    });
                }

                if (data.stderr) {
                    $('#eventview-tabs li:eq(2)').show();
                    EventAddPreFormattedText({
                        id: 'stderr-form-container',
                        val: data.stderr
                    });
                }

                if (data.traceback) {
                    $('#eventview-tabs li:eq(3)').show();
                    EventAddPreFormattedText({
                        id: 'traceback-form-container',
                        val: data.traceback
                    });
                }

                elem = angular.element(document.getElementById('eventviewer-modal-dialog'));
                $compile(elem)(scope);

                CreateDialog({
                    scope: scope,
                    width: 675,
                    height: 600,
                    minWidth: 450,
                    callback: 'ModalReady',
                    id: 'eventviewer-modal-dialog',
                    // onResizeStop: resizeText,
                    title: ( (title) ? title : 'Event Details' ),
                    onOpen: function() {
                        $('#eventview-tabs a:first').tab('show');
                        $('#dialog-ok-button').focus();
                    }
                });
            });

            GetEvent({
                url: url,
                scope: scope
            });

            scope.modalOK = function() {
                $('#eventviewer-modal-dialog').dialog('close');
                scope.$destroy();
            };
        };
    }])

    .factory('GetEvent', ['Wait', 'Rest', 'ProcessErrors', function(Wait, Rest, ProcessErrors) {
        return function(params) {
            var url = params.url,
                scope = params.scope;

            function getStatus(data) {
                return (data.results[0].event === "runner_on_unreachable") ? "unreachable" : (data.results[0].event === "runner_on_skipped") ? 'skipped' : (data.results[0].failed) ? 'failed' :
                            (data.results[0].changed) ? 'changed' : 'ok';
            }

            Wait('start');
            Rest.setUrl(url);
            Rest.get()
                .success( function(data) {
                    var key, event_data = {};
                    if (data.results.length > 0 && data.results[0].event_data.res) {
                        for (key in data.results[0].event_data) {
                            if (key !== "res") {
                                data.results[0].event_data.res[key] = data.results[0].event_data[key];
                            }
                        }
                        if (data.results[0].event_data.res.ansible_facts) {
                            // don't show fact gathering results
                            delete data.results[0].event_data.res.ansible_facts;
                        }
                        data.results[0].event_data.res.status = getStatus(data);
                        event_data = data.results[0].event_data.res;
                    }
                    else {
                        data.results[0].event_data.status = getStatus(data);
                        event_data = data.results[0].event_data;
                    }
                    // convert results to stdout
                    if (event_data.results && typeof event_data.results === "object" && Array.isArray(event_data.results)) {
                        event_data.stdout = "";
                        event_data.results.forEach(function(row) {
                            event_data.stdout += row + "\n";
                        });
                        delete event_data.results;
                    }
                    if (event_data.invocation) {
                        for (key in event_data.invocation) {
                            event_data[key] = event_data.invocation[key];
                        }
                        delete event_data.invocation;
                    }
                    event_data.parent = data.results[0].parent;
                    event_data.play = data.results[0].play;
                    event_data.task = data.results[0].task;
                    event_data.created = data.results[0].created;
                    event_data.role = data.results[0].role;
                    event_data.host_id = data.results[0].host;
                    event_data.host_name = data.results[0].host_name;
                    event_data.id = data.results[0].id;
                    event_data.parent = data.results[0].parent;
                    event_data.event = (data.results[0].event_display) ? data.results[0].event_display : data.results[0].event;
                    scope.$emit('EventReady', event_data);
                })
                .error(function(data, status) {
                    ProcessErrors(scope, data, status, null, { hdr: 'Error!',
                        msg: 'Failed to get event ' + url + '. GET returned: ' + status });
                });
        };
    }])

    .factory('EventAddTable', ['$compile', '$filter', 'Empty', function($compile, $filter, Empty) {
        return function(params) {
            var scope = params.scope,
                id = params.id,
                event = params.event,
                html = '', e;

            function keyToLabel(key) {
                var label = '';
                switch(key) {
                    case "id":
{'fraction_non_alphanumeric': 0.1340938588701414, 'fraction_numerical': 0.006079650249333971, 'mean_word_length': 1.250576479631053, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, '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': '236360', 'n_tokens_mistral': 3782, 'n_tokens_neox': 3518, 'n_words': 607}