Ejemplo n.º 1
0
        public IActionResult GetTileMetrics()
        {
            DashboardMetricsViewModel dashboardMetricsViewModel = new DashboardMetricsViewModel();

            string userId = Request.Headers[Constants.UserToken];
            User   user;

            _memoryCache.TryGetValue(userId, out user);
            if (user == null)
            {
                return(Unauthorized(Messages.UserNotFoundError));
            }

            if (_memoryCache.TryGetValue($"dashboard_{user.UserGuid}", out dashboardMetricsViewModel))
            {
                return(Ok(dashboardMetricsViewModel));
            }

            dashboardMetricsViewModel = new DashboardMetricsViewModel();

            List <Poll> polls = _dBContext.Poll.Where(x => x.CreatedBy == user.Userid && x.StatusId != 3).ToList();

            //Update total Polls
            dashboardMetricsViewModel.polls = polls.Count;

            List <int>       pollIds   = polls.Select(x => x.PollId).ToList();
            List <PollVotes> pollVotes = (from eachPoll in _dBContext.PollVotes
                                          where pollIds.Contains(eachPoll.PollId)
                                          select eachPoll).ToList();
            var pollVotesReceived = (from eachPoll in pollVotes
                                     group new { eachPoll.PollId } by new { eachPoll.CreatedDate, eachPoll.PollId } into eachGroup
                                     select eachGroup).ToList();

            //Update total Poll Votes
            dashboardMetricsViewModel.pollVotes = pollVotesReceived.Count;

            List <Survey> surveys = _dBContext.Survey.Where(x => x.CreatedBy == user.Userid && x.StatusId != 3).ToList();

            //Update total Surveys
            dashboardMetricsViewModel.surveys = surveys.Count;

            List <int>            surveyIds   = surveys.Select(x => x.Surveyid).ToList();
            List <SurveyFeedback> surveyUsers = (from eachSurvey in _dBContext.SurveyFeedback
                                                 where surveyIds.Contains(eachSurvey.SurveyId) && eachSurvey.CompletedDatetime != null
                                                 select eachSurvey).ToList();

            //Update total Surveys Feedbacks
            dashboardMetricsViewModel.surveyFeedbacks = surveyUsers.Count;
            _memoryCache.Set($"dashboard_{user.UserGuid}", dashboardMetricsViewModel);
            return(Ok(dashboardMetricsViewModel));
        }
