Skip to content

Commit

Permalink
Merge pull request #549 from WildernessLabs/feature/analytics
Browse files Browse the repository at this point in the history
Add telemetry support
  • Loading branch information
adrianstevens authored Apr 25, 2024
2 parents 2603425 + e6e4b2c commit 774b27c
Show file tree
Hide file tree
Showing 21 changed files with 762 additions and 72 deletions.
5 changes: 5 additions & 0 deletions Source/v2/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.csproj]
indent_style = space
indent_size = 2
69 changes: 69 additions & 0 deletions Source/v2/Meadow.CLI/Commands/Current/TelemetryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using CliFx.Attributes;
using CliFx.Extensibility;
using Meadow.Telemetry;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("telemetry", Description = "Manage participation in telemetry sharing")]
public class TelemetryCommand : BaseCommand<TelemetryCommand>
{
public TelemetryCommand(
ILoggerFactory loggerFactory)
: base(loggerFactory)
{
}

protected override ValueTask ExecuteCommand()
{
throw new CommandException("Specify one of the telemetry commands", true);
}
}

[Command("telemetry enable", Description = "Enable and opt in to telemetry sharing")]
public class TelemetryEnableCommand : BaseCommand<TelemetryCommand>
{
private readonly ISettingsManager _settingsManager;

public TelemetryEnableCommand(
ISettingsManager settingsManager,
ILoggerFactory loggerFactory)
: base(loggerFactory)
{
_settingsManager = settingsManager;
}

protected override ValueTask ExecuteCommand()
{
_settingsManager.SaveSetting(MeadowTelemetry.TelemetryEnabledSettingName, "true");

return ValueTask.CompletedTask;
}
}

[Command("telemetry disable", Description = "Disable and opt out of telemetry sharing")]
public class TelemetryDisableCommand : BaseCommand<TelemetryCommand>
{
private readonly ISettingsManager _settingsManager;

public TelemetryDisableCommand(
ISettingsManager settingsManager,
ILoggerFactory loggerFactory)
: base(loggerFactory)
{
_settingsManager = settingsManager;
}

protected override ValueTask ExecuteCommand()
{
_settingsManager.SaveSetting(MeadowTelemetry.TelemetryEnabledSettingName, "false");
_settingsManager.DeleteSetting(MeadowTelemetry.MachineIdSettingName);

return ValueTask.CompletedTask;
}
}
26 changes: 26 additions & 0 deletions Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using CliFx;
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Telemetry;
using Microsoft.Extensions.Logging;
using Spectre.Console;
using System.Reflection;

namespace Meadow.CLI.Commands.DeviceManagement;

Expand Down Expand Up @@ -28,6 +32,23 @@ public async ValueTask ExecuteAsync(IConsole console)
_console = console;
CancellationToken = _console.RegisterCancellationHandler();

try
{
if (MeadowTelemetry.Current.ShouldAskForConsent)
{
AnsiConsole.MarkupLine(Strings.Telemetry.ConsentMessage);

var result = AnsiConsole.Confirm(Strings.Telemetry.AskToParticipate, defaultValue: true);
MeadowTelemetry.Current.SetTelemetryEnabled(result);
}

MeadowTelemetry.Current.TrackCommand(GetCommandName());
}
catch
{
// Swallow any telemetry-related exceptions
}

await ExecuteCommand();
}
catch (Exception ex) when (ex is not CommandException && ex is not CliFx.Exceptions.CommandException)
Expand All @@ -40,4 +61,9 @@ public async ValueTask ExecuteAsync(IConsole console)
throw new CommandException("Cancelled", CommandExitCode.UserCancelled);
}
}

private string? GetCommandName()
{
return GetType().GetCustomAttribute<CommandAttribute>(true)?.Name;
}
}
19 changes: 10 additions & 9 deletions Source/v2/Meadow.Cli/Meadow.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="CliFx" Version="*" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
<PackageReference Include="System.Management" Version="7.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="CliFx" Version="*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
<PackageReference Include="System.Management" Version="7.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions Source/v2/Meadow.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Meadow.Cloud.Client.Identity;
using Meadow.Package;
using Meadow.Software;
using Meadow.Telemetry;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -93,6 +94,10 @@ public static async Task<int> Main(string[] _)
Console.WriteLine($"Operation failed: {ex.Message}");
returnCode = 1;
}
finally
{
MeadowTelemetry.Current.Dispose();
}

