public async Task <DeploymentFrequencyModel> GetGitHubDeploymentFrequency(bool getSampleData, string clientId, string clientSecret, TableStorageConfiguration tableStorageConfig, string owner, string repo, string branch, string workflowName, string workflowId, int numberOfDays, int maxNumberOfItems, bool useCache) { ListUtility <Build> utility = new ListUtility <Build>(); DeploymentFrequency deploymentFrequency = new DeploymentFrequency(); if (getSampleData == false) { //Get a list of builds BuildsDA buildsDA = new BuildsDA(); List <GitHubActionsRun> gitHubRuns = await buildsDA.GetGitHubActionRuns(clientId, clientSecret, tableStorageConfig, owner, repo, workflowName, workflowId, useCache); if (gitHubRuns != null) { //Translate the GitHub build to a generic build object List <Build> builds = new List <Build>(); foreach (GitHubActionsRun item in gitHubRuns) { //Only return completed builds on the target branch, within the targeted date range if (item.status == "completed" && item.head_branch == branch && item.created_at > DateTime.Now.AddDays(-numberOfDays)) { builds.Add( new Build { Id = item.run_number, Branch = item.head_branch, BuildNumber = item.run_number, StartTime = item.created_at, EndTime = item.updated_at, BuildDurationPercent = item.buildDurationPercent, Status = item.status, Url = item.html_url } ); } } //Get the total builds used in the calculation int buildTotal = builds.Count; //then build the calcuation, loading the dates into a date array List <KeyValuePair <DateTime, DateTime> > dateList = new List <KeyValuePair <DateTime, DateTime> >(); foreach (Build item in builds) { KeyValuePair <DateTime, DateTime> newItem = new KeyValuePair <DateTime, DateTime>(item.StartTime, item.EndTime); dateList.Add(newItem); } //then build the calcuation, loading the dates into a date array float deploymentsPerDay; deploymentsPerDay = deploymentFrequency.ProcessDeploymentFrequency(dateList, numberOfDays); //Filter the results to return the last n (maxNumberOfItems), to return to the UI builds = utility.GetLastNItems(builds, maxNumberOfItems); //Find the max build duration float maxBuildDuration = 0f; foreach (Build item in builds) { if (item.BuildDuration > maxBuildDuration) { maxBuildDuration = item.BuildDuration; } } //Calculate the percent scaling foreach (Build item in builds) { float interiumResult = ((item.BuildDuration / maxBuildDuration) * 100f); item.BuildDurationPercent = Scaling.ScaleNumberToRange(interiumResult, 0, 100, 20, 100); } //Return the completed model DeploymentFrequencyModel model = new DeploymentFrequencyModel { TargetDevOpsPlatform = DevOpsPlatform.GitHub, DeploymentName = workflowName, BuildList = builds, DeploymentsPerDayMetric = deploymentsPerDay, DeploymentsPerDayMetricDescription = deploymentFrequency.GetDeploymentFrequencyRating(deploymentsPerDay), NumberOfDays = numberOfDays, MaxNumberOfItems = builds.Count, TotalItems = buildTotal }; return(model); } else { return(null); } } else { List <Build> builds = utility.GetLastNItems(GetSampleGitHubBuilds(), maxNumberOfItems); DeploymentFrequencyModel model = new DeploymentFrequencyModel { TargetDevOpsPlatform = DevOpsPlatform.GitHub, DeploymentName = workflowName, BuildList = builds, DeploymentsPerDayMetric = 10f, DeploymentsPerDayMetricDescription = "Elite", NumberOfDays = numberOfDays, MaxNumberOfItems = builds.Count, TotalItems = builds.Count }; return(model); } }
public async Task <LeadTimeForChangesModel> GetGitHubLeadTimesForChanges(bool getSampleData, string clientId, string clientSecret, TableStorageConfiguration tableStorageConfig, string owner, string repo, string mainBranch, string workflowName, string workflowId, int numberOfDays, int maxNumberOfItems, bool useCache) { ListUtility <PullRequestModel> utility = new ListUtility <PullRequestModel>(); LeadTimeForChanges leadTimeForChanges = new LeadTimeForChanges(); List <PullRequestModel> pullRequests = new List <PullRequestModel>(); if (getSampleData == false) { List <GitHubActionsRun> initialRuns = new List <GitHubActionsRun>(); BuildsDA buildsDA = new BuildsDA(); initialRuns = await buildsDA.GetGitHubActionRuns(clientId, clientSecret, tableStorageConfig, owner, repo, workflowName, workflowId, useCache); //Process all builds, filtering by main and feature branchs List <GitHubActionsRun> mainBranchRuns = new List <GitHubActionsRun>(); List <GitHubActionsRun> featureBranchRuns = new List <GitHubActionsRun>(); List <string> branches = new List <string>(); foreach (GitHubActionsRun item in initialRuns) { if (item.status == "completed" && item.created_at > DateTime.Now.AddDays(-numberOfDays)) { if (item.head_branch == mainBranch) { //Save the main branch mainBranchRuns.Add(item); } else { //Save the feature branches featureBranchRuns.Add(item); //Record all unique branches if (branches.Contains(item.head_branch) == false) { branches.Add(item.head_branch); } } } } //Process the lead time for changes List <KeyValuePair <DateTime, TimeSpan> > leadTimeForChangesList = new List <KeyValuePair <DateTime, TimeSpan> >(); foreach (string branch in branches) { List <GitHubActionsRun> branchBuilds = featureBranchRuns.Where(a => a.head_branch == branch).ToList(); PullRequestsDA pullRequestDA = new PullRequestsDA(); GitHubPR pr = await pullRequestDA.GetGitHubPullRequest(clientId, clientSecret, tableStorageConfig, owner, repo, branch, useCache); if (pr != null) { List <GitHubPRCommit> pullRequestCommits = await pullRequestDA.GetGitHubPullRequestCommits(clientId, clientSecret, tableStorageConfig, owner, repo, pr.number, useCache); List <Commit> commits = new List <Commit>(); foreach (GitHubPRCommit item in pullRequestCommits) { commits.Add(new Commit { commitId = item.sha, name = item.commit.committer.name, date = item.commit.committer.date }); } DateTime minTime = DateTime.MaxValue; DateTime maxTime = DateTime.MinValue; foreach (GitHubPRCommit pullRequestCommit in pullRequestCommits) { if (minTime > pullRequestCommit.commit.committer.date) { minTime = pullRequestCommit.commit.committer.date; } if (maxTime < pullRequestCommit.commit.committer.date) { maxTime = pullRequestCommit.commit.committer.date; } } foreach (GitHubActionsRun branchBuild in branchBuilds) { if (minTime > branchBuild.updated_at) { minTime = branchBuild.updated_at; } if (maxTime < branchBuild.updated_at) { maxTime = branchBuild.updated_at; } } PullRequestModel pullRequest = new PullRequestModel { PullRequestId = pr.number, Branch = branch, BuildCount = branchBuilds.Count, Commits = commits, StartDateTime = minTime, EndDateTime = maxTime, Status = pr.state, Url = $"https://github.com/{owner}/{repo}/pull/{pr.number}" }; //Convert the pull request status to the standard UI status if (pullRequest.Status == "closed") { pullRequest.Status = "completed"; } else if (pullRequest.Status == "open") { pullRequest.Status = "inProgress"; } else { pullRequest.Status = pullRequest.Status; } leadTimeForChangesList.Add(new KeyValuePair <DateTime, TimeSpan>(minTime, pullRequest.Duration)); pullRequests.Add(pullRequest); } } //Calculate the lead time for changes value, in hours float leadTime = leadTimeForChanges.ProcessLeadTimeForChanges(leadTimeForChangesList, numberOfDays); List <PullRequestModel> uiPullRequests = utility.GetLastNItems(pullRequests, maxNumberOfItems); float maxPullRequestDuration = 0f; foreach (PullRequestModel item in uiPullRequests) { if (item.Duration.TotalMinutes > maxPullRequestDuration) { maxPullRequestDuration = (float)item.Duration.TotalMinutes; } } foreach (PullRequestModel item in uiPullRequests) { float interiumResult = (((float)item.Duration.TotalMinutes / maxPullRequestDuration) * 100f); item.DurationPercent = Scaling.ScaleNumberToRange(interiumResult, 0, 100, 20, 100); } double totalHours = 0; foreach (GitHubActionsRun item in mainBranchRuns) { totalHours += (item.updated_at - item.created_at).TotalHours; } float averageBuildHours = 0; if (mainBranchRuns.Count > 0) { averageBuildHours = (float)totalHours / (float)mainBranchRuns.Count; } LeadTimeForChangesModel model = new LeadTimeForChangesModel { ProjectName = repo, TargetDevOpsPlatform = DevOpsPlatform.GitHub, AverageBuildHours = averageBuildHours, AveragePullRequestHours = leadTime, LeadTimeForChangesMetric = leadTime + averageBuildHours, LeadTimeForChangesMetricDescription = leadTimeForChanges.GetLeadTimeForChangesRating(leadTime), PullRequests = utility.GetLastNItems(pullRequests, maxNumberOfItems), NumberOfDays = numberOfDays, MaxNumberOfItems = uiPullRequests.Count, TotalItems = pullRequests.Count }; return(model); } else { List <PullRequestModel> samplePullRequests = utility.GetLastNItems(CreatePullRequestsSample(DevOpsPlatform.GitHub), maxNumberOfItems); LeadTimeForChangesModel model = new LeadTimeForChangesModel { ProjectName = repo, TargetDevOpsPlatform = DevOpsPlatform.GitHub, AverageBuildHours = 1f, AveragePullRequestHours = 20.33f, LeadTimeForChangesMetric = 20.33f + 1f, LeadTimeForChangesMetricDescription = "Elite", PullRequests = samplePullRequests, NumberOfDays = numberOfDays, MaxNumberOfItems = samplePullRequests.Count, TotalItems = samplePullRequests.Count }; return(model); } }
public async Task <DeploymentFrequencyModel> GetAzureDevOpsDeploymentFrequency(bool getSampleData, string patToken, TableStorageConfiguration tableStorageConfig, string organization, string project, string branch, string buildName, int numberOfDays, int maxNumberOfItems, bool useCache) { ListUtility <Build> utility = new ListUtility <Build>(); DeploymentFrequency deploymentFrequency = new DeploymentFrequency(); if (getSampleData == false) { //Get a list of builds BuildsDA buildsDA = new BuildsDA(); List <AzureDevOpsBuild> azureDevOpsBuilds = await buildsDA.GetAzureDevOpsBuilds(patToken, tableStorageConfig, organization, project, buildName, useCache); if (azureDevOpsBuilds != null) { //Translate the Azure DevOps build to a generic build object List <Build> builds = new List <Build>(); foreach (AzureDevOpsBuild item in azureDevOpsBuilds) { //Only return completed builds on the target branch, within the targeted date range if (item.status == "completed" && item.sourceBranch == branch && item.queueTime > DateTime.Now.AddDays(-numberOfDays)) { builds.Add( new Build { Id = item.id, Branch = item.sourceBranch, BuildNumber = item.buildNumber, StartTime = item.queueTime, EndTime = item.finishTime, BuildDurationPercent = item.buildDurationPercent, Status = item.status, Url = item.url } ); } } //Get the total builds used in the calculation int buildTotal = builds.Count; //then build the calcuation, loading the dates into a date array List <KeyValuePair <DateTime, DateTime> > dateList = new List <KeyValuePair <DateTime, DateTime> >(); foreach (Build item in builds) { KeyValuePair <DateTime, DateTime> newItem = new KeyValuePair <DateTime, DateTime>(item.StartTime, item.EndTime); dateList.Add(newItem); } //then build the calcuation, loading the dates into a date array float deploymentsPerDay; deploymentsPerDay = deploymentFrequency.ProcessDeploymentFrequency(dateList, numberOfDays); //Filter the results to return the last n (maxNumberOfItems), to return to the UI builds = utility.GetLastNItems(builds, maxNumberOfItems); //Find the max build duration float maxBuildDuration = 0f; foreach (Build item in builds) { if (item.BuildDuration > maxBuildDuration) { maxBuildDuration = item.BuildDuration; } } //Calculate the percent scaling foreach (Build item in builds) { float interiumResult = ((item.BuildDuration / maxBuildDuration) * 100f); item.BuildDurationPercent = Scaling.ScaleNumberToRange(interiumResult, 0, 100, 20, 100); } //Return the completed model DeploymentFrequencyModel model = new DeploymentFrequencyModel { TargetDevOpsPlatform = DevOpsPlatform.AzureDevOps, DeploymentName = buildName, BuildList = builds, DeploymentsPerDayMetric = deploymentsPerDay, DeploymentsPerDayMetricDescription = deploymentFrequency.GetDeploymentFrequencyRating(deploymentsPerDay), NumberOfDays = numberOfDays, MaxNumberOfItems = builds.Count, TotalItems = buildTotal }; return(model); } else { return(null); } } else { //Get sample data List <Build> builds = utility.GetLastNItems(GetSampleAzureDevOpsBuilds(), maxNumberOfItems); DeploymentFrequencyModel model = new DeploymentFrequencyModel { TargetDevOpsPlatform = DevOpsPlatform.AzureDevOps, DeploymentName = buildName, BuildList = builds, DeploymentsPerDayMetric = 10f, DeploymentsPerDayMetricDescription = "Elite", NumberOfDays = numberOfDays, MaxNumberOfItems = builds.Count, TotalItems = builds.Count }; return(model); } }
public async Task <LeadTimeForChangesModel> GetAzureDevOpsLeadTimesForChanges(bool getSampleData, string patToken, TableStorageConfiguration tableStorageConfig, string organization, string project, string repository, string mainBranch, string buildName, int numberOfDays, int maxNumberOfItems, bool useCache) { ListUtility <PullRequestModel> utility = new ListUtility <PullRequestModel>(); LeadTimeForChanges leadTimeForChanges = new LeadTimeForChanges(); List <PullRequestModel> pullRequests = new List <PullRequestModel>(); if (getSampleData == false) { List <AzureDevOpsBuild> initialBuilds = new List <AzureDevOpsBuild>(); BuildsDA buildsDA = new BuildsDA(); initialBuilds = await buildsDA.GetAzureDevOpsBuilds(patToken, tableStorageConfig, organization, project, buildName, useCache); //Process all builds, filtering by main and feature branchs List <AzureDevOpsBuild> mainBranchBuilds = new List <AzureDevOpsBuild>(); List <AzureDevOpsBuild> featureBranchBuilds = new List <AzureDevOpsBuild>(); List <string> branches = new List <string>(); foreach (AzureDevOpsBuild item in initialBuilds) { if (item.status == "completed" && item.queueTime > DateTime.Now.AddDays(-numberOfDays)) { if (item.sourceBranch == mainBranch) { //Save the main branch mainBranchBuilds.Add(item); } else { //Save the feature branches featureBranchBuilds.Add(item); //Record all unique branches if (branches.Contains(item.branch) == false) { branches.Add(item.branch); } } } } //Process the lead time for changes List <KeyValuePair <DateTime, TimeSpan> > leadTimeForChangesList = new List <KeyValuePair <DateTime, TimeSpan> >(); foreach (string branch in branches) { List <AzureDevOpsBuild> branchBuilds = featureBranchBuilds.Where(a => a.sourceBranch == branch).ToList(); PullRequestsDA pullRequestDA = new PullRequestsDA(); AzureDevOpsPR pr = await pullRequestDA.GetAzureDevOpsPullRequest(patToken, tableStorageConfig, organization, project, repository, branch, useCache); if (pr != null) { List <AzureDevOpsPRCommit> pullRequestCommits = await pullRequestDA.GetAzureDevOpsPullRequestCommits(patToken, tableStorageConfig, organization, project, repository, pr.PullRequestId, useCache); List <Commit> commits = new List <Commit>(); foreach (AzureDevOpsPRCommit item in pullRequestCommits) { commits.Add(new Commit { commitId = item.commitId, name = item.committer.name, date = item.committer.date }); } DateTime minTime = DateTime.MaxValue; DateTime maxTime = DateTime.MinValue; foreach (AzureDevOpsPRCommit pullRequestCommit in pullRequestCommits) { if (minTime > pullRequestCommit.committer.date) { minTime = pullRequestCommit.committer.date; } if (maxTime < pullRequestCommit.committer.date) { maxTime = pullRequestCommit.committer.date; } } foreach (AzureDevOpsBuild branchBuild in branchBuilds) { if (minTime > branchBuild.finishTime) { minTime = branchBuild.finishTime; } if (maxTime < branchBuild.finishTime) { maxTime = branchBuild.finishTime; } } PullRequestModel pullRequest = new PullRequestModel { PullRequestId = pr.PullRequestId, Branch = branch, BuildCount = branchBuilds.Count, Commits = commits, StartDateTime = minTime, EndDateTime = maxTime, Status = pr.status, Url = $"https://dev.azure.com/{organization}/{project}/_git/{repository}/pullrequest/{pr.PullRequestId}" }; leadTimeForChangesList.Add(new KeyValuePair <DateTime, TimeSpan>(minTime, pullRequest.Duration)); pullRequests.Add(pullRequest); } } //Calculate the lead time for changes value, in hours float leadTime = leadTimeForChanges.ProcessLeadTimeForChanges(leadTimeForChangesList, numberOfDays); List <PullRequestModel> uiPullRequests = utility.GetLastNItems(pullRequests, maxNumberOfItems); float maxPullRequestDuration = 0f; foreach (PullRequestModel item in uiPullRequests) { if (item.Duration.TotalMinutes > maxPullRequestDuration) { maxPullRequestDuration = (float)item.Duration.TotalMinutes; } } foreach (PullRequestModel item in uiPullRequests) { float interiumResult = (((float)item.Duration.TotalMinutes / maxPullRequestDuration) * 100f); item.DurationPercent = Scaling.ScaleNumberToRange(interiumResult, 0, 100, 20, 100); } double totalHours = 0; foreach (AzureDevOpsBuild item in mainBranchBuilds) { totalHours += (item.finishTime - item.queueTime).TotalHours; } float averageBuildHours = 0; if (mainBranchBuilds.Count > 0) { averageBuildHours = (float)totalHours / (float)mainBranchBuilds.Count; } LeadTimeForChangesModel model = new LeadTimeForChangesModel { ProjectName = project, TargetDevOpsPlatform = DevOpsPlatform.AzureDevOps, AverageBuildHours = averageBuildHours, AveragePullRequestHours = leadTime, LeadTimeForChangesMetric = leadTime + averageBuildHours, LeadTimeForChangesMetricDescription = leadTimeForChanges.GetLeadTimeForChangesRating(leadTime), PullRequests = uiPullRequests, NumberOfDays = numberOfDays, MaxNumberOfItems = uiPullRequests.Count, TotalItems = pullRequests.Count }; return(model); } else { //Get sample data List <PullRequestModel> samplePullRequests = utility.GetLastNItems(CreatePullRequestsSample(DevOpsPlatform.AzureDevOps), maxNumberOfItems); LeadTimeForChangesModel model = new LeadTimeForChangesModel { ProjectName = project, TargetDevOpsPlatform = DevOpsPlatform.AzureDevOps, AverageBuildHours = 1f, AveragePullRequestHours = 12f, LeadTimeForChangesMetric = 12f + 1f, LeadTimeForChangesMetricDescription = "Elite", PullRequests = samplePullRequests, NumberOfDays = numberOfDays, MaxNumberOfItems = samplePullRequests.Count, TotalItems = samplePullRequests.Count }; return(model); } }