Skip to content

Commit

Permalink
updated algorithm loggin in recomendation
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed Jun 22, 2024
1 parent 553f2d6 commit 0ec8703
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
1 change: 0 additions & 1 deletion Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public async Task<IActionResult> GetRecomendetEvents(string username)
var currentUser = _userService.GetUserByUsername(username);
var interests = currentUser.Interests;

// Poprawka: Dodanie await i poprawienie b³êdu w nazwie metody
var events = await _recomendationService.GetEventsByInterest(interests);

Check warning on line 44 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Possible null reference argument for parameter 'interestsDto' in 'Task<List<EventDto>> RecomendationService.GetEventsByInterest(List<UserInterestDto> interestsDto)'.

Check warning on line 44 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Possible null reference argument for parameter 'interestsDto' in 'Task<List<EventDto>> RecomendationService.GetEventsByInterest(List<UserInterestDto> interestsDto)'.

return Ok(events);
Expand Down
9 changes: 9 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Models/Recomendation/TagInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ReasnAPI.Models.Recomendation
{
public class TagInfo
{
public string Tag_Name { get; set; }

Check warning on line 5 in Server/ReasnAPI/ReasnAPI/Models/Recomendation/TagInfo.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

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

Check warning on line 5 in Server/ReasnAPI/ReasnAPI/Models/Recomendation/TagInfo.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable property 'Tag_Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Interest_Name { get; set; }

Check warning on line 6 in Server/ReasnAPI/ReasnAPI/Models/Recomendation/TagInfo.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

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

Check warning on line 6 in Server/ReasnAPI/ReasnAPI/Models/Recomendation/TagInfo.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable property 'Interest_Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public double Value { get; set; }
}
}
32 changes: 27 additions & 5 deletions Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using ReasnAPI.Mappers;
using ReasnAPI.Models.Recomendation;

namespace ReasnAPI.Services
{
Expand All @@ -27,6 +28,7 @@ public RecomendationService(HttpClient httpClient, ReasnContext context, IConfig
public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> interestsDto)
{
var interests = interestsDto.Select(i => i.Interest.Name).ToList();
var interestsLevels = interestsDto.Select(i => i.Level).ToList();

try
{
Expand All @@ -38,19 +40,39 @@ public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> inte
$"Error fetching tags from Flask API. Status code: {response.StatusCode}");
}

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

if (tagNames == null || tagNames.Count == 0)
if (tagInfoList == null || tagInfoList.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();

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

var events = await context.Events
.Include(e => e.Tags).Include(e => e.Parameters)
.Include(e => e.Tags)
.Include(e => e.Parameters)
.Where(e => e.Tags.Any(t => tagNames.Contains(t.Name)))
.ToListAsync();
.Select(e => new
{
Event = e,
TotalTagValue = e.Tags.Where(t => tagNames.Contains(t.Name))
.Sum(t => values[tagNames.IndexOf(t.Name)])
})
.OrderByDescending(e => e.TotalTagValue)
.Select(e => e.Event)
.ToListAsync(); ;

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

return events.ToDtoList();
return eventDtos;
}
catch (Exception ex)
{
Expand Down
19 changes: 9 additions & 10 deletions recomendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ def connect_to_db():
def get_similar_tags_from_db(conn, interests):
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
query = """
SELECT DISTINCT tag_name
SELECT DISTINCT tag_name, interest_name, value
FROM common.related
WHERE interest_name = ANY(%s) AND value > 0.3
WHERE interest_name IN %s AND value > 0.3
"""
cur.execute(query, (interests,))
cur.execute(query, (tuple(interests),))
results = cur.fetchall()

if not results:
return None
# Printowanie wyników
for row in results:
print(f"tag_name: {row['tag_name']}, interest_name: {row['interest_name']}, value: {row['value']}")

return [row['tag_name'] for row in results]
return [{'tag_name': row['tag_name'], 'interest_name': row['interest_name'], 'value': row['value']} for row in results]


@app.route('/similar-tags', methods=['POST'])
def get_similar_tags():
Expand All @@ -47,12 +49,9 @@ def get_similar_tags():
try:
similar_tags = get_similar_tags_from_db(conn, interests)

if similar_tags is None:
return jsonify([])

return jsonify(similar_tags)
finally:
conn.close()

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
app.run(host='0.0.0.0', port=5000, debug=True)

0 comments on commit 0ec8703

Please sign in to comment.