Add duplicate credits maintenance and fix dedup logic
This commit is contained in:
1 parent
5a0328a17f
commit
3c089ee110
6 files changed
+188
-5
No files matched your search
@@ -53,6 +53,16 @@ class MaintenanceController {
|
|||||||
const result = await _maintenanceService.fixMissingDirectors();
|
const result = await _maintenanceService.fixMissingDirectors();
|
||||||
return res.status(200).json(result);
|
return res.status(200).json(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getDuplicateCredits(req, res) {
|
||||||
|
const result = await _maintenanceService.cleanDuplicateCredits(true);
|
||||||
|
return res.status(200).json(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
async cleanDuplicateCredits(req, res) {
|
||||||
|
const result = await _maintenanceService.cleanDuplicateCredits();
|
||||||
|
return res.status(200).json(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new MaintenanceController();
|
module.exports = new MaintenanceController();
|
||||||
@@ -46,4 +46,16 @@ maintenanceRouter.post(
|
|||||||
maintenanceController.fixMissingDirectors,
|
maintenanceController.fixMissingDirectors,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// * GET - Duplicate credits count
|
||||||
|
maintenanceRouter.get(
|
||||||
|
"/duplicate-credits",
|
||||||
|
maintenanceController.getDuplicateCredits,
|
||||||
|
);
|
||||||
|
|
||||||
|
// * DELETE - Clean duplicate credits
|
||||||
|
maintenanceRouter.delete(
|
||||||
|
"/duplicate-credits",
|
||||||
|
maintenanceController.cleanDuplicateCredits,
|
||||||
|
);
|
||||||
|
|
||||||
module.exports = maintenanceRouter;
|
module.exports = maintenanceRouter;
|
||||||
@@ -382,9 +382,13 @@ class EntryManager {
|
|||||||
|
|
||||||
const existingCreditsMap = new Map();
|
const existingCreditsMap = new Map();
|
||||||
for (const credit of existingCreditsList) {
|
for (const credit of existingCreditsList) {
|
||||||
// unique key: people_id + cast_type (scoped to entry_id already)
|
// unique key: people_id + cast_type + role (character or job)
|
||||||
|
const roleKey =
|
||||||
|
credit.cast_type === "cast"
|
||||||
|
? credit.character || ""
|
||||||
|
: credit.job || "";
|
||||||
existingCreditsMap.set(
|
existingCreditsMap.set(
|
||||||
`${credit.people_id}_${credit.cast_type}`,
|
`${credit.people_id}_${credit.cast_type}_${roleKey}`,
|
||||||
credit,
|
credit,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -396,9 +400,6 @@ class EntryManager {
|
|||||||
const person = existingPeopleMap[c.id];
|
const person = existingPeopleMap[c.id];
|
||||||
if (!person) continue;
|
if (!person) continue;
|
||||||
|
|
||||||
const key = `${person.id}_${c.cast_type}`;
|
|
||||||
const existingCredit = existingCreditsMap.get(key);
|
|
||||||
|
|
||||||
// For TV series (aggregate_credits), character/job are nested inside roles[]/jobs[].
|
// For TV series (aggregate_credits), character/job are nested inside roles[]/jobs[].
|
||||||
// Detect TV format by presence of roles/jobs arrays rather than relying on is_tvseries.
|
// Detect TV format by presence of roles/jobs arrays rather than relying on is_tvseries.
|
||||||
const character =
|
const character =
|
||||||
@@ -421,6 +422,11 @@ class EntryManager {
|
|||||||
: c.job || null
|
: c.job || null
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
// Role key must match map key: use the resolved character/job (after aggregate handling)
|
||||||
|
const roleKey = c.cast_type === "cast" ? character || "" : job || "";
|
||||||
|
const key = `${person.id}_${c.cast_type}_${roleKey}`;
|
||||||
|
const existingCredit = existingCreditsMap.get(key);
|
||||||
|
|
||||||
const tmdbCreditId = Array.isArray(c.roles)
|
const tmdbCreditId = Array.isArray(c.roles)
|
||||||
? c.roles?.[0]?.credit_id
|
? c.roles?.[0]?.credit_id
|
||||||
: Array.isArray(c.jobs)
|
: Array.isArray(c.jobs)
|
||||||
|
|||||||
@@ -249,6 +249,67 @@ class MaintenanceService {
|
|||||||
|
|
||||||
return { count, fixed };
|
return { count, fixed };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find and optionally remove duplicate credits.
|
||||||
|
* A duplicate is the same person in the same entry with the same role.
|
||||||
|
* For cast: same character. For crew: same job.
|
||||||
|
* @param {boolean} [dry=false] - if true only count, if false also delete
|
||||||
|
*/
|
||||||
|
async cleanDuplicateCredits(dry = false) {
|
||||||
|
const { Sequelize } = require("sequelize");
|
||||||
|
|
||||||
|
const duplicates = await Credit.findAll({
|
||||||
|
attributes: [
|
||||||
|
"entry_id",
|
||||||
|
"people_id",
|
||||||
|
"cast_type",
|
||||||
|
"character",
|
||||||
|
"job",
|
||||||
|
[Sequelize.fn("COUNT", Sequelize.col("id")), "cnt"],
|
||||||
|
[Sequelize.fn("GROUP_CONCAT", Sequelize.col("id")), "ids"],
|
||||||
|
],
|
||||||
|
group: ["entry_id", "people_id", "cast_type", "character", "job"],
|
||||||
|
having: Sequelize.literal("COUNT(id) > 1"),
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
let duplicateGroups = duplicates.length;
|
||||||
|
let deletedCount = 0;
|
||||||
|
|
||||||
|
if (!dry) {
|
||||||
|
for (const row of duplicates) {
|
||||||
|
const idList = row.ids
|
||||||
|
.split(",")
|
||||||
|
.map(Number)
|
||||||
|
.sort((a, b) => a - b);
|
||||||
|
// Keep the newest (highest ID), delete the older ones
|
||||||
|
const toDelete = idList.slice(0, -1);
|
||||||
|
await Credit.destroy({ where: { id: toDelete } });
|
||||||
|
deletedCount += toDelete.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalDupes = duplicates.reduce(
|
||||||
|
(sum, r) => sum + parseInt(r.cnt) - 1,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (dry)
|
||||||
|
logToFile(
|
||||||
|
`Dry duplicate credits check: ${duplicateGroups} groups, ${totalDupes} duplicates`,
|
||||||
|
);
|
||||||
|
else
|
||||||
|
logToFile(
|
||||||
|
`Cleaned duplicate credits: ${deletedCount} removed from ${duplicateGroups} groups`,
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
groups: duplicateGroups,
|
||||||
|
duplicates: totalDupes,
|
||||||
|
deleted: deletedCount,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new MaintenanceService();
|
module.exports = new MaintenanceService();
|
||||||
@@ -47,4 +47,22 @@ export const maintenanceApi = {
|
|||||||
api
|
api
|
||||||
.post<{ count: number; fixed: number }>("/maintenance/missing-directors")
|
.post<{ count: number; fixed: number }>("/maintenance/missing-directors")
|
||||||
.then((r) => r.data),
|
.then((r) => r.data),
|
||||||
|
|
||||||
|
scanDuplicateCredits: () =>
|
||||||
|
api
|
||||||
|
.get<{
|
||||||
|
groups: number;
|
||||||
|
duplicates: number;
|
||||||
|
deleted: number;
|
||||||
|
}>("/maintenance/duplicate-credits")
|
||||||
|
.then((r) => r.data),
|
||||||
|
|
||||||
|
cleanDuplicateCredits: () =>
|
||||||
|
api
|
||||||
|
.delete<{
|
||||||
|
groups: number;
|
||||||
|
duplicates: number;
|
||||||
|
deleted: number;
|
||||||
|
}>("/maintenance/duplicate-credits")
|
||||||
|
.then((r) => r.data),
|
||||||
};
|
};
|
||||||
@@ -98,6 +98,68 @@ function MissingDirectorsSection() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function DuplicateCreditsSection() {
|
||||||
|
const [result, setResult] = useState<{
|
||||||
|
groups: number;
|
||||||
|
duplicates: number;
|
||||||
|
deleted: number;
|
||||||
|
} | null>(null);
|
||||||
|
|
||||||
|
const scan = useMutation({
|
||||||
|
mutationFn: maintenanceApi.scanDuplicateCredits,
|
||||||
|
onSuccess: (data) => setResult(data),
|
||||||
|
});
|
||||||
|
|
||||||
|
const clean = useMutation({
|
||||||
|
mutationFn: maintenanceApi.cleanDuplicateCredits,
|
||||||
|
onSuccess: (data) => setResult(data),
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => scan.mutate()}
|
||||||
|
disabled={scan.isPending}
|
||||||
|
>
|
||||||
|
{scan.isPending ? (
|
||||||
|
<Loader2 className="mr-1.5 h-3.5 w-3.5 animate-spin" />
|
||||||
|
) : (
|
||||||
|
<Search className="mr-1.5 h-3.5 w-3.5" />
|
||||||
|
)}
|
||||||
|
Scan
|
||||||
|
</Button>
|
||||||
|
{result && result.duplicates > 0 && (
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => clean.mutate()}
|
||||||
|
disabled={clean.isPending}
|
||||||
|
>
|
||||||
|
{clean.isPending ? (
|
||||||
|
<Loader2 className="mr-1.5 h-3.5 w-3.5 animate-spin" />
|
||||||
|
) : (
|
||||||
|
<Trash2 className="mr-1.5 h-3.5 w-3.5" />
|
||||||
|
)}
|
||||||
|
Clean
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{result && (
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{result.duplicates === 0
|
||||||
|
? "No duplicate credits found."
|
||||||
|
: result.deleted > 0
|
||||||
|
? `Removed ${result.deleted} duplicates across ${result.groups} groups.`
|
||||||
|
: `${result.duplicates} duplicates across ${result.groups} groups.`}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function SettingsPage() {
|
export function SettingsPage() {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const { data: settings, isLoading } = useQuery({
|
const { data: settings, isLoading } = useQuery({
|
||||||
@@ -320,6 +382,20 @@ export function SettingsPage() {
|
|||||||
<MissingDirectorsSection />
|
<MissingDirectorsSection />
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
{/* Duplicate Credits */}
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Duplicate Credits</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Find and remove duplicate credits where the same person appears
|
||||||
|
multiple times with the same role on the same entry.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<DuplicateCreditsSection />
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user