Skip to content

Commit

Permalink
[Monitor][Query] Typing updates
Browse files Browse the repository at this point in the history
This addresses some apieview comments.

Signed-off-by: Paul Van Eck <[email protected]>
  • Loading branch information
pvaneck committed Feb 2, 2023
1 parent 1689659 commit d7d477d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ._exceptions import LogsQueryError

from ._models import (
LogsQueryResponse,
LogsQueryResult,
LogsTable,
LogsQueryPartialResult,
Expand All @@ -37,6 +38,7 @@
__all__ = [
"MetricAggregationType",
"LogsQueryClient",
"LogsQueryResponse",
"LogsQueryResult",
"LogsQueryPartialResult",
"LogsQueryStatus",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
process_error,
process_prefer,
)
from ._models import LogsBatchQuery, LogsQueryResult, LogsQueryPartialResult
from ._models import LogsBatchQuery, LogsQueryResponse, LogsQueryResult, LogsQueryPartialResult
from ._exceptions import LogsQueryError


Expand Down Expand Up @@ -94,7 +94,7 @@ def query_workspace(
visualization to show. If your client requires this information, specify the preference
:keyword additional_workspaces: A list of workspaces that are included in the query.
These can be qualified workspace names, workspace Ids, or Azure resource Ids.
:paramtype additional_workspaces: list[str]
:paramtype additional_workspaces: Optional[list[str]]
:return: LogsQueryResult if there is a success or LogsQueryPartialResult when there is a partial success.
:rtype: Union[~azure.monitor.query.LogsQueryResult, ~azure.monitor.query.LogsQueryPartialResult]
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down Expand Up @@ -149,7 +149,7 @@ def query_batch(
self,
queries: Union[Sequence[Dict], Sequence[LogsBatchQuery]],
**kwargs: Any
) -> List[Union[LogsQueryResult, LogsQueryPartialResult, LogsQueryError]]:
) -> List[LogsQueryResponse]:
"""Execute a list of Kusto queries. Each request can be either a LogsBatchQuery
object or an equivalent serialized model.
Expand All @@ -161,8 +161,7 @@ def query_batch(
For a given query, a LogsQueryResult is returned if the response is a success, LogsQueryPartialResult
is returned when there is a partial success and a LogsQueryError is returned when there is a failure.
The status of each response can be checked using `LogsQueryStatus` enum.
:rtype: list[Union[~azure.monitor.query.LogsQueryResult, ~azure.monitor.query.LogsQueryPartialResult,
~azure.monitor.query.LogsQueryError]
:rtype: list[~azure.monitor.query.LogsQueryResponse]
:raises: ~azure.core.exceptions.HttpResponseError
.. admonition:: Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MetricsQueryClient(object): # pylint: disable=client-accepts-api-version-k
:param credential: The credential to authenticate the client.
:type credential: ~azure.core.credentials.TokenCredential
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
:paramtype endpoint: str
:paramtype endpoint: Optional[str]
"""

def __init__(self, credential: TokenCredential, **kwargs: Any) -> None:
Expand Down Expand Up @@ -66,21 +66,21 @@ def query_resource(self, resource_uri: str, metric_names: List[str], **kwargs: A
:type metric_names: list[str]
:keyword timespan: The timespan for which to query the data. This can be a timedelta,
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]
:paramtype timespan: Optional[Union[~datetime.timedelta, tuple[~datetime.datetime, ~datetime.timedelta],
tuple[~datetime.datetime, ~datetime.datetime]]]
:keyword granularity: The granularity (i.e. timegrain) of the query.
:paramtype granularity: ~datetime.timedelta
:paramtype granularity: Optional[~datetime.timedelta]
:keyword aggregations: The list of aggregation types to retrieve. Use
`azure.monitor.query.MetricAggregationType` enum to get each aggregation type.
:paramtype aggregations: list[str]
:paramtype aggregations: Optional[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
:paramtype max_results: Optional[int]
: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 order_by: str
:paramtype order_by: Optional[str]
:keyword filter: The **$filter** is used to reduce the set of metric data returned. Example:
Metric contains metadata A, B and C. - Return all time series of C where A = a1 and B = b1 or
b2 **$filter=A eq 'a1' and B eq 'b1' or B eq 'b2' and C eq '*'** - Invalid variant: **$filter=A
Expand All @@ -93,9 +93,9 @@ def query_resource(self, resource_uri: str, metric_names: List[str], **kwargs: A
When dimension name is **dim (test) 3** and dimension value is **dim3 (test) val**, instead of using
**$filter= "dim (test) 3 eq 'dim3 (test) val'"** use **$filter= "dim
%2528test%2529 3 eq 'dim3 %2528test%2529 val'"**. Default value is None.
:paramtype filter: str
:paramtype filter: Optional[str]
:keyword metric_namespace: Metric namespace to query metric definitions for.
:paramtype metric_namespace: str
:paramtype metric_namespace: Optional[str]
:return: A MetricsQueryResult object.
:rtype: ~azure.monitor.query.MetricsQueryResult
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down Expand Up @@ -137,7 +137,7 @@ def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> ItemPaged[
:type resource_uri: str
:keyword start_time: The start time from which to query for metric
namespaces. This should be provided as a datetime object.
:paramtype start_time: ~datetime.datetime
:paramtype start_time: Optional[~datetime.datetime]
:return: An iterator like instance of either MetricNamespace or the result of cls(response)
:rtype: ~azure.core.paging.ItemPaged[~azure.monitor.query.MetricNamespace]
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down Expand Up @@ -166,7 +166,7 @@ def list_metric_definitions(self, resource_uri: str, **kwargs: Any) -> ItemPaged
:param resource_uri: The identifier of the resource.
:type resource_uri: str
:keyword namespace: Metric namespace to query metric definitions for.
:paramtype namespace: str
:paramtype namespace: Optional[str]
:return: An iterator like instance of either MetricDefinition or the result of cls(response)
:rtype: ~azure.core.paging.ItemPaged[~azure.monitor.query.MetricDefinition]
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down
32 changes: 20 additions & 12 deletions sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import sys
from typing import Any, Optional, List, Union, Tuple, Dict, Iterator

from typing_extensions import Protocol

from ._enums import LogsQueryStatus, MetricAggregationType, MetricClass, MetricNamespaceClassification, MetricUnit
from ._exceptions import LogsQueryError
from ._generated._serialization import Deserializer
Expand All @@ -24,6 +26,13 @@
JSON = MutableMapping[str, Any] # pylint: disable=unsubscriptable-object


class LogsQueryResponse(Protocol):
"""Protocol compatible with all log query responses."""

status: LogsQueryStatus
"""The status of the response."""


class LogsTableRow:
"""Represents a single row in logs table.
Expand Down Expand Up @@ -184,7 +193,7 @@ class Metric:
"""The unit of the metric. To access these values, use the MetricUnit enum.
Possible values include "Count", "Bytes", "Seconds", "CountPerSecond", "BytesPerSecond", "Percent",
"MilliSeconds", "ByteSeconds", "Unspecified", "Cores", "MilliCores", "NanoCores", "BitsPerSecond"."""
timeseries: TimeSeriesElement
timeseries: List[TimeSeriesElement]
"""The time series returned when a data query is performed."""
display_description: str
"""Detailed description of this metric."""
Expand Down Expand Up @@ -288,7 +297,7 @@ def __getitem__(self, metric):
class LogsBatchQuery:
"""A single request in a batch. The batch query API accepts a list of these objects.
:param workspace_id: Workspace Id to be included in the query.
:param workspace_id: Workspace ID to be included in the query.
:type workspace_id: str
:param query: The Analytics query. Learn more about the `Analytics query syntax
<https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/>`_.
Expand All @@ -298,22 +307,21 @@ class LogsBatchQuery:
:paramtype timespan: ~datetime.timedelta or tuple[~datetime.datetime, ~datetime.timedelta]
or tuple[~datetime.datetime, ~datetime.datetime]
:keyword additional_workspaces: A list of workspaces that are included in the query.
These can be qualified workspace names, workspace Ids, or Azure resource Ids.
:paramtype additional_workspaces: list[str]
:keyword int server_timeout: the server timeout. The default timeout is 3 minutes,
These can be qualified workspace names, workspace IDs, or Azure resource IDs.
:paramtype additional_workspaces: Optional[list[str]]
:keyword server_timeout: the server timeout. The default timeout is 3 minutes,
and the maximum timeout is 10 minutes.
:keyword bool include_statistics: To get information about query statistics.
:keyword bool include_visualization: In the query language, it is possible to specify different
:paramtype server_timeout: Optional[int]
:keyword include_statistics: To get information about query statistics.
:paramtype include_statistics: Optional[bool]
:keyword include_visualization: In the query language, it is possible to specify different
visualization options. By default, the API does not return information regarding the type of
visualization to show.
:paramtype include_visualization: Optional[bool]
"""

id: str
"""The id of the query."""
body: Dict[str, Any]
"""The body of the query."""
headers: Dict[str, str]
"""The headers of the query."""
"""The ID of the query."""
workspace: str
"""The workspace ID to be included in the query."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from .._generated.aio._client import MonitorQueryClient
from .._helpers import construct_iso8601, order_results, process_error, process_prefer
from .._models import LogsQueryResult, LogsBatchQuery, LogsQueryPartialResult
from .._models import LogsQueryResponse, LogsQueryResult, LogsBatchQuery, LogsQueryPartialResult
from ._helpers_async import get_authentication_policy
from .._exceptions import LogsQueryError

Expand All @@ -30,7 +30,7 @@ class LogsQueryClient(object): # pylint: disable=client-accepts-api-version-keyw
:param credential: The credential to authenticate the client
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
:keyword endpoint: The endpoint to connect to. Defaults to 'https://api.loganalytics.io/v1'.
:paramtype endpoint: str
:paramtype endpoint: Optional[str]
"""

def __init__(self, credential: AsyncTokenCredential, **kwargs: Any) -> None:
Expand Down Expand Up @@ -80,7 +80,7 @@ async def query_workspace(
visualization to show. If your client requires this information, specify the preference
:keyword additional_workspaces: A list of workspaces that are included in the query.
These can be qualified workspace names, workspace Ids, or Azure resource Ids.
:paramtype additional_workspaces: list[str]
:paramtype Optional[additional_workspaces: list[str]]
:return: LogsQueryResult if there is a success or LogsQueryPartialResult when there is a partial success.
:rtype: ~azure.monitor.query.LogsQueryResult or ~azure.monitor.query.LogsQueryPartialResult
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down Expand Up @@ -123,7 +123,7 @@ async def query_workspace(
@distributed_trace_async
async def query_batch(
self, queries: Union[Sequence[Dict], Sequence[LogsBatchQuery]], **kwargs: Any
) -> List[Union[LogsQueryResult, LogsQueryError, LogsQueryPartialResult]]:
) -> List[LogsQueryResponse]:
"""Execute a list of analytics queries. Each request can be either a LogsBatchQuery
object or an equivalent serialized model.
Expand All @@ -135,8 +135,7 @@ async def query_batch(
For a given query, a LogsQueryResult is returned if the response is a success, LogsQueryPartialResult
is returned when there is a partial success and a LogsQueryError is returned when there is a failure.
The status of each response can be checked using `LogsQueryStatus` enum.
:rtype: list[~azure.monitor.query.LogsQueryResult or ~azure.monitor.query.LogsQueryPartialResult
or ~azure.monitor.query.LogsQueryError]
:rtype: list[~azure.monitor.query.LogsQueryResponse]
:raises: ~azure.core.exceptions.HttpResponseError
"""
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MetricsQueryClient(object): # pylint: disable=client-accepts-api-version-k
:param credential: The credential to authenticate the client
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
:paramtype endpoint: str
:paramtype endpoint: Optional[str]
"""

def __init__(self, credential: AsyncTokenCredential, **kwargs: Any) -> None:
Expand Down Expand Up @@ -61,21 +61,21 @@ async def query_resource(
:type metric_names: list
:keyword timespan: The timespan for which to query the data. This can be a timedelta,
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]
:paramtype timespan: Optional[Union[~datetime.timedelta, tuple[~datetime.datetime, ~datetime.timedelta],
tuple[~datetime.datetime, ~datetime.datetime]]]
:keyword granularity: The interval (i.e. timegrain) of the query.
:paramtype granularity: ~datetime.timedelta
:paramtype granularity: Optional[~datetime.timedelta]
:keyword aggregations: The list of aggregation types to retrieve.
Use `azure.monitor.query.MetricAggregationType` enum to get each aggregation type.
:paramtype aggregations: list[str]
:paramtype aggregations: Optional[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
:paramtype max_results: Optional[int]
: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 order_by: str
:paramtype order_by: Optional[str]
:keyword filter: The **$filter** is used to reduce the set of metric data returned. Example:
Metric contains metadata A, B and C. - Return all time series of C where A = a1 and B = b1 or
b2 **$filter=A eq 'a1' and B eq 'b1' or B eq 'b2' and C eq '*'** - Invalid variant: **$filter=A
Expand All @@ -88,9 +88,9 @@ async def query_resource(
When dimension name is **dim (test) 3** and dimension value is **dim3 (test) val**, instead of using
**$filter= "dim (test) 3 eq 'dim3 (test) val'"** use **$filter= "dim
%2528test%2529 3 eq 'dim3 %2528test%2529 val'"**. Default value is None.
:paramtype filter: str
:paramtype filter: Optional[str]
:keyword metric_namespace: Metric namespace to query metric definitions for.
:paramtype metric_namespace: str
:paramtype metric_namespace: Optional[str]
:return: A MetricsQueryResult object.
:rtype: ~azure.monitor.query.MetricsQueryResult
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down Expand Up @@ -124,7 +124,7 @@ def list_metric_namespaces(
:type resource_uri: str
:keyword start_time: The start time from which to query for metric
namespaces. This should be provided as a datetime object.
:paramtype start_time: ~datetime.datetime
:paramtype start_time: Optional[~datetime.datetime]
:return: An iterator like instance of either MetricNamespace or the result of cls(response)
:rtype: ~azure.core.paging.AsyncItemPaged[:class: `~azure.monitor.query.MetricNamespace`]
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down Expand Up @@ -155,7 +155,7 @@ def list_metric_definitions(
:param resource_uri: The identifier of the resource.
:type resource_uri: str
:keyword namespace: Metric namespace to query metric definitions for.
:paramtype namespace: str
:paramtype namespace: Optional[str]
:return: An iterator like instance of either MetricDefinition or the result of cls(response)
:rtype: ~azure.core.paging.AsyncItemPaged[:class: `~azure.monitor.query.MetricDefinition`]
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down

0 comments on commit d7d477d

Please sign in to comment.