Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 14bab1c

Browse files
Merge pull request #917 from github-for-unity/fixes/save-username
Update metrics model
2 parents 12891c2 + 1eb33c3 commit 14bab1c

File tree

13 files changed

+65
-13
lines changed

13 files changed

+65
-13
lines changed

octorun/src/bin/app-usage.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ if (fileContents && host) {
4444
'Content-Type': 'application/json'
4545
}
4646
};
47-
if (config.token) {
48-
options.headers['Authorization'] = 'token ' + config.token;
49-
}
5047

5148
var req = https.request(options, function (res) {
5249
var success = res.statusCode == 200;

octorun/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f497f7aa3d
1+
bd66a20a

script

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void ContinueLogin(LoginResult loginResult, string code)
205205
.Start();
206206
}
207207

208-
private GitHubUser GetCurrentUser()
208+
public GitHubUser GetCurrentUser()
209209
{
210210
var keychainConnection = keychain.Connections.FirstOrDefault(x => x.Host == OriginalUrl);
211211
if (keychainConnection == null)

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected void Initialize()
4848
ApplicationConfiguration.GitTimeout = UserSettings.Get(Constants.GitTimeoutKey, ApplicationConfiguration.GitTimeout);
4949
Platform.Initialize(ProcessManager, TaskManager);
5050
progress.OnProgress += progressReporter.UpdateProgress;
51-
UsageTracker = new UsageTracker(TaskManager, GitClient, ProcessManager, UserSettings, Environment, InstanceId.ToString());
51+
UsageTracker = new UsageTracker(TaskManager, GitClient, ProcessManager, UserSettings, Environment, Platform.Keychain, InstanceId.ToString());
5252

5353
#if ENABLE_METRICS
5454
var metricsService = new MetricsService(ProcessManager,

src/GitHub.Api/Installer/OctorunInstaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class OctorunInstallDetails
8585
public const string DefaultZipMd5Url = "http://github-vs.s3.amazonaws.com/unity/octorun/octorun.zip.md5";
8686
public const string DefaultZipUrl = "http://github-vs.s3.amazonaws.com/unity/octorun/octorun.zip";
8787

88-
public const string PackageVersion = "f497f7aa3d";
88+
public const string PackageVersion = "bd66a20a";
8989
private const string PackageName = "octorun";
9090
private const string zipFile = "octorun.zip";
9191

src/GitHub.Api/Metrics/UsageModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Dimensions
2222
public string UnityVersion { get; set; }
2323
public string Lang { get; set; }
2424
public string CurrentLang { get; set; }
25+
public string GitHubUser { get; set; }
2526
}
2627

2728
public class Measures

src/GitHub.Api/Metrics/UsageTracker.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ namespace GitHub.Unity
88
{
99
class UsageTrackerSync : IUsageTracker
1010
{
11+
12+
#if DEVELOPER_BUILD
13+
protected internal const int MetrisReportTimeout = 30;
14+
#else
15+
protected internal const int MetrisReportTimeout = 3 * 60;
16+
#endif
17+
1118
private static ILogging Logger { get; } = LogHelper.GetLogger<UsageTracker>();
1219

1320
private static object _lock = new object();
@@ -44,7 +51,7 @@ public UsageTrackerSync(ISettings userSettings, IUsageLoader usageLoader,
4451

4552
Logger.Trace("userId:{0} instanceId:{1}", userId, instanceId);
4653
if (Enabled)
47-
RunTimer(3 * 60);
54+
RunTimer(MetrisReportTimeout);
4855
}
4956

5057
private void RunTimer(int seconds)
@@ -94,6 +101,11 @@ private void SendUsage()
94101
return;
95102
}
96103

104+
var username = GetUsername();
105+
if (!String.IsNullOrEmpty(username)) {
106+
extractReports.ForEach(x => x.Dimensions.GitHubUser = username);
107+
}
108+
97109
try
98110
{
99111
MetricsService.PostUsage(extractReports);
@@ -316,6 +328,11 @@ public virtual void UpdateLfsDiskUsage(int kilobytes)
316328
}
317329
}
318330

331+
protected virtual string GetUsername()
332+
{
333+
return "";
334+
}
335+
319336
public bool Enabled
320337
{
321338
get
@@ -344,7 +361,9 @@ class UsageTracker : UsageTrackerSync
344361
{
345362
public UsageTracker(ITaskManager taskManager, IGitClient gitClient, IProcessManager processManager,
346363
ISettings userSettings,
347-
IEnvironment environment, string instanceId)
364+
IEnvironment environment,
365+
IKeychain keychain,
366+
string instanceId)
348367
: base(userSettings,
349368
new UsageLoader(environment.UserCachePath.Combine(Constants.UsageFile)),
350369
environment.UnityVersion, instanceId)
@@ -353,6 +372,7 @@ public UsageTracker(ITaskManager taskManager, IGitClient gitClient, IProcessMana
353372
Environment = environment;
354373
GitClient = gitClient;
355374
ProcessManager = processManager;
375+
Keychain = keychain;
356376
}
357377

358378
protected override void CaptureRepoSize()
@@ -377,6 +397,18 @@ protected override void CaptureRepoSize()
377397
catch {}
378398
}
379399

400+
protected override string GetUsername()
401+
{
402+
string username = "";
403+
try {
404+
var apiClient = new ApiClient("", Keychain, ProcessManager, TaskManager, Environment);
405+
var user = apiClient.GetCurrentUser();
406+
username = user.Login;
407+
} catch {
408+
}
409+
return username;
410+
}
411+
380412
public override void IncrementApplicationMenuMenuItemCommandLine() => TaskManager.Run(base.IncrementApplicationMenuMenuItemCommandLine);
381413
public override void IncrementAuthenticationViewButtonAuthentication() => TaskManager.Run(base.IncrementAuthenticationViewButtonAuthentication);
382414
public override void IncrementBranchesViewButtonCheckoutLocalBranch() => TaskManager.Run(base.IncrementBranchesViewButtonCheckoutLocalBranch);
@@ -400,6 +432,7 @@ protected override void CaptureRepoSize()
400432
protected IEnvironment Environment { get; }
401433
protected IGitClient GitClient { get; }
402434
public IProcessManager ProcessManager { get; }
435+
protected IKeychain Keychain { get; }
403436
}
404437

405438
interface IUsageLoader

src/GitHub.Api/Resources/octorun.zip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:c7287864b7d86835175494c3743832f47a10481ab7fb1c257f871b808744e448
3-
size 1399629
2+
oid sha256:95a82d30f9e3a3f1eaec7d4a382983748d35320b87a2ec91134beb5d75738af3
3+
size 1399526
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a41ad2fd5ceaacb20574a0fc2841e82d
1+
f6865e64072e9b65fa31ac9087fe1363

src/tests/TestWebServer/HttpServer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Net.Sockets;
99
using System.Text;
1010
using System.Threading;
11+
using Newtonsoft.Json;
1112

1213
namespace TestWebServer
1314
{
@@ -106,6 +107,18 @@ private void Process(HttpListenerContext context)
106107

107108
if (context.Request.Url.AbsolutePath == "/api/usage/unity")
108109
{
110+
var streamReader = new StreamReader(context.Request.InputStream);
111+
string body = null;
112+
using (streamReader)
113+
{
114+
body = streamReader.ReadToEnd();
115+
}
116+
117+
var parsedJson = JsonConvert.DeserializeObject(body);
118+
var formattedJson = JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
119+
120+
Logger.Info(formattedJson);
121+
109122
var json = new { result = "Cool unity usage" }.ToJson();
110123
context.Response.StatusCode = (int)HttpStatusCode.OK;
111124
context.Response.ContentLength64 = json.Length;

src/tests/TestWebServer/TestWebServer.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
3333
<ItemGroup>
34+
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
35+
<HintPath>..\..\..\packages\Newtonsoft.Json.11.0.2\lib\net35\Newtonsoft.Json.dll</HintPath>
36+
</Reference>
3437
<Reference Include="System" />
3538
<Reference Include="System.Core" />
3639
<Reference Include="System.Xml.Linq" />
@@ -81,6 +84,7 @@
8184
<None Include="files\unity\releases\github-for-unity-99.2.0-beta1.unitypackage">
8285
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8386
</None>
87+
<None Include="packages.config" />
8488
</ItemGroup>
8589
<ItemGroup>
8690
<ProjectReference Include="..\..\GitHub.Api\GitHub.Api.csproj">
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net35" />
4+
</packages>

0 commit comments

Comments
 (0)