return returnCode;
}
Expand Down
19 changes: 18 additions & 1 deletion Source/v2/Meadow.Cli/Strings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Meadow.CLI;
using Meadow.Telemetry;

namespace Meadow.CLI;

public static class Strings
{
Expand Down Expand Up @@ -65,4 +67,19 @@ public static class Strings
public const string NewMeadowDeviceNotFound = "New Meadow device not found";
public const string NoFirmwarePackagesFound = "No firmware packages found, run 'meadow firmware download' to download the latest firmware";
public const string NoDefaultFirmwarePackageSet = "No default firmware package set, run 'meadow firmware default' to set the default firmware";

public static class Telemetry
{
public const string ConsentMessage = @$"
Let's improve the Meadow experience together
--------------------------------------------
To help improve the Meadow experience, we'd like to collect anonymous usage data. This data helps us understand how our tools are used, so we can make them better for everyone. This usage data is not tied to individuals and no personally identifiable information is collected.
Our privacy policy is available at https://www.wildernesslabs.co/privacy-policy.
You can change your mind at any time by running the ""[bold]meadow telemetry [[enable|disable]][/]"" command or by setting the [bold]{MeadowTelemetry.TelemetryEnvironmentVariable}[/] environment variable to '1' or '0' ('true' or 'false', respectively).
";

public const string AskToParticipate = "Would you like to participate?";
}
}
8 changes: 4 additions & 4 deletions Source/v2/Meadow.Cloud.Client/Meadow.Cloud.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
Expand All @@ -8,12 +8,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CredentialManagement.Standard" Version="1.0.4" />
<PackageReference Include="CredentialManagement.Standard" Version="1.0.4" />
<PackageReference Include="IdentityModel.OidcClient" Version="3.1.2" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.IO.Hashing" Version="7.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
</ItemGroup>
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions Source/v2/Meadow.Dfu/Meadow.Dfu.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Source/v2/Meadow.Firmware/Meadow.Firmware.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Hcom/Meadow.HCom.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
32 changes: 16 additions & 16 deletions Source/v2/Meadow.Linker/Meadow.Linker.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,18 +12,18 @@
</ItemGroup>

<ItemGroup>
<None Update="lib\illink.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\illink.runtimeconfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\meadow_link.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\Mono.Cecil.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<None Update="lib\illink.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\illink.runtimeconfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\meadow_link.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\Mono.Cecil.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
11 changes: 6 additions & 5 deletions Source/v2/Meadow.Tooling.Core/Meadow.Tooling.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Glob" Version="1.1.9" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
<PackageReference Include="Glob" Version="1.1.9" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
<PackageReference Include="System.Management" Version="7.0.2" />
<PackageReference Include="System.Management" Version="7.0.2" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
</ItemGroup>

<ItemGroup>
Expand Down
19 changes: 14 additions & 5 deletions Source/v2/Meadow.Tooling.Core/Settings/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@ public Dictionary<string, string> GetPublicSettings()
public string? GetSetting(string setting)
{
var settings = GetSettings();
if (settings.Public.TryGetValue(setting.ToString(), out var ret))
Dictionary<string, string> target;

if (setting.StartsWith(PrivatePrefix))
{
setting = setting.Substring(PrivatePrefix.Length);
target = settings.Private;
}
else
{
return ret;
target = settings.Public;
}
else if (settings.Private.TryGetValue(setting.ToString(), out var pret))

if (target.TryGetValue(setting, out var value))
{
return pret;
return value;
}

return null;
}

Expand All @@ -59,7 +68,7 @@ public void DeleteSetting(string setting)
target = settings.Public;
}

if (target.ContainsKey(setting.ToString()))
if (target.ContainsKey(setting))
{
target.Remove(setting);

Expand Down
Loading

0 comments on commit 774b27c

Please sign in to comment.