Esempio n. 1
0
        public async Task <bool> NotifyTrendingRepositories(CancellationToken cancellationToken)
        {
            try
            {
                using var timedEvent = _analyticsService.TrackTime($"{nameof(BackgroundFetchService)}.{nameof(NotifyTrendingRepositories)} Triggered");

                var trendingRepositories = await GetTrendingRepositories(cancellationToken).ConfigureAwait(false);

                await _notificationService.TrySendTrendingNotificaiton(trendingRepositories).ConfigureAwait(false);

                return(true);
            }
            catch (Exception e)
            {
                _analyticsService.Report(e);
                return(false);
            }
        }
Esempio n. 2
0
        public async IAsyncEnumerable <Repository> UpdateRepositoriesWithViewsAndClonesData(IReadOnlyList <Repository> repositories, [EnumeratorCancellation] CancellationToken cancellationToken)
        {
            var getRepositoryStatisticsTaskList = new List <Task <(RepositoryViewsResponseModel?, RepositoryClonesResponseModel?)> >(repositories.Select(x => getRepositoryStatistics(x)));

            while (getRepositoryStatisticsTaskList.Any())
            {
                var completedStatisticsTask = await Task.WhenAny(getRepositoryStatisticsTaskList).ConfigureAwait(false);

                getRepositoryStatisticsTaskList.Remove(completedStatisticsTask);

                var(viewsResponse, clonesResponse) = await completedStatisticsTask.ConfigureAwait(false);

                if (viewsResponse != null && clonesResponse != null)
                {
                    var matchingRepository = repositories.Single(x => x.Name == viewsResponse.RepositoryName);

                    yield return(new Repository(matchingRepository.Name, matchingRepository.Description, matchingRepository.ForkCount,
                                                new RepositoryOwner(matchingRepository.OwnerLogin, matchingRepository.OwnerAvatarUrl),
                                                new IssuesConnection(matchingRepository.IssuesCount, null),
                                                matchingRepository.Url,
                                                new StarGazers(matchingRepository.StarCount),
                                                matchingRepository.IsFork,
                                                viewsResponse.DailyViewsList,
                                                clonesResponse.DailyClonesList));
                }
            }

            async Task <(RepositoryViewsResponseModel?ViewsResponse, RepositoryClonesResponseModel?ClonesResponse)> getRepositoryStatistics(Repository repository)
            {
                var getViewStatisticsTask  = GetRepositoryViewStatistics(repository.OwnerLogin, repository.Name, cancellationToken);
                var getCloneStatisticsTask = GetRepositoryCloneStatistics(repository.OwnerLogin, repository.Name, cancellationToken);

                try
                {
                    await Task.WhenAll(getViewStatisticsTask, getCloneStatisticsTask).ConfigureAwait(false);

                    return(await getViewStatisticsTask.ConfigureAwait(false),
                           await getCloneStatisticsTask.ConfigureAwait(false));
                }
                catch (ApiException e) when(e.StatusCode is System.Net.HttpStatusCode.Forbidden)
                {
                    AnalyticsService.Report(e, new Dictionary <string, string>
                    {
                        { nameof(Repository) + nameof(Repository.Name), repository.Name },
                        { nameof(Repository) + nameof(Repository.OwnerLogin), repository.OwnerLogin },
                        { nameof(GitHubUserService) + nameof(GitHubUserService.Alias), _gitHubUserService.Alias },
                        { nameof(GitHubUserService) + nameof(GitHubUserService.Name), _gitHubUserService.Name },
                    });

                    return(null, null);
                }
            }
        }