public void DeploymentFrequencyNullOneDayTest()
        {
            //Arrange
            DeploymentFrequency metrics = new DeploymentFrequency();
            int numberOfDays            = 1;
            List <KeyValuePair <DateTime, DateTime> > deploymentFrequencyList = null;

            //Act
            float metric = metrics.ProcessDeploymentFrequency(deploymentFrequencyList, numberOfDays);
            DeploymentFrequencyModel model = new DeploymentFrequencyModel
            {
                DeploymentsPerDayMetric            = metric,
                DeploymentsPerDayMetricDescription = metrics.GetDeploymentFrequencyRating(metric),
                IsProjectView = true,
                ItemOrder     = 1,
                RateLimitHit  = false
            };

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(0f, model.DeploymentsPerDayMetric);
            Assert.AreEqual("None", model.DeploymentsPerDayMetricDescription);
            Assert.AreEqual(0, model.DeploymentsToDisplayMetric);
            Assert.AreEqual("times per month", model.DeploymentsToDisplayUnit);
            Assert.AreEqual(true, model.IsProjectView);
            Assert.AreEqual(1, model.ItemOrder);
            Assert.AreEqual(false, model.RateLimitHit);
        }
        public void DeploymentFrequencyFiveSevenDaysTest()
        {
            //Arrange
            DeploymentFrequency metrics = new DeploymentFrequency();
            int numberOfDays            = 7;
            List <KeyValuePair <DateTime, DateTime> > deploymentFrequencyList = new List <KeyValuePair <DateTime, DateTime> >
            {
                new KeyValuePair <DateTime, DateTime>(DateTime.Now, DateTime.Now),
                new KeyValuePair <DateTime, DateTime>(DateTime.Now.AddDays(-1), DateTime.Now.AddDays(-1)),
                new KeyValuePair <DateTime, DateTime>(DateTime.Now.AddDays(-2), DateTime.Now.AddDays(-2)),
                new KeyValuePair <DateTime, DateTime>(DateTime.Now.AddDays(-3), DateTime.Now.AddDays(-3)),
                new KeyValuePair <DateTime, DateTime>(DateTime.Now.AddDays(-4), DateTime.Now.AddDays(-4)),
                new KeyValuePair <DateTime, DateTime>(DateTime.Now.AddDays(-8), DateTime.Now.AddDays(-8)) //this record should be out of range, as it's older than 7 days
            };

            //Act
            float metric = metrics.ProcessDeploymentFrequency(deploymentFrequencyList, numberOfDays);
            DeploymentFrequencyModel model = new DeploymentFrequencyModel
            {
                DeploymentsPerDayMetric            = metric,
                DeploymentsPerDayMetricDescription = metrics.GetDeploymentFrequencyRating(metric)
            };

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(0.7143f, model.DeploymentsPerDayMetric);
            Assert.AreEqual("High", model.DeploymentsPerDayMetricDescription);
            Assert.AreEqual(5.0000997f, model.DeploymentsToDisplayMetric);
            Assert.AreEqual("times per week", model.DeploymentsToDisplayUnit);
        }
        public void DeploymentFrequencySingleOneDayTest()
        {
            //Arrange
            DeploymentFrequency metrics = new DeploymentFrequency();
            int numberOfDays            = 1;
            List <KeyValuePair <DateTime, DateTime> > deploymentFrequencyList = new List <KeyValuePair <DateTime, DateTime> >
            {
                new KeyValuePair <DateTime, DateTime>(DateTime.Now, DateTime.Now)
            };

            //Act
            float metric = metrics.ProcessDeploymentFrequency(deploymentFrequencyList, numberOfDays);
            DeploymentFrequencyModel model = new DeploymentFrequencyModel
            {
                DeploymentsPerDayMetric            = metric,
                DeploymentsPerDayMetricDescription = metrics.GetDeploymentFrequencyRating(metric)
            };

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(1f, model.DeploymentsPerDayMetric);
            Assert.AreEqual("High", model.DeploymentsPerDayMetricDescription);
            Assert.AreEqual(7, model.DeploymentsToDisplayMetric);
            Assert.AreEqual("times per week", model.DeploymentsToDisplayUnit);
        }
        public void DeploymentFrequencyRatingZeroNoneTest()
        {
            //Arrange
            DeploymentFrequency metrics = new DeploymentFrequency();
            float metric = 0f; //None

            //Act
            DeploymentFrequencyModel model = new DeploymentFrequencyModel
            {
                DeploymentsPerDayMetric            = metric,
                DeploymentsPerDayMetricDescription = metrics.GetDeploymentFrequencyRating(metric)
            };

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(0f, model.DeploymentsPerDayMetric);
            Assert.AreEqual("None", model.DeploymentsPerDayMetricDescription);
            Assert.AreEqual(0, model.DeploymentsToDisplayMetric);
            Assert.AreEqual("times per month", model.DeploymentsToDisplayUnit);
        }
        public void DeploymentFrequencyRatingLowTest()
        {
            //Arrange
            DeploymentFrequency metrics = new DeploymentFrequency();
            float metric = (1f / 30f) - 0.01f; //monthly

            //Act
            DeploymentFrequencyModel model = new DeploymentFrequencyModel
            {
                DeploymentsPerDayMetric            = metric,
                DeploymentsPerDayMetricDescription = metrics.GetDeploymentFrequencyRating(metric)
            };

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(metric, model.DeploymentsPerDayMetric);
            Assert.AreEqual("Low", model.DeploymentsPerDayMetricDescription);
            Assert.AreEqual(0.70000005f, model.DeploymentsToDisplayMetric);
            Assert.AreEqual("times per month", model.DeploymentsToDisplayUnit);
        }
        public void DeploymentFrequencyRatingHighTest()
        {
            //Arrange
            DeploymentFrequency metrics = new DeploymentFrequency();
            float metric = 1f / 7f; //weekly

            //Act
            DeploymentFrequencyModel model = new DeploymentFrequencyModel
            {
                DeploymentsPerDayMetric            = metric,
                DeploymentsPerDayMetricDescription = metrics.GetDeploymentFrequencyRating(metric)
            };

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(1f / 7f, model.DeploymentsPerDayMetric);
            Assert.AreEqual("High", model.DeploymentsPerDayMetricDescription);
            Assert.AreEqual(1f, model.DeploymentsToDisplayMetric);
            Assert.AreEqual("times per week", model.DeploymentsToDisplayUnit);
        }
        public void DeploymentFrequencyRatingEliteTest()
        {
            //Arrange
            DeploymentFrequency metrics = new DeploymentFrequency();
            float metric = 1.01f; //daily

            //Act
            DeploymentFrequencyModel model = new DeploymentFrequencyModel
            {
                DeploymentsPerDayMetric            = metric,
                DeploymentsPerDayMetricDescription = metrics.GetDeploymentFrequencyRating(metric)
            };

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(1.01f, model.DeploymentsPerDayMetric);
            Assert.AreEqual("Elite", model.DeploymentsPerDayMetricDescription);
            Assert.AreEqual(1.01f, model.DeploymentsToDisplayMetric);
            Assert.AreEqual("per day", model.DeploymentsToDisplayUnit);
        }
        public async Task <DeploymentFrequencyModel> GetAzureDevOpsDeploymentFrequency(bool getSampleData, string patToken, TableStorageAuth tableStorageAuth,
                                                                                       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, tableStorageAuth, 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 <DeploymentFrequencyModel> GetGitHubDeploymentFrequency(bool getSampleData, string clientId, string clientSecret, TableStorageAuth tableStorageAuth,
                                                                                  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, tableStorageAuth, 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);
            }
        }