Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
58 changes: 57 additions & 1 deletion src/Valora.Api/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Valora.Application.Abstractions.Admin;
using Valora.Application.Contracts.Admin;
using Valora.Domain.Constants;
using Valora.Infrastructure.Persistence;
Expand All @@ -19,16 +20,19 @@ public sealed class AdminController : ControllerBase
{
private readonly ValoraDbContext _dbContext;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IModelPerformanceService _modelPerformanceService;

/// <summary>
/// Initializes a new instance of the <see cref="AdminController"/> class.
/// </summary>
public AdminController(
ValoraDbContext dbContext,
UserManager<ApplicationUser> userManager)
UserManager<ApplicationUser> userManager,
IModelPerformanceService modelPerformanceService)
{
_dbContext = dbContext;
_userManager = userManager;
_modelPerformanceService = modelPerformanceService;
}

/// <summary>
Expand Down Expand Up @@ -318,4 +322,56 @@ public async Task<IActionResult> GetAuditLogs(CancellationToken cancellationToke
logs
});
}

/// <summary>
/// Gets the model performance overview.
/// </summary>
[HttpGet("model-performance/overview")]
[ProducesResponseType(typeof(ModelPerformanceOverviewResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> GetModelPerformanceOverview(CancellationToken cancellationToken)
{
ModelPerformanceOverviewResponse response =
await _modelPerformanceService.GetOverviewAsync(cancellationToken);

return Ok(response);
}

/// <summary>
/// Gets model performance for a category.
/// </summary>
[HttpGet("model-performance/{category}")]
[ProducesResponseType(typeof(ModelPerformanceCategoryResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetModelPerformanceByCategory(
[FromRoute] string category,
CancellationToken cancellationToken)
{
ModelPerformanceCategoryResponse? response =
await _modelPerformanceService.GetCategoryAsync(category, cancellationToken);

if (response is null)
{
return NotFound(new ProblemDetails
{
Title = "Model performance not found",
Detail = $"No sold listing performance data exists yet for category '{category}'.",
Status = StatusCodes.Status404NotFound
});
}

return Ok(response);
}

/// <summary>
/// Gets recent listing-level model performance rows.
/// </summary>
[HttpGet("model-performance/listings")]
[ProducesResponseType(typeof(IReadOnlyCollection<ModelPerformanceListingResponse>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetModelPerformanceListings(CancellationToken cancellationToken)
{
IReadOnlyCollection<ModelPerformanceListingResponse> response =
await _modelPerformanceService.GetRecentListingsAsync(cancellationToken);

return Ok(response);
}
}
89 changes: 87 additions & 2 deletions src/Valora.Api/Controllers/ListingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,105 @@ public async Task<IActionResult> Publish([FromRoute] Guid id, CancellationToken
[HttpPost("{id:guid}/unpublish")]
[ProducesResponseType(typeof(ListingResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Unpublish([FromRoute] Guid id, CancellationToken cancellationToken)
{
try
{
Guid userId = GetRequiredUserId();
bool isAdmin = User.IsInRole(ApplicationRoles.Admin);

ListingResponse? response =
await _listingService.UnpublishAsync(id, userId, isAdmin, cancellationToken);

if (response is null)
{
return NotFound(new ProblemDetails
{
Title = "Listing not found",
Detail = $"No unpublishable listing was found with id '{id}'.",
Status = StatusCodes.Status404NotFound
});
}

return Ok(response);
}
catch (ArgumentException ex)
{
return BadRequest(new ProblemDetails
{
Title = "Unpublish failed",
Detail = ex.Message,
Status = StatusCodes.Status400BadRequest
});
}
}

/// <summary>
/// Marks a listing as sold.
/// </summary>
[Authorize]
[HttpPost("{id:guid}/mark-as-sold")]
[ProducesResponseType(typeof(ListingResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> MarkAsSold(
[FromRoute] Guid id,
[FromBody] MarkListingAsSoldRequest request,
CancellationToken cancellationToken)
{
try
{
Guid userId = GetRequiredUserId();
bool isAdmin = User.IsInRole(ApplicationRoles.Admin);

ListingResponse? response =
await _listingService.MarkAsSoldAsync(id, userId, isAdmin, request.SoldPrice, cancellationToken);

if (response is null)
{
return NotFound(new ProblemDetails
{
Title = "Listing not found",
Detail = $"No sellable listing was found with id '{id}'.",
Status = StatusCodes.Status404NotFound
});
}

return Ok(response);
}
catch (ArgumentException ex)
{
return BadRequest(new ProblemDetails
{
Title = "Mark as sold failed",
Detail = ex.Message,
Status = StatusCodes.Status400BadRequest
});
}
}

/// <summary>
/// Archives a listing.
/// </summary>
[Authorize]
[HttpPost("{id:guid}/archive")]
[ProducesResponseType(typeof(ListingResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Archive([FromRoute] Guid id, CancellationToken cancellationToken)
{
Guid userId = GetRequiredUserId();
bool isAdmin = User.IsInRole(ApplicationRoles.Admin);

ListingResponse? response =
await _listingService.UnpublishAsync(id, userId, isAdmin, cancellationToken);
await _listingService.ArchiveAsync(id, userId, isAdmin, cancellationToken);

if (response is null)
{
return NotFound(new ProblemDetails
{
Title = "Listing not found",
Detail = $"No unpublishable listing was found with id '{id}'.",
Detail = $"No archivable listing was found with id '{id}'.",
Status = StatusCodes.Status404NotFound
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/Valora.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Valora.Application.Abstractions.Admin;
using Valora.Application.Abstractions.Listings;
using Valora.Application.Abstractions.Logging;
using Valora.Application.Abstractions.PricePrediction;
Expand All @@ -11,6 +12,7 @@
using Valora.Infrastructure.ML.Services;
using Valora.Infrastructure.Persistence;
using Valora.Infrastructure.Persistence.Identity;
using Valora.Infrastructure.Services.Admin;
using Valora.Infrastructure.Services.Listings;
using Valora.Infrastructure.Services.Logging;

Expand Down Expand Up @@ -101,6 +103,7 @@ public static IServiceCollection AddValoraPersistence(

services.AddScoped<IListingService, ListingService>();
services.AddScoped<IAuditLogService, AuditLogService>();
services.AddScoped<IModelPerformanceService, ModelPerformanceService>();

return services;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Valora.Application.Contracts.Admin;

namespace Valora.Application.Abstractions.Admin;

/// <summary>
/// Service for model performance monitoring.
/// </summary>
public interface IModelPerformanceService
{
/// <summary>
/// Gets the overall performance overview.
/// </summary>
Task<ModelPerformanceOverviewResponse> GetOverviewAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Gets performance for a single category.
/// </summary>
Task<ModelPerformanceCategoryResponse?> GetCategoryAsync(string categoryKey, CancellationToken cancellationToken = default);

/// <summary>
/// Gets recent listing-level performance rows.
/// </summary>
Task<IReadOnlyCollection<ModelPerformanceListingResponse>> GetRecentListingsAsync(CancellationToken cancellationToken = default);
}
19 changes: 19 additions & 0 deletions src/Valora.Application/Abstractions/Listings/IListingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,23 @@ Task<bool> DeleteAsync(
Guid actorUserId,
bool isAdmin,
CancellationToken cancellationToken = default);

/// <summary>
/// Marks a listing as sold.
/// </summary>
Task<ListingResponse?> MarkAsSoldAsync(
Guid listingId,
Guid actorUserId,
bool isAdmin,
decimal soldPrice,
CancellationToken cancellationToken = default);

/// <summary>
/// Archives a listing.
/// </summary>
Task<ListingResponse?> ArchiveAsync(
Guid listingId,
Guid actorUserId,
bool isAdmin,
CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Valora.Application.Contracts.Admin;

/// <summary>
/// Performance information for a single category.
/// </summary>
public sealed class ModelPerformanceCategoryResponse
{
/// <summary>
/// Gets or sets the category key.
/// </summary>
public required string CategoryKey { get; init; }

/// <summary>
/// Gets or sets the sold listing count.
/// </summary>
public int SoldListings { get; init; }

/// <summary>
/// Gets or sets the sold listings with prediction.
/// </summary>
public int SoldListingsWithPrediction { get; init; }

/// <summary>
/// Gets or sets the average absolute error.
/// </summary>
public decimal AverageAbsoluteError { get; init; }

/// <summary>
/// Gets or sets the average percentage error.
/// </summary>
public decimal AveragePercentageError { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Valora.Application.Contracts.Admin;

/// <summary>
/// Listing-level model performance information.
/// </summary>
public sealed class ModelPerformanceListingResponse
{
/// <summary>
/// Gets or sets the listing id.
/// </summary>
public Guid ListingId { get; init; }

/// <summary>
/// Gets or sets the category key.
/// </summary>
public required string CategoryKey { get; init; }

/// <summary>
/// Gets or sets the listing title.
/// </summary>
public required string Title { get; init; }

/// <summary>
/// Gets or sets the asking price.
/// </summary>
public decimal AskingPrice { get; init; }

/// <summary>
/// Gets or sets the predicted price.
/// </summary>
public decimal PredictedPrice { get; init; }

/// <summary>
/// Gets or sets the sold price.
/// </summary>
public decimal SoldPrice { get; init; }

/// <summary>
/// Gets or sets the absolute error.
/// </summary>
public decimal AbsoluteError { get; init; }

/// <summary>
/// Gets or sets the percentage error.
/// </summary>
public decimal PercentageError { get; init; }

/// <summary>
/// Gets or sets the sold timestamp.
/// </summary>
public DateTime SoldAtUtc { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Valora.Application.Contracts.Admin;

/// <summary>
/// Overall model performance overview.
/// </summary>
public sealed class ModelPerformanceOverviewResponse
{
/// <summary>
/// Gets or sets the total sold listings.
/// </summary>
public int TotalSoldListings { get; init; }

/// <summary>
/// Gets or sets the sold listings that also had a predicted price.
/// </summary>
public int SoldListingsWithPrediction { get; init; }

/// <summary>
/// Gets or sets the average absolute error.
/// </summary>
public decimal AverageAbsoluteError { get; init; }

/// <summary>
/// Gets or sets the average percentage error.
/// </summary>
public decimal AveragePercentageError { get; init; }

/// <summary>
/// Gets or sets category performance items.
/// </summary>
public required IReadOnlyCollection<ModelPerformanceCategoryResponse> Categories { get; init; }
}
Loading
Loading