Example #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "teamHealth/{teamId}")] HttpRequest req,
            [DurableClient] IDurableOrchestrationClient starter,
            string teamId,
            ILogger log)
        {
            try
            {
                var connection = await _scheduleConnectorService.GetConnectionAsync(teamId).ConfigureAwait(false);

                var date = DateTime.Today;
                if (req.Query.ContainsKey("date"))
                {
                    DateTime.TryParse(req.Query["date"], out date);
                }
                var weekStartDate = date.StartOfWeek(_options.StartDayOfWeek);

                var employees = await _wfmDataService.GetEmployeesAsync(connection.TeamId, connection.WfmBuId, weekStartDate).ConfigureAwait(false);
                await UpdateEmployeesWithTeamsData(connection.TeamId, employees).ConfigureAwait(false);

                var cachedShifts = await GetCachedShiftsAsync(connection, weekStartDate).ConfigureAwait(false);

                var jobIds = cachedShifts.SelectMany(s => s.Jobs).Select(j => j.WfmJobId).Distinct().ToList();
                var jobs   = await GetJobsAsync(connection, jobIds).ConfigureAwait(false);

                ExpandIds(cachedShifts, employees, jobs);

                var missingUsers = employees.Where(e => string.IsNullOrEmpty(e.TeamsEmployeeId)).ToList();

                var missingShifts = await GetMissingShiftsAsync(connection, weekStartDate, cachedShifts).ConfigureAwait(false);

                jobIds = missingShifts.SelectMany(s => s.Jobs).Select(j => j.WfmJobId).Distinct().ToList();
                jobs   = await GetJobsAsync(connection, jobIds).ConfigureAwait(false);

                ExpandIds(missingShifts, employees, jobs);

                var mappedUsers = await GetMappedUsersAsync(connection.TeamId).ConfigureAwait(false);

                var teamHealthResponseModel = new TeamHealthResponseModel
                {
                    TeamId        = connection.TeamId,
                    WeekStartDate = weekStartDate.AsDateString(),
                    EmployeeCacheOrchestratorStatus        = await starter.GetStatusAsync(string.Format(EmployeeCacheOrchestrator.InstanceIdPattern, connection.TeamId)).ConfigureAwait(false),
                    EmployeeTokenRefreshOrchestratorStatus = await starter.GetStatusAsync(string.Format(EmployeeTokenRefreshOrchestrator.InstanceIdPattern, connection.TeamId)).ConfigureAwait(false),
                    TeamOrchestratorStatus = await starter.GetStatusAsync(string.Format(ShiftsOrchestrator.InstanceIdPattern, connection.TeamId)).ConfigureAwait(false),
                    MappedUsers            = mappedUsers,
                    MissingUsers           = missingUsers,
                    MissingShifts          = missingShifts,
                    CachedShifts           = cachedShifts
                };

                if (_featureOptions.EnableOpenShiftSync)
                {
                    teamHealthResponseModel.OpenShiftsOrchestratorStatus = await starter.GetStatusAsync(string.Format(OpenShiftsOrchestrator.InstanceIdPattern, connection.TeamId)).ConfigureAwait(false);
                }

                if (_featureOptions.EnableTimeOffSync)
                {
                    teamHealthResponseModel.TimeOffOrchestratorStatus = await starter.GetStatusAsync(string.Format(TimeOffOrchestrator.InstanceIdPattern, connection.TeamId)).ConfigureAwait(false);
                }

                if (_featureOptions.EnableAvailabilitySync)
                {
                    teamHealthResponseModel.AvailabilityOrchestratorStatus = await starter.GetStatusAsync(string.Format(AvailabilityOrchestrator.InstanceIdPattern, connection.TeamId)).ConfigureAwait(false);
                }

                // N.B. the following block returns the JSON in a ContentResult rather than in the
                // rather more concise JsonResult because to return the Json with the settings
                // required adding a package dependency to Microsoft.AspNetCore.Mvc.NewtonsoftJson
                // as per https://github.com/Azure/azure-functions-core-tools/issues/1907 which then
                // caused an issue with incompatible dependencies and a significant issue with
                // deserializing json in HttpRequestExtensions
                var json = JsonConvert.SerializeObject(teamHealthResponseModel, Formatting.Indented);
                return(new ContentResult
                {
                    Content = json,
                    ContentType = "application/json",
                    StatusCode = (int)HttpStatusCode.OK
                });
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Team health failed!");
                return(new ContentResult
                {
                    Content = $"Unexpected exception: {ex.Message}",
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }
Example #2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Admin, "get", Route = "teamHealth/{teamId}")] HttpRequest req,
            [OrchestrationClient] DurableOrchestrationClient starter,
            string teamId,
            ILogger log)
        {
            try
            {
                var connection = await _scheduleConnectorService.GetConnectionAsync(teamId);

                var credentials = await _secretsService.GetCredentialsAsync(connection.TeamId);

                _scheduleSourceService.SetCredentials(connection.TeamId, credentials);

                var date = DateTime.Today;
                if (req.Query.ContainsKey("date"))
                {
                    DateTime.TryParse(req.Query["date"], out date);
                }
                var weekStartDate = date.StartOfWeek(_options.StartDayOfWeek);

                var jdaEmployees = await _scheduleSourceService.GetEmployeesAsync(connection.TeamId, connection.StoreId, weekStartDate);
                await UpdateEmployeesWithTeamsData(connection.TeamId, jdaEmployees);

                var cachedShifts = await GetCachedShiftsAsync(connection, weekStartDate);

                var jobIds = cachedShifts.SelectMany(s => s.Jobs).Select(j => j.JdaJobId).Distinct().ToList();
                var jobs   = await GetJobsAsync(connection, jobIds);

                ExpandIds(cachedShifts, jdaEmployees, jobs);

                var missingUsers = jdaEmployees.Where(e => string.IsNullOrEmpty(e.DestinationId)).ToList();

                var missingShifts = await GetMissingShifts(connection, weekStartDate, cachedShifts);

                jobIds = missingShifts.SelectMany(s => s.Jobs).Select(j => j.JdaJobId).Distinct().ToList();
                jobs   = await GetJobsAsync(connection, jobIds);

                ExpandIds(missingShifts, missingUsers, jobs);

                var teamHealthResponseModel = new TeamHealthResponseModel
                {
                    TeamId                 = connection.TeamId,
                    WeekStartDate          = weekStartDate,
                    TeamOrchestratorStatus = await starter.GetStatusAsync(connection.TeamId),
                    MissingUsers           = missingUsers,
                    MissingShifts          = missingShifts,
                    CachedShifts           = cachedShifts
                };

                return(new JsonResult(teamHealthResponseModel, new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                }));
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Team health failed!");
                return(new ContentResult
                {
                    Content = $"Unexpected exception: {ex.Message}",
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }