Skip to content

Commit

Permalink
moved logic from recomendation.py to recomadationService.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed Jun 22, 2024
1 parent 0ec8703 commit a49b576
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 74 deletions.
66 changes: 49 additions & 17 deletions Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using ReasnAPI.Mappers;
using ReasnAPI.Models.Recomendation;
using Npgsql;

namespace ReasnAPI.Services
{
Expand All @@ -17,12 +18,13 @@ public class RecomendationService
private readonly HttpClient httpClient;
private readonly ReasnContext context;
private readonly string flaskApiUrl;

Check warning on line 20 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

The field 'RecomendationService.flaskApiUrl' is never used

Check warning on line 20 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

The field 'RecomendationService.flaskApiUrl' is never used
private readonly string _connectionString;

public RecomendationService(HttpClient httpClient, ReasnContext context, IConfiguration configuration)

Check warning on line 23 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Non-nullable field 'flaskApiUrl' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 23 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Non-nullable field '_connectionString' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 23 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable field 'flaskApiUrl' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 23 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable field '_connectionString' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
this.httpClient = httpClient;
this.context = context;
this.flaskApiUrl = $"{configuration.GetValue<string>("FlaskApi:BaseUrl")}/similar-tags";
_connectionString = configuration.GetConnectionString("DefaultValue");

Check warning on line 27 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 27 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Possible null reference assignment.
}

public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> interestsDto)
Expand All @@ -32,28 +34,20 @@ public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> inte

try
{
var response = await httpClient.PostAsJsonAsync(flaskApiUrl, interests);
var similarTags = await GetSimilarTagsFromDb(interests);

if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException(
$"Error fetching tags from Flask API. Status code: {response.StatusCode}");
}

var tagInfoList = await response.Content.ReadFromJsonAsync<List<TagInfo>>();

if (tagInfoList == null || tagInfoList.Count == 0)
if (similarTags == null || similarTags.Count == 0)
{
return new List<EventDto>();
}

var tagNames = tagInfoList.Select(t => t.Tag_Name).ToList();
var interestNames = tagInfoList.Select(t => t.Interest_Name).ToList();
var values = tagInfoList.Select(t => t.Value).ToList();
var tagNames = similarTags.Select(t => t.Tag_Name).ToList();
var interestNames = similarTags.Select(t => t.Interest_Name).ToList();
var values = similarTags.Select(t => t.Value).ToList();

for (int i = 0; i < values.Count; i++)
{
values[i] *= interestsLevels[interestNames.IndexOf(tagInfoList[i].Interest_Name)];
values[i] *= interestsLevels[interestNames.IndexOf(similarTags[i].Interest_Name)];
}

var events = await context.Events
Expand All @@ -67,8 +61,8 @@ public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> inte
.Sum(t => values[tagNames.IndexOf(t.Name)])
})
.OrderByDescending(e => e.TotalTagValue)
.Select(e => e.Event)
.ToListAsync(); ;
.Select(e => e.Event)
.ToListAsync();

var eventDtos = events.Select(e => e.ToDto()).ToList();

Expand All @@ -80,5 +74,43 @@ public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> inte
throw;
}
}

public async Task<List<TagInfo>> GetSimilarTagsFromDb(List<string> interests)
{
var similarTags = new List<TagInfo>();

await using var conn = new NpgsqlConnection(_connectionString);
await conn.OpenAsync();

await using var cmd = new NpgsqlCommand();
cmd.Connection = conn;


var query = @"
SELECT DISTINCT tag_name, interest_name, value
FROM common.related
WHERE interest_name = any(@interests) AND value > 0.3
";

cmd.CommandText = query;
cmd.Parameters.AddWithValue("interests", interests.ToArray());

await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var tag_name = reader.GetString(reader.GetOrdinal("tag_name"));
var interest_name = reader.GetString(reader.GetOrdinal("interest_name"));
var value = reader.GetDecimal(reader.GetOrdinal("value"));

similarTags.Add(new TagInfo
{
Tag_Name = tag_name,
Interest_Name = interest_name,
Value = (double)value
});
}

return similarTags;
}
}
}
57 changes: 0 additions & 57 deletions recomendation.py

This file was deleted.

0 comments on commit a49b576

Please sign in to comment.