Need help to Understand KUSTO Scan operator - kusto-explorer

Can someone please explain how the output is prepared from this query.
Not able to under why records are repeated in the output.
let tbl = datatable(Ts: timespan, nodeState:string)
[ 0m, "Ready",
1m, "Ready",
2m, "Ready",
3m, "Ready",
4m, "Ready",
5m, "Ready",
6m, "Ready",
7m, "Ready",
8m, "Ready",
9m, "HumanInvestigate" ];
let b = tbl
|order by Ts asc
|scan with_match_id = mid declare (steps:string ) with (
step s1 output = all: nodeState =="Ready" => steps="s1";
step s2: true => steps="s2";
step s3 output =all : nodeState == "HumanInvestigate" => steps="s3"; );

Related

Filter overlapping time periods using Elixir

I have two lists of tuples that contain start and end times. All the start and end times are Time structs.
I want to filter out the time_slots that overlap with any booked_slots:
time_slots = [
{~T[09:00:00], ~T[17:00:00]},
{~T[13:00:00], ~T[17:00:00]},
{~T[13:00:00], ~T[21:00:00]},
{~T[17:00:00], ~T[21:00:00]},
{~T[09:00:00], ~T[13:00:00]},
{~T[09:00:00], ~T[21:00:00]}
]
booked_slots = [{~T[14:00:00], ~T[21:00:00]}]
Which should leave us with:
[
{~T[09:00:00], ~T[13:00:00]}
]
I have tried the following method (as adapted from the answer to a previous related question: Compare two lists to find date and time overlaps in Elixir):
Enum.filter(time_slots, fn {time_start, time_end} ->
Enum.any?(booked_slots, fn {booking_start, booking_end} ->
if Time.compare(booking_start, time_start) == :lt do
Time.compare(booking_end, time_start) == :gt
else
Time.compare(booking_start, time_end) == :lt
end
end)
end)
However this returns:
[
{~T[09:00:00], ~T[17:00:00]},
{~T[13:00:00], ~T[17:00:00]},
{~T[13:00:00], ~T[21:00:00]},
{~T[17:00:00], ~T[21:00:00]},
{~T[09:00:00], ~T[21:00:00]}
]
We also need to factor in times that may be equal, but do not overlap. For example:
time_slots = [
{~T[09:00:00], ~T[17:00:00]},
{~T[13:00:00], ~T[17:00:00]},
{~T[13:00:00], ~T[21:00:00]},
{~T[17:00:00], ~T[21:00:00]},
{~T[09:00:00], ~T[21:00:00]},
{~T[09:00:00], ~T[13:00:00]}
]
booked_slots = [{~T[17:00:00], ~T[21:00:00]}]
Should return…
[
{~T[09:00:00], ~T[17:00:00]},
{~T[13:00:00], ~T[17:00:00]},
{~T[09:00:00], ~T[13:00:00]}
]
You need to filter out all the time slots having either start or end times inside the booked slot.
Enum.reject(time_slots, fn {ts, te} ->
Enum.any?(booked_slots, fn {bs, be} ->
(Time.compare(ts, bs) != :lt and Time.compare(ts, be) == :lt) or
(Time.compare(te, bs) == :gt and Time.compare(te, be) != :gt) or
(Time.compare(ts, bs) == :lt and Time.compare(te, be) == :gt)
end)
end)
The first condition checks for slots having start time inside the booked slot, the second one checks for those having end time within the booked slots, and the third one checks for those containing the whole booked slot.

Elasticsearch: scroll between specified time frame

