Esempio n. 1
0
            public PerMessageAggregation(
                MessageHistoryTableEntity historyEntity,
                IReadOnlyDictionary <string, int> countByState)
            {
                this.MessageId           = historyEntity.MessageId;
                this.MessageCategory     = historyEntity.MessageCategory;
                this.MessageBody         = historyEntity.MessageBody;
                this.SendTimeUnixSeconds = new DateTimeOffset(historyEntity.SendTime).ToUnixTimeSeconds();
                this.TotalTargets        = historyEntity.Targets * BillingHelper.GetTotalSegments(historyEntity.MessageBody);

                if (countByState.TryGetValue(MessageState.DELIVERED.ToString(), out int delivered))
                {
                    this.TotalSucceeded = delivered;
                }
                else
                {
                    this.TotalSucceeded = 0;
                }
            }
Esempio n. 2
0
        public async Task <ServiceProviderResponse> SendMessageAsync(
            [FromHeader(Constant.OperationTrackingIdHeader)] string requestId,
            [FromHeader] string account,
            [FromBody] MessageSendRequest request)
        {
            Account currentAccount = null;

            try
            {
                SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Received, $"Request is received");

                currentAccount = await this.ValidateAccount(account);

                Validator.ArgumentNotNull(request, nameof(request));
                Validator.ArgumentNotNull(request.MessageBody, nameof(request.MessageBody));
                Validator.ArgumentNotNullOrEmpty(request.MessageBody.TemplateName, request.MessageBody.TemplateName);

                request.Targets = this.ValidatePhoneNumbers(request.Targets);

                var pack = await this.BuildInputMessageAsync(currentAccount, request, requestId);

                var message = pack.InputMessage;
                SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Message is ready. messageId={message.MessageInfo.MessageId}");

                var signatureQuotaName = string.Format(Constant.SmsSignatureMAUNamingTemplate, pack.Signature.Value);
                SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Empty, $"If quota is enabled, request {request.Targets.Count} for account {account}. Otherwise, ignore");
                await QuotaCheckClient.AcquireQuotaAsync(account, SmsConstant.SmsQuotaName, request.Targets.Count);

                await QuotaCheckClient.AcquireQuotaAsync(account, signatureQuotaName, request.Targets.Count);

                try
                {
                    await this.reportManager.OnMessageSentAsync(account, message, pack.Extension);

                    SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Report is updated. messageId={message.MessageInfo.MessageId}");

                    await this.DispatchMessageAsync(message, pack.Extension, requestId);

                    SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Message is dispatched. messageId={message.MessageInfo.MessageId}");

                    this.metricManager.LogSendSuccess(1, account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory);
                    this.metricManager.LogSendTotal(BillingHelper.GetTotalSegments(message.MessageInfo.MessageBody), account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory);
                    var response = new MessageSendResponse
                    {
                        MessageId = message.MessageInfo.MessageId,
                        SendTime  = message.MessageInfo.SendTime
                    };

                    return(ServiceProviderResponse.CreateJsonResponse(HttpStatusCode.OK, response));
                }
                catch
                {
                    SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Empty, $"If quota is enabled, release {request.Targets.Count} for account {account}. Otherwise, ignore");
                    await QuotaCheckClient.ReleaseQuotaAsync(account, SmsConstant.SmsQuotaName, request.Targets.Count);

                    await QuotaCheckClient.ReleaseQuotaAsync(account, signatureQuotaName, request.Targets.Count);

                    this.metricManager.LogSendFailed(1, account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory);
                    throw;
                }
            }
            catch
            {
                this.metricManager.LogSendFailed(1, account, currentAccount?.SubscriptionId ?? string.Empty, null);
                throw;
            }
        }