<commit_before>﻿//---------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------------------------------------------------

namespace WebAPI
{
    using System.Web.Http;
    using System;
    using System.Threading.Tasks;
    using ReachingTypeAnalysis.Statistics;
    using ReachingTypeAnalysis;
    using Orleans;
    using System.IO;
    using OrleansInterfaces;
    using System.Collections.ObjectModel;
    using System.Management.Automation.Runspaces;
    using System.Management.Automation;

    public class ExperimentsController : ApiController
    {
		// http://localhost:49176/api/Experiments?testName=Hola&machines=1&numberOfMethods=2&expID=dummy
		[HttpGet]
		public Task<string> RunTestAsync(string testName, int machines, int numberOfMethods, string expID)
		{
			var result = string.Empty;

			try
			{
				var analyzer = SolutionAnalyzer.CreateFromTest(testName);
				var analysisClient = new AnalysisClient(analyzer, machines);
				//var results = await analysisClient.RunExperiment(GrainClient.GrainFactory, expID);
				analysisClient.StartRunningExperiment(GrainClient.GrainFactory, expID);

				//result = string.Format("Ready for queries. Time: {0} ms", results.ElapsedTime);
				result = string.Format("Running test {0}.", testName);
			}
			catch (Exception exc)
			{
				while (exc is AggregateException) exc = exc.InnerException;
				result = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
			}

			return Task.FromResult(result);
		}

		[HttpGet]
		public Task<string> AnalyzeSolutionAsync(string drive, string solutionPath, string solutionName, int machines, string expID)
		{
			var result = string.Empty;

			try
			{
                if (string.IsNullOrEmpty(expID))
                {
                    expID = solutionName;
                }
				solutionPath = Path.Combine(drive + ":\\" + solutionPath, solutionName + ".sln");
				var analyzer = SolutionAnalyzer.CreateFromSolution(solutionPath);
				var analysisClient = new AnalysisClient(analyzer, machines);
				//var results = await analysisClient.RunExperiment(GrainClient.GrainFactory, expID);
				analysisClient.StartRunningExperiment(GrainClient.GrainFactory, expID);

				//result = string.Format("Ready for queries. Time: {0} ms", results.ElapsedTime);
				result = string.Format("Analyzing solution {0}.", solutionName);
			}
			catch (Exception exc)
			{
				while (exc is AggregateException) exc = exc.InnerException;
				result = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
			}

			return Task.FromResult(result);
		}

        [HttpGet]
        public async Task<string> ComputeQueries(string className, string methodPrefix, int machines, int numberOfMethods, int repetitions, string assemblyName, string expID)
        {
            var resultStr = "";
            try
            {
                var solutionGrain = GrainClient.GrainFactory.GetGrain<ISolutionGrain>("Solution");

                var analysisClient = new AnalysisClient(solutionGrain, machines);
                //var result = await analysisClient.ComputeRandomQueries(className, methodPrefix, numberOfMethods, repetitions, assemblyName, expID);
                var result = await analysisClient.ComputeRandomQueries(repetitions, expID);
                var avgTime = result.Item1;
                var minTime = result.Item2;
                var maxTime = result.Item3;
            }
            catch (Exception exc)
            {
                while (exc is AggregateException) exc = exc.InnerException;
                resultStr = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
            }
            return resultStr;
        }

        [HttpGet]
        public async Task<string> ComputeQueries(int machines, int repetitions, string expID)
        {
            var resultStr = "";
            try
            {
                var solutionGrain = GrainClient.GrainFactory.GetGrain<ISolutionGrain>("Solution");
                var analysisClient = new AnalysisClient(solutionGrain, machines);
                var result = await analysisClient.ComputeRandomQueries(repetitions, expID);
                var avgTime = result.Item1;
                var minTime = result.Item2;
                var maxTime = result.Item3;
            }
            catch (Exception exc)
            {
                while (exc is AggregateException) exc = exc.InnerException;
                resultStr = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
            }
            return resultStr;
        }

        //[HttpGet]
        public async Task<string> PerformDeactivationAsync()
		{
			var result = string.Empty;

			try
			{
				var solutionGrain = GrainClient.GrainFactory.GetGrain<ISolutionGrain>("Solution");
				await AnalysisClient.PerformDeactivation(GrainClient.GrainFactory, solutionGrain);

				result = string.Format("All grains are deactivated");
			}
			catch (Exception exc)
			{
				while (exc is AggregateException) exc = exc.InnerException;
				result = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
			}

			return result;
		}

