Small Logging Improvement (#3389)

This commit is contained in:
Joe Milazzo 2024-11-21 06:51:24 -06:00 committed by GitHub
parent 17a5d0d583
commit 349645bf32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 31 deletions

View File

@ -21,7 +21,6 @@ using Kavita.Common;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using MimeTypes; using MimeTypes;
namespace API.Controllers; namespace API.Controllers;
@ -90,7 +89,7 @@ public class ReaderController : BaseApiController
} }
catch (Exception) catch (Exception)
{ {
_cacheService.CleanupChapters(new []{ chapterId }); _cacheService.CleanupChapters([chapterId]);
throw; throw;
} }
} }
@ -105,7 +104,8 @@ public class ReaderController : BaseApiController
/// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param> /// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param>
/// <returns></returns> /// <returns></returns>
[HttpGet("image")] [HttpGet("image")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId", "page", "extractPdf", "apiKey"})] [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["chapterId", "page", "extractPdf", "apiKey"
])]
[AllowAnonymous] [AllowAnonymous]
public async Task<ActionResult> GetImage(int chapterId, int page, string apiKey, bool extractPdf = false) public async Task<ActionResult> GetImage(int chapterId, int page, string apiKey, bool extractPdf = false)
{ {
@ -117,7 +117,7 @@ public class ReaderController : BaseApiController
{ {
var chapter = await _cacheService.Ensure(chapterId, extractPdf); var chapter = await _cacheService.Ensure(chapterId, extractPdf);
if (chapter == null) return NoContent(); if (chapter == null) return NoContent();
_logger.LogInformation("Fetching Page {PageNum} on Chapter {ChapterId}", page, chapterId);
var path = _cacheService.GetCachedPagePath(chapter.Id, page); var path = _cacheService.GetCachedPagePath(chapter.Id, page);
if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path))
return BadRequest(await _localizationService.Translate(userId, "no-image-for-page", page)); return BadRequest(await _localizationService.Translate(userId, "no-image-for-page", page));
@ -127,7 +127,7 @@ public class ReaderController : BaseApiController
} }
catch (Exception) catch (Exception)
{ {
_cacheService.CleanupChapters(new []{ chapterId }); _cacheService.CleanupChapters([chapterId]);
throw; throw;
} }
} }
@ -140,7 +140,7 @@ public class ReaderController : BaseApiController
/// <param name="apiKey"></param> /// <param name="apiKey"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet("thumbnail")] [HttpGet("thumbnail")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId", "pageNum", "apiKey"})] [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["chapterId", "pageNum", "apiKey"])]
[AllowAnonymous] [AllowAnonymous]
public async Task<ActionResult> GetThumbnail(int chapterId, int pageNum, string apiKey) public async Task<ActionResult> GetThumbnail(int chapterId, int pageNum, string apiKey)
{ {
@ -164,14 +164,14 @@ public class ReaderController : BaseApiController
/// <remarks>We must use api key as bookmarks could be leaked to other users via the API</remarks> /// <remarks>We must use api key as bookmarks could be leaked to other users via the API</remarks>
/// <returns></returns> /// <returns></returns>
[HttpGet("bookmark-image")] [HttpGet("bookmark-image")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"seriesId", "page", "apiKey"})] [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["seriesId", "page", "apiKey"])]
[AllowAnonymous] [AllowAnonymous]
public async Task<ActionResult> GetBookmarkImage(int seriesId, string apiKey, int page) public async Task<ActionResult> GetBookmarkImage(int seriesId, string apiKey, int page)
{ {
if (page < 0) page = 0;
var userId = await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey); var userId = await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey);
if (userId == 0) return Unauthorized(); if (userId == 0) return Unauthorized();
if (page < 0) page = 0;
var totalPages = await _cacheService.CacheBookmarkForSeries(userId, seriesId); var totalPages = await _cacheService.CacheBookmarkForSeries(userId, seriesId);
if (page > totalPages) if (page > totalPages)
{ {
@ -188,7 +188,7 @@ public class ReaderController : BaseApiController
} }
catch (Exception) catch (Exception)
{ {
_cacheService.CleanupBookmarks(new []{ seriesId }); _cacheService.CleanupBookmarks([seriesId]);
throw; throw;
} }
} }
@ -202,12 +202,13 @@ public class ReaderController : BaseApiController
/// <param name="extractPdf"></param> /// <param name="extractPdf"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet("file-dimensions")] [HttpGet("file-dimensions")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId", "extractPdf"})] [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["chapterId", "extractPdf"])]
public async Task<ActionResult<IEnumerable<FileDimensionDto>>> GetFileDimensions(int chapterId, bool extractPdf = false) public async Task<ActionResult<IEnumerable<FileDimensionDto>>> GetFileDimensions(int chapterId, bool extractPdf = false)
{ {
if (chapterId <= 0) return ArraySegment<FileDimensionDto>.Empty; if (chapterId <= 0) return ArraySegment<FileDimensionDto>.Empty;
var chapter = await _cacheService.Ensure(chapterId, extractPdf); var chapter = await _cacheService.Ensure(chapterId, extractPdf);
if (chapter == null) return NoContent(); if (chapter == null) return NoContent();
return Ok(_cacheService.GetCachedFileDimensions(_cacheService.GetCachePath(chapterId))); return Ok(_cacheService.GetCachedFileDimensions(_cacheService.GetCachePath(chapterId)));
} }
@ -220,7 +221,8 @@ public class ReaderController : BaseApiController
/// <param name="includeDimensions">Include file dimensions. Only useful for image based reading</param> /// <param name="includeDimensions">Include file dimensions. Only useful for image based reading</param>
/// <returns></returns> /// <returns></returns>
[HttpGet("chapter-info")] [HttpGet("chapter-info")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId", "extractPdf", "includeDimensions"})] [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["chapterId", "extractPdf", "includeDimensions"
])]
public async Task<ActionResult<ChapterInfoDto>> GetChapterInfo(int chapterId, bool extractPdf = false, bool includeDimensions = false) public async Task<ActionResult<ChapterInfoDto>> GetChapterInfo(int chapterId, bool extractPdf = false, bool includeDimensions = false)
{ {
if (chapterId <= 0) return Ok(null); // This can happen occasionally from UI, we should just ignore if (chapterId <= 0) return Ok(null); // This can happen occasionally from UI, we should just ignore
@ -260,6 +262,7 @@ public class ReaderController : BaseApiController
} }
if (info.ChapterTitle is {Length: > 0}) { if (info.ChapterTitle is {Length: > 0}) {
// TODO: Can we rework the logic of generating titles for the UI and instead calculate that in the DB?
info.Title += " - " + info.ChapterTitle; info.Title += " - " + info.ChapterTitle;
} }
@ -291,7 +294,7 @@ public class ReaderController : BaseApiController
/// <param name="includeDimensions">Include file dimensions (extra I/O). Defaults to true.</param> /// <param name="includeDimensions">Include file dimensions (extra I/O). Defaults to true.</param>
/// <returns></returns> /// <returns></returns>
[HttpGet("bookmark-info")] [HttpGet("bookmark-info")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"seriesId", "includeDimensions"})] [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["seriesId", "includeDimensions"])]
public async Task<ActionResult<BookmarkInfoDto>> GetBookmarkInfo(int seriesId, bool includeDimensions = true) public async Task<ActionResult<BookmarkInfoDto>> GetBookmarkInfo(int seriesId, bool includeDimensions = true)
{ {
var totalPages = await _cacheService.CacheBookmarkForSeries(User.GetUserId(), seriesId); var totalPages = await _cacheService.CacheBookmarkForSeries(User.GetUserId(), seriesId);
@ -375,13 +378,10 @@ public class ReaderController : BaseApiController
var chapters = await _unitOfWork.ChapterRepository.GetChaptersAsync(markVolumeReadDto.VolumeId); var chapters = await _unitOfWork.ChapterRepository.GetChaptersAsync(markVolumeReadDto.VolumeId);
await _readerService.MarkChaptersAsUnread(user, markVolumeReadDto.SeriesId, chapters); await _readerService.MarkChaptersAsUnread(user, markVolumeReadDto.SeriesId, chapters);
if (await _unitOfWork.CommitAsync()) if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
{
BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, markVolumeReadDto.SeriesId));
return Ok();
}
return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress")); BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, markVolumeReadDto.SeriesId));
return Ok();
} }
/// <summary> /// <summary>
@ -551,7 +551,7 @@ public class ReaderController : BaseApiController
} }
/// <summary> /// <summary>
/// Save page against Chapter for logged in user /// Save page against Chapter for authenticated user
/// </summary> /// </summary>
/// <param name="progressDto"></param> /// <param name="progressDto"></param>
/// <returns></returns> /// <returns></returns>
@ -769,7 +769,7 @@ public class ReaderController : BaseApiController
/// <param name="volumeId"></param> /// <param name="volumeId"></param>
/// <param name="currentChapterId"></param> /// <param name="currentChapterId"></param>
/// <returns>chapter id for next manga</returns> /// <returns>chapter id for next manga</returns>
[ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = new [] { "seriesId", "volumeId", "currentChapterId"})] [ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = ["seriesId", "volumeId", "currentChapterId"])]
[HttpGet("next-chapter")] [HttpGet("next-chapter")]
public async Task<ActionResult<int>> GetNextChapter(int seriesId, int volumeId, int currentChapterId) public async Task<ActionResult<int>> GetNextChapter(int seriesId, int volumeId, int currentChapterId)
{ {
@ -787,7 +787,7 @@ public class ReaderController : BaseApiController
/// <param name="volumeId"></param> /// <param name="volumeId"></param>
/// <param name="currentChapterId"></param> /// <param name="currentChapterId"></param>
/// <returns>chapter id for next manga</returns> /// <returns>chapter id for next manga</returns>
[ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = new [] { "seriesId", "volumeId", "currentChapterId"})] [ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = ["seriesId", "volumeId", "currentChapterId"])]
[HttpGet("prev-chapter")] [HttpGet("prev-chapter")]
public async Task<ActionResult<int>> GetPreviousChapter(int seriesId, int volumeId, int currentChapterId) public async Task<ActionResult<int>> GetPreviousChapter(int seriesId, int volumeId, int currentChapterId)
{ {
@ -801,7 +801,7 @@ public class ReaderController : BaseApiController
/// <param name="seriesId"></param> /// <param name="seriesId"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet("time-left")] [HttpGet("time-left")]
[ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = new [] { "seriesId"})] [ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = ["seriesId"])]
public async Task<ActionResult<HourEstimateRangeDto>> GetEstimateToCompletion(int seriesId) public async Task<ActionResult<HourEstimateRangeDto>> GetEstimateToCompletion(int seriesId)
{ {
var userId = User.GetUserId(); var userId = User.GetUserId();
@ -899,12 +899,8 @@ public class ReaderController : BaseApiController
[HttpGet("all-chapter-progress")] [HttpGet("all-chapter-progress")]
public async Task<ActionResult<IEnumerable<FullProgressDto>>> GetProgressForChapter(int chapterId) public async Task<ActionResult<IEnumerable<FullProgressDto>>> GetProgressForChapter(int chapterId)
{ {
if (User.IsInRole(PolicyConstants.AdminRole)) var userId = User.IsInRole(PolicyConstants.AdminRole) ? 0 : User.GetUserId();
{ return Ok(await _unitOfWork.AppUserProgressRepository.GetUserProgressForChapter(chapterId, userId));
return Ok(await _unitOfWork.AppUserProgressRepository.GetUserProgressForChapter(chapterId));
}
return Ok(await _unitOfWork.AppUserProgressRepository.GetUserProgressForChapter(chapterId, User.GetUserId()));
} }
} }

View File

@ -118,8 +118,6 @@ public class CacheService : ICacheService
Cache.MaxFiles = originalCacheSize; Cache.MaxFiles = originalCacheSize;
} }
_logger.LogDebug("File Dimensions call for {Length} images took {Time}ms", dimensions.Count, sw.ElapsedMilliseconds);
return dimensions; return dimensions;
} }

View File

@ -345,7 +345,7 @@ public class ScrobblingService : IScrobblingService
_unitOfWork.ScrobbleRepository.Attach(evt); _unitOfWork.ScrobbleRepository.Attach(evt);
await _unitOfWork.CommitAsync(); await _unitOfWork.CommitAsync();
_logger.LogDebug("Added Scrobbling Read update on {SeriesName} with Userid {UserId} ", series.Name, userId); _logger.LogDebug("Added Scrobbling Read update on {SeriesName} - Volume: {VolumeNumber} Chapter: {ChapterNumber} for User: {UserId}", series.Name, evt.VolumeNumber, evt.ChapterNumber, userId);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -291,6 +291,7 @@ public class ReaderService : IReaderService
_unitOfWork.AppUserProgressRepository.Update(userProgress); _unitOfWork.AppUserProgressRepository.Update(userProgress);
} }
_logger.LogDebug("Saving Progress on Chapter {ChapterId} from Series {SeriesId} to {PageNum}", progressDto.ChapterId, progressDto.SeriesId, progressDto.PageNum);
userProgress?.MarkModified(); userProgress?.MarkModified();
if (!_unitOfWork.HasChanges() || await _unitOfWork.CommitAsync()) if (!_unitOfWork.HasChanges() || await _unitOfWork.CommitAsync())