rxjs - searching in observables - search

I am trying to implement a simple search scheme to search for values in one observable from another observable. The buildLookup function below builds a lookup table using values from an observable:
// Build lookup table from an observable.
// Returns a promise
function buildLookup(obs, keyName, valName) {
const map = new Map();
obs.subscribe((obj) => map.set(obj[keyName], obj[valName]));
// use concat to force wait until `obs` is complete
return obs.concat(Observable.from([map])).toPromise();
}
Then I have another function this uses the result of this function (a promise):
// Lookup in a previously built lookup table.
function lookup(source, prom, keyName, fieldName) {
return source.map((obj) => {
const prom2 = prom.then((map) => {
return lodash.assign({}, obj, { [fieldName]: map.get(String(obj[keyName])) });
});
return Observable.fromPromise(prom2);
})
.flatMap((x) => x);
}
For some reason, this implementation does not work, and every other lookup seems to fail. Could you someone guide me on:
what is wrong with this code, and
whether there is a better way to implement something like this?
Thanks a bunch in advance for your help!
I am attaching my test code below:
"use strict";
const lodash = require("lodash");
const rxjs = require("rxjs");
const chai = require("chai");
const Observable = rxjs.Observable;
const assert = chai.assert;
const assign = lodash.assign;
describe("search", () => {
it("simple search", (done) => {
let nextId = 1, nextId2 = 1;
const sourceObs = Observable.interval(5).take(5).map((i) => {
const id = nextId++;
return { id: `${id}` };
});
const searchableObs = Observable.interval(5).take(5).map((i) => {
const id = nextId2++;
return Observable.from([
{ id: `${id}`, code: "square", val: id * id },
]);
}).flatMap((x) => x);
const results = [];
const verifyNext = (x) => {
assert.isDefined(x);
results.push(x);
};
const verifyErr = (err) => done(err);
const verifyComplete = () => {
assert.equal(results.length, 5);
try {
results.forEach((r) => {
console.log(r);
// assert.equal(r.val, r.id*r.id); <== *** fails ***
});
} catch (err) {
done(err);
}
done();
};
// main
const lookupTbl = buildLookup(searchableObs, "id", "val"); // promise that returns a map
lookup(sourceObs, lookupTbl, "id", "val")
.subscribe(verifyNext, verifyErr, verifyComplete)
;
});
});
// output
// { id: '1', val: 1 }
// { id: '2', val: undefined }
// { id: '3', val: 9 }
// { id: '4', val: undefined }
// { id: '5', val: 25 }

So, a bunch of things to address here.
The main issue is that you are doing side-effects in your sourceObs and searchableObs observables, and it is not published, so the side-effects happen multiple times because you subscribe multiple times, giving you a wrong map entirely. For instance, I get maps like:
{"1" => 1, "4" => 16, "7" => 49, "12" => 144}
But you are doing something so trivial that you should really not use mutable variables.
To solve this, here is how you can create the proper observables:
const sourceObs = Rx.Observable.range(1, 5).map(i => ({ id: `${i}` }));
const searchableObs = Rx.Observable.range(1, 5).map(i =>
({ id: `${i}`, code: "square", val: i * i })
);
There is no reason to use a variable since range returns the numbers 1, 2, ...
And your use of o.map(_ => Rx.Observable.from(...)).concatMap(e => e) is really just the same as o...
While I'm here, this is a simplified version of your correct but clumsy functions:
// so buildLookup just needs to return a map once it's finished populating it
function buildLookup(obs, keyName, valName) {
// following your style here, though this could be done using `scan`
const map = new Map();
obs.subscribe((obj) => map.set(obj[keyName], obj[valName]));
// instead of your promise, I just wait for `obs` to complete and return `map` as an observable element
return obs.ignoreElements().concat(Rx.Observable.of(map));
}
// and lookup just needs to wait for the map, and then populate fields in the object
function lookup(source, prom, keyName, fieldName) {
return prom
.concatMap(map => source.map(obj => ({ obj: obj, map: map })))
.map(({ obj, map }) => lodash.assign({}, obj, { [fieldName]: map.get(String(obj[keyName])) }))
;
}
This should work for you.

