Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/522 sort out inactive projects from search result #544

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f2cfb8b
Architecture
BartvanEijkelenburg Dec 3, 2021
197488c
Merge branch 'develop' into feature/522-Sort-out-inactive-projects-fr…
BartvanEijkelenburg Dec 4, 2021
9e60b3c
feat: algorithm example
NealGeilen Dec 6, 2021
d64c2b1
Added DataPoints
BartvanEijkelenburg Dec 6, 2021
38ee09b
fix: update architecture
NealGeilen Dec 6, 2021
8f701e9
added datapoints
BartvanEijkelenburg Dec 7, 2021
9e51697
Comments
BartvanEijkelenburg Dec 7, 2021
50bfc81
feat: added first algorithm test
NealGeilen Dec 9, 2021
b5dcdc3
Revert "feat: added first algorithm test"
NealGeilen Dec 9, 2021
22267f7
feat: Added first algorithm test
NealGeilen Dec 9, 2021
f62bd2b
Improved Algorythm
BartvanEijkelenburg Dec 9, 2021
15ad06a
Merge branch 'feature/522-Sort-out-inactive-projects-from-search-resu…
BartvanEijkelenburg Dec 9, 2021
8ffc1bc
Algorythm updates
BartvanEijkelenburg Dec 9, 2021
618f928
Updated algorithm
BartvanEijkelenburg Dec 13, 2021
0d684c9
update testing for algorithm
NealGeilen Dec 13, 2021
6fb197c
feat: changes to controller authentication & authorization
NealGeilen Dec 13, 2021
6361baf
feat: added AlgorithmWorker.cs to Job scheduler
NealGeilen Dec 13, 2021
9be666d
feat: added summaries to functions
NealGeilen Dec 13, 2021
e0a852e
fix: fix for average like date
NealGeilen Dec 13, 2021
886948d
Added endpoint to update the ActivityMultiplier
BartvanEijkelenburg Dec 13, 2021
a0a5d70
Improved Tests and algorithm.
BartvanEijkelenburg Dec 14, 2021
8966a9a
Update CHANGELOG.md
BartvanEijkelenburg Dec 14, 2021
d7755b4
Added comments
BartvanEijkelenburg Dec 15, 2021
7fc1c90
Fixed warnings....
BartvanEijkelenburg Dec 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions API/Controllers/AlgorithmController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using API.Extensions;
using API.InputOutput.ActivityAlgorithm;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.Defaults;
using Repositories;
using Services.Services;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace API.Controllers
{
/// <summary>
/// This class is responsible for handling HTTP requests that are related
/// to DeX algorithms, for example activating the project ActivityAlgorythm
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class AlgorithmController : ControllerBase
{

private readonly IActivityAlgorithmService activityAlgorithmService;
private readonly IProjectRepository projectRepository;
private readonly IUserService userService;

/// <summary>
/// Initializes a new instance of the <see cref="AlgorithmController" /> class
/// </summary>
/// <param name="activityAlgorithmService">The category service which is used to communicate with the logic layer.</param>
/// <param name="projectRepository">The project category service which is used to communicate with the logic layer.</param>
/// <param name="userService">The userService to get the current user.</param>
public AlgorithmController(IActivityAlgorithmService activityAlgorithmService, IProjectRepository projectRepository, IUserService userService)
{
this.activityAlgorithmService = activityAlgorithmService;
this.projectRepository = projectRepository;
this.userService = userService;
}

/// <summary>
/// This endpoint initiates the Activity Algorithm.
/// </summary>
/// <returns>HttpStatusCode</returns>
[HttpPost("Activity")]
[Authorize]
public async Task<IActionResult> ActivityAlgorithm()
{
bool isAllowed = HttpContext.User.HasClaim("client_role", Defaults.Roles.BackendApplication);

if(isAllowed == false)
{
User currentUser = await HttpContext.GetContextUser(userService)
.ConfigureAwait(false);
if(currentUser.Role.Name == Defaults.Roles.Administrator)
{
isAllowed = true;
}
}
if(isAllowed)
{
return Ok(activityAlgorithmService.CalculateAllProjects(
await projectRepository.GetAllWithUsersCollaboratorsAndInstitutionsAsync())
.OrderByDescending(p => p.ActivityScore).ToList());
}
return Forbid();

}

/// <summary>
/// This endpoint is used to update the activity multiplier
/// </summary>
/// <param name="activityAlgorithmInput">The input model of the activity algorithm multipliers</param>
[HttpPut("UpdateActivityMultiplier")]
[Authorize]
public async Task<IActionResult> UpdateActivityMutliplier([FromBody] ActivityAlgorithmInput activityAlgorithmInput)
{
bool isAllowed = HttpContext.User.HasClaim("client_role", Defaults.Roles.BackendApplication);

if(isAllowed == false)
{
User currentUser = await HttpContext.GetContextUser(userService)
.ConfigureAwait(false);
if(currentUser.Role.Name == Defaults.Roles.Administrator)
{
isAllowed = true;
}
}
if(isAllowed && ModelState.IsValid)
{
ProjectActivityConfig projectActivityConfig = new ProjectActivityConfig()
{
Id = 1,
AverageLikeDateMultiplier = activityAlgorithmInput.AverageLikeDateMultiplier,
ConnectedCollaboratorsMultiplier = activityAlgorithmInput.ConnectedCollaboratorsMultiplier,
RecentCreatedDataMultiplier = activityAlgorithmInput.RecentCreatedDataMultiplier,
InstitutionMultiplier = activityAlgorithmInput.InstitutionMultiplier,
LikeDataMultiplier = activityAlgorithmInput.LikeDataMultiplier,
MetaDataMultiplier = activityAlgorithmInput.MetaDataMultiplier,
RepoScoreMultiplier = activityAlgorithmInput.RepoScoreMultiplier,
UpdatedTimeMultiplier = activityAlgorithmInput.UpdatedTimeMultiplier,
};
activityAlgorithmService.SetActivityAlgorithmMultiplier(projectActivityConfig);
return Ok();
}
if(ModelState.IsValid)
{
return BadRequest();
}
return Forbid();
}
}
}

