async + recursive + database-update operation with nodejs - algorithm

Here is the algorithm which returns the position(in form of a callback) at which an item can be inserted and also update the position of the previous items.
Put the item into infinite length array (assumed), 0<=index<INFINITY,
and for empty array largest_index_occupied = 0. ALSO largest_index_occupied is dynamically retrieved from database.(assumed). No two item can share same index. No item must be lost during any operation. indexes are in increasing order.
1. if index not provided OR index provided is greater than largest_index_occupied then put item at index : largest_index_occupied+5;
2. If index provided AND less than equals to largest_index_occupied, :
a. if (No other item already exists at that index) : simply put the item at that index
b. otherwise (means if a item already exists at that index) : increase the index of all items by one untill we get an empty index. and put the new item at the index actually passed by user(which must be empty now).
2.b example :
say - shows empty index.
previous state
0 - - - - 5 6 7 - - 10
a - - - - b c d - - e
input at index : 5,f
new state :
0 - - - - 5 6 7 8 - 10
a - - - - f b c d - e
Noe f's new index is 5.
My requirement is to write a code for the above in combination of async,recursion and IO operation (db update - of the index of items in case of 2.b) in nodejs.
i am expecting next(err,indexToBeInserted); callback that has to called only after all the updation has taken place.
My function block goes here :
toInsert(mongooseModel,next,indexToBeInserted) {
// async code goes here.
}
ps : if you have any better solution please share.

Related

How to filter a column according to values of another column in Tableau

Suppose that my query is 'A' for the following table. I want to find any value of 'c_index' corresponding to 'A', and then get all the rows of the table which have the corresponding values of 'c_index'.
Node Name
c_index
A
1
B
1
A
2
C
2
B
3
D
3
C
4
E
4
Values of 'c_index' corresponding to 'A' are {1, 2}. So the desired result of the filter is:
Node Name
c_index
A
1
B
1
A
2
C
2
How can I do this filtration in Tableau?
What I tried is:
Defined a filter on 'c_index' (i.e. drag and drop 'c_index' to the filter shelf). And then I tried to define the condition for the filter as: [Node Name] = 'A'.
But it throws an error: "The formula must be an aggregate calculation or refer only to this field".
First Join the (data) table with itself on the column which you want to return linked values. In the example c_index.
Now there will two same data sets in your data pane.
Add node from first dataset to filter, node from second dataset to view and c_index from anyone to view. You'll get what you desire. See GIF below

AngularJs: After removing item from list, subsequent dirty form elements are set to pristine, how to handle this?

I have List of objects which I am iterating using ng-repeat.
List = [{
type = "CC",
cardNum : 125,
accNum : null,
amount =125,
isCard : true
},
{
type = "LN",
cardNum : null,
accNum : 125,
amount =125,
isCard = false
},
....
]
Now, based on selection from Radio of isCard (two radio, is Card and is Non Card), I need to show two respective field such as cardNum or accNum.
Now the problem is, when iterate and add both combination of isCard and isNonCard account and remove element in between, all the elements after that are set to pristine where it was dirty before.
scenario 1:
selected 1st as Card Acc - index 1
2nd as Non Card - index 2
3rd as card acc -index 3
touched the fields such that there is error for all 3 entries above
now, remove 2nd entry by using splice.
Issue: Now the 3rd element index is updated to 1(which is correct) but field is reset to pristine and error is hidden. How can I stop this so that error will still appear for 3rd element at updated index.
I managed to fix this issue. Even after using track by index, it did not work so I had to add trackIdentifier in object. Now when I tracked based on it, it started working and error messages appeared correctly.

How to read list item of same list group?

I would like to read my Word.ListFormat by hierarchy level in docx file by Word.Interop.
for Example, the below list not applied by outline or/and indent to read by hierarchy
1. Item 1 of Level 1 List
a. Item 1 of Level 2 List
b. Item 2 of Level 2 List
c. Item 2 of Level 2 List
2. Item 2 of Level 1 List
a. Item 1 of Level 2 List
1. Item 1 of Level 3 List
2. Item 1 of Level 3 List
b. Item 2 of Level 2 List
c. Item 2 of Level 2 List
3. Item 3 of Level 1 List
My Expectation to read the List by hierarchy, I don't want to read by sequence of paragraph.
First reading: Item 1 of Level 1 List
Second reading: Item 2 of Level 1 List
Third reading: Item 3 of Level 1 List
Once reading complete the Level 1, then need to read Level 2 list.
Here, I have attached the sample screen shot of docx list file.
Thanks to all for supporting,
I found the answer of the question.
foreach (Word.Paragraph firstItem in document.Content.ListParagraphs.OfType<Word.Paragraph>().Reverse())
{
if (firstItem.Range.ListFormat.List != null)
{
foreach (Word.Paragraph item in firstItem.Range.ListFormat.List.ListParagraphs)
{
}
}
}

