@@ -20,8 +20,6 @@ namespace GitHub.Services
2020 [ PartCreationPolicy ( CreationPolicy . Shared ) ]
2121 public class UsageTracker : IUsageTracker
2222 {
23- // Whenever you add a counter make sure it gets added to _both_
24- // BuildUsageModel and ClearCounters
2523 const string GHLastSubmissionKey = "GHLastSubmission" ;
2624 const string GHCreateCountKey = "GHCreateCountKey" ;
2725 const string GHCloneCountKey = "GHCloneCount" ;
@@ -35,6 +33,20 @@ public class UsageTracker : IUsageTracker
3533 const string GHLaunchCountKeyWeek = "GHLaunchCountWeek" ;
3634 const string GHLaunchCountKeyMonth = "GHLaunchCountMonth" ;
3735
36+ readonly Dictionary < string , Action < UsageModel , int > > standardCounters = new Dictionary < string , Action < UsageModel , int > > {
37+ { GHCloneCountKey , ( model , x ) => model . NumberOfClones = x } ,
38+ { GHCreateCountKey , ( model , x ) => model . NumberOfReposCreated = x } ,
39+ { GHPublishCountKey , ( model , x ) => model . NumberOfReposPublished = x } ,
40+ { GHCreateGistCountKey , ( model , x ) => model . NumberOfGists = x } ,
41+ { GHOpenInGitHubCountKey , ( model , x ) => model . NumberOfOpenInGitHub = x } ,
42+ { GHLinkToGitHubCountKey , ( model , x ) => model . NumberOfLinkToGitHub = x } ,
43+ { GHUpstreamPullRequestCount , ( model , x ) => model . NumberOfUpstreamPullRequests = x } ,
44+ { GHLoginCountKey , ( model , x ) => model . NumberOfLogins = x } ,
45+ { GHLaunchCountKeyDay , ( model , x ) => model . NumberOfStartups = x } ,
46+ { GHLaunchCountKeyWeek , ( model , x ) => model . NumberOfStartupsWeek = x } ,
47+ { GHLaunchCountKeyMonth , ( model , x ) => model . NumberOfStartupsMonth = x } ,
48+ } ;
49+
3850 static readonly NLog . Logger log = NLog . LogManager . GetCurrentClassLogger ( ) ;
3951
4052 readonly IMetricsService client ;
@@ -94,9 +106,7 @@ IObservable<Unit> SubmitIfNeeded()
94106 // New day, new stats. This matches the GHfM implementation
95107 // of when to send stats.
96108 if ( lastDate == currentDate )
97- {
98109 return Observable . Return ( Unit . Default ) ;
99- }
100110
101111 // Every time we increment the launch count we increment both GHLaunchCountKeyDay
102112 // and GHLaunchCountKeyWeek but we only submit (and clear) the GHLaunchCountKeyWeek
@@ -149,21 +159,10 @@ static int GetIso8601WeekOfYear(DateTime time)
149159
150160 IObservable < Unit > ClearCounters ( bool weekly , bool monthly )
151161 {
152- var standardCounters = new [ ] {
153- GHLaunchCountKeyDay ,
154- GHCloneCountKey ,
155- GHCreateCountKey ,
156- GHPublishCountKey ,
157- GHCreateGistCountKey ,
158- GHOpenInGitHubCountKey ,
159- GHLinkToGitHubCountKey ,
160- GHLoginCountKey ,
161- GHUpstreamPullRequestCount ,
162- } ;
163-
164- var counters = standardCounters
165- . Concat ( weekly ? new [ ] { GHLaunchCountKeyWeek } : Enumerable . Empty < string > ( ) )
166- . Concat ( monthly ? new [ ] { GHLaunchCountKeyMonth } : Enumerable . Empty < string > ( ) ) ;
162+ var counters = standardCounters . Keys
163+ . Except ( new [ ] { GHLastSubmissionKey } )
164+ . Except ( weekly ? new [ ] { GHLaunchCountKeyWeek } : Enumerable . Empty < string > ( ) )
165+ . Except ( monthly ? new [ ] { GHLaunchCountKeyMonth } : Enumerable . Empty < string > ( ) ) ;
167166
168167 return counters
169168 . Select ( ClearCounter )
@@ -194,45 +193,22 @@ IObservable<UsageModel> BuildUsageModel(bool weekly, bool monthly)
194193 var model = new UsageModel ( ) ;
195194
196195 if ( hosts . GitHubHost ? . IsLoggedIn == true )
197- {
198196 model . IsGitHubUser = true ;
199- }
200197
201198 if ( hosts . EnterpriseHost ? . IsLoggedIn == true )
202- {
203199 model . IsEnterpriseUser = true ;
204- }
205200
206201 model . Lang = CultureInfo . InstalledUICulture . IetfLanguageTag ;
207202 model . AppVersion = AssemblyVersionInformation . Version ;
208- model . VSVersion = GitHub . VisualStudio . Services . VisualStudioVersion ;
203+ model . VSVersion = VisualStudio . Services . VisualStudioVersion ;
209204
210- var counters = new List < IObservable < int > >
211- {
212- GetCounter ( GHLaunchCountKeyDay ) . Do ( x => model . NumberOfStartups = x ) ,
213- GetCounter ( GHLaunchCountKeyWeek ) . Do ( x => model . NumberOfStartupsWeek = x ) ,
214- GetCounter ( GHLaunchCountKeyMonth ) . Do ( x => model . NumberOfStartupsMonth = x ) ,
215- GetCounter ( GHCloneCountKey ) . Do ( x => model . NumberOfClones = x ) ,
216- GetCounter ( GHCreateCountKey ) . Do ( x => model . NumberOfReposCreated = x ) ,
217- GetCounter ( GHPublishCountKey ) . Do ( x => model . NumberOfReposPublished = x ) ,
218- GetCounter ( GHCreateGistCountKey ) . Do ( x => model . NumberOfGists = x ) ,
219- GetCounter ( GHOpenInGitHubCountKey ) . Do ( x => model . NumberOfOpenInGitHub = x ) ,
220- GetCounter ( GHLinkToGitHubCountKey ) . Do ( x => model . NumberOfLinkToGitHub = x ) ,
221- GetCounter ( GHLoginCountKey ) . Do ( x => model . NumberOfLogins = x ) ,
222- GetCounter ( GHUpstreamPullRequestCount ) . Do ( x => model . NumberOfUpstreamPullRequests = x ) ,
223- } ;
224-
225- if ( weekly )
226- {
227- counters . Add ( GetCounter ( GHLaunchCountKeyWeek )
228- . Do ( x => model . NumberOfStartupsWeek = x ) ) ;
229- }
230-
231- if ( monthly )
232- {
233- counters . Add ( GetCounter ( GHLaunchCountKeyMonth )
234- . Do ( x => model . NumberOfStartupsMonth = x ) ) ;
235- }
205+ var counters = standardCounters
206+ . Where ( x => x . Key != GHLastSubmissionKey && // filter out last submission date
207+ ( ! weekly && x . Key == GHLaunchCountKeyWeek ) && // filter out weekly metrics if !weekly
208+ ( ! monthly && x . Key == GHLaunchCountKeyMonth ) // filter out monthly metrics if !monthly
209+ )
210+ . ToObservable ( )
211+ . SelectMany ( c => GetCounter ( GHLaunchCountKeyDay ) . Do ( x => c . Value ( model , x ) ) ) ; // set metric in model
236212
237213 return Observable . Merge ( counters )
238214 . ContinueAfter ( ( ) => Observable . Return ( model ) ) ;
0 commit comments