示例#1
0
        // Uploads the given package to the specified source and returns time elapsed to upload, or -1 if upload fails.
        private long UploadPackage(StopWatches timer, string packageFullPath, string sourceName, string ApiKey)
        {
            string standardOutput = string.Empty;
            string standardError  = string.Empty;

            return(InvokeNugetProcess(timer, string.Join(string.Empty, new string[] { PushCommandString, @"""" + packageFullPath + @"""", APIKeySwitchString, ApiKey, SourceSwitchString, sourceName }), out standardError, out standardOutput));
        }
示例#2
0
        public override void ExecuteCommand()
        {
            StopWatches timer      = new StopWatches();
            DateTime    today      = DateTime.Today;
            string      day        = string.Format("{0:yyyy-MM-dd}", today);
            string      version    = string.Empty;
            string      file       = Path.Combine(Environment.CurrentDirectory, TestPackageName + ".nupkg");
            string      newPackage = GetNewPackage(file, out version);

            //upload
            Console.WriteLine("Pushing :{0}", newPackage);
            long UploadTimeElapsed = UploadPackage(timer, newPackage, Source, ApiKey);

            ReportHelpers.AppendDatatoBlob(StorageAccount, ("UploadPackageTimeElapsed" + day + ".json"), new Tuple <string, string>(string.Format("{0:HH:mm}", DateTime.Now), UploadTimeElapsed.ToString()), 48, ContainerName);
            File.Delete("backup");

            //download
            long          DownloadTimeElapsed = -1;
            Task <string> result = null;

            result = DownloadPackageFromFeed(timer, TestPackageName, version, out DownloadTimeElapsed);
            Console.WriteLine(result.Status);
            DownloadTimeElapsed = timer.DownloadTimeElapsed.ElapsedMilliseconds;
            ReportHelpers.AppendDatatoBlob(StorageAccount, ("DownloadPackageTimeElapsed" + day + ".json"), new Tuple <string, string>(string.Format("{0:HH:mm}", DateTime.Now), DownloadTimeElapsed.ToString()), 48, ContainerName);

            //search
            long SearchTimeElapsed = -1;

            //SearchPackage is called until the uploaded package is seen in the search result
            while (SearchTimeElapsed == -1)
            {
                SearchTimeElapsed = SearchPackage(timer, TestPackageName, version);
            }

            ReportHelpers.AppendDatatoBlob(StorageAccount, ("SearchPackageTimeElapsed" + day + ".json"), new Tuple <string, string>(string.Format("{0:HH:mm}", DateTime.Now), SearchTimeElapsed.ToString()), 48, ContainerName);

            //catalog lag
            JToken timeStampCatalog;
            int    CatalogLag = DBToCatalogLag(timer, TestPackageName, out timeStampCatalog);

            ReportHelpers.AppendDatatoBlob(StorageAccount, ("CatalogLag" + day + ".json"), new Tuple <string, string>(string.Format("{0:HH:mm}", DateTime.Now), CatalogLag.ToString()), 48, ContainerName);
            ReportHelpers.CreateBlob(StorageAccount, ("LastCatalogTimeStamp.json"), ContainerName, "SqlDateTime", ReportHelpers.ToStream(timeStampCatalog));

            //resolver lag
            JToken timeStampResolver;
            double ResolverLag = CatalogToResolverLag(out timeStampResolver);

            ReportHelpers.AppendDatatoBlob(StorageAccount, ("ResolverLag" + day + ".json"), new Tuple <string, string>(string.Format("{0:HH:mm}", DateTime.Now), ResolverLag.ToString()), 48, ContainerName);
            ReportHelpers.CreateBlob(StorageAccount, ("LastResolverTimeStamp.json"), ContainerName, "SqlDateTime", ReportHelpers.ToStream(timeStampResolver));
            SendAlerts(UploadTimeElapsed, DownloadTimeElapsed, SearchTimeElapsed, CatalogLag, ResolverLag);
        }
        public override void ExecuteCommand()
        {

            StopWatches timer = new StopWatches();
            DateTime today = DateTime.Today;
            string day = string.Format("{0:yyyy-MM-dd}", today);
            string version = string.Empty;
            string file = Path.Combine(Environment.CurrentDirectory, TestPackageName + ".nupkg");
            string newPackage = GetNewPackage(file, out version);

            //upload
            Console.WriteLine("Pushing :{0}", newPackage);
            long UploadTimeElapsed = UploadPackage(timer, newPackage, Source, ApiKey);
            ReportHelpers.AppendDatatoBlob(StorageAccount, ("UploadPackageTimeElapsed" + day + ".json"), new Tuple<string, string>(string.Format("{0:HH:mm}", DateTime.Now), UploadTimeElapsed.ToString()), 48, ContainerName);
            File.Delete("backup"); 

            //download
            long DownloadTimeElapsed = -1;
            Task<string> result = null;
            result = DownloadPackageFromFeed(timer, TestPackageName, version, out DownloadTimeElapsed);
            Console.WriteLine(result.Status);
            DownloadTimeElapsed = timer.DownloadTimeElapsed.ElapsedMilliseconds;
            ReportHelpers.AppendDatatoBlob(StorageAccount, ("DownloadPackageTimeElapsed" + day + ".json"), new Tuple<string, string>(string.Format("{0:HH:mm}", DateTime.Now), DownloadTimeElapsed.ToString()), 48, ContainerName);

            //search
            long SearchTimeElapsed = -1;
            //SearchPackage is called until the uploaded package is seen in the search result
            while (SearchTimeElapsed == -1)
            {
                SearchTimeElapsed = SearchPackage(timer, TestPackageName, version);
            }

            ReportHelpers.AppendDatatoBlob(StorageAccount, ("SearchPackageTimeElapsed" + day + ".json"), new Tuple<string, string>(string.Format("{0:HH:mm}", DateTime.Now), SearchTimeElapsed.ToString()), 48, ContainerName);

            //catalog lag
            JToken timeStampCatalog;
            int CatalogLag = DBToCatalogLag(timer, TestPackageName, out timeStampCatalog);
            ReportHelpers.AppendDatatoBlob(StorageAccount, ("CatalogLag" + day + ".json"), new Tuple<string, string>(string.Format("{0:HH:mm}", DateTime.Now), CatalogLag.ToString()), 48, ContainerName);
            ReportHelpers.CreateBlob(StorageAccount, ("LastCatalogTimeStamp.json"), ContainerName, "SqlDateTime", ReportHelpers.ToStream(timeStampCatalog));

            //resolver lag
            JToken timeStampResolver;
            double ResolverLag = CatalogToResolverLag(out timeStampResolver);                       
            ReportHelpers.AppendDatatoBlob(StorageAccount, ("ResolverLag" + day + ".json"), new Tuple<string, string>(string.Format("{0:HH:mm}", DateTime.Now), ResolverLag.ToString()), 48, ContainerName);
            ReportHelpers.CreateBlob(StorageAccount, ("LastResolverTimeStamp.json"), ContainerName, "SqlDateTime", ReportHelpers.ToStream(timeStampResolver));
            SendAlerts(UploadTimeElapsed, DownloadTimeElapsed, SearchTimeElapsed, CatalogLag, ResolverLag);
        }
示例#4
0
        //searches for the package (id, version) specified, returns -1 if not found and is invoked again by the calling code, until the package is found
        private long SearchPackage(StopWatches timer, string TestPackageName, string Version)
        {
            string uri       = SearchAPIBase + "/query?q=%27" + TestPackageName + "%27&luceneQuery=false";
            Uri    searchAPI = new Uri(uri);

            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            string       JSONResult           = client.GetStringAsync(searchAPI).Result;
            SearchResult result = new SearchResult();

            result = new JavaScriptSerializer().Deserialize <SearchResult>(JSONResult);
            foreach (var document in result.data)
            {
                if (document.Version == Version && document.PackageRegistration.Id == TestPackageName)
                {
                    Console.WriteLine(document.PackageRegistration.Id + " " + document.Version);
                    timer.SearchTimeElapsed.Stop();
                    return(timer.SearchTimeElapsed.ElapsedMilliseconds);
                }
            }

            return(-1);
        }
示例#5
0
        //calculates the number of packages added to DB after the catalog was last modified
        private int DBToCatalogLag(StopWatches timer, string TestPackageName, out JToken commitTimeStamp)
        {
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            string  root     = client.GetStringAsync(CatalogUrl).Result;
            JObject indexObj = JObject.Parse(root);
            JToken  context  = null;

            indexObj.TryGetValue("@context", out context);
            commitTimeStamp = null;
            indexObj.TryGetValue("commitTimestamp", out commitTimeStamp);
            SqlDateTime timeStamp = commitTimeStamp.ToObject <SqlDateTime>();

            using (var sqlConnection = new SqlConnection(ConnectionString.ConnectionString))
            {
                using (var dbExecutor = new SqlExecutor(sqlConnection))
                {
                    sqlConnection.Open();
                    string query = string.Format("Select count(*) from Packages where Created> '{0}'", timeStamp);
                    int    lag   = dbExecutor.Query <int>(query).SingleOrDefault();
                    return(lag);
                }
            }
        }
示例#6
0
        //pushes the package to the source specified (Source) and returns the time elapsed in milliseconds to upload the package, or -1 if upload fails
        private long InvokeNugetProcess(StopWatches timer, string arguments, out string standardError, out string standardOutput, string WorkingDir = null)
        {
            Process          nugetProcess          = new Process();
            string           pathToNugetExe        = Path.Combine(Environment.CurrentDirectory, NugetExePath);
            ProcessStartInfo nugetProcessStartInfo = new ProcessStartInfo(pathToNugetExe);

            nugetProcessStartInfo.Arguments              = arguments;
            nugetProcessStartInfo.RedirectStandardError  = true;
            nugetProcessStartInfo.RedirectStandardOutput = true;
            nugetProcessStartInfo.RedirectStandardInput  = true;
            nugetProcessStartInfo.UseShellExecute        = false;
            nugetProcessStartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            nugetProcessStartInfo.CreateNoWindow         = true;
            nugetProcess.StartInfo = nugetProcessStartInfo;
            nugetProcess.StartInfo.WorkingDirectory = WorkingDir;
            timer.UploadTimeElapsed = Stopwatch.StartNew();
            nugetProcess.Start();
            timer.UploadTimeElapsed.Start();
            standardError = nugetProcess.StandardError.ReadToEnd();
            Console.WriteLine(standardError);
            standardOutput = nugetProcess.StandardOutput.ReadToEnd();
            Console.WriteLine(standardOutput);
            nugetProcess.WaitForExit();
            timer.UploadTimeElapsed.Stop();
            timer.DownloadTimeElapsed = Stopwatch.StartNew();
            timer.SearchTimeElapsed   = Stopwatch.StartNew();
            if (nugetProcess.ExitCode == 0)
            {
                return(timer.UploadTimeElapsed.ElapsedMilliseconds);
            }

            else
            {
                return(-1);
            }
        }
示例#7
0
        //downloads the package, given id and version
        private Task <string> DownloadPackageFromFeed(StopWatches timer, string packageId, string version, out long DownloadTimeElapsed, string operation = "Install")
        {
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            string requestUri               = DownloadAPIBase + @"Package/" + packageId + @"/" + version;
            bool   flag                     = false;
            CancellationTokenSource cts     = new CancellationTokenSource();
            HttpRequestMessage      request = new HttpRequestMessage(HttpMethod.Get, requestUri);

            request.Headers.Add("user-agent", "TestAgent");
            request.Headers.Add("NuGet-Operation", operation);
            Task <HttpResponseMessage>    responseTask = client.SendAsync(request);
            TaskCompletionSource <string> tcs          = new TaskCompletionSource <string>();

            responseTask.ContinueWith((rt) =>
            {
                HttpResponseMessage responseMessage = rt.Result;
                if (responseMessage.StatusCode == HttpStatusCode.OK)
                {
                    try
                    {
                        string filename;
                        ContentDispositionHeaderValue contentDisposition = responseMessage.Content.Headers.ContentDisposition;
                        if (contentDisposition != null)
                        {
                            filename = contentDisposition.FileName;
                        }
                        else
                        {
                            filename = packageId;  // if file name not present set the package Id for the file name.
                        }
                        FileStream fileStream = File.Create(filename);
                        Task contentTask      = responseMessage.Content.CopyToAsync(fileStream);

                        contentTask.ContinueWith((ct) =>
                        {
                            try
                            {
                                fileStream.Close();
                                tcs.SetResult(filename);

                                timer.DownloadTimeElapsed.Stop();
                                flag = true;
                                Console.WriteLine(ct.Status);
                                return;
                            }
                            catch (Exception e)
                            {
                                tcs.SetException(e);
                                flag = false;
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        tcs.SetException(e);
                        flag = false;
                    }
                }
                else
                {
                    string msg = string.Format("Http StatusCode: {0}", responseMessage.StatusCode);
                    tcs.SetException(new ApplicationException(msg));
                    flag = false;
                }
            });

            if (flag == true)
            {
                DownloadTimeElapsed = timer.DownloadTimeElapsed.ElapsedMilliseconds;
            }

            else
            {
                DownloadTimeElapsed = -1;
            }

            return(tcs.Task);
        }
 //calculates the number of packages added to DB after the catalog was last modified
 private int DBToCatalogLag(StopWatches timer, string TestPackageName, out JToken commitTimeStamp)
 {
     System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
     string root = client.GetStringAsync(CatalogUrl).Result;
     JObject indexObj = JObject.Parse(root);
     JToken context = null;
     indexObj.TryGetValue("@context", out context);
     commitTimeStamp = null;
     indexObj.TryGetValue("commitTimestamp", out commitTimeStamp);
     SqlDateTime timeStamp = commitTimeStamp.ToObject<SqlDateTime>();
     using (var sqlConnection = new SqlConnection(ConnectionString.ConnectionString))
     {
         using (var dbExecutor = new SqlExecutor(sqlConnection))
         {
             sqlConnection.Open();
             string query = string.Format("Select count(*) from Packages where Created> '{0}'", timeStamp);
             int lag = dbExecutor.Query<int>(query).SingleOrDefault();
             return lag;
         }
     }
 }
        //downloads the package, given id and version
        private Task<string> DownloadPackageFromFeed(StopWatches timer, string packageId, string version, out long DownloadTimeElapsed, string operation = "Install")
        {
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            string requestUri = DownloadAPIBase + @"Package/" + packageId + @"/" + version;
            bool flag = false;
            CancellationTokenSource cts = new CancellationTokenSource();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
            request.Headers.Add("user-agent", "TestAgent");
            request.Headers.Add("NuGet-Operation", operation);
            Task<HttpResponseMessage> responseTask = client.SendAsync(request);
            TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
            responseTask.ContinueWith((rt) =>
             {
                 HttpResponseMessage responseMessage = rt.Result;
                 if (responseMessage.StatusCode == HttpStatusCode.OK)
                 {
                     try
                     {
                         string filename;
                         ContentDispositionHeaderValue contentDisposition = responseMessage.Content.Headers.ContentDisposition;
                         if (contentDisposition != null)
                         {
                             filename = contentDisposition.FileName;
                         }
                         else
                         {
                             filename = packageId; // if file name not present set the package Id for the file name. 
                         }
                         FileStream fileStream = File.Create(filename);
                         Task contentTask = responseMessage.Content.CopyToAsync(fileStream);

                         contentTask.ContinueWith((ct) =>
                         {
                             try
                             {
                                 fileStream.Close();
                                 tcs.SetResult(filename);

                                 timer.DownloadTimeElapsed.Stop();
                                 flag = true;
                                 Console.WriteLine(ct.Status);
                                 return;

                             }
                             catch (Exception e)
                             {
                                 tcs.SetException(e);
                                 flag = false;
                             }
                         });


                     }
                     catch (Exception e)
                     {
                         tcs.SetException(e);
                         flag = false;
                     }
                 }
                 else
                 {
                     string msg = string.Format("Http StatusCode: {0}", responseMessage.StatusCode);
                     tcs.SetException(new ApplicationException(msg));
                     flag = false;
                 }
             });

            if (flag == true)
            {
                DownloadTimeElapsed = timer.DownloadTimeElapsed.ElapsedMilliseconds;
            }

            else
            {
                DownloadTimeElapsed = -1;
            }

            return tcs.Task;
        }
        //searches for the package (id, version) specified, returns -1 if not found and is invoked again by the calling code, until the package is found
        private long SearchPackage(StopWatches timer, string TestPackageName, string Version)
        {
            string uri = SearchAPIBase + "/query?q=%27" + TestPackageName + "%27&luceneQuery=false";
            Uri searchAPI = new Uri(uri);
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            string JSONResult = client.GetStringAsync(searchAPI).Result;
            SearchResult result = new SearchResult();
            result = new JavaScriptSerializer().Deserialize<SearchResult>(JSONResult);
            foreach (var document in result.data)
            {
                if (document.Version == Version && document.PackageRegistration.Id == TestPackageName)
                {
                    Console.WriteLine(document.PackageRegistration.Id + " " + document.Version);
                    timer.SearchTimeElapsed.Stop();
                    return timer.SearchTimeElapsed.ElapsedMilliseconds;
                }
            }

            return -1;
        }
 // Uploads the given package to the specified source and returns time elapsed to upload, or -1 if upload fails.
 private long UploadPackage(StopWatches timer, string packageFullPath, string sourceName, string ApiKey)
 {
     string standardOutput = string.Empty;
     string standardError = string.Empty;
     return InvokeNugetProcess(timer, string.Join(string.Empty, new string[] { PushCommandString, @"""" + packageFullPath + @"""", APIKeySwitchString, ApiKey, SourceSwitchString, sourceName }), out standardError, out standardOutput);
 }
        //pushes the package to the source specified (Source) and returns the time elapsed in milliseconds to upload the package, or -1 if upload fails
        private long InvokeNugetProcess(StopWatches timer, string arguments, out string standardError, out string standardOutput, string WorkingDir = null)
        {
            Process nugetProcess = new Process();
            string pathToNugetExe = Path.Combine(Environment.CurrentDirectory, NugetExePath);
            ProcessStartInfo nugetProcessStartInfo = new ProcessStartInfo(pathToNugetExe);
            nugetProcessStartInfo.Arguments = arguments;
            nugetProcessStartInfo.RedirectStandardError = true;
            nugetProcessStartInfo.RedirectStandardOutput = true;
            nugetProcessStartInfo.RedirectStandardInput = true;
            nugetProcessStartInfo.UseShellExecute = false;
            nugetProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            nugetProcessStartInfo.CreateNoWindow = true;
            nugetProcess.StartInfo = nugetProcessStartInfo;
            nugetProcess.StartInfo.WorkingDirectory = WorkingDir;
            timer.UploadTimeElapsed = Stopwatch.StartNew();
            nugetProcess.Start();
            timer.UploadTimeElapsed.Start();
            standardError = nugetProcess.StandardError.ReadToEnd();
            Console.WriteLine(standardError);
            standardOutput = nugetProcess.StandardOutput.ReadToEnd();
            Console.WriteLine(standardOutput);
            nugetProcess.WaitForExit();
            timer.UploadTimeElapsed.Stop();
            timer.DownloadTimeElapsed = Stopwatch.StartNew();
            timer.SearchTimeElapsed = Stopwatch.StartNew();
            if (nugetProcess.ExitCode == 0)
            {
                return timer.UploadTimeElapsed.ElapsedMilliseconds;
            }

            else
            {

                return -1;
            }
        }