private string GenerateDutyRequestContent(PersonnelDto personnel
                                           , Duty duty, RequestDuration duration, DateTime from, DateTime to)
 {
     return("request type : "
            + " " + (duration == RequestDuration.Daily ? "daily" : "hourly")
            + "duty" + " " + duty.Title
            + ", requested by: " + (personnel.Gender == Gender.Male ? "Mr." : "Mrs.")
            + "" + personnel.FullName
            + $", from date: {from.ToShortDateString()} to date:" +
            $" {to.ToShortDateString()}.");
 }
Ejemplo n.º 2
0
 private void PersonnelDutyDetailsError(RequestDuration dutyDuration, string operation)
 {
     try
     {
         throw new LogicalException();
     }
     catch (LogicalException ex)
     {
         _logger.LogRunTimeError(ex, "The PersonnelDuty dto that has passed to " +
                                 "the service should also have '{0}' personnel duty properties." +
                                 " {1} operation failed.", dutyDuration, operation);
         throw;
     }
 }
        public async Task <string> HandleDutyRequest(int personnelDutyId
                                                     , RequestDuration dutyDuration, DateTime from, DateTime to, string senderUsername)
        {
            var personnel = _personnelService.GetByCode(senderUsername);

            var personnelApprovalProc = _personnelApprovalProcRepository
                                        .Get(q => q.Id == personnel.Id, includeProperties: "DutyApprovalProc")
                                        .SingleOrDefault();

            string notificationReceiverId = null;

            if (personnelApprovalProc != null)
            {
                var receiverInfo = _approvalProcService
                                   .GetNextReceiverId(personnelApprovalProc.DutyApprovalProc);

                if (receiverInfo.ReceiverId != null)
                {
                    var personnelDuty = _personnelDutyRepository
                                        .Get(q => q.Id == personnelDutyId, includeProperties: "Duty")
                                        .SingleOrDefault();

                    notificationReceiverId = await _messageService.Create(new CreateMessageDto
                    {
                        ReceiverId  = receiverInfo.ReceiverId,
                        MessageType = MessageType.Request,
                        Title       = GenerateDutyRequestTitle(personnelDuty.Duty),
                        Content     = GenerateDutyRequestContent(personnel, personnelDuty.Duty
                                                                 , dutyDuration, from, to),
                        Request = new Request
                        {
                            RequestId            = personnelDutyId,
                            RequestType          = RequestType.Duty,
                            ParentApprovalProcId = receiverInfo.ParentApprovalProcId
                        }
                    }, senderUsername);
                }
            }

            return(notificationReceiverId);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Execute a single validation test
        /// </summary>
        /// <param name="client">http client</param>
        /// <param name="server">server URL</param>
        /// <param name="request">Request</param>
        /// <returns>PerfLog</returns>
        public async Task <PerfLog> ExecuteRequest(HttpClient client, string server, Request request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            PerfLog          perfLog;
            ValidationResult valid;

            // send the request
            using (HttpRequestMessage req = new HttpRequestMessage(new HttpMethod(request.Verb), request.Path))
            {
                DateTime dt = DateTime.UtcNow;

                // add the headers to the http request
                if (request.Headers != null && request.Headers.Count > 0)
                {
                    foreach (string key in request.Headers.Keys)
                    {
                        req.Headers.Add(key, request.Headers[key]);
                    }
                }

                // create correlation vector and add to headers
                CorrelationVector cv = new CorrelationVector(CorrelationVectorVersion.V2);
                req.Headers.Add(CorrelationVector.HeaderName, cv.Value);

                // add the body to the http request
                if (!string.IsNullOrWhiteSpace(request.Body))
                {
                    if (!string.IsNullOrWhiteSpace(request.ContentMediaType))
                    {
                        req.Content = new StringContent(request.Body, Encoding.UTF8, request.ContentMediaType);
                    }
                    else
                    {
                        req.Content = new StringContent(request.Body);
                    }
                }

                try
                {
                    // process the response
                    using HttpResponseMessage resp = await client.SendAsync(req).ConfigureAwait(false);

                    string body = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                    double duration = DateTime.UtcNow.Subtract(dt).TotalMilliseconds;

                    // validate the response
                    valid = ResponseValidator.Validate(request, resp, body);

                    // check the performance
                    perfLog = CreatePerfLog(server, request, valid, duration, (long)resp.Content.Headers.ContentLength, (int)resp.StatusCode);

                    // add correlation vector to perf log
                    perfLog.CorrelationVector     = cv.Value;
                    perfLog.CorrelationVectorBase = cv.GetBase();
                }
                catch (Exception ex)
                {
                    double duration = Math.Round(DateTime.UtcNow.Subtract(dt).TotalMilliseconds, 0);
                    valid = new ValidationResult {
                        Failed = true
                    };
                    valid.ValidationErrors.Add($"Exception: {ex.Message}");
                    perfLog = CreatePerfLog(server, request, valid, duration, 0, 500);
                }
            }

            // log the test
            LogToConsole(request, valid, perfLog);

            if (config.Prometheus)
            {
                // map category and mode to app values
                string mode   = GetMode(perfLog);
                string status = GetPrometheusCode(perfLog.StatusCode);

                RequestDuration.WithLabels(status, mode, perfLog.Server, perfLog.Failed.ToString(), config.Zone, config.Region).Observe(perfLog.Duration);
                RequestSummary.WithLabels(status, mode, perfLog.Server, perfLog.Failed.ToString(), config.Zone, config.Region).Observe(perfLog.Duration);
            }

            return(perfLog);
        }