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

Commit 6697363

Browse files
author
Steven Kirk
committed
Updated metrics model/service.
And increment launch count when package initialized.
1 parent 96a989f commit 6697363

File tree

4 files changed

+60
-259
lines changed

4 files changed

+60
-259
lines changed

src/GitHub.App/Services/UsageTracker.cs

Lines changed: 45 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using GitHub.Caches;
1111
using GitHub.Extensions.Reactive;
1212
using GitHub.Models;
13-
using GitHub.Services;
1413
using GitHub.Settings;
1514
using ReactiveUI;
1615
using Rothko;
@@ -19,68 +18,48 @@
1918

2019
namespace GitHub.Services
2120
{
22-
using Microsoft.VisualStudio.Shell;
23-
using Guard = GitHub.Extensions.Guard;
24-
2521
[Export(typeof(IUsageTracker))]
2622
[PartCreationPolicy(CreationPolicy.Shared)]
2723
public class UsageTracker : IUsageTracker
2824
{
29-
readonly IMetricsService client;
30-
31-
readonly Lazy<ISharedCache> cache;
32-
readonly Lazy<IRepositoryHosts> repositoryHosts;
33-
//readonly Lazy<IAppVersionProvider> appVersionProvider;
34-
readonly Lazy<IEnvironment> environment;
35-
////readonly Lazy<IRepositoryHosts> trackedRepositories;
36-
readonly IPackageSettings userSettings;
37-
3825
// Whenever you add a counter make sure it gets added to _both_
3926
// BuildUsageModel and ClearCounters
40-
internal const string GHLastSubmissionKey = "GHLastSubmission";
41-
internal const string GHCommitCountKey = "GHCommitCount";
42-
internal const string GHSyncCountKey = "GHSyncCount";
43-
internal const string GHCloneCountKey = "GHCloneCount";
44-
internal const string GHShellLaunchCountKey = "GHShellLaunchCount";
45-
internal const string GHLaunchCountKeyDay = "GHLaunchCountDay";
46-
internal const string GHLaunchCountKeyWeek = "GHLaunchCountWeek";
47-
internal const string GHLaunchCountKeyMonth = "GHLaunchCountMonth";
48-
internal const string GHPartialCommitCount = "GHPartialCommitCount";
49-
internal const string GHTutorialRunCount = "GHTutorialRunCount";
50-
internal const string GHOpenInExplorerCount = "GHOpenInExplorerCount";
51-
internal const string GHOpenInShellCount = "GHOpenInShellCount";
52-
internal const string GHBranchSwitchCount = "GHBranchSwitchCount";
53-
internal const string GHDiscardChangesCount = "GHDiscardChangesCount";
54-
internal const string GHOpenedURLCount = "GHOpenedURLCount";
55-
internal const string GHLfsDiffCount = "GHLfsDiffCount";
56-
internal const string GHMergeCommitCount = "GHMergeCommitCount";
57-
internal const string GHMergeConflictCount = "GHMergeConflictCount";
58-
internal const string GHOpenInEditorCount = "GHOpenInEditorCount";
59-
internal const string GHUpstreamPullRequestCount = "GHUpstreamPullRequestCount";
27+
const string GHLastSubmissionKey = "GHLastSubmission";
28+
const string GHCommitCountKey = "GHCommitCount";
29+
const string GHCreateCountKey = "GHCreateCountKey";
30+
const string GHCloneCountKey = "GHCloneCount";
31+
const string GHPublishCountKey = "GHPublishCountKey";
32+
const string GHGistCountKey = "GHPublishCountKey";
33+
const string GHOpenInGitHubCountKey = "GHOpenInGitHubCountKey";
34+
const string GHLinkToGitHubCountKey = "GHLinkToGitHubCountKey";
35+
const string GHLoginCountKey = "GHLoginCountKey";
36+
const string GHLaunchCountKeyDay = "GHLaunchCountDay";
37+
const string GHLaunchCountKeyWeek = "GHLaunchCountWeek";
38+
const string GHLaunchCountKeyMonth = "GHLaunchCountMonth";
39+
const string GHUpstreamPullRequestCount = "GHUpstreamPullRequestCount";
6040

6141
static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
6242

63-
IBlobCache localMachineCache { get { return cache.Value.LocalMachine; } }
43+
readonly IMetricsService client;
44+
readonly Lazy<ISharedCache> cache;
45+
readonly Lazy<IRepositoryHosts> repositoryHosts;
46+
readonly IPackageSettings userSettings;
6447

6548
[ImportingConstructor]
6649
public UsageTracker(
6750
Lazy<ISharedCache> cache,
68-
////Lazy<ITrackedRepositories> trackedRepositories,
6951
Lazy<IRepositoryHosts> repositoryHosts,
70-
////Lazy<IAppVersionProvider> appVersionProvider,
71-
Lazy<IEnvironment> environment,
7252
IPackageSettings userSettings,
7353
IUIProvider serviceProvider)
7454
{
7555
this.cache = cache;
76-
////this.trackedRepositories = trackedRepositories;
7756
this.repositoryHosts = repositoryHosts;
78-
////this.appVersionProvider = appVersionProvider;
79-
this.environment = environment;
8057
this.userSettings = userSettings;
8158
this.client = (IMetricsService)serviceProvider.GetService(typeof(IMetricsService));
8259
}
8360

61+
IBlobCache LocalMachineCache => cache.Value.LocalMachine;
62+
8463
IObservable<Unit> SubmitIfNeeded()
8564
{
8665
if (client != null)
@@ -122,13 +101,13 @@ IObservable<Unit> SubmitIfNeeded()
122101

123102
IObservable<DateTimeOffset> GetLastUpdated()
124103
{
125-
return Observable.Defer(() => localMachineCache.GetObject<DateTimeOffset>(GHLastSubmissionKey))
104+
return Observable.Defer(() => LocalMachineCache.GetObject<DateTimeOffset>(GHLastSubmissionKey))
126105
.Catch<DateTimeOffset, KeyNotFoundException>(_ => Observable.Return(DateTimeOffset.MinValue));
127106
}
128107

129108
IObservable<Unit> StoreLastUpdated(DateTimeOffset lastUpdated)
130109
{
131-
return Observable.Defer(() => localMachineCache.InsertObject(GHLastSubmissionKey, lastUpdated));
110+
return Observable.Defer(() => LocalMachineCache.InsertObject(GHLastSubmissionKey, lastUpdated));
132111
}
133112

134113
static Calendar cal = CultureInfo.InvariantCulture.Calendar;
@@ -152,23 +131,16 @@ static int GetIso8601WeekOfYear(DateTime time)
152131
IObservable<Unit> ClearCounters(bool weekly, bool monthly)
153132
{
154133
var standardCounters = new[] {
155-
GHCommitCountKey,
156-
GHSyncCountKey,
157-
GHCloneCountKey,
158-
GHShellLaunchCountKey,
159134
GHLaunchCountKeyDay,
160-
GHPartialCommitCount,
161-
GHTutorialRunCount,
162-
GHOpenInExplorerCount,
163-
GHOpenInShellCount,
164-
GHBranchSwitchCount,
165-
GHDiscardChangesCount,
166-
GHOpenedURLCount,
167-
GHLfsDiffCount,
168-
GHMergeCommitCount,
169-
GHMergeConflictCount,
170-
GHOpenInEditorCount,
171-
GHUpstreamPullRequestCount
135+
GHUpstreamPullRequestCount,
136+
GHCloneCountKey,
137+
GHCreateCountKey,
138+
GHPublishCountKey,
139+
GHCommitCountKey,
140+
GHGistCountKey,
141+
GHOpenInGitHubCountKey,
142+
GHLinkToGitHubCountKey,
143+
GHLoginCountKey,
172144
};
173145

174146
var counters = standardCounters
@@ -183,98 +155,52 @@ IObservable<Unit> ClearCounters(bool weekly, bool monthly)
183155

184156
IObservable<Unit> ClearCounter(string key)
185157
{
186-
return Observable.Defer(() => localMachineCache.InvalidateObject<int>(key));
158+
return Observable.Defer(() => LocalMachineCache.InvalidateObject<int>(key));
187159
}
188160

189161
IObservable<int> GetCounter(string key)
190162
{
191-
return Observable.Defer(() => localMachineCache.GetObject<int>(key))
163+
return Observable.Defer(() => LocalMachineCache.GetObject<int>(key))
192164
.Catch<int, KeyNotFoundException>(_ => Observable.Return(0));
193165
}
194166

195167
IObservable<Unit> SaveCounter(string key, int value)
196168
{
197-
return Observable.Defer(() => localMachineCache.InsertObject(key, value));
169+
return Observable.Defer(() => LocalMachineCache.InsertObject(key, value));
198170
}
199171

200172
IObservable<UsageModel> BuildUsageModel(bool weekly, bool monthly)
201173
{
202-
////var repositories = trackedRepositories.Value.Repositories;
203174
var hosts = repositoryHosts.Value;
204175

205-
var model = new UsageModel
206-
{
207-
////NumberOfRepositories = repositories.Count,
208-
////NumberOfGitHubRepositories = repositories.Count(x => x.IsHosted),
209-
////NumberOfGitHubForks = repositories.Count(x => x.IsHosted && x.IsFork),
210-
////NumberOfRepositoryOwners = repositories.Count(r => IsOwner(r, hosts))
211-
};
212-
176+
var model = new UsageModel();
213177

214178
if (hosts.GitHubHost?.IsLoggedIn == true)
215179
{
216180
model.IsGitHubUser = true;
217-
////model.NumberOfOrgs = gitHubHost.Organizations.Count();
218181
}
219182

220183
if (hosts.EnterpriseHost?.IsLoggedIn == true)
221184
{
222185
model.IsEnterpriseUser = true;
223186
}
224187

225-
var env = environment.Value;
226-
227-
model.OsVersion = env.OSVersion.Version.ToString();
228-
model.Is64BitOperatingSystem = env.Is64BitOperatingSystem;
229188
model.Lang = CultureInfo.InstalledUICulture.IetfLanguageTag;
230-
231-
////try
232-
////{
233-
//// model.RamMB = (int)(env.GetTotalInstalledPhysicalMemory() / 1024 / 1024);
234-
////}
235-
////catch (Exception ex)
236-
////{
237-
//// // This shouldn't really throw but let's be super defensive.
238-
//// log.Warn("Could not get total installed physical memory", ex);
239-
////}
240-
241-
try
242-
{
243-
var currentProcess = Process.GetCurrentProcess();
244-
var elapsedSinceStart = DateTime.Now - currentProcess.StartTime;
245-
model.SecondsSinceLaunch = Math.Max(0, (int)elapsedSinceStart.TotalSeconds);
246-
}
247-
catch (Exception ex)
248-
{
249-
log.Warn("Could not get process uptime", ex);
250-
}
251-
252-
////model.AppVersion = appVersionProvider.Value.Version.ToString();
189+
model.AppVersion = AssemblyVersionInformation.Version;
253190

254191
var counters = new List<IObservable<int>>
255192
{
256-
GetCounter(GHCommitCountKey).Do(x => model.NumberOfCommits = x),
257-
GetCounter(GHCloneCountKey).Do(x => model.NumberOfClones = x),
258-
GetCounter(GHSyncCountKey).Do(x => model.NumberOfSyncs = x),
259-
260-
// NB: We're using this in a slightly different way than MAC. On mac this means
261-
// whether or not a user has installed their command line tools, for us (since it's
262-
// always installed I've made it track whether or not the user has launched the Git
263-
// shell. We might be able to get some insight into how many of our users use the CLI.
264-
GetCounter(GHShellLaunchCountKey).Do(x => model.InstalledCommandLineTools = x > 0),
265193
GetCounter(GHLaunchCountKeyDay).Do(x => model.NumberOfStartups = x),
266-
GetCounter(GHPartialCommitCount).Do(x => model.NumberOfPartialCommits = x),
267-
GetCounter(GHTutorialRunCount).Do(x => model.NumberOfTutorialRuns = x),
268-
GetCounter(GHOpenInExplorerCount).Do(x => model.NumberOfOpenOnDisks = x),
269-
GetCounter(GHOpenInShellCount).Do(x => model.NumberOfOpenInShells = x),
270-
GetCounter(GHBranchSwitchCount).Do(x => model.NumberOfBranchSwitches = x),
271-
GetCounter(GHDiscardChangesCount).Do(x => model.NumberOfDiscardChanges = x),
272-
GetCounter(GHOpenedURLCount).Do(x => model.NumberOfOpenedURLs = x),
273-
GetCounter(GHLfsDiffCount).Do(x => model.NumberOfLFSDiffs = x),
274-
GetCounter(GHMergeCommitCount).Do(x => model.NumberOfMergeCommits = x),
275-
GetCounter(GHMergeConflictCount).Do(x => model.NumberOfMergeConflicts = x),
276-
GetCounter(GHOpenInEditorCount).Do(x => model.NumberOfOpenInExternalEditors = x),
194+
GetCounter(GHLaunchCountKeyWeek).Do(x => model.NumberOfStartupsWeek = x),
195+
GetCounter(GHLaunchCountKeyMonth).Do(x => model.NumberOfStartupsMonth = x),
277196
GetCounter(GHUpstreamPullRequestCount).Do(x => model.NumberOfUpstreamPullRequests = x),
197+
GetCounter(GHCloneCountKey).Do(x => model.NumberOfClones = x),
198+
GetCounter(GHCreateCountKey).Do(x => model.NumberOfReposCreated = x),
199+
GetCounter(GHPublishCountKey).Do(x => model.NumberOfReposPublished = x),
200+
GetCounter(GHGistCountKey).Do(x => model.NumberOfGists = x),
201+
GetCounter(GHOpenInGitHubCountKey).Do(x => model.NumberOfOpenInGitHub = x),
202+
GetCounter(GHLinkToGitHubCountKey).Do(x => model.NumberOfLinkToGitHub = x),
203+
GetCounter(GHLoginCountKey).Do(x => model.NumberOfLogins = x),
278204
};
279205

280206
if (weekly)
@@ -293,31 +219,6 @@ IObservable<UsageModel> BuildUsageModel(bool weekly, bool monthly)
293219
.ContinueAfter(() => Observable.Return(model));
294220
}
295221

296-
static bool IsOwner(IRepositoryModel repo, IRepositoryHosts hosts)
297-
{
298-
Guard.ArgumentNotNull(repo, "repo");
299-
Guard.ArgumentNotNull(hosts, "hosts");
300-
301-
////if (!repo.IsHosted || !repo.OwnerId.HasValue)
302-
//// return false;
303-
304-
////if (hosts.GitHubHost != null && IsOwner(repo, hosts.GitHubHost))
305-
//// return true;
306-
307-
////if (hosts.EnterpriseHost != null && IsOwner(repo, hosts.EnterpriseHost))
308-
//// return true;
309-
310-
return false;
311-
}
312-
313-
////static bool IsOwner(IRepositoryModel repo, IRepositoryHost host)
314-
////{
315-
//// Guard.ArgumentNotNull(repo, "repo");
316-
//// Guard.ArgumentNotNull(host, "host");
317-
318-
//// return host.User != null && repo.OwnerId.HasValue && host.User.Id == repo.OwnerId.Value;
319-
////}
320-
321222
IObservable<Unit> Run()
322223
{
323224
return Observable.Defer(() =>
@@ -349,6 +250,7 @@ IObservable<int> IncrementCounter(string key)
349250
return GetCounter(key)
350251
.Select(x => x + 1)
351252
.SelectMany(x => SaveCounter(key, x).Select(_ => x))
253+
.Do(x => Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Incremented {0} to {1}", key, x)))
352254
.Catch<int, Exception>(ex =>
353255
{
354256
log.Warn("Could not increment usage data counter", ex);
@@ -373,30 +275,12 @@ public void IncrementCommitCount()
373275
.Subscribe();
374276
}
375277

376-
public void IncrementSyncCount()
377-
{
378-
IncrementCounter(GHSyncCountKey)
379-
.Subscribe();
380-
}
381-
382278
public void IncrementCloneCount()
383279
{
384280
IncrementCounter(GHCloneCountKey)
385281
.Subscribe();
386282
}
387283

388-
public void IncrementShellLaunchCount()
389-
{
390-
IncrementCounter(GHShellLaunchCountKey)
391-
.Subscribe();
392-
}
393-
394-
public void IncrementLfsDiffCount()
395-
{
396-
IncrementCounter(GHLfsDiffCount)
397-
.Subscribe();
398-
}
399-
400284
public void IncrementLaunchCount()
401285
{
402286
IncrementCounter(GHLaunchCountKeyDay)
@@ -405,65 +289,6 @@ public void IncrementLaunchCount()
405289
.Subscribe();
406290
}
407291

408-
public void IncrementPartialCommitCount()
409-
{
410-
IncrementCounter(GHPartialCommitCount)
411-
.Subscribe();
412-
}
413-
414-
public void IncrementTutorialRunCount()
415-
{
416-
IncrementCounter(GHTutorialRunCount)
417-
.Subscribe();
418-
}
419-
420-
public void IncrementOpenInExplorerCount()
421-
{
422-
IncrementCounter(GHOpenInExplorerCount)
423-
.Subscribe();
424-
}
425-
public void IncrementOpenInShellCount()
426-
{
427-
IncrementCounter(GHOpenInShellCount)
428-
.Subscribe();
429-
}
430-
431-
public void IncrementBranchSwitchCount()
432-
{
433-
IncrementCounter(GHBranchSwitchCount)
434-
.Subscribe();
435-
}
436-
437-
public void IncrementDiscardChangesCount()
438-
{
439-
IncrementCounter(GHDiscardChangesCount)
440-
.Subscribe();
441-
}
442-
443-
public void IncrementNumberOfOpenedURLs()
444-
{
445-
IncrementCounter(GHOpenedURLCount)
446-
.Subscribe();
447-
}
448-
449-
public void IncrementMergeCommitCount()
450-
{
451-
IncrementCounter(GHMergeCommitCount)
452-
.Subscribe();
453-
}
454-
455-
public void IncrementMergeConflictCount()
456-
{
457-
IncrementCounter(GHMergeConflictCount)
458-
.Subscribe();
459-
}
460-
461-
public void IncrementOpenInEditorCount()
462-
{
463-
IncrementCounter(GHOpenInEditorCount)
464-
.Subscribe();
465-
}
466-
467292
public void IncrementUpstreamPullRequestCount()
468293
{
469294
IncrementCounter(GHUpstreamPullRequestCount)

0 commit comments

Comments
 (0)