Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 861a41b

Browse files
committed
Add some working metrics tests
1 parent abcbda9 commit 861a41b

File tree

17 files changed

+368
-125
lines changed

17 files changed

+368
-125
lines changed

GitHubVS.sln

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHub.Logging", "src\GitHu
106106
EndProject
107107
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHub.Services.Vssdk", "src\GitHub.Services.Vssdk\GitHub.Services.Vssdk.csproj", "{2D3D2834-33BE-45CA-B3CC-12F853557D7B}"
108108
EndProject
109+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Metrics", "Metrics", "{C2D88962-BD6B-4F11-B914-535B38377962}"
110+
EndProject
111+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MetricsServer", "test\MetricsTests\MetricsServer\MetricsServer.csproj", "{14FDEE91-7301-4247-846C-049647BF8E99}"
112+
EndProject
113+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MetricsTests", "test\MetricsTests\MetricsTests\MetricsTests.csproj", "{09313E65-7ADB-48C1-AD3A-572020C5BDCB}"
114+
EndProject
109115
Global
110116
GlobalSection(SolutionConfigurationPlatforms) = preSolution
111117
Debug|Any CPU = Debug|Any CPU
@@ -415,6 +421,26 @@ Global
415421
{2D3D2834-33BE-45CA-B3CC-12F853557D7B}.Release|Any CPU.Build.0 = Release|Any CPU
416422
{2D3D2834-33BE-45CA-B3CC-12F853557D7B}.ReleaseWithoutVsix|Any CPU.ActiveCfg = Release|Any CPU
417423
{2D3D2834-33BE-45CA-B3CC-12F853557D7B}.ReleaseWithoutVsix|Any CPU.Build.0 = Release|Any CPU
424+
{14FDEE91-7301-4247-846C-049647BF8E99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
425+
{14FDEE91-7301-4247-846C-049647BF8E99}.Debug|Any CPU.Build.0 = Debug|Any CPU
426+
{14FDEE91-7301-4247-846C-049647BF8E99}.DebugCodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
427+
{14FDEE91-7301-4247-846C-049647BF8E99}.DebugCodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
428+
{14FDEE91-7301-4247-846C-049647BF8E99}.DebugWithoutVsix|Any CPU.ActiveCfg = Debug|Any CPU
429+
{14FDEE91-7301-4247-846C-049647BF8E99}.DebugWithoutVsix|Any CPU.Build.0 = Debug|Any CPU
430+
{14FDEE91-7301-4247-846C-049647BF8E99}.Release|Any CPU.ActiveCfg = Release|Any CPU
431+
{14FDEE91-7301-4247-846C-049647BF8E99}.Release|Any CPU.Build.0 = Release|Any CPU
432+
{14FDEE91-7301-4247-846C-049647BF8E99}.ReleaseWithoutVsix|Any CPU.ActiveCfg = Release|Any CPU
433+
{14FDEE91-7301-4247-846C-049647BF8E99}.ReleaseWithoutVsix|Any CPU.Build.0 = Release|Any CPU
434+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
435+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
436+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.DebugCodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
437+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.DebugCodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
438+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.DebugWithoutVsix|Any CPU.ActiveCfg = Debug|Any CPU
439+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.DebugWithoutVsix|Any CPU.Build.0 = Debug|Any CPU
440+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
441+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.Release|Any CPU.Build.0 = Release|Any CPU
442+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.ReleaseWithoutVsix|Any CPU.ActiveCfg = Release|Any CPU
443+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB}.ReleaseWithoutVsix|Any CPU.Build.0 = Release|Any CPU
418444
EndGlobalSection
419445
GlobalSection(SolutionProperties) = preSolution
420446
HideSolutionNode = FALSE
@@ -438,5 +464,8 @@ Global
438464
{7B835A7D-CF94-45E8-B191-96F5A4FE26A8} = {8A7DA2E7-262B-4581-807A-1C45CE79CDFD}
439465
{110B206F-8554-4B51-BF86-94DAA32F5E26} = {8A7DA2E7-262B-4581-807A-1C45CE79CDFD}
440466
{17EB676B-BB91-48B5-AA59-C67695C647C2} = {8A7DA2E7-262B-4581-807A-1C45CE79CDFD}
467+
{C2D88962-BD6B-4F11-B914-535B38377962} = {8A7DA2E7-262B-4581-807A-1C45CE79CDFD}
468+
{14FDEE91-7301-4247-846C-049647BF8E99} = {C2D88962-BD6B-4F11-B914-535B38377962}
469+
{09313E65-7ADB-48C1-AD3A-572020C5BDCB} = {C2D88962-BD6B-4F11-B914-535B38377962}
441470
EndGlobalSection
442471
EndGlobal