Core Data. Is it possible creating a view like you would do with normal SQL

In normal SQL world you would use Create View .... to define a view on one or more tables, e.g. to get a join and already a group by. Is that also possible somehow in Core data?
The reason I'm asking is, I have a table with a details. Each detail record has two keys and an amount. Now I need to show the sum of the amounts grouped by the two keys in a table view - i.e. The first key in the section and the second as normal entry with the sum amount. I thought FRC would work, but it does not group (add up the detail records). With a normal fetch request I can group and get everything - but it seems to be a lot of work to handle the sections manual. So I thought, the best is, I put a view on the table and use the FRC to bring it in the table view. Does that make sense? Any help ist very much appreciated.
example:
I have three fields:
A X 2
A X 2
A Z 3
B X 2
B Y 2
B Y 1
B Z 8
as a result I need
Section : A
X 4
Z 3
Section: B
Y 2
Z 8
So I am not sure if there is a shorter answer but here's how you can do it.
I'll assume the first column, second column and third column are called: firstCol, secondCol, thirdCol.
You can use this predicate to get all object for "A" and put it in resultArray:
//loop over the letters A to Z. Here's what it would look like:
NSPredicate *aPredicate = [NSPredicate predicateWithFormat:#"firstCol = %#)", #"A"];
Then find all the second column letters for objects that have A in first column (resultArray):
NSArray *allLetters = [resultArray valueForKeyPath:#"#distinctUnionOfObjects.secondCol"];
In case of "A" allLetters will include X and Z. Then loop over allLetters and add up the third column:
For (NSString *letter in allLetters) {
int sum = [allLetters valueForKeyPath:[NSString stringWithFormat:#"#sum.%#", letter]];
//this sums up each letter for example returns 4 for X in case of "A"
//insert the sum in an Array and then a Dictionary that can be used for data source of the table.
}

SPSS - How to isolate the maximum value in a variable so you can use it in syntax?

I have a table of areas with data. For a particular operation, I want to exclude the top and bottom 1% of areas, as they include extreme outliers.
Seems to me that a way forward is:
SORT CASES BY theVariableIwantToAnalyse (A) .
NUMERIC id (F12.0) . * create a casenum label "id"
COMPUTE id = $CASENUM. * populate it with casenum
EXECUTE.
NUMERIC idmax (F12.4) . * create a variable to contain the highest value for "id"
NUMERIC id1perc (F12.4) . * create a variable to contain 1% of the highest value for "id"
COMPUTE idmax = MAX(id) . * determine the highest value for id. This 'mock-syntax' line does not work.
COMPUTE id1perc = idmax / 100 . * 1% of the highest value for "id"
SELECT CASES WHERE ID >= id1perc or ID <= idmax - id1perc .
Draw graphs etc. I then need to
SORT CASES BY theNextVariableIwantToAnalyse (A) .
COMPUTE id = $CASENUM. * populate it with the NEW casenum order
EXECUTE.
etc ...
Try this to simply filter out the top and bottom 1% - just add FILTER BY filter. to turn off all extreme cases, or SELECT IF filter. ... EXECUTE. to delete them
EDIT: note that repeated values will be condensed by the RANK method (specifically the /TIESoption). This might not be ideal if you have the possibility of repeated values. Change the /TIES option if that's the case.
************* GENERATE RANDOM DATA *****************.
INPUT PROGRAM.
- LOOP #I = 1 TO 1000.
- COMPUTE Y = RV.NORMAL(100,10).
- END CASE.
- END LOOP.
- END FILE.
END INPUT PROGRAM.
dataset name exampleData WINDOW=front.
EXECUTE.
************* RANK DATA *************.
DATASET ACTIVATE exampleData.
RANK VARIABLES=Y (A)
/RFRACTION INTO fractile
/TIES=CONDENSE.
************* MAKE A FILTER *************.
COMPUTE filter = (fractile>0.01 AND fractile < 0.99).
EXECUTE.
* Chart Builder.
GGRAPH
/GRAPHDATASET NAME="graphdataset" VARIABLES=Y filter MISSING=LISTWISE REPORTMISSING=NO
/GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
SOURCE: s=userSource(id("graphdataset"))
DATA: Y=col(source(s), name("Y"))
DATA: filter=col(source(s), name("filter"), unit.category())
GUIDE: axis(dim(1), label("Y"))
GUIDE: axis(dim(2), label("Frequency"))
GUIDE: legend(aesthetic(aesthetic.color.interior), label("filter"))
ELEMENT: interval.stack(position(summary.count(bin.rect(Y))), color.interior(filter),
shape.interior(shape.square))
END GPL.
A MUCH easier solution is just to use RANK and then select on the ranks you want to exclude.

Resources