I have some data in elasticsearch. as shown in the image
I used below link example to do the scrolling
https://gist.github.com/drorata/146ce50807d16fd4a6aa
page = es.search(
index = INDEX_NAME,
scroll = '1m',
size = 1000,
body={"query": {"match_all": {}}})
sid = page['_scroll_id']
scroll_size = page['hits']['total']
# Start scrolling
print( "Scrolling...")
while (scroll_size > 0):
print("Page: ",count)
page = es.scroll(scroll_id = sid, scroll = '10m')
# Update the scroll ID
sid = page['_scroll_id']
for hit in page['hits']['hits']:
#some code processing here
Currently my requirement is that i want to scroll but want to specify the start timestamp and end timestamp
Need help as to how to do this using scroll.
Simply replace
body={"query": {"match_all": {}}})
by
body={"query": {"range": {"timestamp":{"gte":"2018-08-05T05:30:00Z", "glte":"2018-08-06T05:30:00Z"}}}})
example code. time range should be in es query. Also You should process the first query result.
es_query_dict = {"query": {"range": {"timestamp":{
"gte":"2018-08-00T00:00:00Z", "lte":"2018-08-17T00:00:00Z"}}}}
def get_es_logs():
es_client = Elasticsearch([source_es_ip], port=9200, timeout=300)
total_docs = 0
page = es_client.search(scroll=scroll_time,
size=scroll_size,
body=json.dumps(es_query_dict))
while True:
sid = page['_scroll_id']
details = page["hits"]["hits"]
doc_count = len(details)
if len(details) > 0:
total_docs += doc_count
print("scroll size: " + str(doc_count))
print("start bulk index docs")
# index_bulk(details)
print("end success")
else:
break
page = es_client.scroll(scroll_id=sid, scroll=scroll_time)
print("total docs: " + str(total_docs))
Also have a look at elasticsearch.helpers.scan where you already have the loop logic implemented for you, just pass it query={"query": {"range": {"timestamp": {"gt": ..., "lt": ...}}}}

DAX in calculated column

I have two tables as below.
Table1
CaseId
66787
Table2
PrimaryKey CaseId SeqNo Status Primary Code CodeCareNo
85248 66787 6 Active N 876 8775568
70728 66787 1 Inactive N 876 3661794
79008 66787 5 Active Y 876 3766066
86868 66787 7 Active Y 876 3287735
Table 1 has one to many relationships with Table2 and the relating column in CaseId.
I have a requirement to create a calculated column in Table1 in my model project. The calculated column should display a text like (Code) CodeCareNo (eg: (876) 3766066) for each CaseId in Table1 with the values from Code and CodeCareNo columns of Table2 which has Primary = “Y” and Status = “Active” with Seq No as the minimum value among the primary active code numbers for the CaseId. Also if the Code or CodeCareNo is null the calculated column should show blank value. I am able to get the desired result with below query but I feel it bit messy. Can someone help me in simplifying the same?
=IF("(" & LOOKUPVALUE(Table2[Code], Table2[CaseId], Table1[CaseId],
Table2[Primary], "Y", Table2[Status], "Active", Table2[SeqNo],
MINX(FILTER(Table2, ( Table2[Primary] = "Y" && Table2[Status] = "Active" &&
Table2[CaseId] = Table1[CaseId])), Table2[SeqNo])) & ") " &
LOOKUPVALUE(Table2[CodeCareNo], Table2[CaseId], Table1[CaseId],
Table2[Primary], "Y", Table2[Status], "Active", Table2[SeqNo],
MINX(FILTER(Table2, ( Table2[Primary] = "Y" && Table2[Status] = "Active" &&
Table2[CaseId] = Table1[CaseId])), Table2[SeqNo])) = "() ", BLANK(), "(" &
LOOKUPVALUE(Table2[Code], Table2[CaseId], Table1[CaseId], Table2[Primary],
"Y", Table2[Status], "Active", Table2[SeqNo], MINX(FILTER(Table2, (
Table2[Primary] = "Y" && Table2[Status] = "Active" && Table2[CaseId] =
Table1[CaseId])), Table2[SeqNo])) & ") " & LOOKUPVALUE(Table2[CodeCareNo],
Table2[CaseId], Table1[CaseId], Table2[Primary], "Y", Table2[Status],
"Active", Table2[SeqNo], MINX(FILTER(Table2, ( Table2[Primary] = "Y" &&
Table2[Status] = "Active" && Table2[CaseId] = Table1[CaseId])),
Table2[SeqNo])) )
That's quite the mess. Try this formulation:
CodeCase =
VAR SeqNo = CALCULATE(MIN(Table2[SeqNo]), Table2[Primary] = "Y", Table2[Status] = "Active")
VAR Code = LOOKUPVALUE(Table2[Code], Table2[SeqNo], SeqNo)
VAR CodeCareNo = LOOKUPVALUE(Table2[CodeCareNo], Table2[SeqNo], SeqNo)
RETURN IF(ISBLANK(Code) || ISBLANK(CodeCareNo), BLANK(), "(" & Code & ") " & CodeCareNo)
(I like to use variables for computational efficiency and readability.)

Count number of dictionarys in dictionary in swift

