incorporate CR feedback for the first PR

This commit is contained in:
Lakshan Fernando 2016-03-28 17:17:21 -07:00 committed by Dan Quirk
parent 6b84a10e57
commit 689a72ffb5
6 changed files with 80 additions and 72 deletions

View file

@ -14,8 +14,8 @@ namespace Microsoft.DotNet.Cli.Utils
public static readonly string AnsiPassThru = Prefix + "ANSI_PASS_THRU";
}
private static Lazy<bool> _verbose = new Lazy<bool>(() => Env.GetBool(Variables.Verbose));
private static Lazy<bool> _ansiPassThru = new Lazy<bool>(() => Env.GetBool(Variables.AnsiPassThru));
private static Lazy<bool> _verbose = new Lazy<bool>(() => Env.GetEnvironmentVariableAsBool(Variables.Verbose));
private static Lazy<bool> _ansiPassThru = new Lazy<bool>(() => Env.GetEnvironmentVariableAsBool(Variables.AnsiPassThru));
public static bool IsVerbose()
{

View file

@ -34,28 +34,9 @@ namespace Microsoft.DotNet.Cli.Utils
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
}
public static bool GetBool(string name, bool defaultValue = false)
public static bool GetEnvironmentVariableAsBool(string name, bool defaultValue = false)
{
var str = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrEmpty(str))
{
return defaultValue;
}
switch (str.ToLowerInvariant())
{
case "true":
case "1":
case "yes":
return true;
case "false":
case "0":
case "no":
return false;
default:
return defaultValue;
}
return _environment.GetEnvironmentVariableAsBool(name, defaultValue);
}
}
}

View file

@ -93,5 +93,29 @@ namespace Microsoft.DotNet.Cli.Utils
return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr);
}
public bool GetEnvironmentVariableAsBool(string name, bool defaultValue)
{
var str = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrEmpty(str))
{
return defaultValue;
}
switch (str.ToLowerInvariant())
{
case "true":
case "1":
case "yes":
return true;
case "false":
case "0":
case "no":
return false;
default:
return defaultValue;
}
}
}
}

View file

@ -16,5 +16,7 @@ namespace Microsoft.DotNet.Cli.Utils
string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions);
string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions);
bool GetEnvironmentVariableAsBool(string name, bool defaultValue);
}
}

View file

@ -7,51 +7,44 @@ namespace Microsoft.DotNet.Cli.Utils
{
public class Telemetry
{
private class Variables
{
public static readonly string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
private static readonly string Prefix = "DOTNET_CLI_TELEMETRY_";
public static readonly string Optout = Prefix + "OPTOUT";
}
private class Properties
{
public static readonly string OSVersion = "OS Version";
public static readonly string OSPlatform = "OS Platform";
public static readonly string RuntimeId = "Runtime Id";
public static readonly string ProductVersion = "Product Version";
}
private static bool _isInitialized = false;
private static TelemetryClient _client = null;
private static Dictionary<string, string> _commonProperties = null;
private static Dictionary<string, double> _commonMeasurements = null;
static Telemetry()
private static readonly string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT";
private const string OSVersion = "OS Version";
private const string OSPlatform = "OS Platform";
private const string RuntimeId = "Runtime Id";
private const string ProductVersion = "Product Version";
public Telemetry()
{
if (_isInitialized)
return;
bool Optout = Env.GetBool(Variables.Optout);
bool optout = Env.GetEnvironmentVariableAsBool(TelemetryOptout);
if (Optout)
if (optout)
return;
try
{
_client = new TelemetryClient();
_client.InstrumentationKey = Variables.InstrumentationKey;
_client.InstrumentationKey = InstrumentationKey;
_client.Context.Session.Id = Guid.NewGuid().ToString();
var runtimeEnvironment = PlatformServices.Default.Runtime;
_client.Context.Device.OperatingSystem = runtimeEnvironment.OperatingSystem;
_commonProperties = new Dictionary<string, string>();
_commonProperties.Add(Properties.OSVersion, runtimeEnvironment.OperatingSystemVersion);
_commonProperties.Add(Properties.OSPlatform, runtimeEnvironment.OperatingSystemPlatform.ToString());
_commonProperties.Add(Properties.RuntimeId, runtimeEnvironment.GetRuntimeIdentifier());
_commonProperties.Add(Properties.ProductVersion, Product.Version);
_commonProperties.Add(OSVersion, runtimeEnvironment.OperatingSystemVersion);
_commonProperties.Add(OSPlatform, runtimeEnvironment.OperatingSystemPlatform.ToString());
_commonProperties.Add(RuntimeId, runtimeEnvironment.GetRuntimeIdentifier());
_commonProperties.Add(ProductVersion, Product.Version);
_commonMeasurements = new Dictionary<string, double>();
_isInitialized = true;
@ -59,23 +52,24 @@ namespace Microsoft.DotNet.Cli.Utils
catch (Exception) { }
}
public static void TrackCommand(string command, IDictionary<string, string> properties = null, IDictionary<string, double> measurements = null)
public void TrackCommand(string command, IDictionary<string, string> properties = null, IDictionary<string, double> measurements = null)
{
if (!_isInitialized)
return;
Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties);
if (properties != null)
{
foreach (var p in properties)
{
if (eventProperties.ContainsKey(p.Key))
eventProperties[p.Key] = p.Value;
else
eventProperties.Add(p.Key, p.Value);
}
}
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements);
Dictionary<string, string> eventProperties = GetEventProperties(properties);
try
{
_client.TrackEvent(command, eventProperties, eventMeasurements);
_client.Flush();
}
catch (Exception) { }
}
private Dictionary<string, double> GetEventMeasures(IDictionary<string, double> measurements)
{
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);
if (measurements != null)
{
@ -87,13 +81,23 @@ namespace Microsoft.DotNet.Cli.Utils
eventMeasurements.Add(m.Key, m.Value);
}
}
return eventMeasurements;
}
try
private Dictionary<string, string> GetEventProperties(IDictionary<string, string> properties)
{
Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties);
if (properties != null)
{
_client.TrackEvent(command, eventProperties, eventMeasurements);
_client.Flush();
foreach (var p in properties)
{
if (eventProperties.ContainsKey(p.Key))
eventProperties[p.Key] = p.Value;
else
eventProperties.Add(p.Key, p.Value);
}
}
catch (Exception) { }
return eventProperties;
}
}
}

View file

@ -117,18 +117,13 @@ namespace Microsoft.DotNet.Cli
["test"] = TestCommand.Run
};
int exitCode = 100;
string arguments = string.Empty;
int exitCode;
var arguments = string.Empty;
Func<string[], int> builtIn;
if (builtIns.TryGetValue(command, out builtIn))
{
exitCode = builtIn(appArgs.ToArray());
appArgs.ToList().ForEach(a => { arguments += a + " "; });
arguments = string.Join(" ", appArgs);
}
else
{
@ -140,7 +135,9 @@ namespace Microsoft.DotNet.Cli
exitCode = result.ExitCode;
}
Telemetry.TrackCommand(
Telemetry telemetryClient = new Telemetry();
telemetryClient.TrackCommand(
command,
new Dictionary<string, string>
{
@ -155,7 +152,7 @@ namespace Microsoft.DotNet.Cli
}
private static void PrintVersion()
private static void PrintVersion()
{
Reporter.Output.WriteLine(Product.Version);
}