public String Get()
        {
            string reply = GetOtherTelemetry("", "", "", "query", "query=exceptions | where timestamp > ago(7d) | summarize exceptioncount = count() by bin(timestamp, 1d), type | top 5 by exceptioncount desc");

            ReplyData parsed = JsonConvert.DeserializeObject <ReplyData>(reply);

            ChartData charted = new ChartData();

            charted.chartData = new List <DataPair>();

            foreach (List <object> item in parsed.tables.ElementAt(0).rows)
            {
                DataPair temp = new DataPair();
                temp.label        = ((DateTime)item.ElementAt(0)).ToString();
                temp.hour         = (DateTime)item.ElementAt(0);
                temp.except_type  = (item.ElementAt(1)).ToString();
                temp.except_count = (long)item.ElementAt(2);
                charted.chartData.Add(temp);
            }

            charted.chartData = charted.chartData.OrderBy(c => c.hour).ToList();

            ChartDataWrapper sorted = new ChartDataWrapper();

            sorted.chartData              = new PostSortData();
            sorted.chartData.labels       = new List <string>();
            sorted.chartData.except_type  = new List <string>();
            sorted.chartData.except_count = new List <long>();

            foreach (DataPair item in charted.chartData)
            {
                sorted.chartData.labels.Add(item.label);
                sorted.chartData.except_type.Add(item.except_type);
                sorted.chartData.except_count.Add(item.except_count);
            }

            string chartified = JsonConvert.SerializeObject(sorted);

            return(chartified);
        }
        public String Get([FromQuery] string name)
        {
            string reply = "";

            /*
             * The possible options for name are:
             *      All
             *      Regression Cornerstone Group
             *      Communicator Test
             *      QA CIS BATCH TEST
             *      Other Partners
             */
            // If the option selected is "All", then use data from all partners
            if (name.CompareTo("\"All\"") == 0)
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" | summarize avg(duration) | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" | summarize avg(duration) by bin(timestamp,1h) | extend  a = \"b\") on a" +
                                          "| project timestamp, avg_duration1");
            }
            // If the option selected is "Other Partners", only use data from the groups not specified by name
            else if (name.CompareTo("\"Other Partners\"") == 0)
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\"" +
                                          " and customDimensions.GroupName != \"All\" and customDimensions.GroupName != \"Regression Cornerstone Group\"" +
                                          " and customDimensions.GroupName != \"Communicator Test\" and customDimensions.GroupName != \"QA CIS BATCH TEST\"" +
                                          "| summarize avg(duration) | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\"" +
                                          " and customDimensions.GroupName != \"All\" and customDimensions.GroupName != \"Regression Cornerstone Group\"" +
                                          " and customDimensions.GroupName != \"Communicator Test\" and customDimensions.GroupName != \"QA CIS BATCH TEST\"" +
                                          "| summarize avg(duration) by bin(timestamp,1h) | extend  a = \"b\") on a" +
                                          "| project timestamp, avg_duration1");
            }
            // If the option selected is not "All" nor "Other Partners", make a query for the specified partner group
            else
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" and customDimensions.GroupName == " + name + " " +
                                          "| summarize avg(duration) | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" and customDimensions.GroupName == " + name + " " +
                                          "| summarize avg(duration) by bin(timestamp,1h) | extend  a = \"b\") on a" +
                                          "| project timestamp, avg_duration1");
            }

            ReplyData parsed = JsonConvert.DeserializeObject <ReplyData>(reply);

            ChartData charted = new ChartData();

            charted.chartData = new List <DataPair>();

            foreach (List <object> item in parsed.tables.ElementAt(0).rows)
            {
                DataPair temp = new DataPair();
                temp.hour         = (DateTime)item.ElementAt(0);
                temp.label        = ((DateTime)item.ElementAt(0)).ToString();
                temp.avg_duration = (double)item.ElementAt(1);
                charted.chartData.Add(temp);
            }

            charted.chartData = charted.chartData.OrderBy(c => c.hour).ToList();

            ChartDataWrapper sorted = new ChartDataWrapper();

            sorted.chartData              = new PostSortData();
            sorted.chartData.labels       = new List <string>();
            sorted.chartData.avg_duration = new List <double>();

            foreach (DataPair item in charted.chartData)
            {
                sorted.chartData.labels.Add(item.label);
                sorted.chartData.avg_duration.Add(item.avg_duration);
            }

            string chartified = JsonConvert.SerializeObject(sorted);

            return(chartified);
        }
        public String Get([FromQuery] string name)
        {
            string reply = "";

            //string reply = GetOtherTelemetry("", "", "", "query",
            //    "query=requests | where name contains \"Patient\" | where customDimensions.Controller contains \"\" | where timestamp > ago(24h) " +
            //    "| summarize endpointcount = count() by bin(timestamp, 26h), tostring(customDimensions.Controller)");

            /*
             * The possible options for name are:
             *      All
             *      Regression Cornerstone Group
             *      Communicator Test
             *      QA CIS BATCH TEST
             *      Other Partners
             */
            // If the option selected is "All", then use data from all partners
            if (name.CompareTo("\"All\"") == 0)
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" " + "and customDimensions contains \"Controller\"" +
                                          "| summarize endpoint = count() | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" " + "and customDimensions contains \"Controller\"" +
                                          "| summarize endpoint = count() by bin(timestamp,2d), tostring(customDimensions.Controller) | extend  a = \"b\") on a" +
                                          "| project timestamp, customDimensions_Controller, endpoint1" +
                                          "| top 5 by endpoint1 desc");
            }
            // If the option selected is "Other Partners", only use data from the groups not specified by name
            else if (name.CompareTo("\"Other Partners\"") == 0)
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\"" + "and customDimensions contains \"Controller\"" +
                                          " and customDimensions.GroupName != \"All\" and customDimensions.GroupName != \"Regression Cornerstone Group\"" +
                                          " and customDimensions.GroupName != \"Communicator Test\" and customDimensions.GroupName != \"QA CIS BATCH TEST\"" +
                                          "| summarize endpoint = count() | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\"" + "and customDimensions contains \"Controller\"" +
                                          " and customDimensions.GroupName != \"All\" and customDimensions.GroupName != \"Regression Cornerstone Group\"" +
                                          " and customDimensions.GroupName != \"Communicator Test\" and customDimensions.GroupName != \"QA CIS BATCH TEST\"" +
                                          "| summarize endpoint = count() by bin(timestamp,2d), tostring(customDimensions.Controller) | extend  a = \"b\") on a" +
                                          "| project timestamp, customDimensions_Controller, endpoint1" +
                                          "| top 5 by endpoint1 desc");
            }
            // If the option selected is not "All" nor "Other Partners", make a query for the specified partner group
            else
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" and customDimensions.GroupName == " + name + " " +
                                          "and customDimensions contains \"Controller\"" +
                                          "| summarize endpoint = count() | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" and customDimensions.GroupName == " + name + " " +
                                          "and customDimensions contains \"Controller\"" +
                                          "| summarize endpoint = count() by bin(timestamp,2d), tostring(customDimensions.Controller) | extend  a = \"b\") on a" +
                                          "| project timestamp, customDimensions_Controller, endpoint1" +
                                          "| top 5 by endpoint1 desc");
            }

            ReplyData parsed = JsonConvert.DeserializeObject <ReplyData>(reply);

            ChartData charted = new ChartData();

            charted.chartData = new List <DataPair>();

            foreach (List <object> item in parsed.tables.ElementAt(0).rows)
            {
                DataPair temp = new DataPair();
                temp.label        = "";
                temp.hour         = (DateTime)item.ElementAt(0);
                temp.except_type  = (item.ElementAt(1).ToString());
                temp.except_count = (long)item.ElementAt(2);
                charted.chartData.Add(temp);
            }

            charted.chartData = charted.chartData.OrderBy(c => c.except_count).ToList();

            ChartDataWrapper sorted = new ChartDataWrapper();

            sorted.chartData              = new PostSortData();
            sorted.chartData.labels       = new List <string>();
            sorted.chartData.except_type  = new List <string>();
            sorted.chartData.except_count = new List <long>();

            foreach (DataPair item in charted.chartData)
            {
                sorted.chartData.labels.Add(item.label);
                sorted.chartData.except_type.Add(item.except_type);
                sorted.chartData.except_count.Add(item.except_count);
            }

            string chartified = JsonConvert.SerializeObject(sorted);

            return(chartified);
        }
        public String Get([FromQuery] string name)
        {
            // Retrieve percentage of requests by resultCode over the past 24 hours
            string reply = "";

            /*
             * The possible options for name are:
             *      All
             *      Regression Cornerstone Group
             *      Communicator Test
             *      QA CIS BATCH TEST
             *      Other Partners
             */
            // If the option selected is "All", then use data from all partners
            if (name.CompareTo("\"All\"") == 0)
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" | summarize count() | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" | summarize count() by resultCode | extend  a = \"b\") on a" +
                                          "| project resultCode, percentage = (todouble(count_1) * 100 / todouble(count_))");
            }
            // If the option selected is "Other Partners", only use data from the groups not specified by name
            else if (name.CompareTo("\"Other Partners\"") == 0)
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\"" +
                                          " and customDimensions.GroupName != \"All\" and customDimensions.GroupName != \"Regression Cornerstone Group\"" +
                                          " and customDimensions.GroupName != \"Communicator Test\" and customDimensions.GroupName != \"QA CIS BATCH TEST\"" +
                                          "| summarize count() | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\"" +
                                          " and customDimensions.GroupName != \"All\" and customDimensions.GroupName != \"Regression Cornerstone Group\"" +
                                          " and customDimensions.GroupName != \"Communicator Test\" and customDimensions.GroupName != \"QA CIS BATCH TEST\"" +
                                          "| summarize count() by resultCode | extend  a = \"b\") on a" +
                                          "| project resultCode, percentage = (todouble(count_1) * 100 / todouble(count_))");
            }
            // If the option selected is not "All" nor "Other Partners", make a query for the specified partner group
            else
            {
                reply = GetOtherTelemetry("", "", "", "query", "query=requests" +
                                          "| where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" and customDimensions.GroupName == " + name + " " +
                                          "| summarize count() | extend a = \"b\" | join" +
                                          "(requests  | where timestamp > ago(1d) " + "and customDimensions contains \"GroupName\" and customDimensions.GroupName == " + name + " " +
                                          "| summarize count() by resultCode | extend  a = \"b\") on a" +
                                          "| project resultCode, percentage = (todouble(count_1) * 100 / todouble(count_))");
            }

            ReplyData parsed = JsonConvert.DeserializeObject <ReplyData>(reply);

            ChartData charted = new ChartData();

            charted.chartData = new List <DataPair>();
            int lessThan2 = 0;

            foreach (List <object> item in parsed.tables.ElementAt(0).rows)
            {
                DataPair temp      = new DataPair();
                string   itemLabel = (item.ElementAt(0)).ToString();

                // Remove "(see exception telemetries)" from the labels
                if (itemLabel.Contains("("))
                {
                    itemLabel  = itemLabel.Substring(0, itemLabel.IndexOf("(") - 1);
                    itemLabel += "]";
                }

                temp.label = itemLabel;

                // If the percentage is an integer, convert the percentage to a double
                if (item.ElementAt(1) is Int64 @int)
                {
                    temp.percent_responses = @int * 1.0;
                }
                else
                {
                    temp.percent_responses = (double)item.ElementAt(1);
                }

                // Count the number of categories with a percentage lower than 1%
                if (temp.percent_responses < 1)
                {
                    lessThan2++;
                }

                charted.chartData.Add(temp);
            }

            charted.chartData = charted.chartData.OrderBy(c => c.label).ToList();

            ChartDataWrapper sorted = new ChartDataWrapper();

            sorted.chartData                   = new PostSortData();
            sorted.chartData.labels            = new List <string>();
            sorted.chartData.percent_responses = new List <double>();

            // If there is more than one category with a percentage that is lower than 1%,
            // combine all categories with percentage lower than 1% into one category
            if (lessThan2 > 1)
            {
                DataPair otherPair = new DataPair();
                otherPair.label             = "Other (";
                otherPair.percent_responses = 0;
                foreach (DataPair item in charted.chartData)
                {
                    double percent = item.percent_responses;

                    if (percent < 1)
                    {
                        if (otherPair.percent_responses > 0)
                        {
                            otherPair.label += ", ";
                        }
                        otherPair.label              = otherPair.label + item.label;
                        otherPair.percent_responses += percent;
                    }
                    else
                    {
                        sorted.chartData.labels.Add(item.label);
                        sorted.chartData.percent_responses.Add(Math.Round(percent, 2));
                    }
                }
                otherPair.label += ")";
                sorted.chartData.labels.Add(otherPair.label);
                sorted.chartData.percent_responses.Add(Math.Round(otherPair.percent_responses, 2));
            }
            // When there is one or less categories with a percentage lower than 1%, leave the list alone
            else
            {
                foreach (DataPair item in charted.chartData)
                {
                    sorted.chartData.labels.Add(item.label);
                    sorted.chartData.percent_responses.Add(Math.Round(item.percent_responses, 2));
                }
            }

            string chartified = JsonConvert.SerializeObject(sorted);

            return(chartified);
        }