Files
orion/app/modules/monitoring/static/admin/js/background-tasks.js
Samir Boulahtit 0b4291d893 refactor(js): migrate JavaScript files to module directories
Move 47 JS files from static/{admin,vendor,shared}/js/ to their
respective module directories app/modules/*/static/*/js/:

- Orders: orders.js, order-detail.js
- Catalog: products.js (renamed from vendor-products.js), product-*.js
- Inventory: inventory.js (admin & vendor)
- Customers: customers.js, users.js, user-*.js
- Billing: billing-history.js, subscriptions.js, subscription-tiers.js,
  billing.js, invoices.js, feature-store.js, upgrade-prompts.js
- Messaging: messages.js, notifications.js, email-templates.js
- Marketplace: marketplace*.js, letzshop*.js, onboarding.js
- Monitoring: monitoring.js, background-tasks.js, imports.js, logs.js
- Dev Tools: testing-*.js, code-quality-*.js

Update 39 templates to reference new module static paths using
url_for('{module}_static', path='...') pattern.

Files staying in static/ (platform core):
- admin: dashboard, login, platforms, vendors, companies, admin-users,
  settings, components, init-alpine, module-config
- vendor: dashboard, login, profile, settings, team, media, init-alpine
- shared: api-client, utils, money, icons, log-config, vendor-selector,
  media-picker

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 22:08:20 +01:00

138 lines
4.3 KiB
JavaScript

// noqa: js-006 - async init pattern is safe, loadData has try/catch
/**
* Background Tasks Monitoring Component
* Manages the background tasks monitoring page
*/
// Use centralized logger
const backgroundTasksLog = window.LogConfig.createLogger('BACKGROUND-TASKS');
function backgroundTasks() {
return {
// Extend base data
...data(),
// Set current page for navigation
currentPage: 'background-tasks',
// Page-specific data
loading: false,
error: null,
filterType: null,
pollInterval: null,
// Statistics
stats: {
total_tasks: 0,
running: 0,
completed: 0,
failed: 0,
tasks_today: 0,
avg_duration_seconds: null,
import_jobs: {},
test_runs: {}
},
// Tasks
tasks: [],
runningTasks: [],
async init() {
// Guard against multiple initialization
if (window._adminBackgroundTasksInitialized) return;
window._adminBackgroundTasksInitialized = true;
try {
backgroundTasksLog.info('Initializing background tasks monitor');
await this.loadStats();
await this.loadTasks();
await this.loadRunningTasks();
// Poll for updates every 5 seconds
this.pollInterval = setInterval(() => {
this.loadRunningTasks();
if (this.runningTasks.length > 0) {
this.loadStats();
}
}, 5000);
} catch (error) {
backgroundTasksLog.error('Failed to initialize background tasks:', error);
}
},
destroy() {
if (this.pollInterval) {
clearInterval(this.pollInterval);
}
},
async loadStats() {
try {
const stats = await apiClient.get('/admin/background-tasks/tasks/stats');
this.stats = stats;
backgroundTasksLog.info('Stats loaded:', stats);
} catch (err) {
backgroundTasksLog.error('Failed to load stats:', err);
}
},
async loadTasks() {
this.loading = true;
this.error = null;
try {
let url = '/admin/background-tasks/tasks?limit=50';
if (this.filterType) {
url += `&task_type=${this.filterType}`;
}
const tasks = await apiClient.get(url);
this.tasks = tasks;
backgroundTasksLog.info('Tasks loaded:', tasks.length);
} catch (err) {
backgroundTasksLog.error('Failed to load tasks:', err);
this.error = err.message;
if (err.message.includes('Unauthorized')) {
window.location.href = '/admin/login';
}
} finally {
this.loading = false;
}
},
async loadRunningTasks() {
try {
const running = await apiClient.get('/admin/background-tasks/tasks/running');
this.runningTasks = running;
// Update elapsed time for running tasks
const now = new Date();
this.runningTasks.forEach(task => {
if (task.started_at) {
const started = new Date(task.started_at);
task.duration_seconds = (now - started) / 1000;
}
});
} catch (err) {
backgroundTasksLog.error('Failed to load running tasks:', err);
}
},
async refresh() {
await this.loadStats();
await this.loadTasks();
await this.loadRunningTasks();
},
formatDuration(seconds) {
if (seconds === null || seconds === undefined) return 'N/A';
if (seconds < 1) return `${Math.round(seconds * 1000)}ms`;
if (seconds < 60) return `${Math.round(seconds)}s`;
const minutes = Math.floor(seconds / 60);
const secs = Math.round(seconds % 60);
return `${minutes}m ${secs}s`;
}
};
}