Fix for incorrect stat count

Forgot to add archived items exclusion when tallying stats
This feature has been extended to seasons and seasons/episodes as some episodes may be archived but not the entire show
This commit is contained in:
Thegan Govender
2023-11-26 20:06:33 +02:00
parent 0035a1cc08
commit c284d59454
8 changed files with 174 additions and 8 deletions

View File

@@ -59,16 +59,19 @@ async function deleteBulk(table_name, data) {
return { Result: result, message: '' + message };
}
async function updateSingleFieldBulk(table_name, data,field_name, new_value) {
async function updateSingleFieldBulk(table_name, data,field_name, new_value, where_field) {
const client = await pool.connect();
let result = 'SUCCESS';
let message = '';
if(where_field===undefined || where_field===null || where_field===''){
where_field='Id'
}
try {
await client.query('BEGIN');
if (data && data.length !== 0) {
const updateQuery = {
text: `UPDATE ${table_name} SET "${field_name}"='${new_value}' WHERE "Id" IN (${pgp.as.csv(data)})`,
text: `UPDATE ${table_name} SET "${field_name}"='${new_value}' WHERE "${where_field}" IN (${pgp.as.csv(data)})`,
};
// console.log(deleteQuery);
await client.query(updateQuery);

View File

@@ -0,0 +1,26 @@
exports.up = async function(knex) {
try
{
const hasTable = await knex.schema.hasTable('jf_library_episodes');
if (hasTable) {
await knex.schema.alterTable('jf_library_episodes', function(table) {
table.boolean('archived').defaultTo(false);
});
}
}catch (error) {
console.error(error);
}
};
exports.down = async function(knex) {
try {
await knex.schema.alterTable('jf_library_episodes', function(table) {
table.dropColumn('archived');
});
} catch (error) {
console.error(error);
}
};

View File

@@ -0,0 +1,26 @@
exports.up = async function(knex) {
try
{
const hasTable = await knex.schema.hasTable('jf_library_seasons');
if (hasTable) {
await knex.schema.alterTable('jf_library_seasons', function(table) {
table.boolean('archived').defaultTo(false);
});
}
}catch (error) {
console.error(error);
}
};
exports.down = async function(knex) {
try {
await knex.schema.alterTable('jf_library_seasons', function(table) {
table.dropColumn('archived');
});
} catch (error) {
console.error(error);
}
};

View File

@@ -0,0 +1,39 @@
exports.up = async function(knex) {
try
{
await knex.schema.raw(`
CREATE OR REPLACE VIEW public.js_library_metadata
AS
SELECT l."Id",
l."Name",
sum(ii."Size") AS "Size",
count(*) AS files
FROM jf_libraries l
JOIN jf_library_items i ON i."ParentId" = l."Id" AND i.archived=false
LEFT JOIN jf_library_episodes e ON e."SeriesId" = i."Id" AND e.archived=false
LEFT JOIN jf_item_info ii ON ii."Id" = i."Id" OR ii."Id" = e."EpisodeId"
GROUP BY l."Id", l."Name";`);
}catch (error) {
console.error(error);
}
};
exports.down = async function(knex) {
try {
await knex.schema.raw(`
CREATE OR REPLACE VIEW public.js_library_metadata
AS
SELECT l."Id",
l."Name",
sum(ii."Size") AS "Size",
count(*) AS files
FROM jf_libraries l
JOIN jf_library_items i ON i."ParentId" = l."Id"
LEFT JOIN jf_library_episodes e ON e."SeriesId" = i."Id"
LEFT JOIN jf_item_info ii ON ii."Id" = i."Id" OR ii."Id" = e."EpisodeId"
GROUP BY l."Id", l."Name";`);
} catch (error) {
console.error(error);
}
};

View File

@@ -0,0 +1,45 @@
exports.up = async function(knex) {
try
{
await knex.schema.raw(`
CREATE OR REPLACE VIEW public.jf_library_count_view
AS
SELECT l."Id",
l."Name",
l."CollectionType",
count(DISTINCT i."Id") AS "Library_Count",
count(DISTINCT s."Id") AS "Season_Count",
count(DISTINCT e."Id") AS "Episode_Count"
FROM jf_libraries l
JOIN jf_library_items i ON i."ParentId" = l."Id" AND i.archived=false
LEFT JOIN jf_library_seasons s ON s."SeriesId" = i."Id" AND s.archived=false
LEFT JOIN jf_library_episodes e ON e."SeasonId" = s."Id" AND e.archived=false
GROUP BY l."Id", l."Name"
ORDER BY (count(DISTINCT i."Id")) DESC;`);
}catch (error) {
console.error(error);
}
};
exports.down = async function(knex) {
try {
await knex.schema.raw(`
CREATE OR REPLACE VIEW public.jf_library_count_view
AS
SELECT l."Id",
l."Name",
l."CollectionType",
count(DISTINCT i."Id") AS "Library_Count",
count(DISTINCT s."Id") AS "Season_Count",
count(DISTINCT e."Id") AS "Episode_Count"
FROM jf_libraries l
JOIN jf_library_items i ON i."ParentId" = l."Id"
LEFT JOIN jf_library_seasons s ON s."SeriesId" = i."Id"
LEFT JOIN jf_library_episodes e ON e."SeasonId" = s."Id"
GROUP BY l."Id", l."Name"
ORDER BY (count(DISTINCT i."Id")) DESC;`);
} catch (error) {
console.error(error);
}
};

View File

@@ -19,6 +19,7 @@
"SeasonId",
"SeasonName",
"SeriesName",
"archived",
];
const jf_library_episodes_mapping = (item) => ({
@@ -44,6 +45,7 @@
SeasonId: item.SeasonId,
SeasonName: item.SeasonName,
SeriesName: item.SeriesName,
archived: false,
});
module.exports = {

View File

@@ -11,6 +11,7 @@
"SeriesName",
"SeriesId",
"SeriesPrimaryImageTag",
"archived",
];
const jf_library_seasons_mapping = (item) => ({
@@ -28,6 +29,7 @@
SeriesName: item.SeriesName,
SeriesId: item.SeriesId,
SeriesPrimaryImageTag: item.SeriesPrimaryImageTag ? item.SeriesPrimaryImageTag : null,
archived: false,
});
module.exports = {

View File

@@ -78,9 +78,9 @@ class sync {
}
}
async updateSingleFieldOnDB(tablename,dataToUpdate,field_name,field_value)
async updateSingleFieldOnDB(tablename,dataToUpdate,field_name,field_value,where_field)
{
let result = await db.updateSingleFieldBulk(tablename,dataToUpdate,field_name,field_value);
let result = await db.updateSingleFieldBulk(tablename,dataToUpdate,field_name,field_value,where_field);
if (result.Result === "SUCCESS") {
syncTask.loggedData.push(dataToUpdate.length + " Rows updated.");
} else {
@@ -201,7 +201,7 @@ async function syncLibraryItems(data)
async function syncShowItems(data)
{
const _sync = new sync();
syncTask.loggedData.push({ color: "lawngreen", Message: "Syncing... 2/4" });
sendUpdate(syncTask.wsKey,{type:"Update",message:"Beginning Show Item Sync (2/4)"});
syncTask.loggedData.push({color: "yellow", Message: "Beginning Seasons and Episode sync",});
@@ -286,11 +286,26 @@ async function syncShowItems(data)
}
}
if(syncTask.taskName===taskName.fullsync)
{
let toArchiveSeasons = existingIdsSeasons.filter((id) =>!seasonsToInsert.some((row) => row.Id === id ));
let toArchiveEpisodes = existingIdsEpisodes.filter((EpisodeId) =>!episodesToInsert.some((row) => row.EpisodeId === EpisodeId ));
if (toArchiveSeasons.length > 0) {
await _sync.updateSingleFieldOnDB("jf_library_seasons",toArchiveSeasons,"archived",true);
syncTask.loggedData.push({color: "orange",Message: toArchiveSeasons.length + " Seasons Archived.",});
}
if (toArchiveEpisodes.length > 0) {
await _sync.updateSingleFieldOnDB("jf_library_episodes",toArchiveEpisodes,"archived",true);
syncTask.loggedData.push({color: "orange",Message: toArchiveEpisodes.length + " Episodes Archived.",});
}
}
}
}
syncTask.loggedData.push({color: "dodgerblue",Message: `Seasons: ${insertSeasonsCount} Rows Inserted. ${updateSeasonsCount} Rows Updated.`});
@@ -405,11 +420,19 @@ async function syncItemInfo(seasons_and_episodes,library_items)
async function removeOrphanedData()
{
const _sync = new sync();
syncTask.loggedData.push({ color: "lawngreen", Message: "Syncing... 4/4" });
sendUpdate(syncTask.wsKey,{type:"Update",message:"Cleaning up FileInfo/Episode/Season Records (4/4)"});
syncTask.loggedData.push({color: "yellow", Message: "Removing Orphaned FileInfo/Episode/Season Records",});
await db.query('CALL jd_remove_orphaned_data()');
const archived_items=await db.query(`select "Id" from jf_library_items where archived=true and "Type"='Series'`).then((res) => res.rows.map((row) => row.Id));
const archived_seasons=await db.query(`select "Id" from jf_library_seasons where archived=true`).then((res) => res.rows.map((row) => row.Id));
await _sync.updateSingleFieldOnDB("jf_library_seasons",archived_items,"archived",true,"SeriesId");
await _sync.updateSingleFieldOnDB("jf_library_episodes",archived_items,"archived",true,"SeriesId");
await _sync.updateSingleFieldOnDB("jf_library_episodes",archived_seasons,"archived",true,"SeasonId");
syncTask.loggedData.push({color: "dodgerblue",Message: "Orphaned FileInfo/Episode/Season Removed.",});