Use variable with Array in Application Insights - azure-application-insights

I would like to use a variable which contains an array, so I can use it with in filter.
this works:
traces
| where cloud_RoleName in ("A", "B")
this does not work (syntax error):
let cloudRoleNames = ("A", "B");
traces
| where cloud_RoleName in cloudRoleNames
I would like to use the array as variable to be able to use the same filter in more joins at once. As a workaround I use the first working variant but it is not ideal. I tried various ways - as well via parsing json but nothing works. Am I missing something obvious? Thx

Try this instead:
let cloudRoleNames = dynamic(["A", "B"]);
traces
| where cloud_RoleName in (cloudRoleNames)
Relevant docs:
in operator
the dynamic data type

Related

Iterate over a map in Rascal

I'm trying to create a map comprehension over a map in this structure:
map[Node, set[Node]]
And my map comprehension is something like:
(currentNode: {currentNode} | <currentNode, _> <- dominanceSet);
But I'm getting this error:
Is there a way to achieve a similar behaviour using another strategy?
The generator for maps in Rascal produces the key values (and not some form of "entries" or tuples).
So the code would be:
(currentNode: {currentNode} | currentNode <- dominanceSet)
Note that there are some specialized functions in the standard library for faster indexing relations, like in Relations there is map[&K, set[&V]] index(rel[&K, &V] R);

has_any() or in() on dynamic fails if values passed could map to GUIDS

Consider this query:
let set1=dynamic(["6ab1c993-c138-92ab-abac-49c4d4680812"]);
let set2=dynamic(["6ab1c993c13892ababac49c4d4680812#email.com"]);
let set3=dynamic(["6ab1c993-c138-92ab-abac"]);
let dummyData=datatable(Timestamp:datetime, DynamicData:dynamic) [
datetime(2019-12-30 01:00:00), dynamic({"Id": "6ab1c993-c138-92ab-abac-49c4d4680812", "Email": "6ab1c993c13892ababac49c4d4680812#email.com"})
];
// query1: fails: Relop semantic error: 'has_any' has the following semantic error: SEM0024: The source expression is of type 'dynamic' and cannot be compared with numeric arguments.
dummyData | where DynamicData has_any (toscalar(set1));
//
// query2: succeeds
dummyData | where DynamicData has_any (toscalar(set2));
//
// query3: succeeds
dummyData | where DynamicData has_any (toscalar(set3));
// query4: fails
dummyData | where DynamicData.Id in (toscalar(set1));
The first query fails because it thinks the RHS is numeric.
The second and 3rd queries succeed. If the values are coming from data, a query succeeding today could fail on a compile error later.
Please note that in this case, in() or set_intersect() aren't options since DynamicData is a complex property bag. Even if this worked for in(), 'DynamicData has_any ()' is drastically faster than 'DynamicData.child1.child2 in ()'.
Edit1: this is failing on in() as well, and seems to occur whenever the values in the list could possibly map to GUIDs.
Title updated and another example added.
Edit2: found a workaround that doesn't degrade performance: wrap the LHS in a tostring, like 'dummyData | where tostring(DynamicData) has_any (toscalar(set1));'
Found a workaround that doesn't degrade performance:
Wrap the LHS in a tostring.
dummyData | where tostring(DynamicData) has_any (toscalar(set1));

Trying to understand the SML option structure

