Splunk jquery没有按顺序执行

时间:2017-12-28 17:35:16

标签: javascript jquery splunk

我正在使用Splunk中的简单XML仪表板来计算和显示新Splunk环境的基础设施要求。以下是代码的摘录,我无法做到:

    $(document).on('click', '#submitDemand', function(e) {
    e.preventDefault();
    //hostname, ip, storage, ram, os, component, cores
    indexers = indexernum($('[name="form_totalgb"]').val());
    console.log(indexers);
    shs = shnum($('[name="form_totalgb"]').val());
    console.log(shs);
    spinfraTableSearch = new SearchManager({
        id: "search2",
        search: "| inputlookup dm_splunk_infra_lookup| search demand_id=" + demand_id + " | table provisioned_hostname, provisioned_ip, splunk_component, cpu_cores, ram, storage_tb, os",
        preview: true,
        autostart: false,
        cache: true
    });
    spInfraCleanSearch = new SearchManager({
        id: "cleansearch",
        search: "| inputlookup dm_splunk_infra_lookup | eval key=_key | WHERE NOT demand_id=" + demand_id + " | outputlookup dm_splunk_infra_lookup",
        preview: true,
        autostart: false,
        cache: true
    });
    hf1search = new SearchManager({
        id: "hf1",
        search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"HeavyForwarder1\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Windows\"| eval splunk_component = \"Heavy Forwarder\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
        preview: true,
        autostart: false,
        cache: true
    });
    hf2search = new SearchManager({
        id: "hf2",
        search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"HeavyForwarder2\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Linux\"| eval splunk_component = \"Heavy Forwarder\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
        preview: true,
        autostart: false,
        cache: true
    });
    dssearch = new SearchManager({
        id: "ds",
        search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"DeploymentServer\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Linux\"| eval splunk_component = \"Deployment Server\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
        preview: true,
        autostart: false,
        cache: true
    });
    //spInfraTokens("DeploymentServer", "53.xx.xxx.xxx", "0.5 TB", "8 GB", "Linux", "Deployment Server", "8");

    if (indexers > 1) {
        cmsearch = new SearchManager({
            id: "cm",
            search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"ClusterMaster\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Linux\"| eval splunk_component = \"Cluster Master\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
            preview: true,
            autostart: false,
            cache: true
        });
    }
    for (i = 0; i < indexers; i++) {
        idxsearch[i] = new SearchManager({
            id: "idx" + i,
            search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"Indexer" + (i + 1) + "\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"" + ($('[name="form_totalstorage"]').val() / indexers) + " TB\"| eval ram = \"64 GB\"| eval os = \"Linux\"| eval splunk_component = \"Indexer\"| eval cpu_cores = \"24\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
            preview: true,
            autostart: false,
            cache: true
        });
    }
    //console.log(idxsearch);
    if (shs > 1) {
        depsearch = new SearchManager({
            id: "dep",
            search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"Deployer\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Linux\"| eval splunk_component = \"Deployer\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
            preview: true,
            autostart: false,
            cache: true
        });
    }
    for (i = 0; i < shs; i++) {
        shssearch[i] = new SearchManager({
            id: "shs" + i,
            search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"SearchHead" + (i + 1) + "\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"32 GB\"| eval os = \"Linux\"| eval splunk_component = \"Search Head\"| eval cpu_cores = \"16\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
            preview: true,
            autostart: false,
            cache: true
        });
    }
    spInfraCleanSearch.startSearch();
    spInfraCleanSearch.on('search:done', function() {
        console.log('cleanup completed');
        hf1search.startSearch();
        spInfraTokens();
    });
    hf1search.on('search:done', function() {
        console.log('hf1 completed');
        spinfraTableSearch.startSearch();
        hf2search.startSearch();
        spInfraTokens();
    });
    hf2search.on('search:done', function() {
        console.log('hf2 completed');
        spinfraTableSearch.startSearch();
        dssearch.startSearch();
        spInfraTokens();
    });
    dssearch.on('search:done', function() {
        console.log('ds completed');
        if (indexers > 1) {
            console.log('start cmsearch');
            spinfraTableSearch.startSearch();
            cmsearch.startSearch();
            cmsearch.on('search:done', function() {
                console.log('cm completed');
                console.log('start idxsearch[0]');
                spinfraTableSearch.startSearch();
                idxsearch[0].startSearch();
            });
        } else {
            console.log('start idxsearch[0]');
            spinfraTableSearch.startSearch();
            idxsearch[0].startSearch();
            idxsearch[0].on('search:done', function() {
                console.log('idx0 completed');
                console.log('start shssearch[0]');
                spinfraTableSearch.startSearch();
                shssearch[0].startSearch();
            });
        }
    });
    var i = 1;
    for (i = 1; i < indexers; i++) {
        console.log('in for register idx[' + (i - 1) + ']');
        idxsearch[i - 1].on('search:done', function() {
            console.log('idx' + (i - 1) + ' completed');
            console.log('start idxsearch[' + i + ']');
            spinfraTableSearch.startSearch();
            idxsearch[i].startSearch();
        });
    }
    if (shs > 1) {
        console.log('register idx[' + (i - 1) + ']');
        idxsearch[i - 1].on('search:done', function() {
            console.log('idx' + (i - 1) + ' completed');
            console.log('start depsearch');
            spinfraTableSearch.startSearch();
            depsearch.startSearch();
        });
        console.log('register deployer');
        depsearch.on('search:done', function() {
            console.log('deployer completed');
            console.log('start shssearch[0]');
            spinfraTableSearch.startSearch();
            shssearch[0].startSearch();
        });
    } else {
        console.log('register idx[' + (i - 1) + ']');
        idxsearch[i - 1].on('search:done', function() {
            console.log('idx' + (i - 1) + ' completed');
            console.log('start shssearch[0]');
            spinfraTableSearch.startSearch();
            shssearch[0].startSearch();
            console.log('register shs[0]');
            shssearch[0].on('search:done', function() {
                console.log('shs0 completed');
                spinfraTableSearch.startSearch();
                spInfraTokens();
            });
        });
    }
    var i = 1;
    for (i = 1; i < shs; i++) {
        console.log('register shs[' + (i - 1) + ']');
        shssearch[i - 1].on('search:done', function() {
            console.log('shs' + (i - 1) + ' completed');
            console.log('start shssearch[' + i + ']');
            spinfraTableSearch.startSearch();
            shssearch[i].startSearch();
        });
    }
    console.log('register shs[' + (i - 1) + ']');
    shssearch[i - 1].on('search:done', function() {
        console.log('shs' + (i - 1) + ' completed');
        spinfraTableSearch.startSearch();
        spInfraTokens();
    });

});
createSpInfra.on('search:done', function() {
    console.log('createspinfra completed');
    spinfraTableSearch.startSearch();
    tokens.unset('tok_sp_create');
});
spinfraTable.on('click', function(e) {
    e.preventDefault();
});

