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
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed May 18, 2024
1 parent 9d2e3e1 commit e7aecd8
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 148 deletions.
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
310 changes: 172 additions & 138 deletions Jellyfin.Plugin.Themerr/Configuration/configPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,150 +191,184 @@ <h2 class="sectionTitle">Themerr</h2>
}
}

function updateProgress() {
let request = {
url: _ApiClient.getUrl('/Themerr/GetProgress'),
type: 'GET',
dataType: 'json' // this should automatically return response.json()
}
async function updateProgress() {
let all_items = []
let page = 1
let pageSize = 10
let total_media_count = 0
let total_media_with_themes = 0
let total_pages = 1

for (; page <= total_pages; page++) {
let request = {
url: _ApiClient.getUrl(
'/Themerr/GetProgress',
{
page: page,
pageSize: pageSize
},
),
type: 'GET',
dataType: 'json' // this should automatically return response.json()
};

_ApiClient.fetch(request).then(function (response) {
console.log(LogPrefix + "media_count: " + response["media_count"])
console.log(LogPrefix + "media_percent_complete: " + response["media_percent_complete"])
try {
let response = await _ApiClient.fetch(request);

if (response["media_count"] === 0) {
let message = "No media found!"
console.log(LogPrefix + message)
return
}
console.log(LogPrefix + "processing page " + page + " of " + response["total_pages"]);
console.log(LogPrefix + "media_count: " + response["media_count"]);
console.log(LogPrefix + "media_with_themes: " + response["media_with_themes"]);

// get dashboard container
let ThemerrDashboard = document.querySelector('#ThemerrDashboard')

// create progress bar
let progressBar = document.createElement('div')
progressBar.setAttribute('is', 'emby-progressbar')
progressBar.setAttribute('class', 'itemProgressBar')
progressBar.setAttribute('style', 'height: 20px;')
ThemerrDashboard.appendChild(progressBar)

// progress bar foreground
let progressBarForeground = document.createElement('div')
progressBarForeground.setAttribute('class', 'itemProgressBarForeground')
progressBarForeground.setAttribute('style', 'width: ' + response["media_percent_complete"] + '%;')
progressBar.appendChild(progressBarForeground)

// progress bar text
let progressBarText = document.createElement('span')
progressBarText.setAttribute('style', 'margin-left: 0.5em;')
progressBarText.innerText = response["media_percent_complete"] + '%'
progressBarForeground.appendChild(progressBarText)

// add break
ThemerrDashboard.appendChild(document.createElement('br'))

// create table
let table = document.createElement('table')
table.setAttribute('id', 'ThemerrProgressTable')
table.setAttribute('class', 'detailTable')
ThemerrDashboard.appendChild(table)

// create table header
let tableHeader = document.createElement('thead')
table.appendChild(tableHeader)

// create table header row
let tableHeaderRow = document.createElement('tr')
tableHeader.appendChild(tableHeaderRow)

let columns = ['Title', 'Year', 'Type', 'Contribute', 'Status']

// create table header columns
for (let column in columns) {
let tableHeaderColumn = document.createElement('th')
tableHeaderColumn.setAttribute('class', 'detailTableHeaderCell')
tableHeaderColumn.setAttribute('scope', 'col')
tableHeaderColumn.innerText = translate(columns[column].toLowerCase())
tableHeaderRow.appendChild(tableHeaderColumn)
if (response["media_count"] === 0) {
let message = "No media found!";
console.log(LogPrefix + message);
return;
}

total_media_count += response["media_count"];
total_media_with_themes += response["media_with_themes"];
all_items = all_items.concat(response["items"]);

total_pages = response["total_pages"]; // Update totalPages
} catch (error) {
let message = translate('unexpected_error_occured_collecting_progress');
Dashboard.alert({
message: message
});
console.log(LogPrefix + message);
console.log(LogPrefix + error);
}
}

// loop over items
for (let item in response["items"]) {
console.log(LogPrefix + "------------------")
console.log(LogPrefix + "item: " + item)
console.log(LogPrefix + "name: " + response["items"][item]["name"])
console.log(LogPrefix + "id: " + response["items"][item]["id"])
console.log(LogPrefix + "issue_url: " + response["items"][item]["issue_url"])
console.log(LogPrefix + "type: " + response["items"][item]["type"])
console.log(LogPrefix + "theme_provider: " + response["items"][item]["theme_provider"])
console.log(LogPrefix + "year: " + response["items"][item]["year"])

// create table row
let tableRow = document.createElement('tr')
table.appendChild(tableRow)

// create table columns
let tableColumnTitle = document.createElement('td')
tableColumnTitle.setAttribute('class', 'detailTableBodyCell')
tableColumnTitle.innerText = response["items"][item]["name"]
tableRow.appendChild(tableColumnTitle)

let tableColumnYear = document.createElement('td')
tableColumnYear.setAttribute('class', 'detailTableBodyCell')
tableColumnYear.innerText = response["items"][item]["year"]
tableRow.appendChild(tableColumnYear)

let tableColumnType = document.createElement('td')
tableColumnType.setAttribute('class', 'detailTableBodyCell')
tableColumnType.innerText = translate(response["items"][item]["type"].toLowerCase())
tableRow.appendChild(tableColumnType)

let contributeButton = document.createElement('a')
contributeButton.setAttribute('is', 'emby-linkbutton')
contributeButton.setAttribute('class', 'raised headerHelpButton button-submit emby-button')
contributeButton.setAttribute('target', '_blank')
contributeButton.setAttribute('href', response["items"][item]["issue_url"])
// text added when status is set

let tableColumnContribute = document.createElement('td')
tableColumnContribute.appendChild(contributeButton)
tableRow.appendChild(tableColumnContribute)

let tableColumnStatus = document.createElement('td')
tableColumnStatus.setAttribute('class', 'detailTableBodyCell')

let statusSpan = document.createElement('span')
let statusText = document.createElement('span')
statusSpan.setAttribute('class', 'material-icons')
statusText.setAttribute('class', 'detailTableBodyCell')
if (response["items"][item]["theme_provider"] === 'themerr') {
contributeButton.innerText = translate('edit_button')
statusSpan.classList.add('check')
statusSpan.setAttribute('style', 'margin-right: 6px; color: #00ff00;')
statusText.innerText = translate('themerr_provided')
} else if (response["items"][item]["theme_provider"] === 'user') {
contributeButton.innerText = translate('contribute_button')
statusSpan.classList.add('person')
statusSpan.setAttribute('style', 'margin-right: 6px; color: #00a4dc;')
statusText.innerText = translate('user_provided')
} else {
contributeButton.innerText = translate('add_button')
statusSpan.classList.add('cancel')
statusSpan.setAttribute('style', 'margin-right: 6px; color: #ff0000;')
statusText.innerText = translate('no_theme_song')
}
tableColumnStatus.appendChild(statusSpan)
tableColumnStatus.appendChild(statusText)
tableRow.appendChild(tableColumnStatus)
createDashboard(total_media_count, total_media_with_themes, all_items);
}

function createDashboard(total_media_count, total_media_with_themes, all_items) {
// get dashboard container
let ThemerrDashboard = document.querySelector('#ThemerrDashboard')

// create progress bar
let progressBar = document.createElement('div')
progressBar.setAttribute('is', 'emby-progressbar')
progressBar.setAttribute('class', 'itemProgressBar')
progressBar.setAttribute('style', 'height: 20px;')
ThemerrDashboard.appendChild(progressBar)

// calculate percent complete
let percentComplete = 0
if (total_media_count > 0) {
percentComplete = Math.round((total_media_with_themes / total_media_count) * 100)
}

// progress bar foreground
let progressBarForeground = document.createElement('div')
progressBarForeground.setAttribute('class', 'itemProgressBarForeground')
progressBarForeground.setAttribute('style', 'width: ' + percentComplete + '%;')
progressBar.appendChild(progressBarForeground)

// progress bar text
let progressBarText = document.createElement('span')
progressBarText.setAttribute('style', 'margin-left: 0.5em;')
progressBarText.innerText = percentComplete + '%'
progressBarForeground.appendChild(progressBarText)

// add break
ThemerrDashboard.appendChild(document.createElement('br'))

// create table
let table = document.createElement('table')
table.setAttribute('id', 'ThemerrProgressTable')
table.setAttribute('class', 'detailTable')
ThemerrDashboard.appendChild(table)

// create table header
let tableHeader = document.createElement('thead')
table.appendChild(tableHeader)

// create table header row
let tableHeaderRow = document.createElement('tr')
tableHeader.appendChild(tableHeaderRow)

let columns = ['Title', 'Year', 'Type', 'Contribute', 'Status']

// create table header columns
for (let column in columns) {
let tableHeaderColumn = document.createElement('th')
tableHeaderColumn.setAttribute('class', 'detailTableHeaderCell')
tableHeaderColumn.setAttribute('scope', 'col')
tableHeaderColumn.innerText = translate(columns[column].toLowerCase())
tableHeaderRow.appendChild(tableHeaderColumn)
}

// loop over items
for (let item in all_items) {
console.log(LogPrefix + "------------------")
console.log(LogPrefix + "item: " + item)
console.log(LogPrefix + "name: " + all_items[item]["name"])
console.log(LogPrefix + "id: " + all_items[item]["id"])
console.log(LogPrefix + "issue_url: " + all_items[item]["issue_url"])
console.log(LogPrefix + "type: " + all_items[item]["type"])
console.log(LogPrefix + "theme_provider: " + all_items[item]["theme_provider"])
console.log(LogPrefix + "year: " + all_items[item]["year"])

// create table row
let tableRow = document.createElement('tr')
table.appendChild(tableRow)

// create table columns
let tableColumnTitle = document.createElement('td')
tableColumnTitle.setAttribute('class', 'detailTableBodyCell')
tableColumnTitle.innerText = all_items[item]["name"]
tableRow.appendChild(tableColumnTitle)

let tableColumnYear = document.createElement('td')
tableColumnYear.setAttribute('class', 'detailTableBodyCell')
tableColumnYear.innerText = all_items[item]["year"]
tableRow.appendChild(tableColumnYear)

let tableColumnType = document.createElement('td')
tableColumnType.setAttribute('class', 'detailTableBodyCell')
tableColumnType.innerText = translate(all_items[item]["type"].toLowerCase())
tableRow.appendChild(tableColumnType)

let contributeButton = document.createElement('a')
contributeButton.setAttribute('is', 'emby-linkbutton')
contributeButton.setAttribute('class', 'raised headerHelpButton button-submit emby-button')
contributeButton.setAttribute('target', '_blank')
contributeButton.setAttribute('href', all_items[item]["issue_url"])
// text added when status is set

let tableColumnContribute = document.createElement('td')
tableColumnContribute.appendChild(contributeButton)
tableRow.appendChild(tableColumnContribute)

let tableColumnStatus = document.createElement('td')
tableColumnStatus.setAttribute('class', 'detailTableBodyCell')

let statusSpan = document.createElement('span')
let statusText = document.createElement('span')
statusSpan.setAttribute('class', 'material-icons')
statusText.setAttribute('class', 'detailTableBodyCell')
if (all_items[item]["theme_provider"] === 'themerr') {
contributeButton.innerText = translate('edit_button')
statusSpan.classList.add('check')
statusSpan.setAttribute('style', 'margin-right: 6px; color: #00ff00;')
statusText.innerText = translate('themerr_provided')
} else if (all_items[item]["theme_provider"] === 'user') {
contributeButton.innerText = translate('contribute_button')
statusSpan.classList.add('person')
statusSpan.setAttribute('style', 'margin-right: 6px; color: #00a4dc;')
statusText.innerText = translate('user_provided')
} else {
contributeButton.innerText = translate('add_button')
statusSpan.classList.add('cancel')
statusSpan.setAttribute('style', 'margin-right: 6px; color: #ff0000;')
statusText.innerText = translate('no_theme_song')
}
}).catch(function (error) {
let message = translate('unexpected_error_occured_creating_dashboard')
Dashboard.alert({
message: message
})
console.log(LogPrefix + message)
console.log(LogPrefix + error)
})
tableColumnStatus.appendChild(statusSpan)
tableColumnStatus.appendChild(statusText)
tableRow.appendChild(tableColumnStatus)
}
}

function localize() {
Expand Down

0 comments on commit e7aecd8

Please sign in to comment.