<commit_before>/*
 * Copyright (c) 2016
 * Juniper Networks, Inc. All rights reserved.
 */

define([
    "underscore",
    "contrail-view"
], function (_, ContrailView) {
    var IntrospectJSGridView = ContrailView.extend({
        el: $(window.contentContainer),

        render: function() {
            var self = this,
                viewConfig = self.attributes.viewConfig,
                introspectNode = viewConfig.node,
                introspectPort = viewConfig.port,
                modelMap = contrail.handleIfNull(self.modelMap, {}),
                jsonData = viewConfig.jsonData,
                jsGridViewConfig = getIntrospectJSGridViewConfig(jsonData, introspectNode, introspectPort);

            self.renderView4Config(self.$el, null, jsGridViewConfig, null, null, modelMap, null);
        }
    });

    function getIntrospectJSGridViewConfig(jsonData, introspectNode, introspectPort) {
        jsonData = (null != jsonData) ? jsonData : {};
        var sandeshData = parseData(jsonData),
            gridViewConfigs = [];

        _.each(sandeshData, function(value, key){
            gridViewConfigs.push({
                columns: [
                    {
                        elementId: "introspect-result-js-grid-" + introspectNode + "-" + introspectPort + "-" + key,
                        view: "GridView",
                        viewConfig: {
                            elementConfig: getIntrospectJSGridConfig(value)
                        }
                    }
                ]
            });
        });

        return {
            elementId: "introspect-js-grids",
            view: "SectionView",
            viewConfig: {
                rows: gridViewConfigs
            }
        };
    }

    function getIntrospectJSGridConfig(value) {
        var dataObj, gridData =  [], gridColumnsObj = {columns: []};

        if (isSandeshDataHavingObject(value)) {
            dataObj = parseDataObject(value.data);

            if (contrail.checkIfExist(dataObj)) {
                gridColumnsObj = createGridColumns(dataObj);
                gridData = createGridData(dataObj);
            }
        }

        return {
            header: {
                title: {text: value.title},
                defaultControls: {
                    collapseable: false,
                    columnPickable: true
                }
            },
            columnHeader: {columns: gridColumnsObj.columns},
            body: {
                options: {
                    forceFitColumns: gridColumnsObj.columns.length < 3 ? true : false,
                    checkboxSelectable: false,
                    fixedRowHeight: gridColumnsObj.isFixedRowHeight ? 30 : false
                },
                dataSource: {data: gridData}
            }
        };
    }

    function createGridColumns(dataObj) {
        var dataLength = dataObj.length,
            dataRecord = {}, gridColumns = [],
            isFixedRowHeight = true;

        if (!_.isArray(dataObj)) {
            dataRecord = dataObj;
        } else if (dataLength > 1) {
            dataRecord = dataObj[0];
        }

        _.each(dataRecord, function (value, key) {
            if (key.charAt(0) !== "_" && ["more", "next_batch"].indexOf(key) === -1 ) {
                var gridColumn = {
                    id: key, field: key,
                    name: getLabel4Key(key),
                    width: getColoumnWidth(value, key),
                    sortable: true
                };

                if (contrail.checkIfExist(value._link)) {
                    gridColumn.formatter = function (r, c, v, cd, dc) {
                        return '<a class="introspect-link" data-link="' + value._link + '" ' +
                            'x="' + dc[key] + '">' + dc[key] + "</a>";
                    };
                    gridColumn.exportConfig = { allow: true };

                }

                if (_.contains(["list", "struct", "sandesh"], value._type) ||
                    (!contrail.checkIfExist(value._type) && _.isArray(value))) {
                    gridColumn.formatter = {
                        format: "json2html", options: {jsonValuePath: key, htmlValuePath: key + "HTML", expandLevel: 0}
                    };
                    gridColumn.exportConfig = { allow: true, advFormatter: function(dc) { return JSON.stringify(dc[key]);}};
                    isFixedRowHeight = false;
                }

                gridColumns.push(gridColumn);
            }
        });

        return { columns: gridColumns, isFixedRowHeight: isFixedRowHeight };
    }

    function getColoumnWidth(value, key) {
        var type = value._type,
            keyLength = key.length,
            headerPixelFactor = 10, strPixelFactor = 15,
            columnWidth = keyLength * headerPixelFactor,
            maxWidth = 360, minWidth = 80, textValue, textValueLength;

        columnWidth = columnWidth < minWidth ? minWidth : columnWidth;<commit_msg>Related-Bug: #1657280 - Sanitizing the values for introspect page. For others its handled in the core.<commit_after>/*
 * Copyright (c) 2016
 * Juniper Networks, Inc. All rights reserved.
 */

define([
    "underscore",
    "contrail-view"
], function (_, ContrailView) {
    var IntrospectJSGridView = ContrailView.extend({
        el: $(window.contentContainer),

        render: function() {
            var self = this,
                viewConfig = self.attributes.viewConfig,
                introspectNode = viewConfig.node,
                introspectPort = viewConfig.port,
                modelMap = contrail.handleIfNull(self.modelMap, {}),
                jsonData = viewConfig.jsonData,
                jsGridViewConfig = getIntrospectJSGridViewConfig(jsonData, introspectNode, introspectPort);

            self.renderView4Config(self.$el, null, jsGridViewConfig, null, null, modelMap, null);
        }
    });

    function getIntrospectJSGridViewConfig(jsonData, introspectNode, introspectPort) {
        jsonData = (null != jsonData) ? jsonData : {};
        var sandeshData = parseData(jsonData),
            gridViewConfigs = [];

        _.each(sandeshData, function(value, key){
            gridViewConfigs.push({
                columns: [
                    {
                        elementId: "introspect-result-js-grid-" + introspectNode + "-" + introspectPort + "-" + key,
                        view: "GridView",
                        viewConfig: {
                            elementConfig: getIntrospectJSGridConfig(value)
                        }
                    }
                ]
            });
        });

        return {
            elementId: "introspect-js-grids",
            view: "SectionView",
            viewConfig: {
                rows: gridViewConfigs
            }
        };
    }

    function getIntrospectJSGridConfig(value) {
        var dataObj, gridData =  [], gridColumnsObj = {columns: []};

        if (isSandeshDataHavingObject(value)) {
            dataObj = parseDataObject(value.data);

            if (contrail.checkIfExist(dataObj)) {
                gridColumnsObj = createGridColumns(dataObj);
                gridData = createGridData(dataObj);
            }
        }

        return {
            header: {
                title: {text: value.title},
                defaultControls: {
                    collapseable: false,
                    columnPickable: true
                }
            },
            columnHeader: {columns: gridColumnsObj.columns},
            body: {
                options: {
                    forceFitColumns: gridColumnsObj.columns.length < 3 ? true : false,
                    checkboxSelectable: false,
                    fixedRowHeight: gridColumnsObj.isFixedRowHeight ? 30 : false
                },
                dataSource: {data: gridData}
            }
        };
    }

    function createGridColumns(dataObj) {
        var dataLength = dataObj.length,
            dataRecord = {}, gridColumns = [],
            isFixedRowHeight = true;

        if (!_.isArray(dataObj)) {
            dataRecord = dataObj;
        } else if (dataLength > 1) {
            dataRecord = dataObj[0];
        }

        _.each(dataRecord, function (value, key) {
            value = JSON.parse(cowu.sanitize(JSON.stringify(value)));
            if (key.charAt(0) !== "_" && ["more", "next_batch"].indexOf(key) === -1 ) {
                var gridColumn = {
                    id: key, field: key,
                    name: getLabel4Key(key),
                    width: getColoumnWidth(value, key),
                    sortable: true
                };

                if (contrail.checkIfExist(value._link)) {
                    gridColumn.formatter = function (r, c, v, cd, dc) {
                        return '<a class="introspect-link" data-link="' + value._link + '" ' +
                            'x="' + cowu.sanitize(dc[key]) + '">' + cowu.sanitize(dc[key]) + "</a>";
                    };
                    gridColumn.exportConfig = { allow: true };

                }

                if (_.contains(["list", "struct", "sandesh"], value._type) ||
                    (!contrail.checkIfExist(value._type) && _.isArray(value))) {
                    gridColumn.formatter = {
                        format: "json2html", options: {jsonValuePath: key, htmlValuePath: key + "HTML", expandLevel: 0}
                    };
                    gridColumn.exportConfig = { allow: true, advFormatter: function(dc) { return JSON.stringify(dc[key]);}};
                    isFixedRowHeight = false;
                }

                gridColumns.push(gridColumn);
            }
        });

        return { columns: gridColumns, isFixedRowHeight: isFixedRowHeight };
    }

    function getColoumnWidth(value, key) {
        var type = value._type,
            keyLength = key.length,
            headerPixelFactor = 10, strPixelFactor = 15,
            columnWidth = keyLength * headerPixelFactor,
            maxWidth = 360, minWidth = 80, textValue, textValueLength;

        columnWidth = columnWidth < minWidth ? minWidth : columnWidth;
{'fraction_non_alphanumeric': 0.1083670848562679, 'fraction_numerical': 0.0054331719845895484, 'mean_word_length': 1.5159045725646123, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '827579', 'n_tokens_mistral': 2771, 'n_tokens_neox': 2557, 'n_words': 511}