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

Update metrics model #917

Merged
merged 6 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions octorun/src/bin/app-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ if (fileContents && host) {
'Content-Type': 'application/json'
}
};
if (config.token) {
options.headers['Authorization'] = 'token ' + config.token;
}

var req = https.request(options, function (res) {
var success = res.statusCode == 200;
Expand Down
2 changes: 1 addition & 1 deletion octorun/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f497f7aa3d
bd66a20a
2 changes: 1 addition & 1 deletion script
2 changes: 1 addition & 1 deletion src/GitHub.Api/Application/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void ContinueLogin(LoginResult loginResult, string code)
.Start();
}

private GitHubUser GetCurrentUser()
public GitHubUser GetCurrentUser()
{
var keychainConnection = keychain.Connections.FirstOrDefault(x => x.Host == OriginalUrl);
if (keychainConnection == null)
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.Api/Application/ApplicationManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void Initialize()
ApplicationConfiguration.GitTimeout = UserSettings.Get(Constants.GitTimeoutKey, ApplicationConfiguration.GitTimeout);
Platform.Initialize(ProcessManager, TaskManager);
progress.OnProgress += progressReporter.UpdateProgress;
UsageTracker = new UsageTracker(TaskManager, GitClient, ProcessManager, UserSettings, Environment, InstanceId.ToString());
UsageTracker = new UsageTracker(TaskManager, GitClient, ProcessManager, UserSettings, Environment, Platform.Keychain, InstanceId.ToString());

#if ENABLE_METRICS
var metricsService = new MetricsService(ProcessManager,
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.Api/Installer/OctorunInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class OctorunInstallDetails
public const string DefaultZipMd5Url = "http://github-vs.s3.amazonaws.com/unity/octorun/octorun.zip.md5";
public const string DefaultZipUrl = "http://github-vs.s3.amazonaws.com/unity/octorun/octorun.zip";

public const string PackageVersion = "f497f7aa3d";
public const string PackageVersion = "bd66a20a";
private const string PackageName = "octorun";
private const string zipFile = "octorun.zip";

Expand Down
1 change: 1 addition & 0 deletions src/GitHub.Api/Metrics/UsageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Dimensions
public string UnityVersion { get; set; }
public string Lang { get; set; }
public string CurrentLang { get; set; }
public string GitHubUser { get; set; }
}

public class Measures
Expand Down
37 changes: 35 additions & 2 deletions src/GitHub.Api/Metrics/UsageTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace GitHub.Unity
{
class UsageTrackerSync : IUsageTracker
{

#if DEVELOPER_BUILD
protected internal const int MetrisReportTimeout = 30;
#else
protected internal const int MetrisReportTimeout = 3 * 60;
#endif

private static ILogging Logger { get; } = LogHelper.GetLogger<UsageTracker>();

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

Logger.Trace("userId:{0} instanceId:{1}", userId, instanceId);
if (Enabled)
RunTimer(3 * 60);
RunTimer(MetrisReportTimeout);
}

private void RunTimer(int seconds)
Expand Down Expand Up @@ -94,6 +101,11 @@ private void SendUsage()
return;
}

var username = GetUsername();
if (!String.IsNullOrEmpty(username)) {
extractReports.ForEach(x => x.Dimensions.GitHubUser = username);
}

try
{
MetricsService.PostUsage(extractReports);
Expand Down Expand Up @@ -316,6 +328,11 @@ public virtual void UpdateLfsDiskUsage(int kilobytes)
}
}

protected virtual string GetUsername()
{
return "";
}

public bool Enabled
{
get
Expand Down Expand Up @@ -344,7 +361,9 @@ class UsageTracker : UsageTrackerSync
{
public UsageTracker(ITaskManager taskManager, IGitClient gitClient, IProcessManager processManager,
ISettings userSettings,
IEnvironment environment, string instanceId)
IEnvironment environment,
IKeychain keychain,
string instanceId)
: base(userSettings,
new UsageLoader(environment.UserCachePath.Combine(Constants.UsageFile)),
environment.UnityVersion, instanceId)
Expand All @@ -353,6 +372,7 @@ public UsageTracker(ITaskManager taskManager, IGitClient gitClient, IProcessMana
Environment = environment;
GitClient = gitClient;
ProcessManager = processManager;
Keychain = keychain;
}

protected override void CaptureRepoSize()
Expand All @@ -377,6 +397,18 @@ protected override void CaptureRepoSize()
catch {}
}

protected override string GetUsername()
{
string username = "";
try {
var apiClient = new ApiClient("", Keychain, ProcessManager, TaskManager, Environment);
var user = apiClient.GetCurrentUser();
username = user.Login;
} catch {
}
return username;
}

public override void IncrementApplicationMenuMenuItemCommandLine() => TaskManager.Run(base.IncrementApplicationMenuMenuItemCommandLine);
public override void IncrementAuthenticationViewButtonAuthentication() => TaskManager.Run(base.IncrementAuthenticationViewButtonAuthentication);
public override void IncrementBranchesViewButtonCheckoutLocalBranch() => TaskManager.Run(base.IncrementBranchesViewButtonCheckoutLocalBranch);
Expand All @@ -400,6 +432,7 @@ protected override void CaptureRepoSize()
protected IEnvironment Environment { get; }
protected IGitClient GitClient { get; }
public IProcessManager ProcessManager { get; }
protected IKeychain Keychain { get; }
}

interface IUsageLoader
Expand Down
4 changes: 2 additions & 2 deletions src/GitHub.Api/Resources/octorun.zip
Git LFS file not shown
2 changes: 1 addition & 1 deletion src/GitHub.Api/Resources/octorun.zip.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a41ad2fd5ceaacb20574a0fc2841e82d
f6865e64072e9b65fa31ac9087fe1363
13 changes: 13 additions & 0 deletions src/tests/TestWebServer/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Newtonsoft.Json;

namespace TestWebServer
{
Expand Down Expand Up @@ -106,6 +107,18 @@ private void Process(HttpListenerContext context)

if (context.Request.Url.AbsolutePath == "/api/usage/unity")
{
var streamReader = new StreamReader(context.Request.InputStream);
string body = null;
using (streamReader)
{
body = streamReader.ReadToEnd();
}

var parsedJson = JsonConvert.DeserializeObject(body);
var formattedJson = JsonConvert.SerializeObject(parsedJson, Formatting.Indented);

Logger.Info(formattedJson);

var json = new { result = "Cool unity usage" }.ToJson();
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentLength64 = json.Length;
Expand Down
4 changes: 4 additions & 0 deletions src/tests/TestWebServer/TestWebServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Newtonsoft.Json.11.0.2\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -81,6 +84,7 @@
<None Include="files\unity\releases\github-for-unity-99.2.0-beta1.unitypackage">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\GitHub.Api\GitHub.Api.csproj">
Expand Down
4 changes: 4 additions & 0 deletions src/tests/TestWebServer/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net35" />
</packages>