src/GitHub.Exports/Models/UsageModel.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ namespace GitHub.Models
44
{
55
public class UsageModel
66
{
7-
public DimensionsModel Dimensions { get; set; }
8-
public MeasuresModel Measures { get; set; }
7+
public DimensionsModel Dimensions { get; set; } = new DimensionsModel();
8+
public MeasuresModel Measures { get; set; } = new MeasuresModel();
9+
10+
protected UsageModel() { }
911

1012
public static UsageModel Create(Guid guid)
1113
{
@@ -14,9 +16,8 @@ public static UsageModel Create(Guid guid)
1416
Dimensions = new DimensionsModel
1517
{
1618
Guid = guid,
17-
Date = DateTime.Now,
18-
},
19-
Measures = new MeasuresModel(),
19+
Date = DateTimeOffset.Now,
20+
}
2021
};
2122
}
2223

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Reflection;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
[assembly: AssemblyTitle("GitHub.Exports")]
56
[assembly: AssemblyDescription("GitHub interfaces for mef exports")]
67
[assembly: Guid("9aea02db-02b5-409c-b0ca-115d05331a6b")]
8+
[assembly: InternalsVisibleTo("MetricsTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c1a49bb6a01873a4e5434cbaa9993f5a75d3ba66fecfff4d85066c7094913d43f6f9bb706d2eb65f4990a7adfe7321e120ba8be5a8367fc5551c0a6eeb4850c5646377d314142abe69a5fb8ec0957a63ba5893901adb5b4cf0eb7f7e5861376e0cb4fd975576094b2fb4843df9c186eb17232365291b93e17f5e2b6cc1cda2c2")]

src/GitHub.Exports/Services/MetricsService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Octokit;
1111
using Octokit.Internal;
1212

13-
namespace GitHub.App
13+
namespace GitHub.Services
1414
{
1515
[Export(typeof(IMetricsService))]
1616
[PartCreationPolicy(CreationPolicy.NonShared)]
@@ -99,7 +99,7 @@ public Task SendOptIn()
9999
*/
100100
}
101101

