Skip to content

Commit

Permalink
[ai-did-you-mean-this] Allow users to receive recommendations if they…
Browse files Browse the repository at this point in the history
…'ve opted out of telemetry. (#1985)
  • Loading branch information
christopher-o-toole authored Jul 14, 2020
1 parent aa0b851 commit d2f4da8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
8 changes: 6 additions & 2 deletions src/ai-did-you-mean-this/azext_ai_did_you_mean_this/_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
'\nHere are the most common ways users succeeded after [{command}] failed:'
)

TELEMETRY_MUST_BE_ENABLED_STR = (
'User must agree to telemetry before failure recovery recommendations can be presented.'
TELEMETRY_IS_DISABLED_STR = (
'User has not opted into telemetry.'
)

TELEMETRY_IS_ENABLED_STR = (
'User has opted into telemetry.'
)

TELEMETRY_MISSING_SUBSCRIPTION_ID_STR = (
Expand Down
72 changes: 38 additions & 34 deletions src/ai-did-you-mean-this/azext_ai_did_you_mean_this/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
from azext_ai_did_you_mean_this._const import (
RECOMMENDATION_HEADER_FMT_STR,
UNABLE_TO_HELP_FMT_STR,
TELEMETRY_MUST_BE_ENABLED_STR,
TELEMETRY_IS_DISABLED_STR,
TELEMETRY_IS_ENABLED_STR,
TELEMETRY_MISSING_SUBSCRIPTION_ID_STR,
TELEMETRY_MISSING_CORRELATION_ID_STR,
UNABLE_TO_CALL_SERVICE_STR
TELEMETRY_MISSING_CORRELATION_ID_STR
)
from azext_ai_did_you_mean_this._cmd_table import CommandTable

Expand All @@ -32,7 +32,7 @@
# Commands
# note: at least one command is required in order for the CLI to load the extension.
def show_extension_version():
print('Current version: 0.2.0')
print('Current version: 0.2.1')


def _log_debug(msg, *args, **kwargs):
Expand Down Expand Up @@ -128,11 +128,6 @@ def recommend_recovery_options(version, command, parameters, extension):
_log_debug('recommend_recovery_options: version: "%s", command: "%s", parameters: "%s", extension: "%s"',
version, command, parameters, extension)

# if the user doesn't agree to telemetry...
if not telemetry.is_telemetry_enabled():
_log_debug(TELEMETRY_MUST_BE_ENABLED_STR)
return result

# if the command is empty...
if not command:
# try to get the raw command field from telemetry.
Expand Down Expand Up @@ -204,39 +199,48 @@ def call_aladdin_service(command, parameters, version):

correlation_id = telemetry._session.correlation_id # pylint: disable=protected-access
subscription_id = telemetry._get_azure_subscription_id() # pylint: disable=protected-access
is_telemetry_enabled = telemetry.is_telemetry_enabled()

if subscription_id and correlation_id:
context = {
"correlationId": correlation_id,
"subscriptionId": subscription_id,
"versionNumber": version
}
telemetry_context = {
'correlationId': correlation_id,
'subscriptionId': subscription_id
}

query = {
"command": command,
"parameters": parameters
}
telemetry_context = {k: v for k, v in telemetry_context.items() if v is not None and is_telemetry_enabled}

api_url = 'https://app.aladdin.microsoft.com/api/v1.0/suggestions'
headers = {'Content-Type': 'application/json'}

try:
response = requests.get(
api_url,
params={
'query': json.dumps(query),
'clientType': 'AzureCli',
'context': json.dumps(context)
},
headers=headers)
except RequestException as ex:
_log_debug('requests.get() exception: %s', ex)
if not is_telemetry_enabled:
_log_debug(TELEMETRY_IS_DISABLED_STR)
else:
_log_debug(TELEMETRY_IS_ENABLED_STR)

if subscription_id is None:
_log_debug(TELEMETRY_MISSING_SUBSCRIPTION_ID_STR)
if correlation_id is None:
_log_debug(TELEMETRY_MISSING_CORRELATION_ID_STR)

_log_debug(UNABLE_TO_CALL_SERVICE_STR)
context = {
**telemetry_context,
"versionNumber": version
}

query = {
"command": command,
"parameters": parameters
}

api_url = 'https://app.aladdin.microsoft.com/api/v1.0/suggestions'
headers = {'Content-Type': 'application/json'}

try:
response = requests.get(
api_url,
params={
'query': json.dumps(query),
'clientType': 'AzureCli',
'context': json.dumps(context)
},
headers=headers)
except RequestException as ex:
_log_debug('requests.get() exception: %s', ex)

return response
2 changes: 1 addition & 1 deletion src/ai-did-you-mean-this/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from distutils import log as logger
logger.warn("Wheel is not available, disabling bdist_wheel hook")

VERSION = '0.2.0'
VERSION = '0.2.1'

# The full list of classifiers is available at
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down

0 comments on commit d2f4da8

Please sign in to comment.