Use http request to to query all cloudant databases and then establish which are used for the historical data connection in the Watson IoT Platform and query those

This flow uses the http request node to query a cloudant service for all known databases. Then is parses out those databases matching the naming convention for Watson IoT historical databases. It uses node.send to query each of those databases for the first record in the by-deviceId view.

It requires that you enter your cloudant URL and basic authentication credentials in the http request nodes. These can be found in the Credentials of your cloudant service.

Further details on the configuration of Watson IoT for historical data can be found here:

Configure Cloudant NoSQL DB as Historian Data Storage for IBM Watson IoT

Further details on the Cloudant api: https://docs.cloudant.com/api.html

[{"id":"b6e503ad.b7d59","type":"inject","z":"b4aeef03.c16228","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":88.20001220703125,"y":414.2000274658203,"wires":[["7ee1b81d.caf718"]]},{"id":"7ee1b81d.caf718","type":"http request","z":"b4aeef03.c16228","name":"List all Cloudant DBs","method":"GET","ret":"obj","url":"https://<Replace with correct value>/_all_dbs","x":341.2000732421875,"y":411,"wires":[["f1683a52.70d1f8"]]},{"id":"f1683a52.70d1f8","type":"function","z":"b4aeef03.c16228","name":"Parse out Device DBs and Query them","func":"var dbs = msg.payload;\n\n//I am only interested in DBs with iotp in the name\nvar regex = /iotp/;\n//Then I need to exclude the configuration db\nvar regex2 = /configuration/;\n\n//Loop though the DBs calling the query one by one.\n//Be cautious of this if you have a lot of DBs\nfor (var i=0; i < dbs.length ; i++){\n    //check if it has iotp in the name\n    if (regex.test(dbs[i])){\n        //check if it has configuration in the name\n        if (regex2.test(dbs[i]) === false){\n            msg.db=dbs[i];\n            //async call with just the dbname\n            node.send({db:dbs[i]});\n        }\n    }\n}\n\n","outputs":1,"noerr":0,"x":470.800048828125,"y":492.60003662109375,"wires":[["ec8900e2.314ec","82ecb617.fbb918"]]},{"id":"82ecb617.fbb918","type":"http request","z":"b4aeef03.c16228","name":"Query DB by-deviceID for 1 Doc in each DB","method":"GET","ret":"obj","url":"https://<Replace with correct value>/{{{db}}}/_design/iotp/_view/by-deviceId?limit=1","x":873.5,"y":492.4000244140625,"wires":[["6a6f8c01.53af24"]]}]

Flow Info

Created 8 years ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • function (x1)
  • http request (x2)
  • inject (x1)

Tags

  • cloudant
  • watson-iot
  • node.send
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option