Saturday 1 April 2017

using influxdb nodejs library in reactjs

I'm in the processes of switching out elasticsearch in a reactjs app with influxdb and am trying to use the influxdb node-js library (https://node-influx.github.io/class/src/index.js~InfluxDB.html) but am having some issues running a query.

My previous code looked like this which works(this action gets dispatch once the store is initialized):

export function loadHosts() {

    return function(dispatch) {
        console.log("elastic");
        return eSclient.search({

            index: "sdpjmx",
            type: "kafkajmx",
            body:  {
                    "size": 0,
                    "sort" : { "kafka_topic" : {"order" : "desc"}},
                    "aggs" : {
                        "hosts" : {
                            "terms" : {
                              "field" : "host",
                              "size": 500
                            }
                        }
                    },  
                    "query": {
                    "bool": {
                      "filter": [
                        {
                          "range": {
                            "@timestamp": {
                              "gte": "now-60s"
                            }
                          }
                        }
                        ]
                      }
                    }
                }

        }).then(hosts => {

                hosts = {host_selected: "Select Host", hosts}
                dispatch(loadHostsSuccess(hosts));

        }).catch(error => {
            throw(error);
        });
    };

}

and then I added in another promise just to get the influxdata and to just test to see if the call would actually happen...here is how I modified it:

export function loadHosts() {

    return function(dispatch) {
        console.log("elastic");
        return eSclient.search({

            index: "sdpjmx",
            type: "kafkajmx",
            body:  {
                    "size": 0,
                    "sort" : { "kafka_topic" : {"order" : "desc"}},
                    "aggs" : {
                        "hosts" : {
                            "terms" : {
                              "field" : "host",
                              "size": 500
                            }
                        }
                    },  
                    "query": {
                    "bool": {
                      "filter": [
                        {
                          "range": {
                            "@timestamp": {
                              "gte": "now-60s"
                            }
                          }
                        }
                        ]
                      }
                    }
                }

        }).then(hosts => {

            return influx.query(

                'select distinct(host) from jmx where time > now() - 180m'

            ).then(infltest => {            


                hosts = {host_selected: "Select Host", hosts}
                dispatch(loadHostsSuccess(hosts));

            }).catch(err => {
                throw(err);
            });

        }).catch(error => {
            throw(error);
        });
    };

}

When I do this, react-redux fails to send a state so the components are failing, so it looks like I'm not using the influxdb library right but I'm not able to figure out what I'm doing wrong.

I import the library and create the connection like this:

import * as Influx from 'influx';

const influx = new Influx.InfluxDB({
    host: "http://10.111.11.88:9050",
    database: "sdp_metrics"
})



via user2061886

No comments:

Post a Comment