How to implement dynamic subscribers with KDB? - kdb

I'm trying to implement dynamic subscribers in a kdb-tick system whereby a subset of the events are passed to a given consumer based on a query supplied by the consumer.
For instance given a batch of events i.e.:
flip `source`channel`value!(10?`a`b`c;10?`a`b`c;10?10)
source channel value
--------------------
a a 4
b b 5
a c 4
b a 2
c c 7
c b 8
c a 5
a c 6
b a 4
b a 1
The tickerplant should only send the events without a channel of `c i.e.
source channel value
--------------------
a a 4
b b 5
b a 2
c b 8
c a 5
b a 4
b a 1
I have tried to implement this by parsing a dynamic conditional as follows:
q).tp.subscribers
hp | host isOpen port h subs
----------| --------------------------------------------------
:test:5000| test 0 5000 6 "enlist(not;(in;`channel;enlist`c))"
Whereby subs is a conditional argument to a functional select statement that is used in the following code:
.tp.send:{neg[x] y};
.tp.reval:{[batch;subscriber]
.tp.send[raze .subscriber`h] reval[parse["?[batch;",raze[subscriber`subs],";0b;()]"]]
};
// Called with event batch
.tp.broadcast:{[batch]
.tp.reval[batch]'[select from .tp.subscribers where isOpen]
};
This fails on account of batch not being addressable in a non global context through a functional select statement.
I was wondering how this functionality might be effectively achieved?
Could anyone advise me on or point me to information pertaining to a solution for this problem statement.
Your advice would very much appreciated.
Apologies if this is a newbie question.
Thanks

I think the fact that you're expecting a string form of a conditional argument is part of your problem (that in turn requires you to parse a stringified functional select and that parse assumes global).
Why not expect a list form of the conditional argument instead? Then there's no need to parse and you can create a local functional select. E.g.
.tp.subscribers:([hp:1#`:test:5000]subs:enlist(not;(in;`channel;1#`c)))
q){[batch] reval ?[batch;(0!.tp.subscribers)`subs;0b;()]}flip `source`channel`value!(10?`a`b`c;10?`a`b`c;10?10)
source channel value
--------------------
a a 4
b b 5
b a 2
c b 8
c a 5
b a 4
b a 1
Or have the user specify a lambda and run that (though I guess you would lose the ability to use reval in that case):
.tp.subscribers:([hp:1#`:test:5000]subs:enlist{select from x where not channel=`c})
q){[batch] #[first(0!.tp.subscribers)`subs;batch;()]}flip `source`channel`value!(10?`a`b`c;10?`a`b`c;10?10)
source channel value
--------------------
a b 9
c b 0
b b 0
b a 9
b a 3
b a 9

Related

Google Form is not following section based answering. It redirects on the basis of the answer chosen but shows the section supposed to be skipped too

My form has the following sections
A
B
C
D
E
F
I have one question in section A on the basis of which it is supposed to redirect to either B or C. those filling b should be seeing c and vice versa and should directly continue to fill D E F normally. However, those filling B are redirected to C even though it says to go to section D (or 4 in the picture). What is at the end of section BThe redirection question in Section A I am not a coder (as may be apparent) but wanted to know if this a logic issue or a google forms issue
I was expecting it to skip section C and go to D, but that did not happen.

Pivot table export excel for header group

How to excel export for pivot table?
Name Code shop sum1 sum2
A 1 16 2 3
B 2 14 4 3
C 4 13 2 5
D 3 33 1 6
Name, code => rowGroup / shop => pivot
Then, it looks like on the screen.
Name Code total1 total2 sum1 sum2 sum1 sum2
A 1 6 6 2 3 4 3 ...
but I export excel. It looks like
I want to export excel like this picture. What should I change to look the same as this?
I was right in my comment on the question. This is because groupHideOpenParents is not applied to the exported file. This is a known issue. It is among the "Standard Feature Requests" in the ag-Grid Pipeline: https://www.ag-grid.com/ag-grid-pipeline/. Ticket key: AG-3756 [Excel Export] Allow groupHideOpenParents to apply in Excel export
Unfortunately it is in the backlog, so it can't be known when it will be fixed and released. Based on this blog entry: https://ag-grid.zendesk.com/hc/en-us/articles/360002213531-Where-Is-My-Ticket-
"Standard Feature Requests are usually fixed within 2 to 4 releases after they have been raised."

qPython Functional Queries

We are trying to build functional queries using qPython. Strating with simple examples to build where conditions at the run time.
we defined a q function on out KDB server like
fn:{[c]
t: (select from tbl);
:?[t;c;0b;()];
}
in Python we open a connection and send the condition
c = [['=', numpy.string_('TradeId'), 123456]]
result = conn.sendSync('fn', c)
when I do this, in q console I see that = operator as "=".
the question is how to pass operators
#terrylynch answer works
for specifically qPython
from python sending works
c = [[ qtype.QLambda( '{x in y}'), numpy.string_('TradeId'), [123, 456,789]]]
You could value the string/char on the kdb side to convert it from a string/char to the underlying kdb operator. This works in your example but you might need some extra work/testing to generalise it to all possible operators that you might send:
q)tbl:([]TradeId:0 123456 123456 123456;col2:1 2 3 4)
q)fn:{[c] c:.[c;(::;0);value];t:(select from tbl); :?[t;c;0b;()];}
q)0(`fn;enlist("=";`TradeId;123456))
TradeId col2
------------
123456 2
123456 3
123456 4
q)0(`fn;(("=";`TradeId;123456);("in";`col2;2 4)))
TradeId col2
------------
123456 2
123456 4
Note - Im using 0() to send requests within kdb itself but this should be the equivalent of sending from qPython. The bit you need to change is the c:.[c;(::;0);value]; in your kdb function

loop through values in richPipe : scalding

I am trying to solve a issue where I have to loop through all values in pipe.To simulate my problem I am explain through a sample problem
Input file :
number
1
2
3
4
Output should be
number sumOfSmaller
1 0
2 1
3 3
4 6
So for each value I have to read all of the records in pipe and apply function sumOfSmaller.
I have no idea on how to loop through values in scalding pipe.
Using map I can apply function of each element of list but I want to avoid this approch
You can get the contents of the whole pipe with
val wholePipe = pipe.groupAll.toList, and then join it with itself, and apply your function: pipe.groupAll.join(wholePipe).values.map { case (x, list) => sumOfSmaller(x, list) }
This is not a very good idea though, especially, if your pipe is of any decent size. Knowing more details about what it is you are really trying to do should almost certainly allow for a better approach.

How to simplify boolean function into two logic gates?

Can anyone help me to simplify this boolean function into two logic gates?
C(out) = AC(in) + BC(in) + AB
This expression represents what is commonly known as a three input majority gate - the output is TRUE only when the majority of inputs are true (2 or 3 inputs must be true for the 3 input case). In general it takes 4 basic logic gates to implement this (5 if you you are restricted to 2-input gates).
If you Google for "majority gate" you will find a variety of implementations, e.g. on this page I found the following, which I think matches your criteria (other than the unfeasible requirement of doing it with only 2 gates):
About majority function with n boolean variables.
for n variables, f(x1,x2,...xn) there will be total nC[n/2] terms for OR operation. Each term contains [n/2] variables for AND operation.
ex: f(00111)= OR{ and(0,0,1) and(0,0,1) and(0,0,1) and(0,1,1) and(,0,1,1) and(0,1,1,) and(0,1,1) and (0,1,1,) and(0,1,1,) and(1,1,1 )
=0 OR 0 OR 0 OR...... OR 1=1=majority of ones is true.