Okay so I started learning SML for a class, and I'm stuck with the option structure.
What I have so far for this example:
datatype suit = spades|hearts|clubs|diamonds;
datatype rank = ace|two|three|...|j|q|k|joker;
type card = suit*rank;
My lecturer was trying to explain the use of the option structure by saying that not all cards necessarily have a suit; jokers don't have a suit associated with them.
So when designing a function getsuit to get the suit of a card, we have the following:
datatype 'a option = NONE | SOME of 'a;
fun getsuit ((joker,_):card):suit option = NONE
| getsuit ((_,s):card):suit option = SOME s;
But using emacs, I get two errors, one saying how the pattern and constraint don't agree,
pattern: rank * ?.suit
constraint: rank * suit
and the other saying how the expression type and the resulting types don't agree.
expression: ?.suit option
result type: suit option
This was the code provided by the lecturer so clearly they're not of much help if it results in errors.
What's the meaning of "?." and why does it show up? How would I correctly define this function?
Not really a problem with option as you've defined it.
You've got the order of suit and rank in your card patterns the wrong way:
Try:
datatype 'a option = NONE | SOME of 'a;
fun getsuit ((_, joker):card):suit option = NONE
| getsuit ((s, _):card):suit option = SOME s;
My version of ML probably prints errors differently so I'm not sure how to explain the the meaning of ?. etc. But it's simple enough if you take it bit by bit:
Try
(clubs, ace);
The interpreter (or emacs if that's what you're using) tells you the type is a product of a suit * rank. That's ML's type inference at work, but you can specify the type (you expect) like this:
(clubs, ace): suit*rank;
Or
(clubs, ace): card; (* Works, since card is defined as (suit*rank) *)
And you won't have any complaints. But obviously you would if you did
(clubs, ace): rank*suit;
Or
(clubs, ace): card; (* card is defined as (rank*) *)
You placed a constraint on the type of getsuit's argument (that it must be a card, or the compatible (suit*rank) product), but the pattern is of type (rank*?) or (?*rank), neither of which are compatible with (suit*rank).

Is there a nice idiom for adding a new list or appending to a list (if present) in a dictionary?

This is the second time I've implemented something like this and I suspect there has to be a better (read: more pythonic) way to do this:
phone_book = {}
def add_number(name,number):
if name in phone_book:
phone_book['name'].append(number)
else:
phone_book['name'] = [number]
I realize the code can likely be made more concise with conditional assignments, but I suspect there's likely a better way to go about this. I'm not interested in only making the code shorter.
Yep, you can use defaultdict. With this dict subclass, when you access an element in the dictionary, if a value doesn't already exist, it automatically creates one using a constructor function you specify.
from collections import defaultdict
phone_book = defaultdict(list)
def add_number(name, number):
phone_book[name].append(number)
Use dict's setdefault like this:
phone_book.setdefault('name', []).append(number)

How to do typeof of a module in a fsx file?

Let's say I have a Foo.fsx script that contains this code:
module Bar =
let foobar = "foo"+"bar"
open Bar
let a = System.Reflection.Assembly.GetExecutingAssembly()
let ty = a.GetType("Foo.Bar") // but it returns null here
How can I achieve that? Thanks!
This is a tricky question, because F# interactive compiles all types into types with some mangled names (and the executing assembly can contain multiple versions of the same type).
I managed to do it using a simple trick - you'd add an additional type to the module - then you can use typeof<..> to get information about this type. The type inside a module is compiled as a nested type, so you can get the name of the type representing the module from this (nested) type:
module Bar =
let foobar = "foo"+"bar"
type A = A
// Get type of 'Bar.A', the name will be something like "FSI_0001+Bar+A",
// so we remove the "+A" from the name and get "FSI_0001+Bar"
let aty = typeof<Bar.A>
let barName = aty.FullName.Substring(0, aty.FullName.Length - "+A".Length)
let barTy = aty.Assembly.GetType(barName)
// Get value of the 'foobar' property - this works!
barTy.GetProperty("foobar").GetValue(null, [| |])
You could probably simply search all types in the assembly looking for +Bar. That would work as well. The benefit of the trick above is that you get a reference to the specific version of the type (if you interactively run the code multiple times, you'll get a reference to the module corresponding to the current Bar.A type)
As a side-note, there were some discussions about supporting moduleof<Bar> (or something like that) in future versions of F# - this is a bit inelegant as it is not a real function/value like typeof, but it would very useful!

Resources