Dashboards & Visualizations

Javascript SDK oneshotsearch , unable to fecth two query

yitian92
New Member
var http = new splunkjs.ProxyHttp("/proxy");

        // Create a Service instance and log in 
        var service = new splunkjs.Service(http, {
            username: "admin",
            password: "",
            scheme: "https",
            host: "localhost",
            port:"8089",
            version:"7.1.0"
        });
        var searchParams = {
                    exec_mode:"normal",
        earliest_time: "2011-06-19T12:00:00.000-07:00"
        };

    function displayApps() {

        service.search(
  "search * | stats count by DPT | sort -count | head 10",
  searchParams,
  function(err, job) {

    // Display the job's search ID
    console.log("Job SID: ", job.sid);

    // Poll the status of the search job
    job.track({period: 200}, {
      done: function(job) {
        console.log("Done!");

        // Print out the statics
        console.log("Job statistics:");
        console.log("  Event count:  " + job.properties().eventCount); 
        console.log("  Result count: " + job.properties().resultCount);
        console.log("  Disk usage:   " + job.properties().diskUsage + " bytes");
        console.log("  Priority:     " + job.properties().priority);

        // Get the results and print them
        job.results({}, function(err, results, job) {
          var fields = results.fields;
          var rows = results.rows;
          for(var i = 0; i < rows.length; i++) {
            var values = rows[i];
            console.log("Row " + i + ": ");
            for(var j = 0; j < values.length; j++) {
              var field = fields[j];
              var value = values[j];
              console.log("  " + field + ": " + value);
            }
          }
        });

      },
      failed: function(job) {
        console.log("Job failed")
      },
      error: function(err) {
        done(err);
      }
    });

  }
);


service.search(
  "search * | stats count by SRC | sort -count | head 10",
  searchParams,
  function(err, job) {

    // Display the job's search ID
    console.log("Job SID: ", job.sid);

    // Poll the status of the search job
    job.track({period: 200}, {
      done: function(job) {
        console.log("Done!");

        // Print out the statics
        console.log("Job statistics:");
        console.log("  Event count:  " + job.properties().eventCount); 
        console.log("  Result count: " + job.properties().resultCount);
        console.log("  Disk usage:   " + job.properties().diskUsage + " bytes");
        console.log("  Priority:     " + job.properties().priority);

        // Get the results and print them
        job.results({}, function(err, results, job) {
          var fields = results.fields;
          var rows = results.rows;
          for(var i = 0; i < rows.length; i++) {
            var values = rows[i];
            console.log("Row " + i + ": ");
            for(var j = 0; j < values.length; j++) {
              var field = fields[j];
              var value = values[j];
              console.log("  " + field + ": " + value);
            }
          }
        });

      },
      failed: function(job) {
        console.log("Job failed")
      },
      error: function(err) {
        done(err);
      }
    });

  }
);

}
    document.getElementById("cb").innerHTML=displayApps();

For this code, all my result in the console will be my 2nd query. Seem like my frist query result has been replace.

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...