public async Task <ActionResult> GetAllAsync(string offerName) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); _logger.LogInformation($"Get all custom meters in offer {offerName}."); return(Ok(await _customMeterService.GetAllAsync(offerName))); }
public async Task ReportBatchMeterEvents() { List <Offer> offers = await _offerService.GetAllAsync(); foreach (var offer in offers) { List <CustomMeter> customMeters = await _customMeterService.GetAllAsync(offer.OfferName); foreach (var meter in customMeters) { DateTime effectiveStartTime = await _subscriptionCustomMeterUsageService.GetEffectiveStartTimeByMeterIdAsync(meter.Id); if (effectiveStartTime == DateTime.MaxValue) { _logger.LogInformation($"The meter {meter.MeterName} is not used by any subscription."); continue; } // Try to query and report meter events from the earliest attempted/failed time while (true) { // Give 1 hours grace period for all telemetry data being stored if (effectiveStartTime > DateTime.UtcNow.AddHours(-2)) { _logger.LogInformation($"The events of meter {meter.MeterName} was lastly processed at {effectiveStartTime}. The current time is {DateTime.UtcNow}."); break; } await ReportBatchMeterEventsInternal(offer, meter, effectiveStartTime); effectiveStartTime = effectiveStartTime.AddHours(1); } } } }
/// <summary> /// Creates a subscription within a plan within an offer. /// </summary> /// <param name="subscription">The subscription to create.</param> /// <returns>The created subscription.</returns> public async Task <Subscription> CreateAsync(Subscription subscription) { if (subscription is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(Subscription).Name), UserErrorCode.PayloadNotProvided); } if (await ExistsAsync(subscription.SubscriptionId)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(Subscription).Name, subscription.SubscriptionId.ToString())); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(Subscription).Name, subscription.Name, offerName: subscription.OfferName, planName: subscription.PlanName, payload: JsonSerializer.Serialize(subscription))); var offerParameters = await _offerParameterService.GetAllAsync(subscription.OfferName); foreach (var param in offerParameters) { // Check if value of all offer parameters are provided with correct type if (subscription.InputParameters.Where(x => x.Name.Equals(param.ParameterName) && x.Type.Equals(param.ValueType)).Count() < 1) { throw new LunaBadRequestUserException($"Value of parameter {param.ParameterName} is not provided, or the type doesn't match.", UserErrorCode.ParameterNotProvided); } } // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(subscription.OfferName); // Get the plan associated with the planUniqueName provided var plan = await _planService.GetAsync(subscription.OfferName, subscription.PlanName); // Set the FK to offer subscription.OfferId = offer.Id; // Set the FK to plan subscription.PlanId = plan.Id; // Always set quantity to 1 to walkaround a marketplace service bug subscription.Quantity = 1; // Set the created time subscription.CreatedTime = DateTime.UtcNow; subscription.Status = nameof(FulfillmentState.PendingFulfillmentStart); subscription.ProvisioningStatus = nameof(ProvisioningState.ProvisioningPending); subscription.ProvisioningType = nameof(ProvisioningType.Subscribe); subscription.RetryCount = 0; List <CustomMeter> customMeterList = await _customMeterService.GetAllAsync(offer.OfferName); using (var transaction = await _context.BeginTransactionAsync()) { // Add subscription to db _context.Subscriptions.Add(subscription); await _context._SaveChangesAsync(); // Add subscription parameters foreach (var param in subscription.InputParameters) { param.SubscriptionId = subscription.SubscriptionId; _context.SubscriptionParameters.Add(param); } await _context._SaveChangesAsync(); foreach (var meter in customMeterList) { _context.SubscriptionCustomMeterUsages.Add(new SubscriptionCustomMeterUsage(meter.Id, subscription.SubscriptionId)); } await _context._SaveChangesAsync(); transaction.Commit(); } _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(Subscription).Name, subscription.Name, offerName: subscription.OfferName, planName: subscription.PlanName)); return(subscription); }
public async Task <ActionResult> GetAllAsync() { _logger.LogInformation($"Get all custom meters"); return(Ok(await _customMeterService.GetAllAsync())); }