protected void Page_Load(object sender, EventArgs e)
        {
            JiraIssueRepository repository = new JiraIssueRepository();
            List <JiraIssue>    ticketsCreatedLast30Days = repository.GetIssuesCreatedDuring(DateTime.Now.AddDays(-30), DateTime.Now);
            List <JiraIssue>    ticketsClosedLast30Days  = repository.GetIssuesCreatedDuring(DateTime.Now.AddDays(-30), DateTime.Now);
            List <JiraIssue>    allOpenTickets           = repository.GetAllUnresolved();
            List <JiraIssue>    allClosedTickets         = repository.GetAllResolved();

            Response.Clear();
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType     = "application/json; charset=utf-8";
            Response.Write("{\n\"Stats\": {");

            Response.Write("\"ServiceRequests\": {\n");
            Response.Write("\"Total\": " + (int)((int)allOpenTickets.Count + (int)allClosedTickets.Count) + ",\n");
            Response.Write("\"Open\": " + allOpenTickets.Count + ",\n");
            Response.Write("\"Closed\": " + allClosedTickets.Count + ",\n");
            Response.Write("\"Recent\": {");
            {
                Response.Write("\"Today\": {\n");
                Response.Write("\"Created\": " + ticketsCreatedLast30Days.Count(i => i.DateCreated >= DateTime.Today && i.DateCreated < DateTime.Today.AddDays(1)) + ",\n");
                Response.Write("\"Closed\": " + ticketsClosedLast30Days.Count(i => i.DateResolved >= DateTime.Today && i.DateResolved < DateTime.Today.AddDays(1)) + "\n");
                Response.Write("},\n");

                Response.Write("\"Yesterday\": {\n");
                Response.Write("\"Created\": " + ticketsCreatedLast30Days.Count(i => i.DateCreated >= DateTime.Today.AddDays(-1) && i.DateCreated < DateTime.Today) + ",\n");
                Response.Write("\"Closed\": " + ticketsClosedLast30Days.Count(i => i.DateResolved >= DateTime.Today.AddDays(-1) && i.DateResolved < DateTime.Today) + "\n");
                Response.Write("},\n");

                Response.Write("\"Last7Days\": {\n");
                Response.Write("\"Created\": " + ticketsCreatedLast30Days.Count(i => i.DateCreated >= DateTime.Today.AddDays(-7) && i.DateCreated < DateTime.Today.AddDays(1)) + ",\n");
                Response.Write("\"Closed\": " + ticketsClosedLast30Days.Count(i => i.DateResolved >= DateTime.Today.AddDays(-7) && i.DateResolved < DateTime.Today.AddDays(1)) + "\n");
                Response.Write("},\n");

                Response.Write("\"Last30Days\": {\n");
                Response.Write("\"Created\": " + ticketsCreatedLast30Days.Count() + ",\n");
                Response.Write("\"Closed\": " + ticketsClosedLast30Days.Count() + "\n");
                Response.Write("}\n");
            }
            Response.Write("}\n");

            Response.Write("}\n");

            Response.Write("}\n");
            Response.Write("}\n");
            Response.End();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            JiraIssueRepository repository       = new JiraIssueRepository();
            List <string>       allFacilities    = repository.GetAllFacilities();
            List <JiraIssue>    allOpenTickets   = repository.GetAllUnresolved();
            List <JiraIssue>    allTicketsLast30 = repository.GetIssuesCreatedDuring(DateTime.Now.AddDays(-30), DateTime.Now);

            List <TicketsByFacilityRecord> ticketsByFacility = new List <TicketsByFacilityRecord>();

            // Set up dictionaries
            foreach (string facility in allFacilities)
            {
                ticketsByFacility.Add(new TicketsByFacilityRecord()
                {
                    Facility             = facility,
                    AllTicketsLast30Days = allTicketsLast30.Where(i => i.Facility == facility).ToList(),
                    OpenTickets          = allOpenTickets.Where(i => i.Facility == facility).ToList()
                });
            }
            // Add any that have no facility set
            ticketsByFacility.Add(new TicketsByFacilityRecord()
            {
                Facility             = "No facility set",
                AllTicketsLast30Days = allTicketsLast30.Where(i => string.IsNullOrEmpty(i.Facility)).ToList(),
                OpenTickets          = allOpenTickets.Where(i => string.IsNullOrEmpty(i.Facility)).ToList()
            });


            // Sort by facility
            Response.Clear();
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType     = "application/json; charset=utf-8";

            Response.Write("{\n \"TotalUnresolved\" : " + allOpenTickets.Count + ",\n");
            Response.Write("\"Facilities\": [\n");

            int facilityCounter = 0;

            foreach (TicketsByFacilityRecord facilityWithTickets in ticketsByFacility)
            {
                double TicketsPerDayLast7         = JiraStatistics.TicketsPerDay(facilityWithTickets.AllTicketsLast30Days.Where(i => i.DateCreated <= DateTime.Now && i.DateCreated >= DateTime.Today.AddDays(-7)).ToList(), DateTime.Now, DateTime.Now.AddDays(-7));
                double TicketsPerDayPreviousLast7 = JiraStatistics.TicketsPerDay(facilityWithTickets.AllTicketsLast30Days.Where(i => i.DateCreated <= DateTime.Now.AddDays(-7) && i.DateCreated >= DateTime.Today.AddDays(-14)).ToList(), DateTime.Now.AddDays(-7), DateTime.Now.AddDays(-14));
                string trend = string.Empty;
                if (TicketsPerDayPreviousLast7 > TicketsPerDayLast7)
                {
                    trend = "Down";
                }
                else if (TicketsPerDayPreviousLast7 == TicketsPerDayLast7)
                {
                    trend = "Same";
                }
                else
                {
                    trend = "Up";
                }

                Response.Write("{ " +
                               "\"Facility\" : \"" + facilityWithTickets.Facility + "\", " +
                               "\"Unresolved\" : " + facilityWithTickets.OpenTickets.Count + ", " +
                               "\"CreatedLast7Days\" : " + facilityWithTickets.AllTicketsLast30Days.Count(i => i.DateCreated <= DateTime.Now && i.DateCreated >= DateTime.Today.AddDays(-7)) + ", " +
                               "\"CreatedPreviousLast7Days\" : " + facilityWithTickets.AllTicketsLast30Days.Count(i => i.DateCreated <= DateTime.Now.AddDays(-7) && i.DateCreated >= DateTime.Today.AddDays(-14)) + ", " +
                               "\"TicketsPerDayLast7Days\" : " + TicketsPerDayLast7 + ", " +
                               "\"TicketsPerDayPreviousLast7Days\" : " + TicketsPerDayPreviousLast7 + ", " +
                               "\"Trend\" : \"" + trend + "\"" +
                               "}");


                facilityCounter++;
                if (facilityCounter < ticketsByFacility.Count)
                {
                    Response.Write(",");
                }

                Response.Write("\n");
            }

            Response.Write("]\n");
            Response.Write("}\n");

            Response.End();
        }