Rethinkdb group sequence by N elements - rethinkdb

I have a sequence inside a document
{
errorTicker: [1,2,3,4,5,6,7,8,9]
}
I would like to do a group by N elements and get
for N = 3
{
errorTicker: [[1,2,3],[4,5,6],[7,8,9]]
}
What would have been ideal solution:
r.expr([1,2,3,4,5,6,7,8,9]).group(function(entry, index){
return r.expr(index).div(3).coerceTo('string').split('.')(0);
})
but group does not accept current index, only the entry being processed...
What magic could I use here to achieve the desired result?
P.S. order of the sequence is important to me i.e. group [1,2,3] is not the same as [1,3,5]

I'm almost on the right track, thanks to .range() command
Here is the intermediate version i'm working on right now
r.expr({tickers: [9,6,3,9,6,7,8,1,2]})
.do(function (e){
return r.range(e('tickers').count())
.map(function(index){
return [index, e('tickers')(index)]
})
.group(function(indexValue){
return r.expr(indexValue(0)).div(3).coerceTo('string').split('.')(0);
})
})
The final version that seems to do what I need:
r.expr({tickers: [9,6,3,9,6,7,8,1,2]})
.do(function (e){
return r.range(e('tickers').count())
.map(function(index){
return [index, e('tickers')(index)]
})
.group(function(indexValue){
return r.expr(indexValue(0)).div(3).coerceTo('string').split('.')(0);
})
.ungroup()
.map(function(e){
return e('reduction').map(function(e){
return e(1)
})
})
})
Would appreciate any improvements ;)
updated: to get more efficience we can replace
r.expr(indexValue(0)).div(3).coerceTo('string').split('.')(0)
with
r.expr(index).sub(r.expr(index).mod(3)).div(3);
P.S. rethinkdb totally rocks! it's the most powerful and intuitive NoSQL database I've ever used :)

Related

Cypress Custom Command - Break from a .each() loop and return a value to be used in further steps

