groups: AI enhance for gallery photos (photo_jobs.group_photo_id, before/after compare modal)
This commit is contained in:
@@ -52,10 +52,10 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
const DEFAULT_MODEL = String(defaultModel || PHOTO_AI_DEFAULT_MODEL).trim() || PHOTO_AI_DEFAULT_MODEL;
|
||||
const DEFAULT_FACE_MODEL = String(defaultFaceModel || PHOTO_AI_DEFAULT_FACE_MODEL).trim() || PHOTO_AI_DEFAULT_FACE_MODEL;
|
||||
|
||||
async function sendNotification(entryId, payload) {
|
||||
async function sendNotification(job, payload) {
|
||||
if (!notifyEvent) return;
|
||||
try {
|
||||
await notifyEvent(entryId, payload);
|
||||
await notifyEvent(job, payload);
|
||||
} catch (e) {
|
||||
console.error('Photo worker notification failed:', e.message);
|
||||
}
|
||||
@@ -112,7 +112,7 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
const { rows } = await client.query(
|
||||
`SELECT j.id, j.entry_id, j.action, j.params
|
||||
`SELECT j.id, j.entry_id, j.group_photo_id, j.action, j.params
|
||||
FROM photo_jobs j
|
||||
WHERE j.status = 'pending'
|
||||
ORDER BY j.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED`
|
||||
@@ -297,7 +297,8 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
);
|
||||
if (logAudit) {
|
||||
await logAudit(null, 'photo.job.preview', {
|
||||
entry_id: job.entry_id,
|
||||
entry_id: job.entry_id || null,
|
||||
group_photo_id: job.group_photo_id || null,
|
||||
job_id: job.id,
|
||||
action: job.action,
|
||||
after_path: newPath,
|
||||
@@ -310,18 +311,22 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
warnings: m.warnings && m.warnings.length ? m.warnings : null,
|
||||
});
|
||||
}
|
||||
const isGroup = !!job.group_photo_id;
|
||||
const title = job.action === 'ai_face'
|
||||
? 'Фото обработано нейросетью с восстановлением лиц: {student}'
|
||||
? (isGroup ? 'Фото группы обработано нейросетью с восстановлением лиц: {group}' : 'Фото обработано нейросетью с восстановлением лиц: {student}')
|
||||
: job.action === 'ai_upscale'
|
||||
? 'Фото обработано нейросетью (апскейл): {student}'
|
||||
? (isGroup ? 'Фото группы обработано нейросетью (апскейл): {group}' : 'Фото обработано нейросетью (апскейл): {student}')
|
||||
: job.action === 'ai'
|
||||
? 'Фото обработано нейросетью: {student}'
|
||||
: 'Фото улучшено на сервере: {student}';
|
||||
? (isGroup ? 'Фото группы обработано нейросетью: {group}' : 'Фото обработано нейросетью: {student}')
|
||||
: (isGroup ? 'Фото группы улучшено на сервере: {group}' : 'Фото улучшено на сервере: {student}');
|
||||
const warn = m.warnings && m.warnings.length ? ' · ' + m.warnings.join('; ').slice(0, 300) : '';
|
||||
await sendNotification(job.entry_id, {
|
||||
const body = isGroup
|
||||
? `Группа {group} · фото #${job.group_photo_id} · результат ждёт применения${warn}`
|
||||
: `Группа {group} · запись #${job.entry_id} · результат ждёт применения${warn}`;
|
||||
await sendNotification(job, {
|
||||
type: 'photo.job.done',
|
||||
title,
|
||||
body: `Группа {group} · запись #${job.entry_id} · результат ждёт применения${warn}`,
|
||||
body,
|
||||
target: {
|
||||
job_id: job.id,
|
||||
action: job.action,
|
||||
@@ -332,6 +337,8 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
device: m.device,
|
||||
faces_found: m.faces_found,
|
||||
elapsed_ms: m.elapsed_ms,
|
||||
group_photo_id: job.group_photo_id || null,
|
||||
entry_id: job.entry_id || null,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -343,7 +350,8 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
await pool.query(`UPDATE photo_jobs SET status = 'pending', error = $1 WHERE id = $2`, [message, job.id]);
|
||||
if (logAudit && (n === 1 || n % 10 === 0)) {
|
||||
await logAudit(null, 'photo.job.soft_retry', {
|
||||
entry_id: job.entry_id,
|
||||
entry_id: job.entry_id || null,
|
||||
group_photo_id: job.group_photo_id || null,
|
||||
job_id: job.id,
|
||||
soft_attempt: n,
|
||||
soft_limit: PHOTO_SOFT_MAX_RETRIES,
|
||||
@@ -362,37 +370,46 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
stats.errors++;
|
||||
stats.last_error = text;
|
||||
if (logAudit) {
|
||||
await logAudit(null, 'photo.job.error', { entry_id: job.entry_id, job_id: job.id, error: text, soft_attempts: PHOTO_SOFT_MAX_RETRIES });
|
||||
await logAudit(null, 'photo.job.error', { entry_id: job.entry_id || null, group_photo_id: job.group_photo_id || null, job_id: job.id, error: text, soft_attempts: PHOTO_SOFT_MAX_RETRIES });
|
||||
}
|
||||
await sendNotification(job.entry_id, {
|
||||
const isGroup = !!job.group_photo_id;
|
||||
await sendNotification(job, {
|
||||
type: 'photo.job.error',
|
||||
title: 'Ошибка обработки фото: {student}',
|
||||
body: `Группа {group} · запись #${job.entry_id} · ${text}`,
|
||||
target: { job_id: job.id, action: job.action, error: text },
|
||||
title: isGroup ? 'Ошибка обработки фото группы: {group}' : 'Ошибка обработки фото: {student}',
|
||||
body: isGroup ? `Группа {group} · фото #${job.group_photo_id} · ${text}` : `Группа {group} · запись #${job.entry_id} · ${text}`,
|
||||
target: { job_id: job.id, action: job.action, error: text, group_photo_id: job.group_photo_id || null, entry_id: job.entry_id || null },
|
||||
});
|
||||
return { ok: true, delay: 0 };
|
||||
}
|
||||
|
||||
async function processOne(job) {
|
||||
stats.jobs++;
|
||||
const { rows } = await pool.query('SELECT photo_path FROM entries WHERE id = $1', [job.entry_id]);
|
||||
if (!rows.length || !rows[0].photo_path) {
|
||||
let photoPath = null;
|
||||
if (job.group_photo_id) {
|
||||
const { rows } = await pool.query('SELECT photo_path FROM group_photos WHERE id = $1', [job.group_photo_id]);
|
||||
if (rows.length) photoPath = rows[0].photo_path;
|
||||
} else {
|
||||
const { rows } = await pool.query('SELECT photo_path FROM entries WHERE id = $1', [job.entry_id]);
|
||||
if (rows.length) photoPath = rows[0].photo_path;
|
||||
}
|
||||
if (!photoPath) {
|
||||
const errMsg = job.group_photo_id ? 'У фото группы не найден путь' : 'У записи нет фото';
|
||||
await pool.query(
|
||||
`UPDATE photo_jobs SET status = 'error', error = 'У записи нет фото', finished_at = now() WHERE id = $1`,
|
||||
[job.id]
|
||||
`UPDATE photo_jobs SET status = 'error', error = $1, finished_at = now() WHERE id = $2`,
|
||||
[errMsg, job.id]
|
||||
);
|
||||
stats.errors++;
|
||||
stats.last_at = new Date().toISOString();
|
||||
stats.last_error = 'У записи нет фото';
|
||||
await sendNotification(job.entry_id, {
|
||||
stats.last_error = errMsg;
|
||||
const isGroup = !!job.group_photo_id;
|
||||
await sendNotification(job, {
|
||||
type: 'photo.job.error',
|
||||
title: 'Ошибка обработки фото: {student}',
|
||||
body: `Группа {group} · запись #${job.entry_id} · у записи нет фото для обработки`,
|
||||
target: { job_id: job.id, action: job.action },
|
||||
title: isGroup ? 'Ошибка обработки фото группы: {group}' : 'Ошибка обработки фото: {student}',
|
||||
body: isGroup ? `Группа {group} · фото #${job.group_photo_id} · нет фото для обработки` : `Группа {group} · запись #${job.entry_id} · у записи нет фото для обработки`,
|
||||
target: { job_id: job.id, action: job.action, group_photo_id: job.group_photo_id || null, entry_id: job.entry_id || null },
|
||||
});
|
||||
return { ok: true, delay: 0 };
|
||||
}
|
||||
const photoPath = rows[0].photo_path;
|
||||
try {
|
||||
const result = PHOTO_AI_ACTIONS.has(job.action)
|
||||
? await enhanceWithAi(photoPath, job)
|
||||
@@ -412,7 +429,7 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
const tries = ((cur[0] && cur[0].attempts) || 0) + 1;
|
||||
if (tries < PHOTO_MAX_ATTEMPTS) {
|
||||
await pool.query(`UPDATE photo_jobs SET status = 'pending', attempts = $1, error = $2 WHERE id = $3`, [tries, message, job.id]);
|
||||
if (logAudit) await logAudit(null, 'photo.job.retry', { entry_id: job.entry_id, job_id: job.id, attempt: tries, error: message });
|
||||
if (logAudit) await logAudit(null, 'photo.job.retry', { entry_id: job.entry_id || null, group_photo_id: job.group_photo_id || null, job_id: job.id, attempt: tries, error: message });
|
||||
return { ok: false, delay: 0 };
|
||||
}
|
||||
softTries.delete(job.id);
|
||||
@@ -422,11 +439,12 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
);
|
||||
stats.errors++;
|
||||
if (logAudit) await logAudit(null, 'photo.job.error', { entry_id: job.entry_id, job_id: job.id, error: message });
|
||||
await sendNotification(job.entry_id, {
|
||||
const isGroup = !!job.group_photo_id;
|
||||
await sendNotification(job, {
|
||||
type: 'photo.job.error',
|
||||
title: 'Ошибка обработки фото: {student}',
|
||||
body: `Группа {group} · запись #${job.entry_id} · ${message}`,
|
||||
target: { job_id: job.id, action: job.action, error: message },
|
||||
title: isGroup ? 'Ошибка обработки фото группы: {group}' : 'Ошибка обработки фото: {student}',
|
||||
body: isGroup ? `Группа {group} · фото #${job.group_photo_id} · ${message}` : `Группа {group} · запись #${job.entry_id} · ${message}`,
|
||||
target: { job_id: job.id, action: job.action, error: message, group_photo_id: job.group_photo_id || null, entry_id: job.entry_id || null },
|
||||
});
|
||||
return { ok: true, delay: 0 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user