<commit_before>Ext.ns("org.systemsbiology.addama.js");

org.systemsbiology.addama.js.DatasourcesView = Ext.extend(Object, {
    constructor: function(config) {
        Ext.apply(this, config);

        org.systemsbiology.addama.js.DatasourcesView.superclass.constructor.call(this);

        this.loadTree();
        this.loadDatabases();
    },

    loadTree: function() {
        this.rootNode = new Ext.tree.AsyncTreeNode();

        this.treePanel = new Ext.tree.TreePanel({
            title: "Datasources",
            region:"west",
            split: true,
            minSize: 150,
            autoScroll: true,
            border: true,
            margins: "5 0 5 5",
            width: 275,
            maxSize: 500,
            // tree-specific configs:
            rootVisible: false,
            lines: false,
            singleExpand: true,
            useArrows: true,
            loader: new Ext.tree.TreeLoader(),
            root: this.rootNode
        });
        this.treePanel.on("expandnode", this.expandNode, this);
        this.treePanel.on("click", this.selectTable, this);

        var dataPanel = {
            margins: "5 5 5 0",
            layout: "border",
            region: "center",
            border: true,
            split: true,
            items:[
                new Ext.Panel({
                    title: "Query",
                    region: "north",
                    contentEl: "container_sql",
                    width:810,
                    bbar: new Ext.Toolbar({
                        items: [
                            { text: "Show Results", handler: this.queryHtml, scope: this },
                            { text: "Export to CSV", handler: this.queryCsv, scope: this },
                            { text: "Export to TSV", handler: this.queryTsv, scope: this }
                        ]
                    })
                }),
                { title: "Results", region: "center", contentEl: "container_preview", width:810, height: 400 }
            ]
        };

        this.mainPanel = new Ext.Panel({
            title: "Main",
            contentEl: this.contentEl,
            margins: "5 5 5 0",
            layout: "border",
            border: true,
            split: true,
            items:[
                this.treePanel,
                dataPanel
            ]
        });
    },

    loadDatabases: function() {
        Ext.Ajax.request({
            url: "/addama/datasources",
            method: "GET",
            success: function(o) {
                var json = Ext.util.JSON.decode(o.responseText);
                if (json && json.items) {
                    Ext.each(json.items, function(database) {
                        database.isDb = true;
                    });
                    this.addNodes(this.rootNode, json.items);
                }
            },
            failure: this.displayFailure,
            scope: this
        });
    },

    expandNode: function(node) {
        if (node.id == "addamatreetopid") {
            return;
        }

        Ext.Ajax.request({
            url: node.id,
            method: "GET",
            success: function(o) {
                var json = Ext.util.JSON.decode(o.responseText);
                if (json && json.items) {
                    this.addNodes(node, json.items);
                }
            },
            scope: this,
            failure: function() {
                node.expandable = false;
            }
        });
    },

    addNodes: function(node, items) {
        var parentPath = "/";
        if (!node.isRoot) {
            parentPath = node.attributes.path;
        }

        Ext.each(items, function(item) {
            if (!this.treePanel.getNodeById(item.name)) {
                item.text = item.label ? item.label : item.name;
                item.id = parentPath + "/" + item.name;
                item.path = parentPath + "/" + item.name;
                item.leaf = node.attributes.isTable;
                item.isTable = node.attributes.isDb;
                if (item.leaf) {
                    item.cls = "file";
                } else {
                    item.cls = "folder";
                    item.children = [];
                }
                if (item.datatype) {
                    item.text = item.name + " [" + item.datatype + "]";
                }
                node.appendChild(item);
            }
        }, this);
        node.renderChildren();
    },

    displayFailure: function(o) {
        org.systemsbiology.addama.js.Message.error("Datasources", "Error Loading: " + o.statusText);
    },

    selectTable: function(node) {
        if (node.attributes.isTable) {
            this.selectedTable = node;
            org.systemsbiology.addama.js.Message.show("Datasources", "Table Selected: " + node.attributes.text);
        }
    },

    queryHtml: function() {
        if (!this.isReadyToQuery()) {
            return;
        }

        var tableUri = this.selectedTable.id;
        var childNodes = this.selectedTable.childNodes;
        var querySql = Ext.getDom("textarea_sql").value;

        Ext.Ajax.request({
            url: tableUri + "/query",
            method: "GET",
            params: {
                tq: querySql,
                tqx: "out:json_array"
            },
            success: function(o) {
                var data = Ext.util.JSON.decode(o.responseText);
                if (data) {
                    org.systemsbiology.addama.js.Message.show("Datasources", "Retrieved " + data.length + " Records");

                    var fields = [];
                    var columns = [];
                    Ext.each(childNodes, function(childNode) {
                        fields.push(childNode.name);
                        // TODO : Insert data type
                        columns.push({<commit_msg>added fetching nodes on selected table<commit_after>Ext.ns("org.systemsbiology.addama.js");

org.systemsbiology.addama.js.DatasourcesView = Ext.extend(Object, {
    constructor: function(config) {
        Ext.apply(this, config);

        org.systemsbiology.addama.js.DatasourcesView.superclass.constructor.call(this);

        this.loadTree();
        this.loadDatabases();
    },

    loadTree: function() {
        this.rootNode = new Ext.tree.AsyncTreeNode();

        this.treePanel = new Ext.tree.TreePanel({
            title: "Datasources",
            region:"west",
            split: true,
            minSize: 150,
            autoScroll: true,
            border: true,
            margins: "5 0 5 5",
            width: 275,
            maxSize: 500,
            // tree-specific configs:
            rootVisible: false,
            lines: false,
            singleExpand: true,
            useArrows: true,
            loader: new Ext.tree.TreeLoader(),
            root: this.rootNode
        });
        this.treePanel.on("expandnode", this.expandNode, this);
        this.treePanel.on("click", this.selectTable, this);

        var dataPanel = {
            margins: "5 5 5 0",
            layout: "border",
            region: "center",
            border: true,
            split: true,
            items:[
                new Ext.Panel({
                    title: "Query",
                    region: "north",
                    contentEl: "container_sql",
                    width:810,
                    bbar: new Ext.Toolbar({
                        items: [
                            { text: "Show Results", handler: this.queryHtml, scope: this },
                            { text: "Export to CSV", handler: this.queryCsv, scope: this },
                            { text: "Export to TSV", handler: this.queryTsv, scope: this }
                        ]
                    })
                }),
                { title: "Results", region: "center", contentEl: "container_preview", width:810, height: 400 }
            ]
        };

        this.mainPanel = new Ext.Panel({
            title: "Main",
            contentEl: this.contentEl,
            margins: "5 5 5 0",
            layout: "border",
            border: true,
            split: true,
            items:[
                this.treePanel,
                dataPanel
            ]
        });
    },

    loadDatabases: function() {
        Ext.Ajax.request({
            url: "/addama/datasources",
            method: "GET",
            success: function(o) {
                var json = Ext.util.JSON.decode(o.responseText);
                if (json && json.items) {
                    Ext.each(json.items, function(database) {
                        database.isDb = true;
                    });
                    this.addNodes(this.rootNode, json.items);
                }
            },
            failure: this.displayFailure,
            scope: this
        });
    },

    expandNode: function(node) {
        if (node.id == "addamatreetopid") {
            return;
        }

        Ext.Ajax.request({
            url: node.id,
            method: "GET",
            success: function(o) {
                var json = Ext.util.JSON.decode(o.responseText);
                if (json && json.items) {
                    this.addNodes(node, json.items);
                }
            },
            scope: this,
            failure: function() {
                node.expandable = false;
            }
        });
    },

    addNodes: function(node, items) {
        var parentPath = "/";
        if (!node.isRoot) {
            parentPath = node.attributes.path;
        }

        Ext.each(items, function(item) {
            if (!this.treePanel.getNodeById(item.name)) {
                item.text = item.label ? item.label : item.name;
                item.id = parentPath + "/" + item.name;
                item.path = parentPath + "/" + item.name;
                item.leaf = node.attributes.isTable;
                item.isTable = node.attributes.isDb;
                if (item.leaf) {
                    item.cls = "file";
                } else {
                    item.cls = "folder";
                    item.children = [];
                }
                if (item.datatype) {
                    item.text = item.name + " [" + item.datatype + "]";
                }
                node.appendChild(item);
            }
        }, this);
        node.renderChildren();
    },

    displayFailure: function(o) {
        org.systemsbiology.addama.js.Message.error("Datasources", "Error Loading: " + o.statusText);
    },

    selectTable: function(node) {
        if (node.attributes.isTable) {
            this.selectedTable = node;
            org.systemsbiology.addama.js.Message.show("Datasources", "Table Selected: " + node.attributes.text);
            this.expandNode(this.selectedTable);
        }
    },

    queryHtml: function() {
        if (!this.isReadyToQuery()) {
            return;
        }

        var tableUri = this.selectedTable.id;
        var childNodes = this.selectedTable.childNodes;
        var querySql = Ext.getDom("textarea_sql").value;

        Ext.Ajax.request({
            url: tableUri + "/query",
            method: "GET",
            params: {
                tq: querySql,
                tqx: "out:json_array"
            },
            success: function(o) {
                var data = Ext.util.JSON.decode(o.responseText);
                if (data) {
                    org.systemsbiology.addama.js.Message.show("Datasources", "Retrieved " + data.length + " Records");

                    var fields = [];
                    var columns = [];
                    Ext.each(childNodes, function(childNode) {
                        fields.push(childNode.name);
                        // TODO : Insert data type
                        columns.push({
{'fraction_non_alphanumeric': 0.11871176070700204, 'fraction_numerical': 0.005098572399728076, 'mean_word_length': 1.2353276353276352, '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': '1463342', 'n_tokens_mistral': 3245, 'n_tokens_neox': 3034, 'n_words': 605}