Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge from master. #1

Merged
merged 9 commits into from
May 5, 2018
14 changes: 7 additions & 7 deletions src/GitHub.Api/Application/ApplicationManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,22 @@ public void RestartRepository()
}
}

protected void SetupMetrics(string unityVersion, bool firstRun)
protected void SetupMetrics(string unityVersion, bool firstRun, Guid instanceId)
{
//Logger.Trace("Setup metrics");

var usagePath = Environment.UserCachePath.Combine(Constants.UsageFile);

string id = null;
string userId = null;
if (UserSettings.Exists(Constants.GuidKey))
{
id = UserSettings.Get(Constants.GuidKey);
userId = UserSettings.Get(Constants.GuidKey);
}

if (String.IsNullOrEmpty(id))
if (String.IsNullOrEmpty(userId))
{
id = Guid.NewGuid().ToString();
UserSettings.Set(Constants.GuidKey, id);
userId = Guid.NewGuid().ToString();
UserSettings.Set(Constants.GuidKey, userId);
}

#if ENABLE_METRICS
Expand All @@ -212,7 +212,7 @@ protected void SetupMetrics(string unityVersion, bool firstRun)
Environment.NodeJsExecutablePath,
Environment.OctorunScriptPath);

UsageTracker = new UsageTracker(metricsService, UserSettings, usagePath, id, unityVersion);
UsageTracker = new UsageTracker(metricsService, UserSettings, usagePath, userId, unityVersion, instanceId.ToString());

if (firstRun)
{
Expand Down
41 changes: 19 additions & 22 deletions src/GitHub.Api/Metrics/UsageModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace GitHub.Unity
{
public class Usage
{
public string InstanceId { get; set; }
public Dimensions Dimensions { get; set; } = new Dimensions();
public Measures Measures { get; set; } = new Measures();
}
Expand All @@ -23,16 +25,16 @@ public class Dimensions
public class Measures
{
public int NumberOfStartups { get; set; }
public int NumberOfCommits { get; set; }
public int NumberOfFetches { get; set; }
public int NumberOfPushes { get; set; }
public int NumberOfPulls { get; set; }
public int NumberOfProjectsInitialized { get; set; }
public int NumberOfAuthentications { get; set; }
public int NumberOfLocalBranchCreations { get; set; }
public int NumberOfLocalBranchDeletion { get; set; }
public int NumberOfLocalBranchCheckouts { get; set; }
public int NumberOfRemoteBranchCheckouts { get; set; }
public int Commits { get; set; }
public int Fetches { get; set; }
public int Pushes { get; set; }
public int Pulls { get; set; }
public int ProjectsInitialized { get; set; }
public int Authentications { get; set; }
public int LocalBranchCreations { get; set; }
public int LocalBranchDeletion { get; set; }
public int LocalBranchCheckouts { get; set; }
public int RemoteBranchCheckouts { get; set; }
}

class UsageModel
Expand All @@ -42,7 +44,7 @@ class UsageModel

private Usage currentUsage;

public Usage GetCurrentUsage(string appVersion, string unityVersion)
public Usage GetCurrentUsage(string appVersion, string unityVersion, string instanceId)
{
Guard.ArgumentNotNullOrWhiteSpace(appVersion, "appVersion");
Guard.ArgumentNotNullOrWhiteSpace(unityVersion, "unityVersion");
Expand All @@ -51,26 +53,21 @@ public Usage GetCurrentUsage(string appVersion, string unityVersion)
if (currentUsage == null)
{
currentUsage = Reports
.FirstOrDefault(usage => usage.Dimensions.Date == date
&& usage.Dimensions.AppVersion == appVersion
&& usage.Dimensions.UnityVersion == unityVersion);
.FirstOrDefault(usage => usage.InstanceId == instanceId);
}

if (currentUsage?.Dimensions.Date == date)
{
// update any fields that might be missing, if we've changed the format
if (currentUsage.Dimensions.Guid != Guid)
currentUsage.Dimensions.Guid = Guid;
}
else
if (currentUsage == null)
{
currentUsage = new Usage
{
InstanceId = instanceId,
Dimensions = {
Date = date,
Guid = Guid,
AppVersion = appVersion,
UnityVersion = unityVersion
UnityVersion = unityVersion,
Lang = CultureInfo.InstalledUICulture.IetfLanguageTag,
CurrentLang = CultureInfo.CurrentCulture.IetfLanguageTag
}
};
Reports.Add(currentUsage);
Expand Down
37 changes: 18 additions & 19 deletions src/GitHub.Api/Metrics/UsageTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ class UsageTracker : IUsageTracker
private readonly NPath storePath;
private readonly ISettings userSettings;
private readonly IMetricsService metricsService;
private readonly string guid;
private readonly string userId;
private readonly string unityVersion;
private readonly string instanceId;
private Timer timer;

public UsageTracker(IMetricsService metricsService, ISettings userSettings, NPath storePath, string guid, string unityVersion)
public UsageTracker(IMetricsService metricsService, ISettings userSettings, NPath storePath, string userId, string unityVersion, string instanceId)
{
this.userSettings = userSettings;
this.metricsService = metricsService;
this.guid = guid;
this.userId = userId;
this.storePath = storePath;
this.unityVersion = unityVersion;
this.instanceId = instanceId;

Logger.Trace("guid:{0}", guid);
Logger.Trace("userId:{0} instanceId:{1}", userId, instanceId);
if (Enabled)
RunTimer(3*60);
}
Expand Down Expand Up @@ -66,7 +68,7 @@ private UsageStore LoadUsage()
result = new UsageStore();

if (String.IsNullOrEmpty(result.Model.Guid))
result.Model.Guid = guid;
result.Model.Guid = userId;

return result;
}
Expand Down Expand Up @@ -154,10 +156,7 @@ private async Task SendUsage()

private Usage GetCurrentUsage(UsageStore usageStore)
{
var usage = usageStore.Model.GetCurrentUsage(ApplicationConfiguration.AssemblyName.Version.ToString(), unityVersion);
usage.Dimensions.Lang = CultureInfo.InstalledUICulture.IetfLanguageTag;
usage.Dimensions.CurrentLang = CultureInfo.CurrentCulture.IetfLanguageTag;
return usage;
return usageStore.Model.GetCurrentUsage(ApplicationConfiguration.AssemblyName.Version.ToString(), unityVersion, instanceId);
}

public void IncrementNumberOfStartups()
Expand All @@ -175,7 +174,7 @@ public void IncrementNumberOfCommits()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfCommits++;
usage.Measures.Commits++;

SaveUsage(usageStore);
}
Expand All @@ -185,7 +184,7 @@ public void IncrementNumberOfFetches()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfFetches++;
usage.Measures.Fetches++;

SaveUsage(usageStore);
}
Expand All @@ -195,7 +194,7 @@ public void IncrementNumberOfPushes()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfPushes++;
usage.Measures.Pushes++;

SaveUsage(usageStore);
}
Expand All @@ -205,7 +204,7 @@ public void IncrementNumberOfProjectsInitialized()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfProjectsInitialized++;
usage.Measures.ProjectsInitialized++;

