// POST api/<controller> public async Task <ActionResult <AssetEstimatedPayloadPerCycleSettingsResponse> > Save(AssetSettingsRequest request) { request = await base.ReadRequestContentAsync(request); AssetSettingsRequestBase serviceRequest = new AssetSettingsRequestBase(); if (request != null) { serviceRequest = new AssetSettingsRequestBase { StartDate = DateTime.Parse(DateTime.UtcNow.ToShortDateString()), TargetValues = new Dictionary <AssetTargetType, double?> { { AssetTargetType.PayloadPerCycleInTonnes, request.TargetValue } }, CustomerUid = base.GetCustomerContext(Request), UserUid = base.GetUserContext(Request), AssetUIds = request.AssetUIds }; } this._loggingService.Debug("Updating Asset Settings Estimated Payload Per Cycle for the given request : " + JsonConvert.SerializeObject(request), MethodInfo.GetCurrentMethod().Name); this._loggingService.Debug("Request : " + JsonConvert.SerializeObject(request), MethodInfo.GetCurrentMethod().Name); var result = await this._assetSettingsService.Save(serviceRequest); this._loggingService.Debug("Updating Asset Settings Estimated Payload per cycle response : " + JsonConvert.SerializeObject(result), MethodInfo.GetCurrentMethod().Name); return(base.SendResponse(HttpStatusCode.OK, new AssetEstimatedPayloadPerCycleSettingsResponse(result.AssetSettingsLists, result.Errors.OfType <AssetErrorInfo>().ToList <AssetErrorInfo>()))); }
protected virtual void CheckForInvalidRecords(AssetSettingsRequestBase request, List <IErrorInfo> errorInfos) { var invalidRecords = errorInfos.Where(x => x.IsInvalid); if (errorInfos.Where(x => x.IsInvalid).Any()) { this._loggingService.Info("Ignoring request since following records are invalid : " + JsonConvert.SerializeObject(invalidRecords), MethodInfo.GetCurrentMethod().Name); throw new DomainException { Errors = errorInfos }; } if (request.AssetUIds == null || !request.AssetUIds.Any()) { throw new DomainException { Errors = errorInfos.Any() ? errorInfos : new List <IErrorInfo> { new ErrorInfo { ErrorCode = (int)ErrorCodes.AssetUIDListNull, Message = UtilHelpers.GetEnumDescription(ErrorCodes.AssetUIDListNull) } } }; } }
public virtual async Task <IList <AssetSettingsResponse> > Fetch(AssetSettingsRequestBase request, IList <IErrorInfo> errorInfos) { List <AssetSettingsResponse> convertedResponse = new List <AssetSettingsResponse>(); try { //Fetch from DB var results = await this.FetchAssetConfig(request, "<="); if (results.Any()) { convertedResponse.AddRange(this._mapper.Map <IEnumerable <AssetSettingsResponse> >(results)); } } catch (DomainException domainException) { this._loggingService.Error("An Error occurred during validation", MethodInfo.GetCurrentMethod().Name, domainException); throw domainException; } catch (Exception ex) { this._loggingService.Error("An Exception has occurred", MethodInfo.GetCurrentMethod().Name, ex); throw ex; } return(convertedResponse); }
public virtual async Task <IList <AssetSettingsResponse> > Save(AssetSettingsRequestBase request, IList <IErrorInfo> errorInfos) { List <AssetSettingsResponse> convertedResponse = new List <AssetSettingsResponse>(); try { //Fetch from DB var assetsSettingsResponse = await this.FetchAssetConfig(request, "<="); var assetsSettingsList = assetsSettingsResponse.ToList(); var configTypeIds = this._assetConfigTypeRepository.FetchByConfigTypeNames( new AssetConfigTypeDto { ConfigTypeNames = request.TargetValues.Select(x => x.Key.ToString()) }).Result; var configTypeDictionary = configTypeIds.ToDictionary(x => x.AssetTargetType, x => x.AssetConfigTypeID); this._loggingService.Debug("Started parallel execution for insertion / updation", MethodInfo.GetCurrentMethod().Name); var updatedAssetSettingsList = this.BuildAssetConfig(assetsSettingsList, request, configTypeDictionary); this.PersistAndPublish(updatedAssetSettingsList, request); this._loggingService.Debug("Ended parallel execution for insertion / updation", MethodInfo.GetCurrentMethod().Name); //Fetch from DB for the updated values var results = await this.FetchAssetConfig(request, "="); if (results.Any()) { convertedResponse.AddRange(this._mapper.Map <List <AssetSettingsResponse> >(results).ToArray()); } } catch (DomainException domainException) { this._loggingService.Error("An Error occurred during validation", MethodInfo.GetCurrentMethod().Name, domainException); throw domainException; } catch (Exception ex) { this._loggingService.Error("An Exception has occurred", MethodInfo.GetCurrentMethod().Name, ex); throw ex; } return(convertedResponse); }
private bool PersistAndPublish(IEnumerable <AssetSettingsDto> assetSettings, AssetSettingsRequestBase request) { bool result = true; List <Action> actions = new List <Action>(); if (assetSettings.Any()) { actions.Add(() => _transactions.Upsert(assetSettings)); actions.Add(() => this._assetSettingsPublisher.PublishAssetSettings(assetSettings)); actions.Add(() => this._assetSettingsPublisher.PublishUserAssetSettings(request)); //history record result = _transactions.Execute(actions); } return(result); }
public void BuildRepositoryStub(int count, AssetSettingsRequestBase request, List <AssetTargetType> assetTargetTypes) { AssetConfigDtoLists = new List <AssetSettingsDto>(); for (int i = 0; i < count; i++) { var guid = Guid.NewGuid(); this.AssetUIDs.Add(guid.ToString()); AssetConfigDtoLists.Add(new AssetSettingsDto { StartDate = DateTime.Now, EndDate = DateTime.Now, InsertUTC = DateTime.UtcNow, UpdateUTC = DateTime.UtcNow, TargetValue = i, TargetType = assetTargetTypes[i % assetTargetTypes.Count].ToString() }); } }
// POST api/<controller> public async Task <ActionResult <AssetMileageSettingsResponse> > Fetch(List <String> assetUIDs = null) { assetUIDs = await base.ReadRequestContentAsync(assetUIDs); var request = new AssetSettingsRequestBase { StartDate = DateTime.Parse(DateTime.UtcNow.ToShortDateString()), TargetValues = new Dictionary <AssetTargetType, double?> { { AssetTargetType.OdometerinKmsPerWeek, 0 } }, CustomerUid = base.GetCustomerContext(Request), UserUid = base.GetUserContext(Request), AssetUIds = assetUIDs }; this._loggingService.Info("Fetching Asset Settings Mileage for the given request : " + JsonConvert.SerializeObject(request), MethodInfo.GetCurrentMethod().Name); var result = await this._assetSettingsService.Fetch(request); this._loggingService.Debug("Fetching Asset Settings Mileage response : " + JsonConvert.SerializeObject(result), MethodInfo.GetCurrentMethod().Name); return(SendResponse(HttpStatusCode.OK, new AssetMileageSettingsResponse(result.AssetSettingsLists, result.Errors.OfType <AssetErrorInfo>().ToList <AssetErrorInfo>()))); }
public void PublishUserAssetSettings(AssetSettingsRequestBase request) { try { var payloadMessages = new List <KafkaMessage>(); _loggingService.Debug("Started Publishing User Asset Settings", "AssetSettingsPublisher.PublishUserAssetSettings"); foreach (var assetUId in request.AssetUIds) { foreach (var targetValue in request.TargetValues) { var userAssetTargetEvent = new UserAssetTargetEvent { AssetUID = Guid.Parse(assetUId), TargetType = targetValue.Key, StartDate = Convert.ToDateTime(request.StartDate), Timestamp = new TimestampDetail { EventUtc = DateTime.UtcNow }, UserUID = request.UserUid, CustomerUID = request.CustomerUid, TargetValue = targetValue.Value }; var payloadMessage = new KafkaMessage { Key = userAssetTargetEvent.AssetUID.ToString(), Message = userAssetTargetEvent, Topic = _userAssetSettingsTopic }; payloadMessages.Add(payloadMessage); // check we need to publish with hypens or not _loggingService.Debug(string.Format("USer Asset Settings Published to Kafka :- {0} ", JsonConvert.SerializeObject(payloadMessage)), "AssetSettingsPublisher.PublishUserAssetSettings"); } } _transactions.Publish(payloadMessages); _loggingService.Debug("Ended Publishing User Asset Settings", "AssetSettingsPublisher.PublishUserAssetSettings"); } catch (Exception ex) { this._loggingService.Error("An Exception has occurred ", MethodInfo.GetCurrentMethod().Name, ex); } }
protected virtual async Task <IEnumerable <AssetSettingsDto> > FetchAssetConfig(AssetSettingsRequestBase request, string startDateComparer) { this._loggingService.Debug("Started fetching AssetConfig details from database", MethodInfo.GetCurrentMethod().Name); var assetsSettingsResponse = await this._assetSettingsRepository.FetchAssetConfig(request.AssetUIds, new AssetSettingsDto { StartDate = request.StartDate, TargetValues = this.AssignAssetTargetValues(request.TargetValues, new AssetSettingsDto()), FilterCriteria = new List <KeyValuePair <string, Tuple <string, object> > > { new KeyValuePair <string, Tuple <string, object> >(startDateComparer, new Tuple <string, object>("AC.StartDate", request.StartDate.ToDateTimeStringWithYearMonthDayFormat())), new KeyValuePair <string, Tuple <string, object> >("is", new Tuple <string, object>("AC.EndDate", null)) } }); this._loggingService.Debug("Ended fetching AssetConfig details from database", MethodInfo.GetCurrentMethod().Name); return(assetsSettingsResponse); }
private IEnumerable <AssetSettingsDto> BuildAssetConfig(List <AssetSettingsDto> assetSettingsList, AssetSettingsRequestBase request, Dictionary <AssetTargetType, int> configTypeDictionary) { DateTime currentDateTime = DateTime.UtcNow; List <AssetSettingsDto> resultAssetSettings = new List <AssetSettingsDto>(); //Build Asset UIDs to be created var assetUidsToBeCreated = request.AssetUIds.Except(assetSettingsList.Distinct().Select(x => x.AssetUID.ToString())).ToList(); this._loggingService.Info("Following AssetUIDs are not having opened AssetConfig records : " + string.Join(",", assetUidsToBeCreated), MethodInfo.GetCurrentMethod().Name); if (assetUidsToBeCreated.Count > 0) { foreach (var requestTargetType in request.TargetValues) { resultAssetSettings.AddRange(assetUidsToBeCreated.Select(x => new AssetSettingsDto { AssetConfigUID = Guid.NewGuid(), AssetUID = Guid.Parse(x), StartDate = request.StartDate, TargetType = requestTargetType.Key.ToString(), TargetValue = requestTargetType.Value ?? 0, AssetConfigTypeID = configTypeDictionary[requestTargetType.Key], InsertUTC = currentDateTime, UpdateUTC = currentDateTime, StatusInd = true })); } } foreach (var assetConfig in assetSettingsList) { AssetTargetType targetType = (AssetTargetType)Enum.Parse(typeof(AssetTargetType), assetConfig.TargetType); assetConfig.AssetConfigTypeID = configTypeDictionary[targetType]; assetConfig.UpdateUTC = currentDateTime; //Unsetting the current config if (!request.TargetValues[targetType].HasValue) { _loggingService.Info("Assigning EndDate as Target values are null for the Asset : " + assetConfig.AssetUID, "AssetSettingsServiceBase.BuildAssetConfig"); if (Convert.ToDateTime(assetConfig.StartDate) == request.StartDate) { assetConfig.EndDate = request.StartDate; } else { assetConfig.EndDate = request.StartDate.AddDays(-1); } _loggingService.Info("Assigned EndDate : " + assetConfig.EndDate + " for the Asset : " + assetConfig.AssetUID, "AssetSettingsServiceBase.BuildAssetConfig"); } //Check if StartDate < request.StartDate ie., Old Records else if (Convert.ToDateTime(assetConfig.StartDate) < request.StartDate) { _loggingService.Info("Assigning EndDate as user is updating on next day for the Asset : " + assetConfig.AssetUID, "AssetSettingsServiceBase.BuildAssetConfig"); assetConfig.EndDate = request.StartDate.AddDays(-1); this._loggingService.Info("Assigned EndDate : " + assetConfig.EndDate + " for the Asset : " + assetConfig.AssetUID, "AssetSettingsServiceBase.BuildAssetConfig"); resultAssetSettings.Add(new AssetSettingsDto { AssetConfigUID = Guid.NewGuid(), AssetUID = assetConfig.AssetUID, TargetType = assetConfig.TargetType, TargetValue = Convert.ToDouble(request.TargetValues[targetType]), InsertUTC = currentDateTime, StartDate = request.StartDate, AssetConfigTypeID = configTypeDictionary[targetType], UpdateUTC = currentDateTime, StatusInd = true }); this._loggingService.Info("Created new AssetConfig with StartDate : " + assetConfig.StartDate + " for the Asset : " + assetConfig.AssetUID, "AssetSettingsServiceBase.BuildAssetConfig"); } else { //Assign Target value to the given target value ie., for updation assetConfig.TargetValue = Convert.ToDouble(request.TargetValues[targetType]); this._loggingService.Info("Assigned TargetValue : " + assetConfig.TargetValue + " for the Asset : " + assetConfig.AssetUID, "AssetSettingsServiceBase.BuildAssetConfig"); } } resultAssetSettings.AddRange(assetSettingsList); return(resultAssetSettings); }