Пример #1
0
        private TimeMachine CreateTimeMachine(LossSimulation lossSimulation)
        {
            _developersContributions = _dbContext.DeveloperContributions.ToArray();

            _logger.LogInformation("{datetime}: initializing the Time Machine.", DateTime.Now);

            var knowledgeShareStrategy = KnowledgeShareStrategyFactory.Create(_logger,
                                                                              lossSimulation.KnowledgeShareStrategyType,
                                                                              lossSimulation.KnowledgeSaveReviewerReplacementType,
                                                                              lossSimulation.NumberOfPeriodsForCalculatingProbabilityOfStay,
                                                                              lossSimulation.PullRequestReviewerSelectionStrategy,
                                                                              lossSimulation.AddOnlyToUnsafePullrequests,
                                                                              lossSimulation.RecommenderOption,
                                                                              lossSimulation.ChangePast);

            var timeMachine = new TimeMachine(knowledgeShareStrategy, _logger);

            _commits = _dbContext.Commits.Where(q => !q.Ignore).ToArray();

            _logger.LogInformation("{datetime}: Commits are loaded.", DateTime.Now);

            _commitBlobBlames = _dbContext.CommitBlobBlames.Where(q => !q.Ignore).ToArray();

            _logger.LogInformation("{datetime}: Blames are loaded.", DateTime.Now);

            var latestCommitDate = _commits.Max(q => q.AuthorDateTime);

            _committedChanges = _dbContext.CommittedChanges.ToArray();

            _logger.LogInformation("{datetime}: Committed Changes are loaded.", DateTime.Now);

            _pullRequests = _dbContext
                            .PullRequests
                            .FromSql($@"SELECT * FROM PullRequests 
                    WHERE MergeCommitSha IS NOT NULL and Merged=1 AND
                    Number NOT IN(select PullRequestNumber FROM PullRequestFiles GROUP BY PullRequestNumber having count(*)>{lossSimulation.MegaPullRequestSize})
                    AND MergedAtDateTime<={latestCommitDate}")
                            .ToArray();

            _logger.LogInformation("{datetime}: Pull Request are loaded.", DateTime.Now);

            _pullRequestFiles = _dbContext
                                .PullRequestFiles
                                .FromSql($@"SELECT * From PullRequestFiles Where PullRequestNumber in
                    (SELECT Number FROM PullRequests WHERE MergeCommitSha IS NOT NULL and Merged=1 AND 
                    Number NOT IN(select PullRequestNumber FROM PullRequestFiles GROUP BY PullRequestNumber having count(*)>{lossSimulation.MegaPullRequestSize})
                    AND MergedAtDateTime<={latestCommitDate})")
                                .ToArray();

            _logger.LogInformation("{datetime}: Pull Request Files are loaded.", DateTime.Now);

            _pullRequestReviewers = _dbContext
                                    .PullRequestReviewers
                                    .FromSql($@"SELECT * From PullRequestReviewers Where PullRequestNumber in
                    (SELECT Number FROM PullRequests WHERE MergeCommitSha IS NOT NULL and Merged=1 AND
                    Number NOT IN(select PullRequestNumber FROM PullRequestFiles GROUP BY PullRequestNumber having count(*)>{lossSimulation.MegaPullRequestSize})
                    AND MergedAtDateTime<={latestCommitDate})")
                                    .Where(q => q.State != "DISMISSED")
                                    .ToArray();

            _logger.LogInformation("{datetime}: Pull Request Reviewers are loaded.", DateTime.Now);

            _pullRequestReviewComments = _dbContext
                                         .PullRequestReviewerComments
                                         .FromSql($@"SELECT * From PullRequestReviewerComments Where PullRequestNumber in
                    (SELECT Number FROM PullRequests WHERE MergeCommitSha IS NOT NULL and Merged=1 AND
                    Number NOT IN(select PullRequestNumber FROM PullRequestFiles GROUP BY PullRequestNumber having count(*)>{lossSimulation.MegaPullRequestSize})
                    AND MergedAtDateTime<={latestCommitDate})")
                                         .ToArray();

            var lgtmTerms = lossSimulation.LgtmTerms.Split(',').Select(q => $"(Body LIKE '{q}')").Aggregate((a, b) => a + " OR " + b);
            var query     = $@"SELECT * From IssueComments Where IssueNumber in
                    (SELECT Number FROM PullRequests WHERE MergeCommitSha IS NOT NULL and Merged=1 AND
                    Number NOT IN(select PullRequestNumber FROM PullRequestFiles GROUP BY PullRequestNumber having count(*)>{lossSimulation.MegaPullRequestSize})
                    AND MergedAtDateTime<='{latestCommitDate}') and ({lgtmTerms})";

            _issueComments = _dbContext.IssueComments.FromSql(query).ToArray();

            _logger.LogInformation("{datetime}: Pull Request Reviewer Comments are loaded.", DateTime.Now);

            _developers = _dbContext.Developers.ToArray();

            _logger.LogInformation("{datetime}: Developers are loaded.", DateTime.Now);

            _developersContributionsDic = _developersContributions.ToDictionary(q => q.PeriodId + "-" + q.NormalizedName);

            _logger.LogInformation("{datetime}: Developers Contributions are loaded.", DateTime.Now);

            _canononicalPathMapper = _dbContext.GetCanonicalPaths();

            _logger.LogInformation("{datetime}: Canonical Paths are loaded.", DateTime.Now);

            _GitHubGitUsernameMapper = _dbContext.GitHubGitUsers.Where(q => q.GitUsername != null).ToArray();

            _periods = _dbContext.Periods.ToArray();

            timeMachine.Initiate(
                _commits,
                _commitBlobBlames,
                _developers,
                _developersContributions,
                _committedChanges,
                _pullRequests,
                _pullRequestFiles,
                _issueComments,
                _pullRequestReviewers,
                _pullRequestReviewComments,
                _canononicalPathMapper,
                _GitHubGitUsernameMapper,
                _periods,
                lossSimulation.FirstPeriod,
                lossSimulation.SelectedReviewersType,
                lossSimulation.MinimumActualReviewersLength,
                lossSimulation.MegaDevelopers);

            return(timeMachine);
        }