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

Commit 630e0ef

Browse files
committed
Get IMetricsService dynamically.
As it may not exist.
1 parent 301a2fa commit 630e0ef

File tree

1 file changed

+60
-40
lines changed

1 file changed

+60
-40
lines changed

src/GitHub.App/Services/UsageTracker.cs

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -69,55 +69,62 @@ public UsageTracker(
6969
////Lazy<IAppVersionProvider> appVersionProvider,
7070
Lazy<IEnvironment> environment,
7171
IPackageSettings userSettings,
72-
IMetricsService client)
72+
IServiceProvider serviceProvider)
7373
{
7474
Guard.ArgumentNotNull(cache, "cache");
7575
////Guard.ArgumentNotNull(trackedRepositories, "trackedRepositories");
7676
Guard.ArgumentNotNull(repositoryHosts, "repositoryHosts");
7777
////Guard.ArgumentNotNull(appVersionProvider, "appVersionProvider");
7878
Guard.ArgumentNotNull(environment, "environment");
79-
////Guard.ArgumentNotNull(userSettings, "userSettings");
80-
Guard.ArgumentNotNull(client, "client");
79+
Guard.ArgumentNotNull(userSettings, "userSettings");
80+
Guard.ArgumentNotNull(serviceProvider, "serviceProvider");
8181

8282
this.cache = cache;
8383
////this.trackedRepositories = trackedRepositories;
8484
this.repositoryHosts = repositoryHosts;
8585
////this.appVersionProvider = appVersionProvider;
8686
this.environment = environment;
8787
this.userSettings = userSettings;
88-
this.client = client;
88+
this.client = (IMetricsService)serviceProvider.GetService(typeof(IMetricsService));
8989
}
9090

9191
IObservable<Unit> SubmitIfNeeded()
9292
{
93-
return GetLastUpdated()
94-
.SelectMany(lastSubmission =>
95-
{
96-
var now = RxApp.MainThreadScheduler.Now;
97-
98-
var lastDate = lastSubmission.LocalDateTime.Date;
99-
var currentDate = now.LocalDateTime.Date;
100-
101-
// New day, new stats. This matches the GHfM implementation
102-
// of when to send stats.
103-
if (lastDate == currentDate)
93+
if (client != null)
94+
{
95+
return GetLastUpdated()
96+
.SelectMany(lastSubmission =>
10497
{
105-
return Observable.Return(Unit.Default);
106-
}
98+
var now = RxApp.MainThreadScheduler.Now;
10799

108-
// Every time we increment the launch count we increment both GHLaunchCountKeyDay
109-
// and GHLaunchCountKeyWeek but we only submit (and clear) the GHLaunchCountKeyWeek
110-
// when we've transitioned into a new week. We've defined a week by the ISO8601
111-
// definition, i.e. week starting on Monday and ending on Sunday.
112-
var includeWeekly = GetIso8601WeekOfYear(lastDate) != GetIso8601WeekOfYear(currentDate);
113-
var includeMonthly = lastDate.Month != currentDate.Month;
114-
115-
return BuildUsageModel(includeWeekly, includeMonthly)
116-
.SelectMany(client.PostUsage)
117-
.Concat(ClearCounters(includeWeekly, includeMonthly))
118-
.Concat(StoreLastUpdated(now));
119-
})
120-
.AsCompletion();
100+
var lastDate = lastSubmission.LocalDateTime.Date;
101+
var currentDate = now.LocalDateTime.Date;
102+
103+
// New day, new stats. This matches the GHfM implementation
104+
// of when to send stats.
105+
if (lastDate == currentDate)
106+
{
107+
return Observable.Return(Unit.Default);
108+
}
109+
110+
// Every time we increment the launch count we increment both GHLaunchCountKeyDay
111+
// and GHLaunchCountKeyWeek but we only submit (and clear) the GHLaunchCountKeyWeek
112+
// when we've transitioned into a new week. We've defined a week by the ISO8601
113+
// definition, i.e. week starting on Monday and ending on Sunday.
114+
var includeWeekly = GetIso8601WeekOfYear(lastDate) != GetIso8601WeekOfYear(currentDate);
115+
var includeMonthly = lastDate.Month != currentDate.Month;
116+
117+
return BuildUsageModel(includeWeekly, includeMonthly)
118+
.SelectMany(client.PostUsage)
119+
.Concat(ClearCounters(includeWeekly, includeMonthly))
120+
.Concat(StoreLastUpdated(now));
121+
})
122+
.AsCompletion();
123+
}
124+
else
125+
{
126+
return Observable.Return(Unit.Default);
127+
}
121128
}
122129

123130
IObservable<DateTimeOffset> GetLastUpdated()
@@ -472,24 +479,37 @@ public void IncrementUpstreamPullRequestCount()
472479

473480
IObservable<Unit> OptOut()
474481
{
475-
return Observable.Defer(() =>
482+
if (client != null)
476483
{
477-
log.Info("User has disabled sending anonymized usage statistics to GitHub");
478-
479-
return ClearCounters(true, true)
480-
.ContinueAfter(() => client.SendOptOut());
481-
});
484+
return Observable.Defer(() =>
485+
{
486+
log.Info("User has disabled sending anonymized usage statistics to GitHub");
482487

488+
return ClearCounters(true, true)
489+
.ContinueAfter(() => client.SendOptOut());
490+
});
491+
}
492+
else
493+
{
494+
return Observable.Return(Unit.Default);
495+
}
483496
}
484497

485498
IObservable<Unit> OptIn()
486499
{
487-
return Observable.Defer(() =>
500+
if (client != null)
488501
{
489-
log.Info("User has enabled sending anonymized usage statistics to GitHub");
502+
return Observable.Defer(() =>
503+
{
504+
log.Info("User has enabled sending anonymized usage statistics to GitHub");
490505

491-
return client.SendOptIn();
492-
});
506+
return client.SendOptIn();
507+
});
508+
}
509+
else
510+
{
511+
return Observable.Return(Unit.Default);
512+
}
493513
}
494514
}
495515
}

0 commit comments

Comments
 (0)