Related

async function doesn't wait of inside await in nodejs

I am implementing function monthlyRevenue.
Simply, it will return total monthly revenue,and it takes arguments of station array which will make revenues, month and year.
Problem
Inside of this function I have getStationPortion which will fetch the revenue portion of user's.
So I would like to make it return object like this.
stationsPortion = {station1 : 30, station2 : 20}
In the monthlyRevenue
const stationPortions = await getStationPortions(stations)
console.log("portion map", stationPortions //it will be shown very beginning with empty
getStationPortions
const getStationPortions = async (stations) => {
let stationPortions = {}
stations.map(async (value) => {
const doc = await fdb.collection('Stations').doc(value).get()
if (!doc.exists) {
console.log("NO DOC")
} else {
stationPortions[value] = doc.data().salesPortion
console.log(stationPortions) //it will be shown at the last.
}
})
return stationPortions
}
I thought that async function should wait for the result, but it does not.
I am kind of confusing if my understanding is wrong.
Thank you
(by the way, fdb is firebase admin(firestore)
Working code
const getStationPortions = async (stations) => {
let stationPortions = {}
await Promise.all(stations.map(async (value) => {
const doc = await fdb.collection('Stations').doc(value).get()
if (!doc.exists) {
console.log("NO DOC")
} else {
stationPortions[value] = doc.data().salesPortion
console.log(stationPortions)
}
}))
return stationPortions
}
module.exports = router;

Synchronising Lodash and mongoose query in nodejs

I am trying to accomplish synchronisation in Mongoose query inside a _.each lodash function like this -
let commentContainer = [];
let comments = {..} //json object having comments
_.each(comments, comment => {
User.findOne({_id: comment.createdBy}).exec()
.then(function(commentor){
var c = {
text: comment.text,
votes: comment.votes.length,
commentor: {
name: commentor.name,
profilePhoto: commentor.profilePhoto,
id: commentor._id
}
}
commentContainer.push(c);
});
});
}
console.log(commentContainer); //it shows []
How can I achieve it, I tried using setTimeout function by giving a delay but it does not seems a valid procedure.
modify your code like this way:
let fun = async() => {
let commentContainer = [];
let comments = {..} //json object having comments
await _.each(comments, comment => {
User.findOne({_id: comment.createdBy}).exec()
.then(function(commentor){
var c = {
text: comment.text,
votes: comment.votes.length,
commentor: {
name: commentor.name,
profilePhoto: commentor.profilePhoto,
id: commentor._id
}
}
commentContainer.push(c);
});
});
}
console.log(commentContainer); //it shows []
}
make your function async and use await keywoed when you need to wait for the process to complete before next iteration
It's because Node.js is asynchronous.You should use async/await or promise or callback when you are dealing with non-blocking call like DB-call or Http client call.
let comments = {..} //json object having comments
console.log(findUSer(comments)); // print result
async function findUSer(comments){
let commentContainer = [];
await _.each(comments, comment => {
User.findOne({_id: comment.createdBy}).exec()
.then(function(commentor){
var c = {
text: comment.text,
votes: comment.votes.length,
commentor: {
name: commentor.name,
profilePhoto: commentor.profilePhoto,
id: commentor._id
}
}
commentContainer.push(c);
});
});
}
return commentContainer;
}

stub never called with sinon and nodejs using chai-as-promised

