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!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...