Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle value types for results #20358

Merged
merged 6 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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