i'm facing a issue with my unit test, stuck completely, the code is simple, please need to understand what's going on, my stub is never called, the set seems to be correct, here the code:
let strategy = fixtures.load('strategy')
chai.use(chaiAsPromised)
describe.only('Spawn Order Job', () => {
let getPositionsStub, createJobStub, daoStub,sandbox
beforeEach(()=>{
sandbox = sinon.createSandbox()
daoStub = sandbox.stub(dao, 'updateActiveOrders').resolves(true) //async
getPositionsStub = sandbox.stub(strategyModule, 'getPositions') //sync
createJobStub = sandbox.stub(helpers, 'createJob') //sync
createJobStub.returns(true)
getPositionsStub.resolves([{fake:'t'}, {fake:'t'}])
})
afterEach(()=>{
sandbox.restore()
})
//OK
it('Should failed with no param, type error context', ()=> {
const promise = spawnOrderJob()
expect(promise).to.be.rejectedWith(TypeError)
})
//OK
it('Should throw error timeout order', () => {
getPositionsStub.resolves([{fake:'t'}, {fake:'t'}])
strategy.lastDateOrder = new Date()
const ctx = { state: {strategy, dashboard, position:null}}
const action = {b: true, s: false}
const promise = spawnOrderJob(action, ctx)
expect(getPositionsStub.called).to.be.true
expect(daoStub.called).to.be.false
expect(createJobStub.called).to.be.false
expect(promise).to.be.rejectedWith(ORDER_ERROR, 'Timeout between order not expired.')
})
//KO stub never called
it.only('Should pass validation on buy', () => {
strategy.lastDateOrder = 0
const ctx = { state: {strategy, dashboard, position: null }}
const action = {b: true, s: false}
const promise = spawnOrderJob(action, ctx)
expect(promise).to.be.fulfilled
expect(getPositionsStub.called).to.be.true //ok
expect(createJobStub.called).to.be.true //never callled ????
expect(daoStub.called).to.be.true //never called ????
})
})
Want to understand what's going now there, the call are correct imo, running with mocha 5.2
Helpers.js : function is described as follow:
async function spawnOrderJob(action, ctx) {
try {
const { strategy, dashboard, position } = ctx.state
const {b, s} = action
//check in case strategy context
if (strategy) {
//pass validation buy contnext
if (b) {
//this stub is working
const positions = await strategyModule.getPositions(ctx)
const { maxPosition } = strategy.allocatedBTC
const { activeOrders, maxActiveOrders, timeBetweenOrder, lastDateOrder } = strategy
debug('Active orders:', strategy.activeOrders)
debug('Position:', positions.length)
if (activeOrders >= maxActiveOrders)
throw new ORDER_ERROR('Max active orders reach.')
if (positions.length + activeOrders >= maxPosition)
throw new ORDER_ERROR('Max positions reach.')
if (!timeoutExpired(lastDateOrder, timeBetweenOrder))
throw new ORDER_ERROR('Timeout between order not expired.')
//increment active orders counter
//stub fail, but not called at all
await dao.updateActiveOrders(strategy, true)
}
//Sell context
if (s) {
if (!position)
throw new ORDER_ERROR('No position to sell')
}
}
//stub fail, but called internally
return createJob(constants.DASHBOARD_CREATE_ORDER, {
orderType: b ? 'BUY' : 'SELL',
title: `Strategy create order ( ${ b ? 'BUY' : 'SELL'} )`,
strategy,
dashboard,
position
})
} catch (e) {
throw e
}
}
function createJob(name, data){
//shortcut queue.create (kue.js)
return queue.c(name,data)
}
module.exports = {
createJob,
spawnOrderJob
}
DAO
const updateActiveOrders = async (strategy, increment) => {
try {
const s = await model.findOne({_id: strategy._id})
if (!s) throw new Error('Strategy not found.')
s.activeOrders = increment ? s.activeOrders+1 :s.activeOrders-1
s.lastDateOrder = new Date()
return await s.save()
}catch(e){
throw e
}
}
module.exports = {updateActiveOrders}

ESOCKETTIMEDOUT Cloud Functions for Firebase