I have a buch of accounts stored in a string dictionary and i would like to count the number of accounts existing, so basicly a ".count" but to find the number of dictionaries created.
var dictionary: [String : [ String ]] = ["" : []]
let storeString = "StoreString"
func addUpdateArray(strings: [String], index: Int) {
let locator = storeString + index.description
dictionary[locator] = strings
}
addUpdateArray(["Account1", "Hy"], 1)
addUpdateArray(["Account2", "Hey"], 3)
and now I would like to see how many accounts are have created of the kind dictionary, is ther a way?
Something like this?
var accounts = [String:[String:String]]() // or whatever your structure is
accounts["Edmund"] = [
"amount": "23.87",
"curreny": "dollars"
]
accounts["Baldrick"] = [
"amount": "23.87",
"curreny": "dollars"
]
accounts["Percy"] = [
"amount": "87.00",
"curreny": "peso"
]
println(accounts.keys.array.count) // 3
If you have dictionary of dictionaries and you want to count the number of actual values inside, you can do it like this:
var accounts = [
"accountsGroup1" : ["account1", "account2", "account3", "account4"],
"accountsGroup2" : ["account1", "account2"],
"accountsGroup3" : ["account1", "account2", "account3", "account4"]
]
let accountsCount = accounts.values.map { $0.count }
let numberOfAllAccounts = reduce(accountsCount, 0) { $0 + $1 }
println(numberOfAllAccounts)

Parsing JSON with AFNetworking give results with round brackets

Here is the simplified JSON file, I need to download it from a net service and parse results in a table!
EDIT: I provide now more precise code, cleaned and formatted by online tool:
{
"main": [
{
"id": 0, <--- float value
"type": "type0", <--- STRING value
"valueA": {
"valueA1": 1, <--- float value
"valueA2": 2, <--- float value
"valueA3": 3 <--- float value
},
"valueB": {
"valueB1": 1, <--- float value
"valueB2": 2 <--- float value
},
"valueC": [
{
"valueC1": "string0C1", <--- STRING value
"valueC2": 2, <--- float value
"valueC3": 3, <--- float value
}
]
},
FORMATTED by online tool jsonviewer.stack.hu:
I need to parse it with AFJSONRequestOperation, and I write this code:
NSMutableArray *main = [JSON objectForKey:#"main"];
arrayID = [main valueForKey:#"id"];
arrayType = [main valueForKey:#"type"];
NSMutableArray *arrayValueC = [main valueForKey:#"valueC"];
NSMutableString *stringC1 = [arrayValueC valueForKey:#"valueC1"];
// I CANT USE objectForKey, XCode give an exception -[__NSArrayI objectForKey:]: unrecognized selector sent to instance
NSLog(#"id: %#",arrayID);
NSLog(#"type: %#",arrayType);
NSLog(#"string: %#",stringC1);
When I parse, I get this results from NSLog:
id: (
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
)
type: (
type0,
type1,
type2,
type3,
type4,
type5,
type6,
type7,
type8,
type9
)
string: (
(
"string0C1"
),
(
"string2C1"
),
(
"string2C1"
),
(
"string3C1"
),
(
"string4C1"
),
(
"string5C1"
),
(
"string6C1"
),
(
"string7C1"
),
(
"string8C1"
),
(
"string9C1"
)
)
As u can see its all perfect, I can extrapolate every value of ID (float) and TYPE (string), but I hate the round brackets in every object of the valueC1 string: how can I get the clean valueC1 without brackets and quotation marks? Please if u can provide some code. Thanks!
Don't use valueForKey:. Use objectForKey: instead. This is probably the major problem. But once you use it, you might run into new problem:
The following contains a problem that will manifest itself when arrayValueA is accessed the first time:
NSMutableArray *arrayValueA = [arrayMain objectForKey:#"valueA"];
The element stored at valueA is an object i.e. a dictionary and not an array.
Finally, your simplified JSON is invalid anyway. Several values are missing double quotes, e.g.:
"id": id0
should be:
"id": "id0"
You better shows the real JSON data and the real code.
Update:
You should be able to access the JSON data with the following code:
NSArray *main = [JSON objectForKey:#"main"];
NSDictionary* main0 = [main objectAtIndex:0];
arrayID = [main0 objectForKey:#"id"];
arrayType = [main0 objectForKey:#"type"];
NSArray *arrayValueC = [main objectForKey:#"valueC"];
NSDictionary *elem0 = [arrayValueC objectAtIndex:0];
NSString *stringC1 = [arrayValueC objectForKey:#"valueC1"];
NSLog(#"id: %#",arrayID);
NSLog(#"type: %#",arrayType);
NSLog(#"string: %#",stringC1);

Resources