4 changes: 2 additions & 2 deletions API/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ public async Task<IActionResult> GetAllProjects(
projectFilterParamsResource.SortBy != "name" &&
projectFilterParamsResource.SortBy != "created" &&
projectFilterParamsResource.SortBy != "updated" &&
projectFilterParamsResource.SortBy != "likes")
projectFilterParamsResource.SortBy != "likes" &&
projectFilterParamsResource.SortBy != "activity")
{
problem.Detail = "Invalid sort value: Use \"name\", \"created\", \"updated\" or \"likes\".";
problem.Instance = "5CE2F569-C0D5-4179-9299-62916270A058";
Expand Down Expand Up @@ -238,7 +239,6 @@ public async Task<IActionResult> GetAllProjects(
Page = projectFilterParams.Page,
TotalPages = await projectService.GetProjectsTotalPages(projectFilterParams)
};

return Ok(resultsResource);
}

Expand Down
3 changes: 3 additions & 0 deletions API/Extensions/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public static IServiceCollection AddServicesAndRepositories(this IServiceCollect
services.AddScoped<IProjectTransferService, ProjectTransferService>();
services.AddScoped<IProjectTransferRepository, ProjectTransferRepository>();

services.AddScoped<IActivityAlgorithmService, ActivityAlgorithmService>();
services.AddScoped<IActivityAlgorithmRepository, ActivityAlgorithmRepository>();

services.AddExternalDataSources();

return services;
Expand Down
51 changes: 51 additions & 0 deletions API/InputOutput/ActivityAlgorithm/ActivityAlgorithmInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.ComponentModel.DataAnnotations;

namespace API.InputOutput.ActivityAlgorithm
{
/// <summary>
/// The class for the ActivityAlgorithmInput
/// </summary>
public class ActivityAlgorithmInput
{
/// <summary>
/// Multiplier for the LikeDataField
/// </summary>
[Required]
public int LikeDataMultiplier { get; set; }
/// <summary>
/// Multiplier for the RecentCreatedDataField
/// </summary>
[Required]
public int RecentCreatedDataMultiplier { get; set; }
/// <summary>
/// Multiplier for the AverageLikeDateField
/// </summary>
[Required]
public int AverageLikeDateMultiplier { get; set; }
/// <summary>
/// Multiplier for the UpdatedTimeField
/// </summary>
[Required]
public int UpdatedTimeMultiplier { get; set; }
/// <summary>
/// Multiplier for the InstitutionField
/// </summary>
[Required]
public int InstitutionMultiplier { get; set; }
/// <summary>
/// Multiplier for the ConnectedCollaboratorsField
/// </summary>
[Required]
public int ConnectedCollaboratorsMultiplier { get; set; }
/// <summary>
/// Multiplier for the MetaDataField
/// </summary>
[Required]
public int MetaDataMultiplier { get; set; }
/// <summary>
/// Multiplier for the RepoScoreField
/// </summary>
[Required]
public int RepoScoreMultiplier { get; set; }
}
}
2 changes: 1 addition & 1 deletion API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static class Program
/// <param name="args">The arguments.</param>
/// <returns>The exit code of the program.</returns>
public static int Main(string[] args)
{
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
Expand Down
5 changes: 5 additions & 0 deletions API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,11 @@ private static void UpdateDatabase(IApplicationBuilder app, IWebHostEnvironment
context.SaveChanges();
}

if(!context.ActivityAlgorithmMultiplier.Any())
{
context.ActivityAlgorithmMultiplier.AddRange(Seed.SeedActivityAlgorithmMultiplier());
context.SaveChanges();
}
SeedHelper.SeedDataSourceWizardPages(context);
}

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Release v.2.0.0-beta - DATE HERE
### Added
- Added JobScheduler unit tests - [#381](https://github.com/DigitalExcellence/dex-backend/pull/539)

- Added a way to order inactive projects - [#522](https://github.com/DigitalExcellence/dex-backend/issues/522)
## Release v.1.9.0-beta - 25-11-2021

### Added
Expand Down
5 changes: 5 additions & 0 deletions Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : ba
/// </summary>
public DbSet<ProjectTransferRequest> ProjectTransferRequest { get; set; }

/// <summary>
/// Gets or sets the ActivityAlgorithmMultiplier which represents the activity algorithm multipliers.
/// </summary>
public DbSet<ProjectActivityConfig> ActivityAlgorithmMultiplier { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Expand Down
14 changes: 14 additions & 0 deletions Data/Helpers/Seed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,20 @@ public static List<DataSource> SeedDataSources()
};
}

public static ProjectActivityConfig SeedActivityAlgorithmMultiplier()
{
return new ProjectActivityConfig()
{
AverageLikeDateMultiplier = 1,
ConnectedCollaboratorsMultiplier = 1,
RecentCreatedDataMultiplier = 1,
InstitutionMultiplier = 1,
LikeDataMultiplier = 1,
MetaDataMultiplier = 1,
RepoScoreMultiplier = 1,
UpdatedTimeMultiplier = 1
};
}
public static User SeedAdminUser2(List<Role> roles)
{
Role adminRole = roles.Find(i => i.Name == nameof(Defaults.Roles.Administrator));
Expand Down
Loading