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

Feat/dashboard #395

Merged
merged 4 commits into from
Jul 30, 2023
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
13 changes: 12 additions & 1 deletion src/Api/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Security.Authentication;
using Api.Controllers.Payload.Requests.Auth;
using Api.Controllers.Payload.Responses;
using Application.Common.Extensions.Logging;
using Application.Common.Interfaces;
using Application.Common.Logging;
using Application.Common.Models;
using Application.Common.Models.Dtos;
using Application.Users.Queries;
Expand All @@ -18,10 +20,14 @@ namespace Api.Controllers;
public class AuthController : ControllerBase
{
private readonly IIdentityService _identityService;
private readonly ILogger<AuthController> _logger;
private readonly IDateTimeProvider _dateTimeProvider;

public AuthController(IIdentityService identityService)
public AuthController(IIdentityService identityService, ILogger<AuthController> logger, IDateTimeProvider dateTimeProvider)
{
_identityService = identityService;
_logger = logger;
_dateTimeProvider = dateTimeProvider;
}

/// <summary>
Expand Down Expand Up @@ -54,6 +60,11 @@ public async Task<IActionResult> Login([FromBody] LoginModel loginModel)
LastName = loginSuccess.UserCredentials.LastName,
};

var dateTimeNow = _dateTimeProvider.DateTimeNow;
using (Logging.PushProperties("Login", loginResult.Id, loginResult.Id))
{
_logger.LogLogin(loginResult.Username, dateTimeNow.ToString("yyyy-MM-dd HH:mm:ss"));
}
return Ok(Result<LoginResult>.Succeed(loginResult));
},
token =>
Expand Down
60 changes: 60 additions & 0 deletions src/Api/Controllers/DashboardController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Api.Controllers.Payload.Requests.Dashboard;
using Application.Common.Interfaces;
using Application.Common.Models;
using Application.Common.Models.Dtos.DashBoard;
using Application.Dashboards.Queries;
using Application.Identity;
using Infrastructure.Identity.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -7,6 +11,62 @@ namespace Api.Controllers;