I am new to Cypress [3 days :)]. I am creating a custom command which is trying to find the column index of column in a table.
I want to return the column index and store it in a variable and use it in future steps.
Here is what I have tried:
Cypress.Commands.add('get_table_column_index', (columnName) => {
cy.getIframeBody().find('table').within(() => {
cy.get('tr th[scope=col]').each((element, index) => {
let colName = element.text();
cy.log(index, colName);
if(colName == columnName)
{
cy.wrap(index).as('index');
return false;
}
});
});
});
let columnIndex = cy.get_table_column_index('ColName').get('#index');
cy.log('index' + columnIndex);
index is [object Object]
Can someone please guide me in resolving this issue?
What am I doing wrong?
I can see that wrap is returning the correct index value but I am not sure how to extract and use it.
======= Update =========
I tried this and looks like it was able to log the correct value:
let columnIndex =
cy.get_table_column_index(column_name).get('#index');
columnIndex.then(index => cy.log(index));
Now, I am trying to expand it to use the index value in the next step like this:
Cypress.Commands.add('get_text', (row_num, column_name) => {
let txt;
cy.test_index(column_name).get('#index').then(index => {
let element = cy.get('main > table').within(() => {
cy.log('${index');
cy.get('tr:nth-child(${row_num}) td:nth-child(${index})');
//txt = element.text();
cy.log(txt);
});
});
return txt;
})
I am not able to figure out how to use the value of index and row_num in cy.get('tr:nth-child(${row_num}) td:nth-child(${index})'; expression.
Error: Syntax error, unrecognized expression: :nth-child
All you need to do is change the way that you are using variable.
cy.log(this.index + columnIndex);
I was able to get this working like this:
.get('#index').then(....
was used to extract the value of the index which is returned by my method get_table_column_index.
Then used cy.wrap again to return the cell value.
Cypress.Commands.add('get_text', (row_num, column_name) => {
cy.get_table_column_index(column_name).get('#index').then(index => {
cy.get('main > table').within(() => {
cy.get(`tr:nth-child(${row_num}) td:nth-child(${index})`)
.invoke('text')
.then((text) => {
cy.log("Text = " + text);
return cy.wrap(text).as('cell_value');
})
});
});
})
Then finally in my method call:
let cell_text = cy.get_text(2, 'Licensing').get('#cell_value');
cell_text.then(cell_value => {
cy.log(cell_value);
expect(cell_value).to.include('QTP');
})
The only thing I am not able to do is to store the value in a variable and use it without chaining any command like then to extract its value.
It would be great if someone can provide more information on that.
Thanks.

How to filter record by custom value from any dimension in dc.js?

How to remove custom records from any dimension. In the below case how do I filter only category 'S' and allow rest of them in dimension ?
Example
let data = [
{category:'A',value:10},
{category:'B',value:11},
{category:'S',value:12},
{category:'A',value:14},
{category:'B',value:12},
]
let ndx = crossfilter(data);
let dim= ndx.dimension(function(d){
if(d.category != "S") return d.category;
})
This above code runs into loop and the application crashes. I don't want to create separate data for this dimension rather link it with other cross filters.
I guess its pretty simple, I did little research after posting the question.
Just manipulate the group parameter being passed to the chart. The code goes something like this.
Since I am trying to remove the value by key lets first write a function for further uses as well.
function removeByKey(source_group, value) {
return {
all: function() {
return source_group.all().filter(function(d) {
return d.key != value;
});
}
};
}
Once this is done the place where you call the group method for the charts call this method. The first parameter of removeByKey method is the group itself the second is the key value which is supposed to be removed from the chart.
chart
.dimension(dimension_data)
.group(removeByKey(dimension_data_group, 'S'))
Thanks :)

Filter multi-key dimension with dc.selectmenu

I have attempted to extend this fiddle
https://jsfiddle.net/rn3ja9n4/
here
https://jsfiddle.net/thyfoubq/32/
but it is unclear what I should use to power the dc.selectmenu or if the it is even capable of working in the multi-key scenario.
Is this possible?
I have tried several variations of this but i can't seem to get the filter to work with the composite key or the single branch key
var storedim =ndx.dimension(function(d){ return [d.store]})
var storegroup =storedim.group()
storeselect
.dimension(storedim)
.group(storegroup)
.multiple(true)
.size(5)
.controlsUseVisibility(true)
.title(kv => kv.key);
----------------------- possible solution that appears to work---------
---change the group to remove empty bins
var suppliersSumGroup = remove_empty_bins(suppliersSumDistinct.group().reduceSum( d => d.earnings ));
function remove_empty_bins(source_group) {
return {
all:function () {
return source_group.all().filter(function(d) {
return d.value !=0; //appears to work borrowed from another SO post
//unsure which one but it seems logical and works for this example
});
}
};
}

Protractor: Verify results are sorted

Using Protractor I'm trying to verify if the sort works after I click on the sort by button. I've seen similar solutions here, but none of them works for my case.
On the page I have different values listed (15, 13, 67, ...) and for this I have home.myValues (using element.all). The flow would be:
store all numbers in an array and sort them with .slice().sort()
click on Sort by values button
store sorted numbers in an array
verify if sorted values match
Currently I have written the following, which doesn't work:
home.myValues.map(function(eachName) {
return eachName.getText().then(function(unSortedValues) {
initialSortedValues = unSortedValues.slice().sort();
return initialSortedValues;
});
});
// click on sort by values
home.sortByValues.click();
home.myValues.map(function(eachName) {
return eachName.getText().then(function(checkSortedValues) {
return checkSortedValues;
});
expect(initialSortedValues).toEqual(checkSortedValues);
});
Any suggestion is appreciated.
After some additional research I found this to work:
// verify my values are sorted
home.myValues.map(function(eachName) {
return eachName.getText().then(function(unSortedValues) {
return unSortedValues;
});
}).then(function(unSortedValues) {
// click on sort by value
home.sortByValue.click();
sortedValues = unSortedValues.slice();
sortedValues = sortedValues.sort(function(a, b){return a-b});
home.myValues.map(function(eachName) {
return eachName.getText().then(function(sortedValuesCheck) {
return sortedValuesCheck;
});
}).then(function(sortedValuesCheck) {
// verify sorted values equal expected
expect(sortedValues).toEqual(sortedValuesCheck);
});
});

d3.js - updating fill color for path works with string array, but not with json data

I'm using d3.js for a project that involves an interactive map.
For testing, I'd just like to fire a function that changes the fill value for a selection of paths (these represent room outlines).
The paths are already drawn on the canvas.
The path ID value comes from another system.
I have JSON data that I wish to use to join on the paths to update their fill color. The join is based on a key value based on the .attr("id") of the path which will equal a same key value on the incoming json data.
When I fire off my function with a simple test array of strings that is local in the file, it works as expected and updates the fill color of the test group of 4 or 5 paths.
However, when I attempt to use my JSON data, I cannot get the paths to update.
The test array that works is just strings:
var handleColorLink = [
["125E0", "red"],
["BC5AC", "orange"],
["BC417", "red"],
["B13D9", "orange"]
];
The JSON data looks like:
[{"handle":"BC5AD","mycolor":"blue"},
{"handle":"125F6","mycolor":"blue"},
{"handle":"171A7","mycolor":"blue"},
{"handle":"17235","mycolor":"blue"},
{"handle":"17236","mycolor":"blue"}]
My hunch is that there is no match found on the JSON side key (handle).
code:
function TestFunction() {
d3.json("http://myjsonURL", function(data) {
d3.selectAll("path")
.datum(function(d) { return [d3.select(this).attr("id")]; })
.data(data, function(d) { return d.handle; }) //doesnt work
.style("fill", function(d) { return d.mycolor; }); //doesnt work
//.data(handleColorLink, function(d) { return d[0]; }) //works on string array
//.style("fill", function(d) { return d[1]; }); //works
});
}//end testfunction
Any help would be greatly appreciated.
A 'think outside the box' solution could be, since you know the test array works with your method, just transforming the JSON data into the same structure as your test array. Like this:
data = data.map(function(item) { return [item.handle, item.mycolor]; });
Then just use it as you are already doing in your d3.js code.
Just an idea ;)

Resources