Reading data from influxDB in a nodeJS application? - node.js

can you help me with getting data out of an influx DB instance inside a nodeJS application?
I have tried https://github.com/node-influx/node-influx and https://github.com/vicanso/influxdb-nodejs with only little success.
const Influx = require('influxdb-nodejs');
//Database connection Influx
const client = new Influx('http://redacted/db');
exports.connectInflux = function () {
var date = new Date();
var timeTo = date.toISOString()
date.setDate(date.getDate() - 4)
var timeFrom = date.toISOString()
var reader = client.query('impulse')
.where('location', 'SA08')
.where('time', timeFrom, '>')
.where('time', timeTo, '<')
.then(function (data) {
console.info(data.results[0].series[0].values)
return data.results[0].series[0].values
})
.catch(console.error);
// I get the measurements
client.showMeasurements()
.then(console.info)
.catch(console.error)
}
With this code snippet, at least I can get the data in the console. But when trying to work with it, I only get an empty array. Can you spot any mistake here?
I am thankful for every hint!
Thanks!

Remember that by default, time must be in nanoseconds. You are using milliseconds.

Related

Nodejs stream.pipe performing asynchronously

I have a large xml which is a combination of xml documents. I'm trying to use a nodejs xml splitter and respond back with the number of documents i have found.
My code looks something like this. I'm looking to get the number of documents outside the function(in the last line). Is there something I can do to achieve this?
var XmlSplit = require('./xmlsplitter.js')
const fs = require('fs')
var xmlsplit = new XmlSplit()
var no_of_docs = 0
var inputStream = fs.createReadStream('./files/input/test.xml')
inputStream.pipe(xmlsplit).on('data', function(data,callback) {
var xmlDocument = data.toString();
no_of_docs = no_of_docs + 1;
})
inputStream.pipe(xmlsplit).on('end', function(){
console.log('Stream ended');
console.log(no_of_docs); <-- This prints the correct value but the value is lost as soon as we exit this.
});
console.log("This is outside the function " + no_of_docs); <-- I need the value here.

Improving performance of a firebase node js function

My node js code usually runs in less than 0.5seconds locally but when I deploy on firebase function it sometimes take up to 3 seconds to 5 seconds to return the response I want. is there a way to fix this perfomance issue? the problem is I do all the computation in a single request function because I need to parse text from that request and use it in my code but I couldn't figure out any other way to fix it. any suggestions would be appreciated and thanks in advance
exports.Response = functions.https.onRequest(async (req, res) => {
const original = req.query.text;
var returned = original.split(',');
var startLat= returned[0];
var startLong = returned[1];
var endLat = returned[2];
var endLong = returned[3];
var maxDistance = returned[4];
var output = binding.Main(startLat, startLong, endLat, endLong, Number(maxDistance));
var Response = output.split(' ');
var Array = [];
for(i = 0; i<Response.length ; i++)
{
Array.push(Response[i]);
}
var data = {};
data.table = [];
var obj =
{
Start: Array[0]
}
data.table.push(obj);
for (i=1; i < Array.length-1 ;i+=7)
{
var obj =
{
transportType: Array[i],
Price: Array[i+1],
Type: Array[i+2],
startLatitude: Array[i+3],
startLongitude: Array[i+4],
endLatitude: Array[i+5],
endLongitude: Array[i+6]
}
data.table.push(obj);
}
var obj = {
TotalPrice: Array[Array.length-1]
}
data.table.push(obj);
res.status(200).json({
data
})
});
I know code is unprofessional but am still new to node js and addons so I tried my best to get the required output. any suggestions/notes would be taken and appreciated. Also if any further code/explanation is required please let me know.
Helping Renaud Tarnec's reply to be posted as an answer:
The delay you encounter after deploying your Cloud function is most probably not caused by your code but by the cold start ("Functions are stateless, and the execution environment is often initialized from scratch, which is called a cold start"). See more details and possible improvement tips at: https://cloud.google.com/functions/docs/bestpractices/tips, https://youtube.com/watch?v=v3eG9xpzNXM, https://youtube.com/watch?v=IOXrwFqR6kY, https://medium.com/#duhroach/improving-cloud-function-cold-start-time-2eb6f5700f6

Compare two Firestore Timestamps in Cloud Functions