        [HttpGet]
        public async Task<string> ExecuteCmd(string command)
        {
            var result = string.Empty;
            try
            {
                switch(command)
                {
                    case "Deactivate":
                        result = await PerformDeactivationAsync();
                        break;
                    case "RemoveGrainState":
                        result = AnalysisClient.EmptyTable("OrleansGrainState").ToString();
                        break;
                    case "RemoveStats":

                        result = AnalysisClient.EmptyTable("OrleansSiloStatistics").ToString()+" " + AnalysisClient.EmptyTable("OrleansClientStatistics").ToString();
                        break;
                    case "Stats":
                        result = await AnalysisClient.PrintGrainStatistics(GrainClient.GrainFactory);
                        break;
					case "Status":
                        result = Convert.ToString(AnalysisClient.ExperimentStatus);
                        if (result.Equals("Running") || result.Equals("Compiling"))
                        {
                            result += " Client messages:" + await AnalysisClient.LastMessage +"\n"+ AnalysisClient.ErrorMessage ; // + await AnalysisClient.CurrentAnalyzedMethodsCount;
                        }
						break;

                    // Unfortunately I cannot run the following scripts from a WebRole
                    // I can run scripts from Azure PowerShell on the development machine
                    // or use the Azure Web API
                    //case "Restart":
                    //    result = RunScript("Stop-Start-CloudService.ps1").ToString();
                    //    break;
                    //case "Instances":
                    //    result = RunScript("ChangeNumberOfInstances.ps1", "-Instances", "2").ToString();
                    //    break;
                }
            }
            catch (Exception exc)
            {
                while (exc is AggregateException) exc = exc.InnerException;
                result = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
            }

            return result;
        }

        public static Collection<PSObject> RunScript(string scriptfile, string pKey1 = "", string pValue1 = "")
        {
            var runspaceConfiguration = RunspaceConfiguration.Create();
            var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);<commit_msg>Just changed the status message in the Experiment Controller<commit_after>﻿//---------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------------------------------------------------

namespace WebAPI
{
    using System.Web.Http;
    using System;
    using System.Threading.Tasks;
    using ReachingTypeAnalysis.Statistics;
    using ReachingTypeAnalysis;
    using Orleans;
    using System.IO;
    using OrleansInterfaces;
    using System.Collections.ObjectModel;
    using System.Management.Automation.Runspaces;
    using System.Management.Automation;

    public class ExperimentsController : ApiController
    {
		// http://localhost:49176/api/Experiments?testName=Hola&machines=1&numberOfMethods=2&expID=dummy
		[HttpGet]
		public Task<string> RunTestAsync(string testName, int machines, int numberOfMethods, string expID)
		{
			var result = string.Empty;

			try
			{
				var analyzer = SolutionAnalyzer.CreateFromTest(testName);
				var analysisClient = new AnalysisClient(analyzer, machines);
				//var results = await analysisClient.RunExperiment(GrainClient.GrainFactory, expID);
				analysisClient.StartRunningExperiment(GrainClient.GrainFactory, expID);

				//result = string.Format("Ready for queries. Time: {0} ms", results.ElapsedTime);
				result = string.Format("Running test {0}.", testName);
			}
			catch (Exception exc)
			{
				while (exc is AggregateException) exc = exc.InnerException;
				result = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
			}

			return Task.FromResult(result);
		}

		[HttpGet]
		public Task<string> AnalyzeSolutionAsync(string drive, string solutionPath, string solutionName, int machines, string expID)
		{
			var result = string.Empty;

			try
			{
                if (string.IsNullOrEmpty(expID))
                {
                    expID = solutionName;
                }
				solutionPath = Path.Combine(drive + ":\\" + solutionPath, solutionName + ".sln");
				var analyzer = SolutionAnalyzer.CreateFromSolution(solutionPath);
				var analysisClient = new AnalysisClient(analyzer, machines);
				//var results = await analysisClient.RunExperiment(GrainClient.GrainFactory, expID);
				analysisClient.StartRunningExperiment(GrainClient.GrainFactory, expID);

				//result = string.Format("Ready for queries. Time: {0} ms", results.ElapsedTime);
				result = string.Format("Analyzing solution {0}.", solutionName);
			}
			catch (Exception exc)
			{
				while (exc is AggregateException) exc = exc.InnerException;
				result = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
			}

			return Task.FromResult(result);
		}