如您所知,单击提交时会调用此函数,并且我已经包含console.log()来调试代码,请原谅快速/脏的性质。对于变量indexersshs,值32会产生以下输出:

3
2
in for register idx[0]
in for register idx[1]
register idx[2]
register deployer
register shs[0]
register shs[1]
cleanup completed
hf1 completed
hf2 completed
ds completed
start cmsearch
cm completed
start idxsearch[0]
idx1 completed
start idxsearch[2]
idx1 completed
start depsearch
deployer completed
start shssearch[0]
shs1 completed
start shssearch[2]
Uncaught TypeError: Cannot read property 'startSearch' of undefined
    at constructor.<anonymous> (crud_set_types.js:564:18)
    at triggerEvents (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:5040)
    at constructor.trigger (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:3949)
    at Backbone.Model.done (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:341:24540)
    at triggerEvents (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:4974)
    at Backbone.Model.trigger (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:3949)
    at http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:383:6359
    at Array.forEach (<anonymous>)
    at Function._.each._.forEach (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:27:10608)
    at _.(anonymous function) [as each] (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:27:27948)
shs1 completed
start shssearch[2]
Uncaught TypeError: Cannot read property 'startSearch' of undefined
    at constructor.<anonymous> (crud_set_types.js:564:18)
    at triggerEvents (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:5040)
    at constructor.trigger (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:3949)
    at Backbone.Model.done (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:341:24540)
    at triggerEvents (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:4974)
    at Backbone.Model.trigger (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:3949)
    at http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:383:6359
    at Array.forEach (<anonymous>)
    at Function._.each._.forEach (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:27:10608)
    at _.(anonymous function) [as each] (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:27:27948)
shs1 completed
start shssearch[2]
and shs1/shssearch[2] keeps on repeating 

我无法弄清楚为什么在执行索引器循环时,即使注册了start idxsearch[1]idx[0]idx[1],也不会执行idx[2]

同样在shs块中,start shssearch[1]应该被称为start shssearch[2]。此外,shs for循环不应在i=2执行,但在此处变为无限循环。

对此事的任何帮助表示赞赏。几个星期以来我一直困在这里,至少重写了4次。

最诚挚的问候, NIKHIL

编辑:缩短版本:

     var i = 1;
for (i = 1; i < indexers; i++) {
    console.log('in for register idx[' + (i - 1) + ']');
    idxsearch[i - 1].on('search:done', function() {
        console.log('idx' + (i - 1) + ' completed');
        console.log('start idxsearch[' + i + ']');
        spinfraTableSearch.startSearch();
        idxsearch[i].startSearch();
    });
}

输出:     启动idxsearch [0]     idx1已完成     启动idxsearch [2] 问题:idx0 completedstart idxsearch[1]idx1 completed丢失。那些代码应该被执行

代码2:

    var i = 1;
for (i = 1; i < shs; i++) {
    console.log('register shs[' + (i - 1) + ']');
    shssearch[i - 1].on('search:done', function() {
        console.log('shs' + (i - 1) + ' completed');
        console.log('start shssearch[' + i + ']');
        spinfraTableSearch.startSearch();
        shssearch[i].startSearch();
    });

问题:缺少类似问题shs0 completedstart shssearch[1]。这是无限循环,i的值2应限制它。

0 个答案:

没有答案