public class DashboardController : ApiControllerBase
{
private readonly ICurrentUserService _currentUserService;

public DashboardController(ICurrentUserService currentUserService)
{
_currentUserService = currentUserService;
}

[RequiresRole(IdentityData.Roles.Admin)]
[HttpPost("import-documents")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<Result<List<MetricResultDto>>>> GetImportDocuments(
[FromBody] GetImportedDocumentsMetricsRequest request)
{
var query = new GetImportDocuments.Query()
{
StartDate = request.StartDate,
EndDate = request.EndDate
};

var result = await Mediator.Send(query);
return Ok(Result<List<MetricResultDto>>.Succeed(result));
}

[RequiresRole(IdentityData.Roles.Admin)]
[HttpPost("logins")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<Result<MetricResultDto>>> GetLoggedInUsers(
[FromBody] DateTime date)
{
var query = new GetLoggedInUser.Query()
{
Date = date
};

var result = await Mediator.Send(query);
return Ok(Result<MetricResultDto>.Succeed(result));
}

[RequiresRole(IdentityData.Roles.Admin)]
[HttpPost("largest-drive")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<Result<LargestDriveDto>>> GetUserWithLargestDrive(
[FromBody] DateTime date)
{
var query = new GetUserWithLargestDriveData.Query()
{
Date = date
};

var result = await Mediator.Send(query);
return Ok(Result<LargestDriveDto>.Succeed(result));
}

[RequiresRole(IdentityData.Roles.Admin)]
[HttpGet("online-users")]
[ProducesResponseType(StatusCodes.Status200OK)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Api.Controllers.Payload.Requests.Dashboard;

public class GetImportedDocumentsMetricsRequest
{

public DateTime StartDate { get; set; }

public DateTime EndDate { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ is BorrowRequestStatus.Approved

using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUserId))
{
_logger.LogApproveBorrowRequest(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
Common.Extensions.Logging.BorrowLogExtensions.LogApproveBorrowRequest(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
}
}

Expand All @@ -130,7 +130,7 @@ is BorrowRequestStatus.Approved

using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUserId))
{
_logger.LogRejectBorrowRequest(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
Common.Extensions.Logging.BorrowLogExtensions.LogRejectBorrowRequest(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Application/Borrows/Commands/BorrowDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ is BorrowRequestStatus.Approved
await _context.SaveChangesAsync(cancellationToken);
using (Logging.PushProperties("Request", document.Id, user.Id))
{
_logger.LogBorrowDocument(document.Id.ToString(), result.Entity.Id.ToString());
Common.Extensions.Logging.BorrowLogExtensions.LogBorrowDocument(_logger, document.Id.ToString(), result.Entity.Id.ToString());
}
return _mapper.Map<BorrowDto>(result.Entity);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Borrows/Commands/CancelBorrowRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task<BorrowDto> Handle(Command request, CancellationToken cancellat
await _context.SaveChangesAsync(cancellationToken);
using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUserId))
{
_logger.LogCancelBorrowRequest(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
Common.Extensions.Logging.BorrowLogExtensions.LogCancelBorrowRequest(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
}
return _mapper.Map<BorrowDto>(result.Entity);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Borrows/Commands/CheckoutDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task<BorrowDto> Handle(Command request, CancellationToken cancellat
await _context.SaveChangesAsync(cancellationToken);
using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentStaff.Id))
{
_logger.LogCheckoutDocument(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
Common.Extensions.Logging.BorrowLogExtensions.LogCheckoutDocument(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
}
return _mapper.Map<BorrowDto>(result.Entity);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Borrows/Commands/ReturnDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public async Task<BorrowDto> Handle(Command request, CancellationToken cancellat
await _context.SaveChangesAsync(cancellationToken);
using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUser.Id))
{
_logger.LogReturnDocument(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
Common.Extensions.Logging.BorrowLogExtensions.LogReturnDocument(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
}
return _mapper.Map<BorrowDto>(result.Entity);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Borrows/Commands/UpdateBorrow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ is BorrowRequestStatus.Approved

using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUser.Id))
{
_logger.LogUpdateBorrow(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
Common.Extensions.Logging.BorrowLogExtensions.LogUpdateBorrow(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString());
}

return _mapper.Map<BorrowDto>(result.Entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Application.Common.Messages;
using Microsoft.Extensions.Logging;

namespace Application.Borrows;
namespace Application.Common.Extensions.Logging;

public static partial class BorrowLogExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using EventId = Application.Common.Logging.EventId;

namespace Application.Documents;
namespace Application.Common.Extensions.Logging;

public static partial class DocumentLogExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using EventId = Application.Common.Logging.EventId;

namespace Application.Entries;
namespace Application.Common.Extensions.Logging;

public static partial class EntryLogExtension
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using EventId = Application.Common.Logging.EventId;

namespace Application.Folders;
namespace Application.Common.Extensions.Logging;

public static partial class FolderLogExtension {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Application.Common.Messages;
using Domain.Entities.Physical;
using Microsoft.Extensions.Logging;
using EventId = Application.Common.Logging.EventId;

namespace Application.ImportRequests;
namespace Application.Common.Extensions.Logging;

public static partial class ImportRequestLogExtensions {
// Approve or reject document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using EventId = Application.Common.Logging.EventId;

namespace Application.Lockers;
namespace Application.Common.Extensions.Logging;

public static partial class LockerLogExtension {

Expand Down
11 changes: 11 additions & 0 deletions src/Application/Common/Extensions/Logging/LoginLogExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Application.Common.Messages;
using Microsoft.Extensions.Logging;
using EventId = Application.Common.Logging.EventId;

namespace Application.Common.Extensions.Logging;

public static partial class LoginLogExtension
{
[LoggerMessage(Level = LogLevel.Information, Message = LoginLogMessages.Login, EventId = EventId.Approve)]
public static partial void LogLogin(this ILogger logger, string username, string time);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using EventId = Application.Common.Logging.EventId;

namespace Application.Rooms;
namespace Application.Common.Extensions.Logging;

public static partial class RoomLogExtension {
[LoggerMessage(Level = LogLevel.Information, Message = RoomLogMessage.Add, EventId = EventId.Add)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using EventId = Application.Common.Logging.EventId;

namespace Application.Staffs;
namespace Application.Common.Extensions.Logging;

public static partial class StaffLogExtensions {
[LoggerMessage(Level = LogLevel.Information, Message = UserLogMessages.Staff.AssignStaff, EventId = EventId.Add)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Application.Common.Messages;
using Microsoft.Extensions.Logging;

namespace Application.Users;
namespace Application.Common.Extensions.Logging;

public static partial class UserLogExtensions
{
Expand Down
6 changes: 6 additions & 0 deletions src/Application/Common/Messages/LoginLogMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Application.Common.Messages;

public class LoginLogMessages
{
public const string Login = "User with username {Username} Log in at {Time}";
}
10 changes: 10 additions & 0 deletions src/Application/Common/Models/Dtos/DashBoard/LargestDriveDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Application.Users.Queries;

namespace Application.Common.Models.Dtos.DashBoard;

public class LargestDriveDto
{
public string Label { get; set; }
public long Value { get; set; }
public UserDto User { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Application.Common.Models.Dtos.DashBoard;

public class MetricResultDto
{
public string Label { get; set; }
public int Value { get; set; }
}
74 changes: 74 additions & 0 deletions src/Application/Dashboards/Queries/GetImportDocuments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Application.Common.Interfaces;
using Application.Common.Models.Dtos.DashBoard;
using Domain.Statuses;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;
using NodaTime;

namespace Application.Dashboards.Queries;

public class GetImportDocuments
{
public class Validator : AbstractValidator<Query>
{
public Validator()
{
RuleLevelCascadeMode = CascadeMode.Stop;

RuleFor(x => x.StartDate)
.NotEmpty().WithMessage("Start date can not be empty.");

RuleFor(x => x.EndDate)
.NotEmpty().WithMessage("End date can not be empty.");
}
}
public record Query : IRequest<List<MetricResultDto>>
{
public DateTime StartDate { get; init; }
public DateTime EndDate { get; init; }

}

public class QueryHandler : IRequestHandler<Query, List<MetricResultDto>>
{
private readonly IApplicationDbContext _context;

public QueryHandler(IApplicationDbContext context)
{
_context = context;
}

public async Task<List<MetricResultDto>> Handle(Query request, CancellationToken cancellationToken)
{
var result = new List<MetricResultDto>();

var currentDate = request.StartDate.Date;
var endDate = request.EndDate.Date;

while (currentDate <= endDate)
{
var nextDate = currentDate.AddDays(1);
var label = $"{currentDate:dd-MM} to {nextDate:dd-MM}";
if (nextDate <= endDate)
{
var date = currentDate;
var importedDocumentsCount = await _context.Documents
.Where(x => x.Created >= LocalDateTime.FromDateTime(date)
&& x.Created <= LocalDateTime.FromDateTime(nextDate)
&& x.Status != DocumentStatus.Issued)
.CountAsync(cancellationToken);

result.Add(new MetricResultDto()
{
Label = label,
Value = importedDocumentsCount
});
}
currentDate = nextDate;
}

return result;
}
}
}
Loading