102-
static StringContent SerializeRequest(UsageModel model)
102+
internal static StringContent SerializeRequest(UsageModel model)
103103
{
104104
var serializer = new SimpleJsonSerializer();
105105
var dictionary = new Dictionary<string, object>

src/GitHub.VisualStudio/Services/UsageTracker.cs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,34 @@ public async Task IncrementCounter(Expression<Func<UsageModel.MeasuresModel, int
5959

6060
IDisposable StartTimer()
6161
{
62-
return service.StartTimer(TimerTick, TimeSpan.FromMinutes(3), TimeSpan.FromHours(8));
62+
return service.StartTimer(TimerTick, TimeSpan.FromMinutes(3), TimeSpan.FromDays(1));
6363
}
6464

6565
async Task Initialize()
6666
{
67+
if (initialized)
68+
return;
69+
6770
// The services needed by the usage tracker are loaded when they are first needed to
6871
// improve the startup time of the extension.
69-
if (!initialized)
70-
{
71-
await ThreadingHelper.SwitchToMainThreadAsync();
72+
await ThreadingHelper.SwitchToMainThreadAsync();
7273

73-
client = gitHubServiceProvider.TryGetService<IMetricsService>();
74-
connectionManager = gitHubServiceProvider.GetService<IConnectionManager>();
75-
vsservices = gitHubServiceProvider.GetService<IVSServices>();
76-
initialized = true;
77-
}
74+
client = gitHubServiceProvider.TryGetService<IMetricsService>();
75+
connectionManager = gitHubServiceProvider.GetService<IConnectionManager>();
76+
vsservices = gitHubServiceProvider.GetService<IVSServices>();
77+
initialized = true;
7878
}
7979

8080
async Task TimerTick()
8181
{
8282
await Initialize();
8383

84+
if (firstTick)
85+
{
86+
await IncrementCounter(x => x.NumberOfStartups);
87+
firstTick = false;
88+
}
89+
8490
if (client == null || !userSettings.CollectMetrics)
8591
{
8692
timer.Dispose();
@@ -89,16 +95,8 @@ async Task TimerTick()
8995
}
9096

9197
var data = await service.ReadLocalData();
92-
var changed = false;
93-
94-
if (firstTick)
95-
{
96-
var current = await GetCurrentReport(data);
97-
current.Measures.NumberOfStartups++;
98-
changed = true;
99-
firstTick = false;
100-
}
10198

99+
var changed = false;
102100
for (var i = data.Reports.Count - 1; i >= 0; --i)
103101
{
104102
if (data.Reports[i].Dimensions.Date.Date != DateTimeOffset.Now.Date)
@@ -138,16 +136,8 @@ async Task<UsageModel> GetCurrentReport(UsageData data)
138136
current.Dimensions.AppVersion = AssemblyVersionInformation.Version;
139137
current.Dimensions.VSVersion = vsservices.VSVersion;
140138

141-
if (connectionManager.Connections.Any(x => x.HostAddress.IsGitHubDotCom()))
142-
{
143-
current.Dimensions.IsGitHubUser = true;
144-
}
145-
146-
if (connectionManager.Connections.Any(x => !x.HostAddress.IsGitHubDotCom()))
147-
{
148-
current.Dimensions.IsEnterpriseUser = true;
149-
}
150-
139+
current.Dimensions.IsGitHubUser = connectionManager.Connections.Any(x => x.HostAddress.IsGitHubDotCom());
140+
current.Dimensions.IsEnterpriseUser = connectionManager.Connections.Any(x => !x.HostAddress.IsGitHubDotCom());
151141
return current;
152142
}
153143
}

test/MetricsTests/MetricsServer/MetricsServer.csproj

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
<NuGetPackageImportStamp>
16+
</NuGetPackageImportStamp>
1517
</PropertyGroup>
1618
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1719
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -22,6 +24,7 @@
2224
<DefineConstants>DEBUG;TRACE</DefineConstants>
2325
<ErrorReport>prompt</ErrorReport>
2426
<WarningLevel>4</WarningLevel>
27+
<NoWarn>8002,618</NoWarn>
2528
</PropertyGroup>
2629
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2730
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -36,18 +39,19 @@
3639
<StartupObject />
3740
</PropertyGroup>
3841
<PropertyGroup>
39-
<SignAssembly>false</SignAssembly>
42+
<SignAssembly>true</SignAssembly>
4043
</PropertyGroup>
4144
<PropertyGroup>
4245
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
4346
</PropertyGroup>
4447
<ItemGroup>
45-
<Reference Include="Nancy, Version=1.4.4.0, Culture=neutral, processorArchitecture=MSIL">
46-
<HintPath>..\packages\Nancy.1.4.4\lib\net40\Nancy.dll</HintPath>
48+
<Reference Include="Nancy, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
49+
<SpecificVersion>False</SpecificVersion>
50+
<HintPath>$(SolutionDir)\packages\Nancy.1.4.1\lib\net40\Nancy.dll</HintPath>
4751
<Private>True</Private>
4852
</Reference>
4953
<Reference Include="Nancy.Hosting.Self, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
50-
<HintPath>..\packages\Nancy.Hosting.Self.1.4.1\lib\net40\Nancy.Hosting.Self.dll</HintPath>
54+
<HintPath>$(SolutionDir)\packages\Nancy.Hosting.Self.1.4.1\lib\net40\Nancy.Hosting.Self.dll</HintPath>
5155
<Private>True</Private>
5256
</Reference>
5357
<Reference Include="System" />
@@ -62,19 +66,32 @@
6266
<ItemGroup>
6367
<Compile Include="Program.cs" />
6468
<Compile Include="Properties\AssemblyInfo.cs" />
65-
<Compile Include="..\..\..\src\GitHub.Exports\Models\UsageData.cs">
69+
<None Include="..\..\..\src\GitHub.Exports\Models\UsageData.cs">
6670
<Link>UsageData.cs</Link>
67-
</Compile>
68-
<Compile Include="..\..\..\src\GitHub.Exports\Models\UsageModel.cs">
71+
</None>
72+
<None Include="..\..\..\src\GitHub.Exports\Models\UsageModel.cs">
6973
<Link>UsageModel.cs</Link>
70-
</Compile>
74+
</None>
7175
</ItemGroup>
7276
<ItemGroup>
7377
<None Include="App.config" />
7478
<None Include="key.snk" />
7579
<None Include="packages.config" />
7680
</ItemGroup>
81+
<ItemGroup>
82+
<ProjectReference Include="..\..\..\src\GitHub.Exports\GitHub.Exports.csproj">
83+
<Project>{9AEA02DB-02B5-409C-B0CA-115D05331A6B}</Project>
84+
<Name>GitHub.Exports</Name>
85+
</ProjectReference>
86+
</ItemGroup>
7787
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
88+
<Import Project="..\..\..\packages\StrongNamer.0.0.6\build\StrongNamer.targets" Condition="Exists('..\..\..\packages\StrongNamer.0.0.6\build\StrongNamer.targets')" />
89+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
90+
<PropertyGroup>
91+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
92+
</PropertyGroup>
93+
<Error Condition="!Exists('..\..\..\packages\StrongNamer.0.0.6\build\StrongNamer.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\StrongNamer.0.0.6\build\StrongNamer.targets'))" />
94+
</Target>
7895
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7996
Other similar extension points exist, see Microsoft.Common.targets.
8097
<Target Name="BeforeBuild">

test/MetricsTests/MetricsServer/Program.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
using Nancy.Responses.Negotiation;
66
using System;
77
using System.Collections.Generic;
8-
using System.Diagnostics;
9-
using System.Text;
10-
using System.Threading;
11-
using Nancy.Extensions;
128

139
namespace MetricsServer
1410
{
@@ -42,21 +38,25 @@ public UsageModule()
4238
{
4339
Post["/api/usage/visualstudio"] = p =>
4440
{
45-
Console.WriteLine(Request.Body.AsString());
4641
var errors = new List<string>();
4742
var usage = this.Bind<UsageModel>();
48-
if (String.IsNullOrEmpty(usage.AppVersion))
43+
if (String.IsNullOrEmpty(usage.Dimensions.AppVersion))
4944
errors.Add("Empty appVersion");
50-
if (String.IsNullOrEmpty(usage.Lang))
45+
Version result = null;
46+
if (!Version.TryParse(usage.Dimensions.AppVersion, out result))
47+
errors.Add("Invalid appVersion");
48+
if (String.IsNullOrEmpty(usage.Dimensions.Lang))
5149
errors.Add("Empty lang");
52-
if (String.IsNullOrEmpty(usage.VSVersion))
50+
if (String.IsNullOrEmpty(usage.Dimensions.VSVersion))
5351
errors.Add("Empty vSVersion");
54-
if (usage.NumberOfStartups == 0)
52+
if (usage.Dimensions.Date == DateTimeOffset.MinValue)
53+
errors.Add("Empty date");
54+
if (usage.Measures.NumberOfStartups == 0)
5555
errors.Add("Startups is 0");
5656
if (errors.Count > 0)
5757
{
5858
return Negotiate
59-
.WithStatusCode(HttpStatusCode.BadRequest)
59+
.WithStatusCode(HttpStatusCode.InternalServerError)
6060
.WithAllowedMediaRange(MediaRange.FromString("application/json"))
6161
.WithMediaRangeModel(
6262
MediaRange.FromString("application/json"),
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Nancy" version="1.4.4" targetFramework="net461" />
3+
<package id="Nancy" version="1.4.1" targetFramework="net461" />
44
<package id="Nancy.Hosting.Self" version="1.4.1" targetFramework="net461" />
5+
<package id="StrongNamer" version="0.0.6" targetFramework="net461" />
56
</packages>

test/MetricsTests/MetricsServerApp/MetricsServerApp.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<None Include="App.config" />
5151
</ItemGroup>
5252
<ItemGroup>
53+
<ProjectReference Include="..\..\..\src\GitHub.Exports\GitHub.Exports.csproj">
54+
<Project>{9aea02db-02b5-409c-b0ca-115d05331a6b}</Project>
55+
<Name>GitHub.Exports</Name>
56+
</ProjectReference>
5357
<ProjectReference Include="..\..\..\submodules\octokit.net\Octokit\Octokit.csproj">
5458
<Project>{08dd4305-7787-4823-a53f-4d0f725a07f3}</Project>
5559
<Name>Octokit</Name>

test/MetricsTests/MetricsServerApp/Program.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,21 @@ async Task Send()
4242
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
4343
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(uri, "/api/usage/visualstudio"));
4444

45+
var model = UsageModel.Create(Guid.NewGuid());
46+
model.Dimensions.AppVersion = "9.9.9";
47+
model.Dimensions.Lang = "en-us";
48+
model.Dimensions.VSVersion = "14";
49+
model.Measures.NumberOfStartups = 1;
50+
4551
var data = new UsageData();
46-
var model = data.Model;
47-
model.AppVersion = "9.9.9";
48-
model.Lang = "en-us";
49-
model.VSVersion = "14";
50-
model.NumberOfStartups = 1;
52+
data.Reports = new List<UsageModel> { model };
5153

5254
request.Content = SerializeRequest(model);
5355

5456
HttpResponseMessage response = null;
5557
try
5658
{
5759
response = await client.SendAsync(request);
58-
5960
}
6061
catch (Exception ex)
6162
{

0 commit comments

Comments
 (0)