diff --git a/sdk/monitor/azure-monitor-query/CHANGELOG.md b/sdk/monitor/azure-monitor-query/CHANGELOG.md index bb72a5d73ad0..199d700da31d 100644 --- a/sdk/monitor/azure-monitor-query/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-query/CHANGELOG.md @@ -7,6 +7,7 @@ - Added enum `AggregationType` which can be used to specify aggregations in the query API. - Added `LogsBatchQueryResult` model that is returned for a logs batch query. +- Added `error` attribute to `LogsQueryResult`. ### Breaking Changes @@ -15,6 +16,7 @@ - `LogsBatchResults` model is now removed. - `LogsQueryRequest` is renamed to `LogsBatchQueryRequest` - `LogsQueryResults` is now renamed to `LogsQueryResult` +- `LogsBatchQueryResult` now has 4 additional attributes - `tables`, `error`, `statistics` and `render` instead of `body` attribute. ### Bugs Fixed diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py index b635ebd7f5e2..5bc5cf00cceb 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py @@ -67,18 +67,23 @@ def __init__(self, **kwargs): class LogsQueryResult(object): """Contains the tables, columns & rows resulting from a query. - :keyword tables: The list of tables, columns and rows. - :paramtype tables: list[~azure.monitor.query.LogsQueryResultTable] - :keyword statistics: Any object. - :paramtype statistics: object - :keyword render: Any object. - :paramtype render: object + :ivar tables: The list of tables, columns and rows. + :vartype tables: list[~azure.monitor.query.LogsQueryResultTable] + :ivar statistics: This will include a statistics property in the response that describes various + performance statistics such as query execution time and resource usage. + :vartype statistics: object + :ivar render: This will include a render property in the response that specifies the type of + visualization selected by the query and any properties for that visualization. + :vartype render: object + :ivar error: Any error info. + :vartype error: object """ def __init__(self, **kwargs): # type: (Any) -> None self.tables = kwargs.get("tables", None) self.statistics = kwargs.get("statistics", None) self.render = kwargs.get("render", None) + self.error = kwargs.get("error", None) @classmethod def _from_generated(cls, generated): @@ -94,7 +99,8 @@ def _from_generated(cls, generated): return cls( tables=tables, statistics=generated.statistics, - render=generated.render + render=generated.render, + error=generated.error ) @@ -209,12 +215,20 @@ def __init__(self, query, workspace_id, duration=None, **kwargs): #pylint: disab class LogsBatchQueryResult(object): """The LogsBatchQueryResult. - :param id: - :type id: str - :param status: - :type status: int - :param body: Contains the tables, columns & rows resulting from a query. - :type body: ~azure.monitor.query.LogsQueryResult + :ivar id: the request id of the request that was sent. + :vartype id: str + :ivar status: status code of the response. + :vartype status: int + :ivar tables: The list of tables, columns and rows. + :vartype tables: list[~azure.monitor.query.LogsQueryResultTable] + :ivar statistics: This will include a statistics property in the response that describes various + performance statistics such as query execution time and resource usage. + :vartype statistics: object + :ivar render: This will include a render property in the response that specifies the type of + visualization selected by the query and any properties for that visualization. + :vartype render: object + :ivar error: Any error info. + :vartype error: object """ def __init__( self, @@ -222,16 +236,29 @@ def __init__( ): self.id = kwargs.get('id', None) self.status = kwargs.get('status', None) - self.body = kwargs.get('body', None) + self.tables = kwargs.get('tables', None) + self.error = kwargs.get('error', None) + self.statistics = kwargs.get('statistics', None) + self.render = kwargs.get('render', None) @classmethod def _from_generated(cls, generated): if not generated: return cls() + tables = None + if generated.body.tables is not None: + tables = [ + LogsQueryResultTable._from_generated( # pylint: disable=protected-access + table + ) for table in generated.body.tables + ] return cls( id=generated.id, status=generated.status, - body=LogsQueryResult._from_generated(generated.body) # pylint: disable=protected-access + tables=tables, + statistics=generated.body.statistics, + render=generated.body.render, + error=generated.body.error ) diff --git a/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py b/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py index 465063ce9819..82a176aca43a 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py @@ -27,7 +27,7 @@ workspace_id= os.environ['LOG_WORKSPACE_ID'] ), LogsBatchQueryRequest( - query= "AppRequests | take 5", + query= "AppRequestss | take 5", workspace_id= os.environ['LOG_WORKSPACE_ID'], include_statistics=True ), @@ -35,13 +35,11 @@ responses = client.batch_query(requests) for response in responses: - body = response.body - print(response.id) - if not body.tables: - print("Something is wrong") - else: - for table in body.tables: - df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) - print(df) + try: + table = response.tables[0] + df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) + print(df) + except TypeError: + print(response.error) # [END send_batch_query] \ No newline at end of file diff --git a/sdk/monitor/azure-monitor-query/samples/sample_batch_query_serialized.py b/sdk/monitor/azure-monitor-query/samples/sample_batch_query_serialized.py index cdf140b22f17..91d8a50473d4 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_batch_query_serialized.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_batch_query_serialized.py @@ -39,13 +39,12 @@ "workspace": os.environ['LOG_WORKSPACE_ID'] } ] -response = client.batch_query(requests) +responses = client.batch_query(requests) -for response in response.responses: - body = response.body - if not body.tables: - print("Something is wrong") - else: - for table in body.tables: - df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) - print(df) \ No newline at end of file +for response in responses: + try: + table = response.tables[0] + df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) + print(df) + except TypeError: + print(response.error) \ No newline at end of file diff --git a/sdk/monitor/azure-monitor-query/samples/sample_log_query_client.py b/sdk/monitor/azure-monitor-query/samples/sample_log_query_client.py index dc2cf6ea361e..7f95875af8f3 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_log_query_client.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_log_query_client.py @@ -28,9 +28,12 @@ if not response.tables: print("No results for the query") -for table in response.tables: +try: + table = response.tables[0] df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) print(df) +except TypeError: + print(response.error) # [END send_logs_query] """ TimeGenerated _ResourceId avgRequestDuration diff --git a/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py b/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py index 28bae17ecea2..30de289faf34 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py @@ -25,10 +25,11 @@ additional_workspaces=[os.environ["SECONDARY_WORKSPACE_ID"]], ) -if not response.tables: - print("No results for the query") - -for table in response.tables: +try: + table = response.tables[0] df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) print(df) +except TypeError: + print(response.error) + diff --git a/sdk/monitor/azure-monitor-query/samples/sample_logs_query_key_value_form.py b/sdk/monitor/azure-monitor-query/samples/sample_logs_query_key_value_form.py index 2989f5e453bd..6e018588b99d 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_logs_query_key_value_form.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_logs_query_key_value_form.py @@ -21,14 +21,13 @@ # returns LogsQueryResult response = client.query(os.environ['LOG_WORKSPACE_ID'], query, duration=timedelta(days=1), end_time=end_time) -if not response.tables: - print("No results for the query") - -for table in response.tables: - pd.json_normalize +try: + table = response.tables[0] df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) key_value = df.to_dict(orient='records') print(key_value) +except TypeError: + print(response.error) """ [ diff --git a/sdk/monitor/azure-monitor-query/samples/sample_server_timeout.py b/sdk/monitor/azure-monitor-query/samples/sample_server_timeout.py index 6d7e8f205259..0f03fde23d0f 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_server_timeout.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_server_timeout.py @@ -17,6 +17,9 @@ server_timeout=1, ) -for table in response.tables: +try: + table = response.tables[0] df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) - print(df) \ No newline at end of file + print(df) +except TypeError: + print(response.error) diff --git a/sdk/monitor/azure-monitor-query/tests/async/test_logs_client_async.py b/sdk/monitor/azure-monitor-query/tests/async/test_logs_client_async.py index ab55eb0bb444..1050949bc33c 100644 --- a/sdk/monitor/azure-monitor-query/tests/async/test_logs_client_async.py +++ b/sdk/monitor/azure-monitor-query/tests/async/test_logs_client_async.py @@ -113,4 +113,4 @@ async def test_logs_batch_query_additional_workspaces(): assert len(response) == 3 for resp in response: - assert len(resp.body.tables[0].rows) == 2 + assert len(resp.tables[0].rows) == 2 diff --git a/sdk/monitor/azure-monitor-query/tests/test_logs_client.py b/sdk/monitor/azure-monitor-query/tests/test_logs_client.py index e2686c8c6ddd..245a6758b57e 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_logs_client.py +++ b/sdk/monitor/azure-monitor-query/tests/test_logs_client.py @@ -123,8 +123,8 @@ def test_logs_batch_query_with_statistics_in_some(): response = client.batch_query(requests) assert len(response) == 3 - assert response[0].body.statistics is None - assert response[2].body.statistics is not None + assert response[0].statistics is None + assert response[2].statistics is not None @pytest.mark.skip('https://github.com/Azure/azure-sdk-for-python/issues/19382') @pytest.mark.live_test_only @@ -171,4 +171,4 @@ def test_logs_batch_query_additional_workspaces(): response = client.batch_query(requests) for resp in response: - assert len(resp.body.tables[0].rows) == 2 + assert len(resp.tables[0].rows) == 2