@@ -22,8 +22,6 @@ namespace GitHub.Services
2222 [ PartCreationPolicy ( CreationPolicy . Shared ) ]
2323 public class UsageTracker : IUsageTracker
2424 {
25- // Whenever you add a counter make sure it gets added to _both_
26- // BuildUsageModel and ClearCounters
2725 const string GHLastSubmissionKey = "GHLastSubmission" ;
2826 const string GHCreateCountKey = "GHCreateCountKey" ;
2927 const string GHCloneCountKey = "GHCloneCount" ;
@@ -37,6 +35,20 @@ public class UsageTracker : IUsageTracker
3735 const string GHLaunchCountKeyWeek = "GHLaunchCountWeek" ;
3836 const string GHLaunchCountKeyMonth = "GHLaunchCountMonth" ;
3937
38+ readonly Dictionary < string , Action < UsageModel , int > > standardCounters = new Dictionary < string , Action < UsageModel , int > > {
39+ { GHCloneCountKey , ( model , x ) => model . NumberOfClones = x } ,
40+ { GHCreateCountKey , ( model , x ) => model . NumberOfReposCreated = x } ,
41+ { GHPublishCountKey , ( model , x ) => model . NumberOfReposPublished = x } ,
42+ { GHCreateGistCountKey , ( model , x ) => model . NumberOfGists = x } ,
43+ { GHOpenInGitHubCountKey , ( model , x ) => model . NumberOfOpenInGitHub = x } ,
44+ { GHLinkToGitHubCountKey , ( model , x ) => model . NumberOfLinkToGitHub = x } ,
45+ { GHUpstreamPullRequestCount , ( model , x ) => model . NumberOfUpstreamPullRequests = x } ,
46+ { GHLoginCountKey , ( model , x ) => model . NumberOfLogins = x } ,
47+ { GHLaunchCountKeyDay , ( model , x ) => model . NumberOfStartups = x } ,
48+ { GHLaunchCountKeyWeek , ( model , x ) => model . NumberOfStartupsWeek = x } ,
49+ { GHLaunchCountKeyMonth , ( model , x ) => model . NumberOfStartupsMonth = x } ,
50+ } ;
51+
4052 static readonly NLog . Logger log = NLog . LogManager . GetCurrentClassLogger ( ) ;
4153
4254 readonly IMetricsService client ;
@@ -96,9 +108,7 @@ IObservable<Unit> SubmitIfNeeded()
96108 // New day, new stats. This matches the GHfM implementation
97109 // of when to send stats.
98110 if ( lastDate == currentDate )
99- {
100111 return Observable . Return ( Unit . Default ) ;
101- }
102112
103113 // Every time we increment the launch count we increment both GHLaunchCountKeyDay
104114 // and GHLaunchCountKeyWeek but we only submit (and clear) the GHLaunchCountKeyWeek
@@ -151,21 +161,10 @@ static int GetIso8601WeekOfYear(DateTime time)
151161
152162 IObservable < Unit > ClearCounters ( bool weekly , bool monthly )
153163 {
154- var standardCounters = new [ ] {
155- GHLaunchCountKeyDay ,
156- GHCloneCountKey ,
157- GHCreateCountKey ,
158- GHPublishCountKey ,
159- GHCreateGistCountKey ,
160- GHOpenInGitHubCountKey ,
161- GHLinkToGitHubCountKey ,
162- GHLoginCountKey ,
163- GHUpstreamPullRequestCount ,
164- } ;
165-
166- var counters = standardCounters
167- . Concat ( weekly ? new [ ] { GHLaunchCountKeyWeek } : Enumerable . Empty < string > ( ) )
168- . Concat ( monthly ? new [ ] { GHLaunchCountKeyMonth } : Enumerable . Empty < string > ( ) ) ;
164+ var counters = standardCounters . Keys
165+ . Except ( new [ ] { GHLastSubmissionKey } )
166+ . Except ( weekly ? new [ ] { GHLaunchCountKeyWeek } : Enumerable . Empty < string > ( ) )
167+ . Except ( monthly ? new [ ] { GHLaunchCountKeyMonth } : Enumerable . Empty < string > ( ) ) ;
169168
170169 return counters
171170 . Select ( ClearCounter )
@@ -196,45 +195,22 @@ IObservable<UsageModel> BuildUsageModel(bool weekly, bool monthly)
196195 var model = new UsageModel ( ) ;
197196
198197 if ( hosts . GitHubHost ? . IsLoggedIn == true )
199- {
200198 model . IsGitHubUser = true ;
201- }
202199
203200 if ( hosts . EnterpriseHost ? . IsLoggedIn == true )
204- {
205201 model . IsEnterpriseUser = true ;
206- }
207202
208203 model . Lang = CultureInfo . InstalledUICulture . IetfLanguageTag ;
209204 model . AppVersion = AssemblyVersionInformation . Version ;
210- model . VSVersion = GitHub . VisualStudio . Services . VisualStudioVersion ;
205+ model . VSVersion = VisualStudio . Services . VisualStudioVersion ;
211206
212- var counters = new List < IObservable < int > >
213- {
214- GetCounter ( GHLaunchCountKeyDay ) . Do ( x => model . NumberOfStartups = x ) ,
215- GetCounter ( GHLaunchCountKeyWeek ) . Do ( x => model . NumberOfStartupsWeek = x ) ,
216- GetCounter ( GHLaunchCountKeyMonth ) . Do ( x => model . NumberOfStartupsMonth = x ) ,
217- GetCounter ( GHCloneCountKey ) . Do ( x => model . NumberOfClones = x ) ,
218- GetCounter ( GHCreateCountKey ) . Do ( x => model . NumberOfReposCreated = x ) ,
219- GetCounter ( GHPublishCountKey ) . Do ( x => model . NumberOfReposPublished = x ) ,
220- GetCounter ( GHCreateGistCountKey ) . Do ( x => model . NumberOfGists = x ) ,
221- GetCounter ( GHOpenInGitHubCountKey ) . Do ( x => model . NumberOfOpenInGitHub = x ) ,
222- GetCounter ( GHLinkToGitHubCountKey ) . Do ( x => model . NumberOfLinkToGitHub = x ) ,
223- GetCounter ( GHLoginCountKey ) . Do ( x => model . NumberOfLogins = x ) ,
224- GetCounter ( GHUpstreamPullRequestCount ) . Do ( x => model . NumberOfUpstreamPullRequests = x ) ,
225- } ;
226-
227- if ( weekly )
228- {
229- counters . Add ( GetCounter ( GHLaunchCountKeyWeek )
230- . Do ( x => model . NumberOfStartupsWeek = x ) ) ;
231- }
232-
233- if ( monthly )
234- {
235- counters . Add ( GetCounter ( GHLaunchCountKeyMonth )
236- . Do ( x => model . NumberOfStartupsMonth = x ) ) ;
237- }
207+ var counters = standardCounters
208+ . Where ( x => x . Key != GHLastSubmissionKey && // filter out last submission date
209+ ( ! weekly && x . Key == GHLaunchCountKeyWeek ) && // filter out weekly metrics if !weekly
210+ ( ! monthly && x . Key == GHLaunchCountKeyMonth ) // filter out monthly metrics if !monthly
211+ )
212+ . ToObservable ( )
213+ . SelectMany ( c => GetCounter ( GHLaunchCountKeyDay ) . Do ( x => c . Value ( model , x ) ) ) ; // set metric in model
238214
239215 return Observable . Merge ( counters )
240216 . ContinueAfter ( ( ) => Observable . Return ( model ) ) ;
0 commit comments