示例#1
0
        public async Task <IEnumerable <Repository> > GetRepositories()
        {
            var(repositoryDatabaseConnection, dailyClonesDatabaseConnection, dailyViewsDatabaseConnection) = await GetDatabaseConnections().ConfigureAwait(false);

            var repositoryDatabaseModels = await AttemptAndRetry(() => repositoryDatabaseConnection.Table <RepositoryDatabaseModel>().ToListAsync()).ConfigureAwait(false);

            var dailyClonesDatabaseModels = await AttemptAndRetry(() => dailyClonesDatabaseConnection.Table <DailyClonesDatabaseModel>().ToListAsync()).ConfigureAwait(false);

            var dailyViewsDatabaseModels = await AttemptAndRetry(() => dailyViewsDatabaseConnection.Table <DailyViewsDatabaseModel>().ToListAsync()).ConfigureAwait(false);

            var sortedRecentDailyClonesDatabaseModels = dailyClonesDatabaseModels.OrderByDescending(x => x.DownloadedAt).ToList();
            var sortedRecentDailyViewsDatabaseModels  = dailyViewsDatabaseModels.OrderByDescending(x => x.DownloadedAt).ToList();

            var mostRecentCloneDay = sortedRecentDailyClonesDatabaseModels.Any() ? sortedRecentDailyClonesDatabaseModels.Max(x => x.Day) : default;
            var mostRecentViewDay  = sortedRecentDailyClonesDatabaseModels.Any() ? sortedRecentDailyViewsDatabaseModels.Max(x => x.Day) : default;

            var mostRecentDate = mostRecentCloneDay.CompareTo(mostRecentViewDay) > 0 ? mostRecentCloneDay : mostRecentViewDay;

            var repositoryList = new List <Repository>();

            foreach (var repositoryDatabaseModel in repositoryDatabaseModels)
            {
                var dailyClones = sortedRecentDailyClonesDatabaseModels.Where(x => x.RepositoryUrl == repositoryDatabaseModel.Url && isWithin14Days(x.Day, mostRecentDate)).GroupBy(x => x.Day).Select(x => x.First()).Take(14);
                var dailyViews  = sortedRecentDailyViewsDatabaseModels.Where(x => x.RepositoryUrl == repositoryDatabaseModel.Url && isWithin14Days(x.Day, mostRecentDate)).GroupBy(x => x.Day).Select(x => x.First()).Take(14);

                var repository = RepositoryDatabaseModel.ToRepository(repositoryDatabaseModel, dailyClones, dailyViews);
                repositoryList.Add(repository);
            }

            return(repositoryList);
示例#2
0
        public async Task SaveRepository(Repository repository)
        {
            var databaseConnection = await GetDatabaseConnection <RepositoryDatabaseModel>().ConfigureAwait(false);

            var repositoryDatabaseModel = RepositoryDatabaseModel.ToRepositoryDatabase(repository);

            await AttemptAndRetry(() => databaseConnection.InsertOrReplaceAsync(repositoryDatabaseModel)).ConfigureAwait(false);

            await SaveStarGazerInfo(repository).ConfigureAwait(false);
            await SaveDailyClones(repository).ConfigureAwait(false);
            await SaveDailyViews(repository).ConfigureAwait(false);
        }
示例#3
0
        public async Task <Repository?> GetRepository(string repositoryUrl)
        {
            var repositoryDatabaseConnection = await GetDatabaseConnection <RepositoryDatabaseModel>().ConfigureAwait(false);

            var repositoryDatabaseModel = await AttemptAndRetry(() => repositoryDatabaseConnection.Table <RepositoryDatabaseModel>().FirstOrDefaultAsync(x => x.Url == repositoryUrl)).ConfigureAwait(false);

            if (repositoryDatabaseModel is null)
            {
                return(null);
            }

            var(dailyClones, dailyViews, starGazers) = await GetStarsClonesViews(repositoryDatabaseModel).ConfigureAwait(false);

            return(RepositoryDatabaseModel.ToRepository(repositoryDatabaseModel, dailyClones, dailyViews, starGazers));
        }
示例#4
0
        public async Task <IReadOnlyList <Repository> > GetRepositories()
        {
            var repositoryDatabaseConnection = await GetDatabaseConnection <RepositoryDatabaseModel>().ConfigureAwait(false);

            var repositoryDatabaseModels = await AttemptAndRetry(() => repositoryDatabaseConnection.Table <RepositoryDatabaseModel>().ToListAsync()).ConfigureAwait(false);

            if (!repositoryDatabaseModels.Any())
            {
                return(Array.Empty <Repository>());
            }

            var repositoryList = new List <Repository>();

            foreach (var repositoryDatabaseModel in repositoryDatabaseModels)
            {
                var(dailyClones, dailyViews, starGazers) = await GetStarsClonesViews(repositoryDatabaseModel).ConfigureAwait(false);

                var repository = RepositoryDatabaseModel.ToRepository(repositoryDatabaseModel, dailyClones, dailyViews, starGazers);
                repositoryList.Add(repository);
            }

            return(repositoryList);
        }
示例#5
0
                     IReadOnlyList <StarGazerInfoDatabaseModel> starGazers)> GetStarsClonesViews(RepositoryDatabaseModel repository)
        {
            var(_, dailyClonesDatabaseConnection, dailyViewsDatabaseConnection, starGazerInfoDatabaseConnection) = await GetDatabaseConnections().ConfigureAwait(false);

            var getStarGazerInfoModelsTask       = AttemptAndRetry(() => starGazerInfoDatabaseConnection.Table <StarGazerInfoDatabaseModel>().Where(x => x.RepositoryUrl == repository.Url).ToListAsync());
            var getDailyClonesDatabaseModelsTask = AttemptAndRetry(() => dailyClonesDatabaseConnection.Table <DailyClonesDatabaseModel>().Where(x => x.RepositoryUrl == repository.Url).ToListAsync());
            var getDailyViewsDatabaseModelsTask  = AttemptAndRetry(() => dailyViewsDatabaseConnection.Table <DailyViewsDatabaseModel>().Where(x => x.RepositoryUrl == repository.Url).ToListAsync());

            await Task.WhenAll(getDailyClonesDatabaseModelsTask, getDailyViewsDatabaseModelsTask, getStarGazerInfoModelsTask).ConfigureAwait(false);

            var starGazerInfoModels = await getStarGazerInfoModelsTask.ConfigureAwait(false);

            var dailyClonesDatabaseModels = await getDailyClonesDatabaseModelsTask.ConfigureAwait(false);

            var dailyViewsDatabaseModels = await getDailyViewsDatabaseModelsTask.ConfigureAwait(false);

            var sortedRecentDailyClonesDatabaseModels = dailyClonesDatabaseModels.OrderByDescending(x => x.DownloadedAt).ToList();
            var sortedRecentDailyViewsDatabaseModels  = dailyViewsDatabaseModels.OrderByDescending(x => x.DownloadedAt).ToList();
            var sortedStarGazerInfoModels             = starGazerInfoModels.OrderByDescending(x => x.StarredAt).ToList();

            var mostRecentCloneDay = sortedRecentDailyClonesDatabaseModels.Any() ? sortedRecentDailyClonesDatabaseModels.Max(x => x.Day) : default;
            var mostRecentViewDay  = sortedRecentDailyViewsDatabaseModels.Any() ? sortedRecentDailyViewsDatabaseModels.Max(x => x.Day) : default;

            var mostRecentDate = mostRecentCloneDay.CompareTo(mostRecentViewDay) > 0 ? mostRecentCloneDay : mostRecentViewDay;

            var dailyClones = sortedRecentDailyClonesDatabaseModels.Where(x => IsWithin14Days(x.Day, mostRecentDate)).GroupBy(x => x.Day).Select(x => x.First()).Take(14);
            var dailyViews  = sortedRecentDailyViewsDatabaseModels.Where(x => IsWithin14Days(x.Day, mostRecentDate)).GroupBy(x => x.Day).Select(x => x.First()).Take(14);

            return(dailyClones.ToList(), dailyViews.ToList(), sortedStarGazerInfoModels);
        }