Ejemplo n.º 2
0
        public IActionResult GetTileMetrics()
        {
            DashboardMetricsViewModel dashboardMetricsViewModel = new DashboardMetricsViewModel();

            string userguid      = Request.Headers[Constants.UserToken];
            string decyrptstring = Security.Decrypt(userguid);

            if (string.IsNullOrEmpty(decyrptstring))
            {
                return(BadRequest("Unauthorized User"));
            }
            User user = _dBContext.User.Where(x => x.UserGuid == decyrptstring).FirstOrDefault();

            if (user == null)
            {
                return(BadRequest(Messages.UserNotFoundError));
            }

            List <Poll> polls = _dBContext.Poll.Where(x => x.CreatedBy == user.Userid && x.StatusId != 3).ToList();

            //Update total Polls
            dashboardMetricsViewModel.polls = polls.Count;

            List <int>       pollIds   = polls.Select(x => x.PollId).ToList();
            List <PollVotes> pollVotes = (from eachPoll in _dBContext.PollVotes
                                          where pollIds.Contains(eachPoll.PollId)
                                          select eachPoll).ToList();
            var pollVotesReceived = (from eachPoll in pollVotes
                                     group new { eachPoll.PollId } by new { eachPoll.CreatedDate, eachPoll.PollId } into eachGroup
                                     select eachGroup).ToList();

            //Update total Poll Votes
            dashboardMetricsViewModel.pollVotes = pollVotesReceived.Count;

            List <Survey> surveys = _dBContext.Survey.Where(x => x.CreatedBy == user.Userid && x.StatusId != 3).ToList();

            //Update total Surveys
            dashboardMetricsViewModel.surveys = surveys.Count;

            List <int>        surveyIds   = surveys.Select(x => x.Surveyid).ToList();
            List <SurveyUser> surveyUsers = (from eachSurvey in _dBContext.SurveyUser
                                             where surveyIds.Contains(eachSurvey.SurveyId) && eachSurvey.CompletedDatetime != null
                                             select eachSurvey).ToList();

            //Update total Surveys Feedbacks
            dashboardMetricsViewModel.surveyFeedbacks = surveyUsers.Count;

            return(Ok(dashboardMetricsViewModel));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Run(HttpRequest req, CancellationToken cancellationToken)
        {
            _ = req;

            if (!_identityService.IsUserLoggedIn)
            {
                return(new UnauthorizedResult());
            }

            var allSecrets = await _managedSecrets.Get(cancellationToken);

            var allResources = await _resources.Get(cancellationToken);

            var allTasks = await _rekeyingTasks.Get(cancellationToken);

            var expiringInNextWeek = allSecrets.Where(s => DateTimeOffset.UtcNow.AddDays(7) < (s.LastChanged + s.ValidPeriod));
            var expired            = allSecrets.Where(s => !s.IsValid);

            var metrics = new DashboardMetricsViewModel()
            {
                SignedInName         = _identityService.UserName,
                SignedInEmail        = _identityService.UserEmail,
                SignedInRoles        = string.Join(", ", _identityService.UserRoles),
                TotalResources       = allResources.Count,
                TotalSecrets         = allSecrets.Count,
                TotalPendingApproval = allTasks.Where(t =>
                                                      t.ConfirmationType.HasFlag(TaskConfirmationStrategies.AdminCachesSignOff) ||
                                                      t.ConfirmationType.HasFlag(TaskConfirmationStrategies.AdminSignsOffJustInTime)).Count(),
                TotalExpiringSoon = expiringInNextWeek.Count(),
                TotalExpired      = expired.Count(),
                ExpiringSoon      = expiringInNextWeek.Select(s => _managedSecretViewModel(s)),
                PercentExpired    = (int)((double)expired.Count() / allSecrets.Count) * 100,
                TasksInError      = allTasks.Count(t => t.RekeyingFailed)
            };

            foreach (var secret in allSecrets)
            {
                var riskScore = 0;
                foreach (var resourceId in secret.ResourceIds)
                {
                    var resource = allResources.FirstOrDefault(r => r.ObjectId == resourceId);

                    var provider = _providerManager.GetProviderInstance(
                        resource.ProviderType,
                        resource.ProviderConfiguration);
                    riskScore += provider.GetRisks(secret.ValidPeriod).Sum(r => r.Score);
                }
                if (riskScore > 85)
                {
                    metrics.RiskOver85++;
                }
                else if (riskScore > 60)
                {
                    metrics.Risk85++;
                }
                else if (riskScore > 35)
                {
                    metrics.Risk60++;
                }
                else if (riskScore > 0)
                {
                    metrics.Risk35++;
                }
                else if (riskScore == 0)
                {
                    metrics.Risk0++;
                }
            }

            return(new OkObjectResult(metrics));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "dashboard")] HttpRequest req,
            ClaimsPrincipal claimsPrincipal,
            ILogger log)
        {
            if (!req.IsValidUser())
            {
                return(new UnauthorizedResult());
            }

            log.LogInformation("Requested Dashboard metrics");

            var allSecrets = await ManagedSecrets.ListAsync();

            var allResources = await Resources.ListAsync();

            var allTasks = await RekeyingTasks.ListAsync();

            var expiringInNextWeek = allSecrets.Where(s => DateTimeOffset.UtcNow.AddDays(7) < (s.LastChanged + s.ValidPeriod));
            var expired            = allSecrets.Where(s => !s.IsValid);

            var metrics = new DashboardMetricsViewModel()
            {
                SignedInName =
                    claimsPrincipal.FindFirst(ClaimTypes.GivenName)?.Value +
                    " " +
                    claimsPrincipal.FindFirst(ClaimTypes.Surname)?.Value,
                SignedInEmail        = claimsPrincipal.FindFirst(ClaimTypes.Email)?.Value,
                SignedInRole         = AuthJanitorRoleExtensions.GetUserRole(req),
                TotalResources       = allResources.Count,
                TotalSecrets         = allSecrets.Count,
                TotalPendingApproval = allTasks.Where(t =>
                                                      t.ConfirmationType.HasFlag(TaskConfirmationStrategies.AdminCachesSignOff) ||
                                                      t.ConfirmationType.HasFlag(TaskConfirmationStrategies.AdminSignsOffJustInTime)).Count(),
                TotalExpiringSoon = expiringInNextWeek.Count(),
                TotalExpired      = expired.Count(),
                ExpiringSoon      = expiringInNextWeek.Select(s => GetViewModel(s)),
                PercentExpired    = (int)((double)expired.Count() / allSecrets.Count) * 100,
                TasksInError      = allTasks.Count(t => t.RekeyingFailed)
            };

            foreach (var secret in allSecrets)
            {
                var riskScore = 0;
                foreach (var resourceId in secret.ResourceIds)
                {
                    var resource = allResources.FirstOrDefault(r => r.ObjectId == resourceId);
                    var provider = GetProvider(new RekeyingAttemptLogger(log), resource.ProviderType, resource.ProviderConfiguration);
                    riskScore += provider.GetRisks(secret.ValidPeriod).Sum(r => r.Score);
                }
                if (riskScore > 85)
                {
                    metrics.RiskOver85++;
                }
                else if (riskScore > 60)
                {
                    metrics.Risk85++;
                }
                else if (riskScore > 35)
                {
                    metrics.Risk60++;
                }
                else if (riskScore > 0)
                {
                    metrics.Risk35++;
                }
                else if (riskScore == 0)
                {
                    metrics.Risk0++;
                }
            }

            return(new OkObjectResult(metrics));
        }