public GetRecsModule(IConfig config, IMyAnimeListApiFactory malApiFactory, IAnimeRecsClientFactory recClientFactory, IAnimeRecsDbConnectionFactory dbConnectionFactory, IViewFactory viewFactory, IViewLocator viewLocator, RazorViewEngine viewEngine, IRenderContextFactory renderContextFactory) { _config = config; _malApiFactory = malApiFactory; _recClientFactory = recClientFactory; _dbConnectionFactory = dbConnectionFactory; _viewLocator = viewLocator; _viewEngine = viewEngine; _renderContextFactory = renderContextFactory; Post["/GetRecs"] = GetRecs; }
public async Task <IActionResult> GetRecs([FromBody] AnimeRecsInputJson input, [FromServices] IOptionsSnapshot <Config.RecommendationsConfig> recConfig, [FromServices] IMyAnimeListApiFactory malApiFactory, [FromServices] IAnimeRecsClientFactory recClientFactory, [FromServices] IAnimeRecsDbConnectionFactory dbConnFactory, [FromServices] IRazorViewEngine viewEngine, [FromServices] ITempDataProvider tempProvider) { if (!ModelState.IsValid) { AjaxError error = new AjaxError(ModelState); _logger.LogDebug("Invalid input received for GetRecs: {0}", error.Message); return(BadRequest(error)); } if (input.RecSourceName == null) { input.RecSourceName = recConfig.Value.DefaultRecSource; } try { MalUserLookupResults userLookup = await GetUserLookupAsync(input, malApiFactory).ConfigureAwait(false); Dictionary <int, MalListEntry> animeList = new Dictionary <int, MalListEntry>(); foreach (MyAnimeListEntry listEntry in userLookup.AnimeList) { animeList[listEntry.AnimeInfo.AnimeId] = new AnimeRecs.RecEngine.MAL.MalListEntry((byte?)listEntry.Score, listEntry.Status, (short)listEntry.NumEpisodesWatched); } Dictionary <int, MalListEntry> animeWithheld = WithholdAnime(input, animeList); MalRecResults <IEnumerable <IRecommendation> > recResults = await GetRecommendationsAsync(input, recConfig.Value, animeList, animeWithheld, recClientFactory).ConfigureAwait(false); GetRecsViewModel viewModel = new GetRecsViewModel( results: recResults, userId: userLookup.UserId, userName: userLookup.CanonicalUserName, userLookup: userLookup, userAnimeList: animeList, maximumRecommendationsToReturn: recConfig.Value.MaximumRecommendationsToReturn, maximumRecommendersToReturn: recConfig.Value.MaximumRecommendersToReturn, animeWithheld: animeWithheld, dbConnectionFactory: dbConnFactory ); RecResultsAsHtmlJson resultsJson = await GetResultHtmlAsync(viewModel, input, viewEngine, tempProvider).ConfigureAwait(false); return(Ok(resultsJson)); } catch (ShortCircuitException ex) { return(ex.Result); } }
private async Task <MalRecResults <IEnumerable <IRecommendation> > > GetRecommendationsAsync(AnimeRecsInputJson input, Config.RecommendationsConfig recConfig, Dictionary <int, MalListEntry> animeList, Dictionary <int, MalListEntry> animeWithheld, IAnimeRecsClientFactory recClientFactory) { int numRecsToTryToGet = recConfig.MaximumRecommendationsToReturn; if (animeWithheld.Count > 0) { // Get rating prediction information about all anime if in debug mode and withholding anime. // For all currently implemented algorithms, this does not cause a performance problem. numRecsToTryToGet = 100000; } using (AnimeRecsClient recClient = recClientFactory.GetClient(input.RecSourceName)) { MalRecResults <IEnumerable <IRecommendation> > recResults; try { if (input.GoodPercentile != null) { decimal targetFraction = input.GoodPercentile.Value / 100; _logger.LogInformation("Querying rec source {0} for {1} recommendations for {2} using target of top {3}%.", input.RecSourceName, numRecsToTryToGet, input.MalName, targetFraction); recResults = await recClient.GetMalRecommendationsWithFractionTargetAsync(animeList, input.RecSourceName, numRecsToTryToGet, targetFraction, TimeSpan.FromMilliseconds(recConfig.TimeoutMilliseconds), CancellationToken.None).ConfigureAwait(false); } else if (input.GoodCutoff != null) { _logger.LogInformation("Querying rec source {0} for {1} recommendations for {2} using target of {3}.", input.RecSourceName, numRecsToTryToGet, input.MalName, input.GoodCutoff.Value); recResults = await recClient.GetMalRecommendationsAsync(animeList, input.RecSourceName, numRecsToTryToGet, input.GoodCutoff.Value, TimeSpan.FromMilliseconds(recConfig.TimeoutMilliseconds), CancellationToken.None).ConfigureAwait(false); } else { decimal targetFraction = recConfig.DefaultTargetPercentile / 100; _logger.LogInformation("Querying rec source {0} for {1} recommendations for {2} using default target of top {3}%.", input.RecSourceName, numRecsToTryToGet, input.MalName, targetFraction); recResults = await recClient.GetMalRecommendationsWithFractionTargetAsync(animeList, input.RecSourceName, numRecsToTryToGet, targetFraction, TimeSpan.FromMilliseconds(recConfig.TimeoutMilliseconds), CancellationToken.None) .ConfigureAwait(false); } } catch (AnimeRecs.RecService.DTO.RecServiceErrorException ex) { if (ex.Error.ErrorCode == AnimeRecs.RecService.DTO.ErrorCodes.Maintenance) { _logger.LogInformation("Could not service recommendation request for {0}. The rec service is currently undergoing maintenance.", input.MalName); AjaxError error = new AjaxError(AjaxError.InternalError, "The site is currently undergoing scheduled maintenance. Check back in a few minutes."); JsonResult result = Json(error); result.StatusCode = 500; throw new ShortCircuitException(result); } else { throw; } } _logger.LogInformation("Got results from rec service for {0}.", input.MalName); return(recResults); } }
public async Task <IActionResult> Index([FromQuery] string algorithm, [FromQuery] bool?detailedResults, [FromQuery] bool?debugMode, [FromServices] IOptionsSnapshot <Config.RecommendationsConfig> recConfig, [FromServices] IAnimeRecsClientFactory recClientFactory) { if (!ModelState.IsValid) { string errorString = ModelBindingHelpers.ConstructErrorString(ModelState); _logger.LogDebug("Invalid input received for home page: {0}", errorString); return(View("Error", new ErrorViewModel(exception: null))); } algorithm = algorithm ?? recConfig.Value.DefaultRecSource; bool displayDetailedResults = detailedResults ?? false; bool debugModeOn = debugMode ?? false; string recSourceType = null; using (AnimeRecsClient client = recClientFactory.GetClient(algorithm)) { try { recSourceType = await client.GetRecSourceTypeAsync(algorithm, TimeSpan.FromMilliseconds(recConfig.Value.TimeoutMilliseconds), CancellationToken.None); } catch { ; } } bool algorithmAvailable = recSourceType != null; bool targetScoreNeeded = false; if (AnimeRecs.RecService.DTO.RecSourceTypes.AnimeRecs.Equals(recSourceType, StringComparison.OrdinalIgnoreCase) && displayDetailedResults) { targetScoreNeeded = true; } HomeViewModel viewModel = new HomeViewModel( algorithm: algorithm, algorithmAvailable: algorithmAvailable, targetScoreNeeded: targetScoreNeeded, displayDetailedResults: displayDetailedResults, debugModeOn: debugModeOn ); return(View(viewModel)); }