I am using Cloud Functions for Firebase together with my Firebase Realtime Database in order to do some data management for my app.
One of my functions though seems to get terminated since it takes about 100-150 seconds to complete. This happens with error : ESOCKETTIMEDOUT.
Is there a way to prevent this?
Here is my function:
function getTopCarsForUserWithPreferences(userId, genres) {
const pathToCars = admin.database().ref('cars');
pathTocars.orderByChild("IsTop").equalTo(true).once("value").then(function(snapshot) {
return writeSuggestedCars(userId, genres, snapshot);
}).catch(reason => {
console.log(reason)
})
}
function writeSuggestedCars(userId, genres, snapshot) {
const carsToWrite = {};
var snapCount = 0
snapshot.forEach(function(carSnapshot) {
snapCount += 1
const carDict = carSnapshot.val();
const carGenres = carDict.taCarGenre;
const genre_one = genres[0];
const genre_two = genres[1];
if (carGenres[genre_one] === true ||carGenres[genre_two] == true) {
carsToWrite[carSnapshot.key] = carDict
}
if (snapshot.numChildren() - 1 == snapCount) {
const pathToSuggest = admin.database().ref('carsSuggested').child(userId);
pathToSuggest.set(carsToWrite).then(snap => {
}).catch(reason => {
console.log(reason)
});
}
});
}
The getTopCarsForUserWithPreferences gets called when a user adds preferences. Also the cars table has about 50k entries.
Well you need to return everytime you use a async task.
Edit: you return 'writeSuggestedCars' but I think it never returns a value. I do not have a compiler, but I thought it was return Promise.resolved(). Can you insert it where I putted 'HERE'?
Maybe this will work:
function getTopCarsForUserWithPreferences(userId, genres) {
const pathToCars = admin.database().ref('cars');
return pathTocars.orderByChild("IsTop").equalTo(true).once("value").then(function(snapshot) {
return writeSuggestedCars(userId, genres, snapshot);
}).catch(reason => {
console.log(reason)
})
}
function writeSuggestedCars(userId, genres, snapshot) {
const carsToWrite = {};
var snapCount = 0
snapshot.forEach(function(carSnapshot) {
snapCount += 1
const carDict = carSnapshot.val();
const carGenres = carDict.taCarGenre;
const genre_one = genres[0];
const genre_two = genres[1];
if (carGenres[genre_one] === true ||carGenres[genre_two] == true) {
carsToWrite[carSnapshot.key] = carDict
}
if (snapshot.numChildren() - 1 == snapCount) {
const pathToSuggest = admin.database().ref('carsSuggested').child(userId);
return pathToSuggest.set(carsToWrite).then(snap => {
// 'HERE' I think return promise/Promise.resolve() will work
}).catch(reason => {
console.log(reason)
});
}
});
}

sequelize.js - Find by id and return result

I have a function,
var findUserDevice = function(userDeviceId){
var device = db.DeviceUser.find({
where: {
id: userDeviceId
}
}).then(function(device) {
if (!device) {
return 'not find';
}
return device.dataValues;
});
};
but this function does not return anything...
var UserDevice = findUserDevice(req.body.deviceUserId);
console.log(UserDevice);// undefined
The operation you are trying to do is async, which means that you need to use a callback. Since sequelize is build on top of Promises, you should actually write your code like this :
var findUserDevice = function(userDeviceId){
// return the promise itself
return db.DeviceUser.find({
where: {
id: userDeviceId
}
}).then(function(device) {
if (!device) {
return 'not find';
}
return device.dataValues;
});
};
And later use it like :
findUserDevice(req.body.deviceUserId).then( function(UserDevice) {
console.log(UserDevice);
});
It's 2020, async & await are becoming more popular. You can change your code to:
const findUserDevice = async function (userDeviceId) {
const device = await db.DeviceUser.findOne({
where: {
id: userDeviceId
}
});
if (device === null) {
return 'device not found';
}
return device.dataValues;
};
(async () => {
// ...
const UserDevice = await findUserDevice(req.body.deviceUserId);
console.log(UserDevice);
// ...
})()
IMHO, the code above is way more readable.
If you are getting undefined instead of 'not find' on the console, it means your function is returning a value. The problem might be dataValues is actually undefined. You need to check for the content of device.
Hint: Try returning just device or device.id
PS. If you want to do the search based on id, should go for findById() function of your model.
var device = db.DeviceUser.findById(userDeviceId).then(function(device) {
if (!device) {
return 'not find';
}
return device.dataValues;
});
This function received params id, this worker for me:
const { customer } = require('../models');
const get = async function(req, res){
let id = req.params.id;
[err, singleCustomer] = await to(customer.findByPk(id, { raw : true }));
return ReS(res, { message :'Obtener cliente: : ', data : JSON.stringify(singleCustomer) });
}

Resources