        [HttpGet]
        public async Task<string> ComputeQueries(string className, string methodPrefix, int machines, int numberOfMethods, int repetitions, string assemblyName, string expID)
        {
            var resultStr = "";
            try
            {
                var solutionGrain = GrainClient.GrainFactory.GetGrain<ISolutionGrain>("Solution");

                var analysisClient = new AnalysisClient(solutionGrain, machines);
                //var result = await analysisClient.ComputeRandomQueries(className, methodPrefix, numberOfMethods, repetitions, assemblyName, expID);
                var result = await analysisClient.ComputeRandomQueries(repetitions, expID);
                var avgTime = result.Item1;
                var minTime = result.Item2;
                var maxTime = result.Item3;
            }
            catch (Exception exc)
            {
                while (exc is AggregateException) exc = exc.InnerException;
                resultStr = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
            }
            return resultStr;
        }

        [HttpGet]
        public async Task<string> ComputeQueries(int machines, int repetitions, string expID)
        {
            var resultStr = "";
            try
            {
                var solutionGrain = GrainClient.GrainFactory.GetGrain<ISolutionGrain>("Solution");
                var analysisClient = new AnalysisClient(solutionGrain, machines);
                var result = await analysisClient.ComputeRandomQueries(repetitions, expID);
                var avgTime = result.Item1;
                var minTime = result.Item2;
                var maxTime = result.Item3;
            }
            catch (Exception exc)
            {
                while (exc is AggregateException) exc = exc.InnerException;
                resultStr = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
            }
            return resultStr;
        }

        //[HttpGet]
        public async Task<string> PerformDeactivationAsync()
		{
			var result = string.Empty;

			try
			{
				var solutionGrain = GrainClient.GrainFactory.GetGrain<ISolutionGrain>("Solution");
				await AnalysisClient.PerformDeactivation(GrainClient.GrainFactory, solutionGrain);

				result = string.Format("All grains are deactivated");
			}
			catch (Exception exc)
			{
				while (exc is AggregateException) exc = exc.InnerException;
				result = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
			}

			return result;
		}

        [HttpGet]
        public async Task<string> ExecuteCmd(string command)
        {
            var result = string.Empty;
            try
            {
                switch(command)
                {
                    case "Deactivate":
                        result = await PerformDeactivationAsync();
                        break;
                    case "RemoveGrainState":
                        result = AnalysisClient.EmptyTable("OrleansGrainState").ToString();
                        break;
                    case "RemoveStats":

                        result = AnalysisClient.EmptyTable("OrleansSiloStatistics").ToString()+" " + AnalysisClient.EmptyTable("OrleansClientStatistics").ToString();
                        break;
                    case "Stats":
                        result = await AnalysisClient.PrintGrainStatistics(GrainClient.GrainFactory);
                        break;
					case "Status":
                        result = Convert.ToString(AnalysisClient.ExperimentStatus);
                        if (!result.Equals("None"))
                        {
                            result += " Message:" + await AnalysisClient.LastMessage +"\n"+ AnalysisClient.ErrorMessage ; // + await AnalysisClient.CurrentAnalyzedMethodsCount;
                        }
						break;

                    // Unfortunately I cannot run the following scripts from a WebRole
                    // I can run scripts from Azure PowerShell on the development machine
                    // or use the Azure Web API
                    //case "Restart":
                    //    result = RunScript("Stop-Start-CloudService.ps1").ToString();
                    //    break;
                    //case "Instances":
                    //    result = RunScript("ChangeNumberOfInstances.ps1", "-Instances", "2").ToString();
                    //    break;
                }
            }
            catch (Exception exc)
            {
                while (exc is AggregateException) exc = exc.InnerException;
                result = "Error connecting to Orleans: " + exc + " at " + DateTime.Now;
            }

            return result;
        }

        public static Collection<PSObject> RunScript(string scriptfile, string pKey1 = "", string pValue1 = "")
        {
            var runspaceConfiguration = RunspaceConfiguration.Create();
            var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
{'fraction_non_alphanumeric': 0.11927948571064859, 'fraction_numerical': 0.0028005855769842785, 'mean_word_length': 1.8729200950813678, 'pattern_counts': {'":': 16, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6122695', 'n_tokens_mistral': 4191, 'n_tokens_neox': 3743, 'n_words': 933}