-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added feign client to connect this api to perustars-ml-service api
- Loading branch information
1 parent
06345b2
commit 4541d99
Showing
11 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
PERUSTARS/PERUSTARS/DataAnalytics/Application/Jobs/DataAnalyticsJob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using PERUSTARS.DataAnalytics.Infrastructure.FeignClients; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace PERUSTARS.DataAnalytics.Application.Jobs | ||
{ | ||
public class DataAnalyticsJob : IHostedService, IDisposable | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private Timer _timer; | ||
private readonly PeruStarsMLServiceFeignClient _peruStarsMLServiceFeignClient; | ||
private readonly ILogger<DataAnalyticsJob> _logger; | ||
|
||
public DataAnalyticsJob(IServiceProvider serviceProvider, PeruStarsMLServiceFeignClient peruStarsMLServiceFeignClient) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_peruStarsMLServiceFeignClient = peruStarsMLServiceFeignClient; | ||
using var scope = _serviceProvider.CreateScope(); | ||
_logger = scope.ServiceProvider.GetRequiredService<ILogger<DataAnalyticsJob>>(); | ||
} | ||
|
||
private async void ComputeRecommendationData(object state) | ||
{ | ||
bool isSuccess = await _peruStarsMLServiceFeignClient.ComputeRecommendationSystem(); | ||
if (isSuccess) _logger.LogInformation("Remote call to perustars-ml-api was successfully executed"); | ||
else _logger.LogInformation("Remote call to perustars-ml-api failed"); | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Starting Data Analytics Scheduled Job"); | ||
_timer = new Timer(ComputeRecommendationData, null, TimeSpan.Zero, TimeSpan.FromHours(5)); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Stopping Data Analytics Scheduled Job"); | ||
_timer?.Change(Timeout.Infinite, 0); | ||
return Task.CompletedTask; | ||
} | ||
public void Dispose() => _timer.Dispose(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...TARS/PERUSTARS/DataAnalytics/Infrastructure/FeignClients/PeruStarsMLServiceFeignClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Microsoft.Extensions.Logging; | ||
using PERUSTARS.DataAnalytics.Infrastructure.FeignClients.Resources; | ||
using PERUSTARS.DataAnalytics.Infrastructure.FeignClients.Resources.Dto; | ||
using PERUSTARS.DataAnalytics.Infrastructure.FeignClients.Resources.Response; | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Net.Http.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace PERUSTARS.DataAnalytics.Infrastructure.FeignClients | ||
{ | ||
public class PeruStarsMLServiceFeignClient | ||
{ | ||
private readonly HttpClient _httpClient; | ||
private readonly ILogger<PeruStarsMLServiceFeignClient> _logger; | ||
|
||
public PeruStarsMLServiceFeignClient(HttpClient httpClient, ILogger<PeruStarsMLServiceFeignClient> logger) | ||
{ | ||
_httpClient = httpClient; | ||
_logger = logger; | ||
_httpClient.BaseAddress = new Uri("https://perustars-ml-service.onrender.com"); | ||
_httpClient.DefaultRequestHeaders.Accept.Clear(); | ||
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
} | ||
public async Task<bool> ComputeRecommendationSystem() | ||
{ | ||
HttpResponseMessage response = await _httpClient.GetAsync("/recommendation-system/compute-data"); | ||
if (!response.IsSuccessStatusCode) return false; | ||
string message = await response.Content.ReadAsStringAsync(); | ||
_logger.LogInformation("Result from ml-api: {}", message); | ||
return true; | ||
} | ||
public async Task<MLResponse> GetHobbyistRecommendedArtist(long hobbyistId) | ||
{ | ||
string path = $"/recommendation-system/hobbyists/{hobbyistId}/recommended-artists"; | ||
_logger.LogInformation($"Calling endpoint {path} from perustars-ml-service"); | ||
HttpResponseMessage response = await _httpClient.GetAsync(path); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
_logger.LogInformation("Something went wrong when calling perustars-ml-api"); | ||
return new MLResponse { Data = null, statusCode = HttpStatusCode.InternalServerError }; | ||
} | ||
MLDto data = await response.Content.ReadFromJsonAsync<MLDto>(); | ||
_logger.LogInformation("API call to {} done successfully", path); | ||
return new MLResponse { Data = data, statusCode = HttpStatusCode.OK }; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
PERUSTARS/PERUSTARS/DataAnalytics/Infrastructure/FeignClients/Resources/Dto/MLDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace PERUSTARS.DataAnalytics.Infrastructure.FeignClients.Resources.Dto | ||
{ | ||
public class MLDto | ||
{ | ||
public long? ArtistId { get; set; } | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...TARS/PERUSTARS/DataAnalytics/Infrastructure/FeignClients/Resources/Response/MLResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using PERUSTARS.DataAnalytics.Infrastructure.FeignClients.Resources.Dto; | ||
using System.Net; | ||
|
||
namespace PERUSTARS.DataAnalytics.Infrastructure.FeignClients.Resources.Response | ||
{ | ||
public class MLResponse | ||
{ | ||
public MLDto? Data { get; set; } | ||
public HttpStatusCode statusCode { get; set; } | ||
} | ||
} |
21 changes: 18 additions & 3 deletions
21
PERUSTARS/PERUSTARS/DataAnalytics/Interface/REST/DataAnalyticsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,31 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using PERUSTARS.DataAnalytics.Infrastructure.FeignClients; | ||
using PERUSTARS.DataAnalytics.Infrastructure.FeignClients.Resources.Response; | ||
using System.Threading.Tasks; | ||
using System.Net; | ||
|
||
namespace PERUSTARS.DataAnalytics.Interface.REST | ||
{ | ||
[ApiController] | ||
[Route("/data-analytics")] | ||
public class DataAnalyticsController : ControllerBase | ||
{ | ||
[HttpGet(Name = "/hobbyists/{hobbyistId}/favourites-artists")] | ||
public IActionResult GetFavouristArtistsFrom(long hobbyistId) | ||
private readonly PeruStarsMLServiceFeignClient _peruStarsMLServiceFeignClient; | ||
|
||
public DataAnalyticsController(PeruStarsMLServiceFeignClient peruStarsMLServiceFeignClient) | ||
{ | ||
_peruStarsMLServiceFeignClient = peruStarsMLServiceFeignClient; | ||
} | ||
|
||
return Ok(); | ||
[HttpGet(Name = "/hobbyists/{hobbyistId}/recommended-artists")] | ||
public async Task<IActionResult> GetFavouristArtistsFrom(long hobbyistId) | ||
{ | ||
MLResponse result = await _peruStarsMLServiceFeignClient.GetHobbyistRecommendedArtist(hobbyistId); | ||
if(result.statusCode == HttpStatusCode.InternalServerError) | ||
{ | ||
return StatusCode((int)HttpStatusCode.InternalServerError, "An error ocurred while trying to call perustars-ml-service"); | ||
} | ||
return Ok(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters