public GetOverviewResult()
 {
     IncidentsPerApplication = new GetOverviewApplicationResult[0];
     TimeAxisLabels          = new string[0];
     StatSummary             = new OverviewStatSummary {
     };
 }
Beispiel #2
0
        public void Should_allow_dates_within_the_given_interval()
        {
            var sut = new GetOverviewApplicationResult("hello", DateTime.Today.AddDays(-30), 31);

            sut.AddValue(DateTime.Today, 10);

            AssertionExtensions.Should((int)sut.Values[sut.Values.Length - 1]).Be(10);
        }
Beispiel #3
0
        public async Task <GetOverviewResult> ExecuteAsync(GetOverview query)
        {
            if (query.NumberOfDays == 0)
            {
                query.NumberOfDays = 30;
            }

            if (query.NumberOfDays == 1)
            {
                return(await GetTodaysOverviewAsync(query));
            }

            var apps      = new Dictionary <int, GetOverviewApplicationResult>();
            var labels    = new string[query.NumberOfDays + 1]; //+1 for today
            var startDate = DateTime.Today.AddDays(-query.NumberOfDays);

            for (var i = 0; i <= query.NumberOfDays; i++)
            {
                labels[i] = startDate.AddDays(i).ToShortDateString();
            }

            var result = new GetOverviewResult();

            using (var cmd = _unitOfWork.CreateDbCommand())
            {
                cmd.CommandText = @"select Applications.Id, Applications.Name, cte.Date, cte.Count
FROM 
(
	select Incidents.ApplicationId , cast(Incidents.CreatedAtUtc as date) as Date, count(Incidents.Id) as Count
	from Incidents
	where Incidents.CreatedAtUtc >= @minDate
	group by Incidents.ApplicationId, cast(Incidents.CreatedAtUtc as date)
) cte
right join applications on (applicationid=applications.id)
;";


                cmd.AddParameter("minDate", startDate);
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        var appId = reader.GetInt32(0);
                        GetOverviewApplicationResult app;
                        if (!apps.TryGetValue(appId, out app))
                        {
                            app = new GetOverviewApplicationResult(reader.GetString(1), startDate,
                                                                   query.NumberOfDays + 1); //+1 for today
                            apps[appId] = app;
                        }
                        //no stats at all for this app
                        if (reader[2] is DBNull)
                        {
                            var startDate2 = DateTime.Today.AddDays(-query.NumberOfDays + 1);
                            for (var i = 0; i < query.NumberOfDays; i++)
                            {
                                app.AddValue(startDate2.AddDays(i), 0);
                            }
                        }
                        else
                        {
                            app.AddValue(reader.GetDateTime(2), reader.GetInt32(3));
                        }
                    }

                    result.TimeAxisLabels          = labels;
                    result.IncidentsPerApplication = apps.Values.ToArray();
                }
            }

            await GetStatSummary(query, result);


            return(result);
        }