SaveUsage(usageStore);
}
Expand All @@ -215,7 +214,7 @@ public void IncrementNumberOfLocalBranchCreations()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfLocalBranchCreations++;
usage.Measures.LocalBranchCreations++;

SaveUsage(usageStore);
}
Expand All @@ -225,7 +224,7 @@ public void IncrementNumberOfLocalBranchDeletions()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfLocalBranchDeletion++;
usage.Measures.LocalBranchDeletion++;

SaveUsage(usageStore);
}
Expand All @@ -235,7 +234,7 @@ public void IncrementNumberOfLocalBranchCheckouts()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfLocalBranchCheckouts++;
usage.Measures.LocalBranchCheckouts++;

SaveUsage(usageStore);
}
Expand All @@ -245,7 +244,7 @@ public void IncrementNumberOfRemoteBranchCheckouts()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfRemoteBranchCheckouts++;
usage.Measures.RemoteBranchCheckouts++;

SaveUsage(usageStore);
}
Expand All @@ -255,7 +254,7 @@ public void IncrementNumberOfPulls()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfPulls++;
usage.Measures.Pulls++;

SaveUsage(usageStore);
}
Expand All @@ -265,7 +264,7 @@ public void IncrementNumberOfAuthentications()
var usageStore = LoadUsage();
var usage = GetCurrentUsage(usageStore);

usage.Measures.NumberOfAuthentications++;
usage.Measures.Authentications++;

SaveUsage(usageStore);
}
Expand Down
29 changes: 29 additions & 0 deletions src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ sealed class ApplicationCache : ScriptObjectSingleton<ApplicationCache>
{
[SerializeField] private bool firstRun = true;
[SerializeField] public string firstRunAtString;
[SerializeField] public string instanceIdString;
[NonSerialized] private Guid? instanceId;
[NonSerialized] private bool? firstRunValue;
[NonSerialized] public DateTimeOffset? firstRunAtValue;

Expand Down Expand Up @@ -60,6 +62,33 @@ private void EnsureFirstRun()
Save(true);
}
}

public Guid InstanceId
{
get
{
EnsureInstanceId();
return instanceId.Value;
}
}

private void EnsureInstanceId()
{
if (instanceId.HasValue)
{
return;
}

if (string.IsNullOrEmpty(instanceIdString))
{
instanceId = Guid.NewGuid();
instanceIdString = instanceId.ToString();
}
else
{
instanceId = new Guid(instanceIdString);
}
}
}

sealed class EnvironmentCache : ScriptObjectSingleton<EnvironmentCache>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ApplicationManager(IMainThreadSynchronizationContext synchronizationConte

protected override void SetupMetrics()
{
SetupMetrics(Environment.UnityVersion, ApplicationCache.Instance.FirstRun);
SetupMetrics(Environment.UnityVersion, ApplicationCache.Instance.FirstRun, ApplicationCache.Instance.InstanceId);
}

protected override void InitializeUI()
Expand Down