Skip to content

Commit

Permalink
More Renaming in query (Azure#20303)
Browse files Browse the repository at this point in the history
* More Reanaming in query

* changelog

* commit 2

* some changes

* remove errror

* Update sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py

* Apply suggestions from code review

Co-authored-by: Adam Ling (MSFT) <adam_ling@outlook.com>

* Update sdk/monitor/azure-monitor-query/CHANGELOG.md

Co-authored-by: Adam Ling (MSFT) <adam_ling@outlook.com>
  • Loading branch information
2 people authored and hildurhodd committed Aug 26, 2021
1 parent 07e1f39 commit 19f4dce
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 26 deletions.
5 changes: 4 additions & 1 deletion sdk/monitor/azure-monitor-query/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features Added

- Added additional `display_description` attribute to the `Metric` type.

### Breaking Changes

- Rename `batch_query` to `query_batch`.
Expand All @@ -15,7 +17,8 @@
- `top` is renamed to `max_results` in the metric's `query` API.
- `metric_namespace_name` is renamed to `fully_qualified_namespace`
- `is_dimension_required` is renamed to `dimension_required`
- `time_grain` is renamed to `granularity`
- `interval` and `time_grain` are renamed to `granularity`
- `orderby` is renamed to `order_by`
- `LogsQueryResult` now returns `datetime` objects for a time values.

### Bugs Fixed
Expand Down
4 changes: 2 additions & 2 deletions sdk/monitor/azure-monitor-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ for metric in response.metrics:

### Handle metrics response

The metrics query API returns a `MetricsResult` object. The `MetricsResult` object contains properties such as a list of `Metric`-typed objects, `interval`, `namespace`, and `timespan`. The `Metric` objects list can be accessed using the `metrics` param. Each `Metric` object in this list contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` contains `data` and `metadata_values` properties. In visual form, the object hierarchy of the response resembles the following structure:
The metrics query API returns a `MetricsResult` object. The `MetricsResult` object contains properties such as a list of `Metric`-typed objects, `granularity`, `namespace`, and `timespan`. The `Metric` objects list can be accessed using the `metrics` param. Each `Metric` object in this list contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` contains `data` and `metadata_values` properties. In visual form, the object hierarchy of the response resembles the following structure:

```
MetricsResult
|---interval
|---granularity
|---timespan
|---cost
|---namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ def query(self, resource_uri, metric_names, **kwargs):
a timedelta and a start datetime, or a start datetime/end datetime.
:paramtype timespan: ~datetime.timedelta or tuple[~datetime.datetime, ~datetime.timedelta]
or tuple[~datetime.datetime, ~datetime.datetime]
:keyword interval: The interval (i.e. timegrain) of the query.
:paramtype interval: ~datetime.timedelta
:keyword granularity: The granularity (i.e. timegrain) of the query.
:paramtype granularity: ~datetime.timedelta
:keyword aggregations: The list of aggregation types to retrieve. Use `azure.monitor.query.AggregationType`
enum to get each aggregation type.
:paramtype aggregations: list[str]
:keyword max_results: The maximum number of records to retrieve.
Valid only if $filter is specified.
Defaults to 10.
:paramtype max_results: int
:keyword orderby: The aggregation to use for sorting results and the direction of the sort.
:keyword order_by: The aggregation to use for sorting results and the direction of the sort.
Only one order can be specified.
Examples: sum asc.
:paramtype orderby: str
:paramtype order_by: str
:keyword filter: The **$filter** is used to reduce the set of metric data
returned.:code:`<br>`Example::code:`<br>`Metric contains metadata A, B and C.:code:`<br>`-
Return all time series of C where A = a1 and B = b1 or b2:code:`<br>`\ **$filter=A eq ‘a1’ and
Expand Down Expand Up @@ -117,6 +117,8 @@ def query(self, resource_uri, metric_names, **kwargs):
kwargs.setdefault("metricnames", ",".join(metric_names))
kwargs.setdefault("timespan", timespan)
kwargs.setdefault("top", kwargs.pop("max_results", None))
kwargs.setdefault("interval", kwargs.pop("granularity", None))
kwargs.setdefault("orderby", kwargs.pop("order_by", None))
generated = self._metrics_op.list(resource_uri, connection_verify=False, **kwargs)
return MetricsResult._from_generated(generated) # pylint: disable=protected-access

Expand Down
14 changes: 9 additions & 5 deletions sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ class MetricsResult(object):
two datetimes concatenated, separated by '/'. This may be adjusted in the future and returned
back from what was originally requested.
:vartype timespan: str
:ivar interval: The interval (window size) for which the metric data was returned in. This
:ivar granularity: The granularity (window size) for which the metric data was returned in. This
may be adjusted in the future and returned back from what was originally requested. This is
not present if a metadata request was made.
:vartype interval: ~datetime.timedelta
:vartype granularity: ~datetime.timedelta
:ivar namespace: The namespace of the metrics that has been queried.
:vartype namespace: str
:ivar resource_region: The region of the resource that has been queried for metrics.
Expand All @@ -130,7 +130,7 @@ def __init__(self, **kwargs):
# type: (Any) -> None
self.cost = kwargs.get("cost", None)
self.timespan = kwargs["timespan"]
self.interval = kwargs.get("interval", None)
self.granularity = kwargs.get("granularity", None)
self.namespace = kwargs.get("namespace", None)
self.resource_region = kwargs.get("resource_region", None)
self.metrics = kwargs["metrics"]
Expand All @@ -142,7 +142,7 @@ def _from_generated(cls, generated):
return cls(
cost=generated.cost,
timespan=generated.timespan,
interval=generated.interval,
granularity=generated.interval,
namespace=generated.namespace,
resource_region=generated.resourceregion,
metrics=[Metric._from_generated(m) for m in generated.value] # pylint: disable=protected-access
Expand Down Expand Up @@ -459,6 +459,8 @@ class Metric(object):
:vartype unit: str
:ivar timeseries: Required. The time series returned when a data query is performed.
:vartype timeseries: list[~monitor_query_client.models.TimeSeriesElement]
:ivar display_description: Detailed description of this metric.
:vartype display_description: str
"""
def __init__(
self,
Expand All @@ -470,6 +472,7 @@ def __init__(
self.name = kwargs['name']
self.unit = kwargs['unit']
self.timeseries = kwargs['timeseries']
self.display_description = kwargs['display_description']

@classmethod
def _from_generated(cls, generated):
Expand All @@ -482,7 +485,8 @@ def _from_generated(cls, generated):
unit=generated.unit,
timeseries=[
TimeSeriesElement._from_generated(t) for t in generated.timeseries # pylint: disable=protected-access
]
],
display_description=generated.display_description,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ async def query(
a timedelta and a start datetime, or a start datetime/end datetime.
:paramtype timespan: ~datetime.timedelta or tuple[~datetime.datetime, ~datetime.timedelta]
or tuple[~datetime.datetime, ~datetime.datetime]
:keyword interval: The interval (i.e. timegrain) of the query.
:paramtype interval: ~datetime.timedelta
:keyword granularity: The interval (i.e. timegrain) of the query.
:paramtype granularity: ~datetime.timedelta
:keyword aggregations: The list of aggregation types to retrieve. Use `azure.monitor.query.AggregationType`
enum to get each aggregation type.
:paramtype aggregations: list[str]
:keyword max_results: The maximum number of records to retrieve.
Valid only if $filter is specified.
Defaults to 10.
:paramtype max_results: int
:keyword orderby: The aggregation to use for sorting results and the direction of the sort.
:keyword order_by: The aggregation to use for sorting results and the direction of the sort.
Only one order can be specified.
Examples: sum asc.
:paramtype orderby: str
:paramtype order_by: str
:keyword filter: The **$filter** is used to reduce the set of metric data
returned.:code:`<br>`Example::code:`<br>`Metric contains metadata A, B and C.:code:`<br>`-
Return all time series of C where A = a1 and B = b1 or b2:code:`<br>`\ **$filter=A eq ‘a1’ and
Expand All @@ -98,6 +98,8 @@ async def query(
kwargs.setdefault("metricnames", ",".join(metric_names))
kwargs.setdefault("timespan", timespan)
kwargs.setdefault("top", kwargs.pop("max_results", None))
kwargs.setdefault("interval", kwargs.pop("granularity", None))
kwargs.setdefault("orderby", kwargs.pop("order_by", None))
aggregations = kwargs.pop("aggregations", None)
if aggregations:
kwargs.setdefault("aggregation", ",".join(aggregations))
Expand Down
20 changes: 14 additions & 6 deletions sdk/monitor/azure-monitor-query/samples/sample_log_query_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@
query = """AppRequests |
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""

query = """
AppRequests
| where TimeGenerated > ago(1h)
| fork
( summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId )
"""

# returns LogsQueryResult
response = client.query(os.environ['LOG_WORKSPACE_ID'], query, timespan=timedelta(days=1))

if not response.tables:
print("No results for the query")

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)
for table in response.tables:
try:
print ([col.name for col in table.columns])
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
response = client.list_metric_namespaces(metrics_uri)

for item in response:
print(item.metric_namespace_name)
print(item.fully_qualified_namespace)
print(item.type)
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
metrics_uri = os.environ['METRICS_RESOURCE_URI']
response = client.query(
metrics_uri,
metric_names=["MatchedEventCount"],
start_time=datetime(2021, 6, 21),
duration=timedelta(days=1),
metric_names=["MatchedEventCount", "DeliverySuccesssCount"],
timespan=timedelta(days=1),
aggregations=[AggregationType.COUNT]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def _credential():
return credential

@pytest.mark.live_test_only
@pytest.mark.asyncio
async def test_metrics_auth():
credential = _credential()
client = MetricsQueryClient(credential)
Expand All @@ -26,6 +27,21 @@ async def test_metrics_auth():
assert response
assert response.metrics

@pytest.mark.live_test_only
@pytest.mark.asyncio
async def test_metrics_granularity():
credential = _credential()
client = MetricsQueryClient(credential)
response = await client.query(
os.environ['METRICS_RESOURCE_URI'],
metric_names=["MatchedEventCount"],
timespan=timedelta(days=1),
granularity=timedelta(minutes=5),
aggregations=[AggregationType.COUNT]
)
assert response
assert response.granularity == timedelta(minutes=5)

@pytest.mark.live_test_only
async def test_metrics_namespaces():
client = MetricsQueryClient(_credential())
Expand Down
14 changes: 14 additions & 0 deletions sdk/monitor/azure-monitor-query/tests/test_metrics_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def test_metrics_auth():
assert response
assert response.metrics

@pytest.mark.live_test_only
def test_metrics_granularity():
credential = _credential()
client = MetricsQueryClient(credential)
response = client.query(
os.environ['METRICS_RESOURCE_URI'],
metric_names=["MatchedEventCount"],
timespan=timedelta(days=1),
granularity=timedelta(minutes=5),
aggregations=[AggregationType.COUNT]
)
assert response
assert response.granularity == timedelta(minutes=5)

@pytest.mark.live_test_only
def test_metrics_namespaces():
client = MetricsQueryClient(_credential())
Expand Down

0 comments on commit 19f4dce

Please sign in to comment.