Beispiel #4
0
        private async Task <GetOverviewResult> GetTodaysOverviewAsync(GetOverview query)
        {
            var result = new GetOverviewResult
            {
                TimeAxisLabels = new string[24]
            };
            var startDate = StartDateForHours;
            var apps      = new Dictionary <int, GetOverviewApplicationResult>();

            for (var i = 0; i < 24; i++)
            {
                result.TimeAxisLabels[i] = startDate.AddHours(i).ToString("HH:mm");
            }

            using (var cmd = _unitOfWork.CreateDbCommand())
            {
                cmd.CommandText = @"select Applications.Id, Applications.Name, cte.Date, cte.Count
FROM 
(
	select Incidents.ApplicationId , DATEPART(HOUR, Incidents.CreatedAtUtc) as Date, count(Incidents.Id) as Count
	from Incidents
	where Incidents.CreatedAtUtc >= @minDate
	group by Incidents.ApplicationId, DATEPART(HOUR, Incidents.CreatedAtUtc)
) cte
right join applications on (applicationid=applications.id)";


                cmd.AddParameter("minDate", startDate);
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        var appId = reader.GetInt32(0);
                        GetOverviewApplicationResult app;
                        if (!apps.TryGetValue(appId, out app))
                        {
                            app         = new GetOverviewApplicationResult(reader.GetString(1), startDate, 1);
                            apps[appId] = app;
                        }

                        if (reader[2] is DBNull)
                        {
                            for (var i = 0; i < 24; i++)
                            {
                                app.AddValue(startDate.AddHours(i), 0);
                            }
                        }
                        else
                        {
                            var hour = reader.GetInt32(2);
                            app.AddValue(
                                hour < DateTime.Now.AddHours(1).Hour //since we want 22:00 if time is 21:30
                                    ? DateTime.Today.AddHours(hour)
                                    : DateTime.Today.AddDays(-1).AddHours(hour), reader.GetInt32(3));
                        }
                    }

                    result.IncidentsPerApplication = apps.Values.ToArray();
                }
            }

            await GetStatSummary(query, result);

            return(result);
        }
        public async Task <GetOverviewResult> HandleAsync(IMessageContext context, GetOverview query)
        {
            if (query.NumberOfDays == 0)
            {
                query.NumberOfDays = 30;
            }
            var labels = CreateTimeLabels(query);

            var isSysAdmin = context.Principal.IsSysAdmin();
            var gotApps    = context.Principal.FindAll(x => x.Type == CoderrClaims.Application).Any();

            if (!isSysAdmin && !gotApps)
            {
                return(new GetOverviewResult()
                {
                    StatSummary = new OverviewStatSummary(),
                    IncidentsPerApplication = new GetOverviewApplicationResult[0],
                    TimeAxisLabels = labels
                });
            }

            if (isSysAdmin)
            {
                var appIds = new List <int>();
                using (var cmd = _unitOfWork.CreateCommand())
                {
                    cmd.CommandText = "SELECT id FROM Applications";
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            appIds.Add(reader.GetInt32(0));
                        }
                    }
                }
                ApplicationIds = string.Join(",", appIds);
            }
            else
            {
                var appIds = context.Principal
                             .FindAll(x => x.Type == CoderrClaims.Application)
                             .Select(x => int.Parse(x.Value).ToString())
                             .ToList();
                ApplicationIds = string.Join(",", appIds);
            }



            if (query.NumberOfDays == 1)
            {
                return(await GetTodaysOverviewAsync(query));
            }

            var apps      = new Dictionary <int, GetOverviewApplicationResult>();
            var startDate = DateTime.Today.AddDays(-query.NumberOfDays);
            var result    = new GetOverviewResult();

            result.Days = query.NumberOfDays;
            using (var cmd = _unitOfWork.CreateDbCommand())
            {
                cmd.CommandText = $@"select Applications.Id, Applications.Name, cte.Date, cte.Count
FROM 
(
	select Incidents.ApplicationId , cast(Incidents.CreatedAtUtc as date) as Date, count(Incidents.Id) as Count
	from Incidents
	where Incidents.CreatedAtUtc >= @minDate 
    AND Incidents.CreatedAtUtc <= GetUtcDate()
	AND Incidents.ApplicationId in ({ApplicationIds})
	group by Incidents.ApplicationId, cast(Incidents.CreatedAtUtc as date)
) cte
right join applications on (applicationid=applications.id)

;";


                cmd.AddParameter("minDate", startDate);
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        var appId = reader.GetInt32(0);
                        GetOverviewApplicationResult app;
                        if (!apps.TryGetValue(appId, out app))
                        {
                            app = new GetOverviewApplicationResult(reader.GetString(1), startDate,
                                                                   query.NumberOfDays + 1); //+1 for today
                            apps[appId] = app;
                        }
                        //no stats at all for this app
                        if (reader[2] is DBNull)
                        {
                            var startDate2 = DateTime.Today.AddDays(-query.NumberOfDays + 1);
                            for (var i = 0; i < query.NumberOfDays; i++)
                            {
                                app.AddValue(startDate2.AddDays(i), 0);
                            }
                        }
                        else
                        {
                            app.AddValue(reader.GetDateTime(2), reader.GetInt32(3));
                        }
                    }

                    result.TimeAxisLabels          = labels;
                    result.IncidentsPerApplication = apps.Values.ToArray();
                }
            }

            await GetStatSummary(query, result);


            return(result);
        }
Beispiel #6
0
        public void ignore_future_dates_to_allow_malconfigured_clients()
        {
            var sut = new GetOverviewApplicationResult("Label", DateTime.Today.AddDays(-30), 30);

            sut.AddValue(DateTime.Today.AddDays(1), 10);
        }