public IHttpActionResult GetRecommendations(Guid id) { // Allow find by ScenarioId (original) or RunId var run = _runRepository.Find(id); if (run == null) // Not a RunId passed, check if a ScenarioId { run = _runRepository.FindByScenarioId(id); } if (run == null) { return(NotFound()); } bool isScenarioId = (run.Id != id); var recommendations = _recommendationRepository.GetByScenarioId(isScenarioId ? id : run.Scenarios[0].Id); var items = _mapper.Map <List <RecommendationModel> >(recommendations); // For Smooth recommendations only then return "Unplaced", instead // of null, for unplaced breaks foreach (var item in items) { if (item.Processor == "smooth" && String.IsNullOrEmpty(item.ExternalBreakNo)) { item.ExternalBreakNo = Globals.UnplacedBreakString; } } return(Ok(items)); }
public IHttpActionResult GetExtendedRecommendation([FromUri] Guid scenarioId) { var data = _recommendationRepository .GetByScenarioId(scenarioId).ToArray(); if (data.Length == 0) { return(NotFound()); } return(Ok(_reportCreator.GenerateReportData(data))); }
public IHttpActionResult GetGroupWithScenarioRecommendations(Guid scenarioId, String group) { var campaignRefList = _mapper.Map <List <CampaignModel> >(_campaignRepository.GetByGroup(group)); //clone to get a deep level copy as the campaignRefList will only contain a shallow copy; //the Clone() methods for all the objects inside the campaign object have all been amended so they will be a copy and not a reference var campaignList = new List <CampaignModel>(); foreach (var campRefDoc in campaignRefList) { var campDoc = campRefDoc.Clone(); campaignList.Add((CampaignModel)campDoc); } var scenarioRecommendations = _recommendationRepository.GetByScenarioId(scenarioId); scenarioRecommendations = scenarioRecommendations.Where(r => r.ExternalCampaignNumber.Contains(group)); decimal recommendationsTotalRatingsForDayPart = 0; Dictionary <NodaTime.Duration, decimal> lengthRatings = new Dictionary <NodaTime.Duration, decimal>(); List <string> salesAreasList = new List <string>(); campaignList.ForEach(campaignDoc => { campaignDoc.SalesAreaCampaignTarget.ForEach(salesAreaCampaignTarget => { salesAreaCampaignTarget.CampaignTargets.ForEach(campaignTarget => { salesAreasList.Clear(); foreach (var item in salesAreaCampaignTarget.SalesAreaGroup.SalesAreas) { salesAreasList.Add(item); } campaignTarget.StrikeWeights.ForEach(strikeWeight => { strikeWeight.DayParts.ForEach(dayPart => { recommendationsTotalRatingsForDayPart = 0; //reset at beginning of each day part //get dictionary list of time lengths and ratings for this dayPart.. lengthRatings = CampaignProjectionProcessing.ProjectRatingsForCampaignDayPart(dayPart, scenarioRecommendations, strikeWeight.StartDate, strikeWeight.EndDate, salesAreasList); //if there are some ratingsPredictions for this day part section then add them to the appropriate parts of the camapaign document to be returned to api call if (lengthRatings.Count > 0) { //update different totals of this campaign document using the array of length counts passed back from each day part.... //individual lengths in salesAreaCampaignTarget totals and campaigndoc actual totals.. foreach (var multi in salesAreaCampaignTarget.Multiparts) { foreach (var len in multi.Lengths) { foreach (var item in lengthRatings) { if (len == item.Key) { multi.CurrentPercentageSplit += item.Value; campaignDoc.ActualRatings += item.Value; } } } } //individual lengths in the strikeweights totals.. int lenCnt = 0; foreach (var len in strikeWeight.Lengths) { foreach (var item in lengthRatings) { if (len.length == item.Key) { strikeWeight.Lengths[lenCnt].CurrentPercentageSplit = strikeWeight.Lengths[lenCnt].CurrentPercentageSplit + (int)item.Value; //keep a running total for where there are diffrent lengths in doc; not the case for Nine recommendationsTotalRatingsForDayPart += item.Value; } } lenCnt++; } //strikeweight totals, from running total above.. strikeWeight.CurrentPercentageSplit += recommendationsTotalRatingsForDayPart; //daypart totals, from running total above.. dayPart.CurrentPercentageSplit += recommendationsTotalRatingsForDayPart; } }); }); }); }); }); return(Ok(campaignList)); }
public async Task Execute(CancellationToken cancellationToken, RecommendationsReportGenerationJobParameters input) { var recommendations = _recommendationRepository .GetByScenarioId(input.ScenarioId) .ToArray(); if (!recommendations.Any()) { status = ReportExportStatus.GenerationFailed; _cache.Set(input.ReportReference, status, CreatePreCompletionCachePolicy()); var message = $"No recommendations are available for the specified scenario ID. ({input.ScenarioId})"; _exportStatusNotifier.NotifyGroup(new ReportExportStatusNotification { reportReference = input.ReportReference, status = status, message = message }); LogError(new ArgumentException(message), message); return; } Stream reportStream = null; try { status = ReportExportStatus.Generating; _cache.Set(input.ReportReference, status, CreatePreCompletionCachePolicy()); _exportStatusNotifier.NotifyGroup(new ReportExportStatusNotification { reportReference = input.ReportReference, status = status }); reportStream = _recommendationsResultReportCreator.GenerateReport(recommendations); } catch (Exception exception) { status = ReportExportStatus.GenerationFailed; _cache.Set(input.ReportReference, status, CreatePreCompletionCachePolicy()); _exportStatusNotifier.NotifyGroup(new ReportExportStatusNotification { reportReference = input.ReportReference, status = status }); LogError(exception, $"Something went wrong while generating a recommendation report for scenario with ID '{input.ScenarioId}'."); reportStream.Dispose(); return; } using (reportStream) { try { status = ReportExportStatus.Uploading; _cache.Set(input.ReportReference, status, CreatePreCompletionCachePolicy()); _exportStatusNotifier.NotifyGroup(new ReportExportStatusNotification { reportReference = input.ReportReference, status = status }); var engine = _storageClientFactory.GetEngine(); engine.Upload(new S3UploadComment() { BucketName = _awsSettings.S3Bucket, DestinationFilePath = input.FilePath, FileStream = reportStream }); } catch (Exception exception) { status = ReportExportStatus.UploadFailed; _cache.Set(input.ReportReference, status, CreatePreCompletionCachePolicy()); _exportStatusNotifier.NotifyGroup(new ReportExportStatusNotification { reportReference = input.ReportReference, status = status }); LogError(exception, $"Something went wrong while uploading the recommendation report for scenario with ID '{input.ScenarioId}'."); return; } } status = ReportExportStatus.Available; _cache.Set(input.ReportReference, status, new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(5) }); _exportStatusNotifier.NotifyGroup(new ReportExportStatusNotification { reportReference = input.ReportReference, status = status }); }