microsoft / jupyter-Kqlmagic

Extension (Magic) to Jupyter notebook and Jupyter lab, that enable notebook experience working with Kusto, ApplicationInsights, and LogAnalytics data.
Other
85 stars 31 forks source link

`assign_var` and `last_raw_result_var` options do not assign result to specified user variables as defined in specs #80

Closed AlenaMarchuk closed 2 years ago

AlenaMarchuk commented 2 years ago

I need to assign results of the query to a variable within a method because it's a multi-threaded call and I cannot use the global _kql_rawresult variable.

Example query: %kql -query queryStr -assign_var "res" -enable_suppress_result ;

I believe that's the intended use of the -assign_var option. When used like so, the res variable gets defined within the python method but nevertheless is always an empty string. So is the _ variable. The _kql_raw_result is still initialized with the query result.

The same is true for the last_raw_result_var option.

mbnshtck commented 2 years ago

All references to variables are to the user's global namespace. Also it was not designed to be thread safe. However, you can try to work directly with the Kqlmagic module and use the kql function that accepts the same string as the magic cell text. It might work. Michael

Michael


From: AlenaMarchuk @.> Sent: Friday, November 19, 2021 3:57:39 AM To: microsoft/jupyter-Kqlmagic @.> Cc: Subscribed @.***> Subject: [microsoft/jupyter-Kqlmagic] assign_var and last_raw_result_var options do not assign result to specified user variables as defined in specs (Issue #80)

I need to assign results of the query to a variable within a method because it's a multi-threaded call and I cannot use the global kql_raw_result variable.

Example query: %kql -query queryStr -assign_var "res" -enable_suppress_result ;

I believe that's the intended use of the -assignvar option. When used like so, the res variable gets defined within the python method but nevertheless is always an empty string. So is the variable. The _kql_raw_result is still initialized with the query result.

The same is true for the last_raw_result_var option.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fjupyter-Kqlmagic%2Fissues%2F80&data=04%7C01%7Cmichabin%40microsoft.com%7Ce598c106dc324e639c9108d9aafff7fd%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637728838648397789%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=xp3d1MDQZE15W72CpLs5fDmJMd20SXZCcKvi2GT9F6Q%3D&reserved=0, or unsubscribehttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACGKLXQVK25CN2BQBE5JZVLUMWVJHANCNFSM5ILBSY6Q&data=04%7C01%7Cmichabin%40microsoft.com%7Ce598c106dc324e639c9108d9aafff7fd%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637728838648407746%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=UOTkrokULPvMR1Y4UwZM9lTTAd0fBZMmsnlcpYj9T5g%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7Cmichabin%40microsoft.com%7Ce598c106dc324e639c9108d9aafff7fd%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637728838648407746%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=Ebxm4RJaH%2BCpAG07FWcpGfCFWcvOrfuv56IYpqrXRL8%3D&reserved=0 or Androidhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7Cmichabin%40microsoft.com%7Ce598c106dc324e639c9108d9aafff7fd%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637728838648417700%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=5Qh%2BQW7J8TE8SjKG9gUWnnxvg6yhVD9mUkCwLkEf6NM%3D&reserved=0.

AlenaMarchuk commented 2 years ago

Michael, Thank you for your quick response. Would you happen to have any example of directly using Kqlmagic module's Kqlmagic class and it's execute method?

Thanks again,

Alena

AlenaMarchuk commented 2 years ago

For posterity, sharing the way I was able to retrieve query results via using locally scoped variable:

from Kqlmagic.kql_magic import Kqlmagic as Magic

def kql(queryString: str, in_cell=False):
    """
    Query Kqlmagic 
    @param queryString: query string
    @param in_cell: force kql to be a cell only query
    """
    kql = Magic(shell=None)
    res = kql.execute(
        line=f"{queryString};" if in_cell is False else "",
        cell=None if in_cell is False else queryString, 
        override_options={"try_vscode_login": True}, 
        override_connection="AzureDataExplorer://code;cluster='VSO';database='VSO'"
    )
    return res