feat: add Settings live stream analysis button and diagnose Laya batch failure (#14)

Add a Settings action to manually rerun Live TV stream analysis:
it triggers an admin-guarded projection rebuild endpoint that
groups streams into logical channels (covering channels present
before the projection existed) and reruns language/Laya detection,
with progress presented via liveAnalysisPresentation. Harden the
Laya batch path that failed with 'Local AI language matching
failed' by surfacing the underlying cause and falling back to
standard matching cleanly. Guard admin-only rebuild through a new
authz helper and cover routes, service and presentation with tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
blomios
2026-10-02 16:09:13 +02:00
parent 9116d90526
commit 67d9869364
14 changed files with 386 additions and 25 deletions

View File

@ -87,6 +87,7 @@ class SettingsPage {
});
this.initLayaSettings();
this.initLiveAnalysis();
// EPG refresh interval
const epgRefreshSelect = document.getElementById('epg-refresh-interval');
@ -105,6 +106,110 @@ class SettingsPage {
this.updateEpgLastRefreshed();
}
initLiveAnalysis() {
document.getElementById('live-analysis-button')?.addEventListener('click', () => void this.runLiveAnalysis());
document.getElementById('live-analysis-details-toggle')?.addEventListener('click', () => {
const button = document.getElementById('live-analysis-details-toggle');
const details = document.getElementById('live-analysis-error-details');
const expanded = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', String(!expanded));
details.hidden = expanded;
});
void this.loadLiveAnalysisStatus();
}
async loadLiveAnalysisStatus() {
try {
this.liveAnalysisStatus = await API.live.getProjectionStatus();
} catch (err) {
this.liveAnalysisStatus = { status: 'error', error: err.message };
}
this.renderLiveAnalysisStatus(this.liveAnalysisStatus);
const presentation = LiveAnalysisPresentation.present(this.liveAnalysisStatus);
if (presentation.busy) this.startLiveAnalysisPolling();
return this.liveAnalysisStatus;
}
renderLiveAnalysisStatus(status = {}) {
const button = document.getElementById('live-analysis-button');
const statusElement = document.getElementById('live-analysis-status');
const progress = document.getElementById('live-analysis-progress');
const track = document.getElementById('live-analysis-progress-track');
const bar = document.getElementById('live-analysis-progress-bar');
const progressText = document.getElementById('live-analysis-progress-text');
const fallback = document.getElementById('live-analysis-fallback');
const detailsButton = document.getElementById('live-analysis-details-toggle');
const details = document.getElementById('live-analysis-error-details');
if (!button || !statusElement) return;
const presentation = LiveAnalysisPresentation.present(status);
button.textContent = presentation.buttonLabel;
button.disabled = presentation.busy;
statusElement.textContent = presentation.statusLabel;
progress.hidden = !presentation.busy;
track.classList.toggle('indeterminate', presentation.progressIndeterminate);
if (presentation.progressPercent === null) {
track.removeAttribute('aria-valuemin');
track.removeAttribute('aria-valuemax');
track.removeAttribute('aria-valuenow');
bar.style.width = '';
} else {
track.setAttribute('aria-valuemin', '0');
track.setAttribute('aria-valuemax', '100');
track.setAttribute('aria-valuenow', String(presentation.progressPercent));
bar.style.width = `${presentation.progressPercent}%`;
}
progressText.textContent = presentation.progressText;
track.setAttribute('aria-valuetext', presentation.progressText);
const mode = this.app.player?.settings?.languageDetectionMode || 'algo';
fallback.textContent = LiveAnalysisPresentation.layaFallbackMessage(mode, this.layaStatus);
detailsButton.hidden = !presentation.errorDetail;
detailsButton.setAttribute('aria-expanded', 'false');
details.hidden = true;
details.textContent = presentation.errorDetail;
}
async runLiveAnalysis() {
const button = document.getElementById('live-analysis-button');
if (button?.disabled) return;
this.liveAnalysisStatus = { ...(this.liveAnalysisStatus || {}), status: 'queued', processed: 0 };
this.renderLiveAnalysisStatus(this.liveAnalysisStatus);
try {
const response = await API.live.rebuildProjection();
this.liveAnalysisStatus = response.projection || response;
this.renderLiveAnalysisStatus(this.liveAnalysisStatus);
this.startLiveAnalysisPolling();
} catch (err) {
this.liveAnalysisStatus = { status: 'error', error: err.message };
this.renderLiveAnalysisStatus(this.liveAnalysisStatus);
}
}
startLiveAnalysisPolling() {
if (this.liveAnalysisPollTimer) return;
this.liveAnalysisPollTimer = setInterval(async () => {
if (this.liveAnalysisPollInFlight) return;
this.liveAnalysisPollInFlight = true;
try {
const status = await this.loadLiveAnalysisStatus();
const presentation = LiveAnalysisPresentation.present(status);
if (!presentation.busy) {
clearInterval(this.liveAnalysisPollTimer);
this.liveAnalysisPollTimer = null;
if (presentation.complete) {
await this.app.channelList?.loadChannels();
const mode = this.app.player?.settings?.languageDetectionMode || 'algo';
if (mode === 'laya' || mode === 'hybrid') {
await this.app.channelList?.refreshLayaLanguages(mode);
}
}
}
} finally {
this.liveAnalysisPollInFlight = false;
}
}, 1000);
}
initLayaSettings() {
const radios = document.querySelectorAll('input[name="language-detection-mode"]');
const currentMode = this.app.player?.settings?.languageDetectionMode || 'algo';
@ -116,6 +221,7 @@ class SettingsPage {
this.app.player.saveSettings();
this.app.channelList.setLanguageDetectionMode(radio.value);
this.renderLayaStatus(this.layaStatus);
this.renderLiveAnalysisStatus(this.liveAnalysisStatus);
});
});
@ -221,6 +327,7 @@ class SettingsPage {
else if (mode !== 'algo' && (!status.modelPresent || !status.runtimeAvailable)) {
messageElement.textContent = 'Local AI is not ready. Download the model and ensure the runtime is running. Live TV will remain available.';
} else messageElement.textContent = '';
if (this.liveAnalysisStatus) this.renderLiveAnalysisStatus(this.liveAnalysisStatus);
}
formatBytes(bytes) {