Repository: Azure/azure-cli-extensions

Issue:
tsv and table outputs won't work with az monitor log-analytics query
- If the issue is to do with Azure CLI 2.0 in-particular, create an issue here at [Azure/azure-cli](https://github.com/Azure/azure-cli/issues)

### Extension name (the extension in question)

az monitor log-analytics query
### Description of issue (in as much detail as possible)

-----
While querying a workspace, the json and yaml output work fine.

But if I changed to tsv, I obtain a number "1" and the table output will show nothing:

C:\windows\system32>az monitor log-analytics query -w 57ebcc86-e39d-4183-947e-5e2aa377e569 --analytics-query "Heartbeat"  --output tsv
1

C:\windows\system32>az monitor log-analytics query -w 57ebcc86-e39d-4183-947e-5e2aa377e569 --analytics-query "Heartbeat"  --output table

C:\windows\system32>

Thanks,
<img width="817" alt="Anotación 2019-09-04 135750" src="https://user-images.githubusercontent.com/22793342/64278978-0582c180-cf1c-11e9-9ff1-7b89bc47ed24.png">



Discussion:
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Dalubash 
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Dalubash 
Taking a look
@jiasli I see you added service attention, but typically output processing is done centrally by CLI or SDK. I'm able to reproduce this and confirmed via debug that the SDK and API output is correct, and contains quite a bit more JSON than is shown here. 

Could you please take a look at this from CLI side, or assign the appropriate person from CLI to look at the output logic? cc @lmazuel in case you have context (I realize this is CLI not SDK, but you seem quite familiar with all the `az` python goodies 😄)
@paratz thanks for reporting, is there a scenario you'd like to use this for that is blocked, or you just prefer the more human-readable output? 

I tend to use TSV when doing automation because it strips quotes unlike JSON. If this is your use, might be nice to pair `az` output with `jq -r` as a workaround. 
One of our customer is looking to automate a script using Az Script that generates a report from a Log Analytics query. This query produces more than 10k results, and they need this report in csv/tsv to be processed by some other system....
@paratz Has the customer tried the workaround that @alexeldeib mentioned? 

> I tend to use TSV when doing automation because it strips quotes unlike JSON. If this is your use, might be nice to pair `az` output with `jq -r` as a workaround.

@jiasli @lmazuel can you investigate this from the CLI side?
@alexeldeib I'm using Windows, I've just tried the windows version of jq with -r and I don't get a "human" readable format like tsv or table.... attaching an example
![Anotación 2019-09-24 182158](https://user-images.githubusercontent.com/22793342/65551401-4442e080-def8-11e9-8090-1a4432b4ec80.png)


Resolving patch:
diff --git a/src/log-analytics/azext_loganalytics/_format.py b/src/log-analytics/azext_loganalytics/_format.py
new file mode 100644
--- /dev/null
+++ b/src/log-analytics/azext_loganalytics/_format.py
@@ -0,0 +1,34 @@
+# --------------------------------------------------------------------------------------------
+# Copyright (c) Microsoft Corporation. All rights reserved.
+# Licensed under the MIT License. See License.txt in the project root for license information.
+# --------------------------------------------------------------------------------------------
+
+
+from collections import OrderedDict
+
+
+def transform_query_output(result):
+    tables_output = []
+
+    def _transform_query_output(table):
+        columns = table.columns
+        name = table.name
+        rows = table.rows
+
+        column_names = []
+        table_output = []
+        for column in columns:
+            column_names.append(column.name)
+        for row in rows:
+            item = OrderedDict()
+            item['TableName'] = name
+            for index, value in enumerate(row):
+                item[column_names[index]] = str(value)
+            table_output.append(item)
+        return table_output
+
+    for table in result.tables:
+        table_output = _transform_query_output(table)
+        tables_output.extend(table_output)
+
+    return tables_output
diff --git a/src/log-analytics/azext_loganalytics/commands.py b/src/log-analytics/azext_loganalytics/commands.py
--- a/src/log-analytics/azext_loganalytics/commands.py
+++ b/src/log-analytics/azext_loganalytics/commands.py
@@ -5,8 +5,10 @@
 
 # pylint: disable=line-too-long
 
+from ._format import transform_query_output
+
 
 def load_command_table(self, _):
 
     with self.command_group('monitor log-analytics') as g:
-        g.custom_command('query', 'execute_query')
+        g.custom_command('query', 'execute_query', transform=transform_query_output)
diff --git a/src/log-analytics/setup.py b/src/log-analytics/setup.py
--- a/src/log-analytics/setup.py
+++ b/src/log-analytics/setup.py
@@ -8,7 +8,7 @@
 from codecs import open
 from setuptools import setup, find_packages
 
-VERSION = "0.1.3"
+VERSION = "0.1.4"
 
 CLASSIFIERS = [
     'Development Status :: 4 - Beta',


Test patch: