Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
fix: modify updateProgress function to handle timeout issues (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored May 18, 2024
1 parent 9d2e3e1 commit 842105b
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 154 deletions.
10 changes: 5 additions & 5 deletions Jellyfin.Plugin.Themerr.Tests/TestThemerrController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ public void TestGetProgress()
var result = _controller.GetProgress();
Assert.IsType<JsonResult>(result);

// ensure result["media_count"] is an int
// ensure the following properties are int
Assert.IsType<int>(((JsonResult)result).Value?.GetType().GetProperty("media_count")?.GetValue(((JsonResult)result).Value, null));

// ensure result["media_percent_complete"] is an int
Assert.IsType<int>(((JsonResult)result).Value?.GetType().GetProperty("media_percent_complete")?.GetValue(((JsonResult)result).Value, null));
Assert.IsType<int>(((JsonResult)result).Value?.GetType().GetProperty("media_with_themes")?.GetValue(((JsonResult)result).Value, null));
Assert.IsType<int>(((JsonResult)result).Value?.GetType().GetProperty("total_pages")?.GetValue(((JsonResult)result).Value, null));

// ensure result["items"] is an array list
Assert.IsType<ArrayList>(((JsonResult)result).Value?.GetType().GetProperty("items")?.GetValue(((JsonResult)result).Value, null));

// ensure int values are 0
Assert.Equal(0, ((JsonResult)result).Value?.GetType().GetProperty("media_count")?.GetValue(((JsonResult)result).Value, null));
Assert.Equal(0, ((JsonResult)result).Value?.GetType().GetProperty("media_percent_complete")?.GetValue(((JsonResult)result).Value, null));
Assert.Equal(0, ((JsonResult)result).Value?.GetType().GetProperty("media_with_themes")?.GetValue(((JsonResult)result).Value, null));
Assert.Equal(0, ((JsonResult)result).Value?.GetType().GetProperty("total_pages")?.GetValue(((JsonResult)result).Value, null));

// ensure array list has no items
Assert.Equal(0, (((JsonResult)result).Value?.GetType().GetProperty("items")?.GetValue(((JsonResult)result).Value, null) as ArrayList)?.Count);
Expand Down
23 changes: 13 additions & 10 deletions Jellyfin.Plugin.Themerr/Api/ThemerrController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,30 @@ public async Task TriggerUpdateRequest()
/// "media_percent_complete": ThemedItems.Count / BaseItems.Count * 100,
/// }
/// </summary>
/// <param name="page">The page number to return.</param>
/// <param name="pageSize">The number of items to return per page.</param>
/// <returns>JSON object containing progress data.</returns>
[HttpGet("GetProgress")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult GetProgress()
public ActionResult GetProgress(int page = 1, int pageSize = 10)
{
var tmpItems = new ArrayList();

var mediaCount = 0;
var mediaWithThemes = 0;
var mediaPercentComplete = 0;

var items = _themerrManager.GetTmdbItemsFromLibrary();

// sort items by name, then year
var enumerable = items.OrderBy(i => i.Name).ThenBy(i => i.ProductionYear);
var enumerable = items.OrderBy(i => i.Name).ThenBy(i => i.ProductionYear).ToList();

foreach (var item in enumerable)
// calculate total media count before applying pagination
var totalMediaCount = enumerable.Count;

// apply pagination
var pagedItems = enumerable.Skip((page - 1) * pageSize).Take(pageSize);

foreach (var item in pagedItems)
{
var year = item.ProductionYear;
var issueUrl = _themerrManager.GetIssueUrl(item);
Expand All @@ -125,16 +132,12 @@ public ActionResult GetProgress()
}
}

if (mediaCount > 0)
{
mediaPercentComplete = (int)Math.Round((double)mediaWithThemes / mediaCount * 100);
}

var tmpObject = new
{
items = tmpItems,
media_count = mediaCount,
media_percent_complete = mediaPercentComplete,
media_with_themes = mediaWithThemes,
total_pages = (int)Math.Ceiling((double)totalMediaCount / pageSize),
};

_logger.LogInformation("Progress Items: {Items}", JsonConvert.SerializeObject(tmpObject));
Expand Down
Loading

0 comments on commit 842105b

Please sign in to comment.