Implemented Telemetry class via an interface for test ease

This commit is contained in:
Lakshan Fernando 2016-03-31 16:37:40 -07:00 committed by Dan Quirk
parent d9e169e77a
commit 25745d9d4e
4 changed files with 75 additions and 20 deletions

View file

@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
namespace Microsoft.DotNet.Cli.Utils
{
public interface ITelemetry
{
void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements);
}
}

View file

@ -6,7 +6,7 @@ using System.Diagnostics;
namespace Microsoft.DotNet.Cli.Utils
{
public class Telemetry
public class Telemetry : ITelemetry
{
private static bool _isInitialized = false;
private static TelemetryClient _client = null;
@ -23,13 +23,12 @@ namespace Microsoft.DotNet.Cli.Utils
public Telemetry()
{
if (_isInitialized)
return;
bool optout = Env.GetEnvironmentVariableAsBool(TelemetryOptout);
if (optout)
{
return;
}
try
{
@ -56,17 +55,19 @@ namespace Microsoft.DotNet.Cli.Utils
}
}
public void TrackCommand(string command, IDictionary<string, string> properties = null, IDictionary<string, double> measurements = null)
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
{
if (!_isInitialized)
{
return;
}
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements);
Dictionary<string, string> eventProperties = GetEventProperties(properties);
try
{
_client.TrackEvent(command, eventProperties, eventMeasurements);
_client.TrackEvent(eventName, eventProperties, eventMeasurements);
_client.Flush();
}
catch (Exception) { }
@ -78,12 +79,16 @@ namespace Microsoft.DotNet.Cli.Utils
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);
if (measurements != null)
{
foreach (var m in measurements)
foreach (var measurement in measurements)
{
if (eventMeasurements.ContainsKey(m.Key))
eventMeasurements[m.Key] = m.Value;
if (eventMeasurements.ContainsKey(measurement.Key))
{
eventMeasurements[measurement.Key] = measurement.Value;
}
else
eventMeasurements.Add(m.Key, m.Value);
{
eventMeasurements.Add(measurement.Key, measurement.Value);
}
}
}
return eventMeasurements;
@ -94,12 +99,16 @@ namespace Microsoft.DotNet.Cli.Utils
Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties);
if (properties != null)
{
foreach (var p in properties)
foreach (var property in properties)
{
if (eventProperties.ContainsKey(p.Key))
eventProperties[p.Key] = p.Value;
if (eventProperties.ContainsKey(property.Key))
{
eventProperties[property.Key] = property.Value;
}
else
eventProperties.Add(p.Key, p.Value);
{
eventProperties.Add(property.Key, property.Value);
}
}
}
return eventProperties;

View file

@ -26,14 +26,13 @@ namespace Microsoft.DotNet.Cli
{
public class Program
{
public static int Main(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
try
{
return ProcessArgs(args);
return new Program().ProcessArgs(args, new Telemetry());
}
catch (CommandUnknownException e)
{
@ -44,7 +43,7 @@ namespace Microsoft.DotNet.Cli
}
private static int ProcessArgs(string[] args)
public int ProcessArgs(string[] args, ITelemetry telemetryClient)
{
// CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.
@ -135,10 +134,9 @@ namespace Microsoft.DotNet.Cli
exitCode = result.ExitCode;
}
Telemetry telemetryClient = new Telemetry();
telemetryClient.TrackCommand(
telemetryClient.TrackEvent(
command,
null,
new Dictionary<string, double>
{
["ExitCode"] = exitCode

View file

@ -0,0 +1,35 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
using FluentAssertions;
namespace Microsoft.DotNet.Tests
{
public class MockTelemetry : ITelemetry
{
public string EventName{get;set;}
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
{
EventName = eventName;
}
}
public class TelemetryCommandTests : TestBase
{
[Fact]
public void TestProjectDependencyIsNotAvailableThroughDriver()
{
Program program = new Program();
MockTelemetry mockTelemetry = new MockTelemetry();
string[] args = { "help" };
program.ProcessArgs(args, mockTelemetry);
Assert.Equal(mockTelemetry.EventName, args[0]);
}
}
}