Skip to content

Commit

Permalink
Handle value types for results (Azure#20358)
Browse files Browse the repository at this point in the history
* Handle value types for results

* update test

* lint

* comprehension

* more precis

* fix test
  • Loading branch information
rakshith91 authored and hildurhodd committed Aug 30, 2021
1 parent 2067cef commit 9e13708
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions sdk/monitor/azure-monitor-query/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- `metric_namespace_name` is renamed to `fully_qualified_namespace`
- `is_dimension_required` is renamed to `dimension_required`
- `time_grain` is renamed to `granularity`
- `LogsQueryResult` now returns `datetime` objects for a time values.

### Bugs Fixed

Expand Down
12 changes: 11 additions & 1 deletion sdk/monitor/azure-monitor-query/azure/monitor/query/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# --------------------------------------------------------------------------
from datetime import datetime, timedelta
from typing import TYPE_CHECKING
from msrest import Serializer
from msrest import Serializer, Deserializer
from azure.core.exceptions import HttpResponseError
from azure.core.pipeline.policies import BearerTokenCredentialPolicy

Expand Down Expand Up @@ -81,3 +81,13 @@ def construct_iso8601(timespan=None):
else:
iso_str = duration
return iso_str

def native_col_type(col_type, value):
if col_type == 'datetime':
value = Deserializer.deserialize_iso(value)
elif col_type in ('timespan', 'guid'):
value = str(value)
return value

def process_row(col_types, row):
return [native_col_type(col_types[ind].type, val) for ind, val in enumerate(row)]
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import uuid
from typing import Any, Optional, List

from ._helpers import construct_iso8601
from ._helpers import construct_iso8601, process_row
from ._generated.models import (
Column as InternalColumn,
BatchQueryRequest as InternalLogQueryRequest,
Expand All @@ -32,7 +32,7 @@ def __init__(self, name, columns, rows):
# type: (str, List[LogsQueryResultColumn], List[List[str]]) -> None
self.name = name
self.columns = columns
self.rows = rows
self.rows = [process_row(self.columns, row) for row in rows]

@classmethod
def _from_generated(cls, generated):
Expand Down
31 changes: 31 additions & 0 deletions sdk/monitor/azure-monitor-query/tests/test_logs_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from datetime import datetime
import pytest
import six
import os

from azure.identity import ClientSecretCredential
from azure.monitor.query import LogsQueryClient

def _credential():
credential = ClientSecretCredential(
client_id = os.environ['AZURE_CLIENT_ID'],
client_secret = os.environ['AZURE_CLIENT_SECRET'],
tenant_id = os.environ['AZURE_TENANT_ID']
)
return credential

@pytest.mark.live_test_only
def test_query_response_types():
credential = _credential()
client = LogsQueryClient(credential)
query = """AppRequests |
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId, Success, ItemCount, DurationMs"""

# returns LogsQueryResult
result = client.query(os.environ['LOG_WORKSPACE_ID'], query, timespan=None)
assert isinstance(result.tables[0].rows[0][0], datetime) # TimeGenerated generated is a datetime
assert isinstance(result.tables[0].rows[0][1], six.string_types) # _ResourceId generated is a string
assert isinstance(result.tables[0].rows[0][2], bool) # Success generated is a bool
assert isinstance(result.tables[0].rows[0][3], int) # ItemCount generated is a int
assert isinstance(result.tables[0].rows[0][4], float) # DurationMs generated is a real

0 comments on commit 9e13708

Please sign in to comment.