Example #1
0
        private RepositoryStats MapTo(ICollection <ArchiveInfo> archivesInfo)
        {
            RepositoryStats repositoryStats = null;

            if (archivesInfo != null)
            {
                ICollection <ArchiveStats> filesStats = archivesInfo
                                                        .GroupBy(f => f.Extension)
                                                        .Select(g => new ArchiveStats
                {
                    Extension   = g.Key,
                    AmountBytes = g.Sum(s => s.Bytes),
                    AmountLines = g.Sum(s => s.AmountLines)
                })
                                                        .OrderByDescending(f => f.AmountBytes)
                                                        .ThenByDescending(f => f.AmountLines)
                                                        .ThenByDescending(f => f.Extension)
                                                        .ToList(
                    );

                repositoryStats = new RepositoryStats
                {
                    ArchivesStats = filesStats
                };
            }

            return(repositoryStats);
        }
Example #2
0
        public async Task <ObjectResult> GetAsync(string path, string brach = "master")
        {
            Result result = new Result();

            try
            {
                RepositoryStats repositoryStats = await _repositoryStatsCore.GetAsync(path, brach);

                RepositoryStatsDto repositoryStatsDto = MapTo(repositoryStats);

                result.Value = repositoryStatsDto;

                result.Success = true;
            }
            catch (Exception e)
            {
                result.Error = new ErrorDto
                {
                    ErrorType = e.GetType().Name,
                    Message   = e.Message
                };
            }

            return(result.Return());
        }
Example #3
0
        public async Task <RepositoryStats> GetAsync(string path, string brach = "master")
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path), $"The {nameof(path)} parameter can not be null.");
            }
            if (brach == null)
            {
                throw new ArgumentNullException(nameof(brach), $"The {nameof(brach)} parameter can not be null.");
            }

            RepositoryStats repositoryStats = await _repositoryStatsRepository.GetByRepositoryPathAsync(path, brach);

            return(repositoryStats);
        }
Example #4
0
        private RepositoryStatsDto MapTo(RepositoryStats repositoryStats)
        {
            ICollection <FileStatsDto> fileStatsDtos = repositoryStats.ArchivesStats
                                                       .Select(fs => new FileStatsDto
            {
                Extension   = fs.Extension,
                AmountLines = fs.AmountLines,
                AmountBytes = fs.AmountBytes,
            })
                                                       .ToList();

            RepositoryStatsDto repositoryStatsDto = new RepositoryStatsDto
            {
                FilesStats = fileStatsDtos
            };

            return(repositoryStatsDto);
        }
Example #5
0
        public async Task <RepositoryStats> GetByRepositoryPathAsync(string path, string branch = "master")
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path), $"The {nameof(path)} parameter can not be null.");
            }
            if (branch == null)
            {
                throw new ArgumentNullException(nameof(branch), $"The {nameof(branch)} parameter can not be null.");
            }

            var cacheKey = $"{MethodBase.GetCurrentMethod().ReflectedType.GUID}_{path}";

            RepositoryInfo repositoryInfoGH = _memoryCache.Get <RepositoryInfo>(cacheKey);

            if (repositoryInfoGH != null)
            {
                _httpClient.DefaultRequestHeaders.IfNoneMatch.Add(repositoryInfoGH.ETag);
            }

            HttpResponseMessage httpResponseMessage = await _httpClient.GetAsync(string.Format(_gitHubRepositoryZipFileUriPattern, path, branch));

            if (httpResponseMessage.StatusCode.Equals(HttpStatusCode.OK))
            {
                var filesInfoGH = await GetAsync(httpResponseMessage);

                RepositoryStats repositoryStats = MapTo(filesInfoGH);

                repositoryInfoGH = new RepositoryInfo
                {
                    ETag            = httpResponseMessage.Headers.ETag,
                    RepositoryStats = repositoryStats
                };

                _memoryCache.Set(cacheKey, repositoryInfoGH);
            }
            else if (!httpResponseMessage.StatusCode.Equals(HttpStatusCode.NotModified))
            {
                throw new RepositoryException("GitHub Repository not found.");
            }

            return(repositoryInfoGH.RepositoryStats);
        }