I'm writing update function in Firestore and I want to compare two Timestamp. I've tried multiple things but not working. Can you point me correct way of comparing two Timestamp in firestore.
exports.updateFunction = functions.firestore
.document('xyz/{xyzId}')
.onUpdate((change, context) => {
var updatedXYZ = change.after.data();
var oldXYZ = change.before.data();
var newTimestamp = updatedXYZ.timing;
var oldTimestamp = oldXYZ.timing;
// I've tried following things but not working
var result = newTimestamp === oldTimestamp; // not working
var result = new Date(newTimestamp) - new Date(oldTimestamp); // not working
return true;
});
I want to check two Timestamp is same or not.
Consult the API docs for Firestore's Timestamp object. Timestamp has a method called isEqual() that will compare two timestamps.
var result = newTimestamp.isEqual(oldTimestamp);
You need to add this line
admin.firestore().settings( { timestampsInSnapshots: true })
in your index.js file.
After adding this line cloud function will read your timestamp field as Timestamp Datatype. Now you can compare two timestamps using
var result = newTimestamp.isEqual(oldTimestamp);
//I assume you have yourTimestampInMilliseconds
const currentTimestamp = Date.now();
console.log("(currentTimestamp <= yourTimestampInMilliseconds)= "+ (currentTimestamp <= yourTimestampInMilliseconds));
console.log("(currentTimestamp > yourTimestampInMilliseconds)= "+ (currentTimestamp > yourTimestampInMilliseconds));

Bulk-delete items from a firebase database in node.js

I'm trying to delete all nodes with a date greater than '2017-04-05' with a bulk operation with a firebase function. Can you spot what I'm doing wrong here?
The 2 nodes that should get deleted are the ones in red:
Here's the code that is failing - can you see what's wrong? Also, I'm concerned about the performance of this (I'll only run it once in a while though). If there are millions of games in the list, should that concern me if I only run this once a day?
exports.remove = functions.https.onRequest((req, res) => {
const deleteBeforeDate = req.query.deleteBeforeDate;
var ref = admin.database().ref('games');
var keysToDelete = {};
for (var game in this.games) {
var date = items[i]['.date'];
if(date.value > '2017-04-05'){
keysToDelete[game.key] = null;
}
}
this.ref.update(keysToDelete);
});
Thank you very much,
Mike
To determine the keys to delete, you'll need to attach a listener. Since that is needed, you might as well create a query that selects the correct children and deletes only those:
var ref = admin.database().ref('games');
var deleteAfterDate = ref.orderByChild('date').startAt('2017-04-05');
deleteAfterDate.once('value').then(function(snapshot) {
var updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null;
});
ref.update(updates);
});

Mongoose Trying to open unclosed connection (callback hell)

I want to add a loop to the database records. But mongoose wrote that I did not close the open connection. Mongoose Trying to open unclosed connection. How to make the whole thing went in sync? Its callback hell in my code
app.get("/dobavit/", function(req, res) {
for(var i=50; i>0; i--)
{
InsertAXIXA("analitika",i,function(data){
});
}
res.end();
});
function InsertAXIXA(qwerty,page,callback){
mongoose.connect('mongodb://localhost/gazprom')
var parsedResults = [];
var req = request('http://xxx.ru/'+qwerty+"?page="+page, function (error, response, html) {
if (!error && response.statusCode == 200) {
// str = iconv.decode(html, 'utf-8');
var $ = cheerio.load(html);
$('.col-1 .col-first').each(function(i, element){
var buf = $(this);
var zagolovok = buf.children(0).children().children().eq(0).text();
var previewText = buf.children(2).children().eq(0).text();
var url = buf.children(0).children().children().eq(0).attr('href');
var picUrl = buf.children(1).children().eq(0).children().children().eq(0).attr('src');
var metadata = {
zagolovok:zagolovok,
previewText:previewText,
url:url,
typeOfnews:qwerty,
picUrl:picUrl,
qwerty:qwerty
};
var news =new News({
zagolovok: zagolovok,
previewText: previewText,
url:url,
picUrl:picUrl,
qwerty:qwerty
// created_at:Date.today()
});
news.save(function(err, news,affected){
});
parsedResults.push(metadata);
});
callback(parsedResults);
}
mongoose.connection.close()
});
You shouldn't actually need to open/close your connection on every request (see here for more about that).
Instead, you can just open your connection once when your app starts and then just let it close when the app closes.
If you leave the connection open, you can reuse the connections instead of wasting time/resources establishing a new one every time that function is called.
In my opinion, you are trying to create another connection without closing the current one. So, you might want to use:
createConnection() instead of connect().
In your case, it would look like this:
db = mongoose.createConnection('mongodb://localhost/mydb');
i was getting the same error today, the solution which i found is, we should not call the mongoose.connect function in loop or anywhere in the code which is being executed again and again.
so my example is, i was doing mongoose.connect on all request of app.
app.all("*",function(){
mongoose.connect();
})
Your code is somewhat similar to because in loop you are calling function and in function you are opening connection.
Thanks

Resources