import { api } from '../core/api.js'; import { autoRefresh } from '../core/live.js'; import { formatBytes, formatDuration, formatRelative, h } from '../core/dom.js'; import { badge, card, grid, renderAsync, stat, table } from '../core/ui.js'; /** Landing page: state of the bot, the organization and the last deployments. */ const TONE = { success: 'success', failed: 'danger', running: 'warning', pending: 'neutral', cancelled: 'neutral', }; const EVENT_LABELS = { 'web.login': 'Connexion au tableau de bord', 'web.release.create': 'Release publiée', 'deploy.success': 'Déploiement réussi', 'deploy.failed': 'Déploiement en échec', 'web.repository.create': 'Dépôt créé', 'web.repository.delete': 'Dépôt supprimé', 'web.announce': 'Annonce publiée', 'ticket.close': 'Ticket clôturé', }; const describeEvent = (type) => EVENT_LABELS[type] ?? type; function botCard(bot) { return card( 'Bot', { subtitle: bot.online ? `Connecté en tant que ${bot.username}` : 'Hors ligne' }, grid( stat('État', bot.online ? 'En ligne' : 'Hors ligne', { tone: bot.online ? 'success' : 'danger' }), stat('Latence', bot.ping === null ? '—' : `${bot.ping} ms`), stat('Actif depuis', formatDuration(bot.uptimeMs)), stat('Commandes', bot.commands), ), h( 'div', { class: 'row advanced-only' }, badge(`Node ${bot.nodeVersion}`), badge(`discord.js ${bot.discordJsVersion}`), badge(`${bot.memoryMb} Mo de mémoire`), ), ); } function guildCard(guild) { if (!guild) return card('Serveur Discord', {}, h('p', { class: 'empty', text: 'Serveur introuvable.' })); return card( 'Serveur Discord', { subtitle: guild.name }, grid( stat('Membres', guild.memberCount, { tone: 'primary' }), stat('Salons', guild.channelCount), stat('Rôles', guild.roleCount), stat('Boosts', `Niveau ${guild.boostTier}`), ), ); } function giteaCard(gitea) { if (!gitea.configured) { return card( 'Organisation', {}, h('p', { class: 'empty', text: 'Gitea n’est pas configuré. Renseigne GITEA_URL et GITEA_TOKEN.' }), ); } return card( 'Organisation', { subtitle: gitea.organization?.fullName || gitea.organization?.name || '—' }, grid( stat('Dépôts', gitea.repositoryCount, { tone: 'primary' }), stat('Issues ouvertes', gitea.openIssues, { tone: gitea.openIssues > 0 ? 'warning' : 'neutral' }), stat('PR ouvertes', gitea.openPulls), stat('Étoiles', gitea.stars), ), h('h3', { text: 'Dépôts récemment actifs' }), table( [ { label: 'Dépôt', render: (repository) => h('a', { href: `#/repos/${repository.fullName}`, text: repository.fullName }), }, { label: 'Issues', className: 'num', render: (repository) => repository.openIssues ?? 0 }, { label: 'PR', className: 'num', render: (repository) => repository.openPulls ?? 0 }, { label: 'Mise à jour', className: 'nowrap', render: (repository) => formatRelative(repository.updatedAt), }, ], gitea.repositories, { rowHref: (repository) => `#/repos/${repository.fullName}` }, ), ); } function activityCard(activity) { const deployTotals = Object.fromEntries(activity.deployments.map((row) => [row.status, row.count])); return card( 'Activité', {}, grid( stat('Tickets ouverts', activity.openTickets, { tone: activity.openTickets > 0 ? 'warning' : 'neutral', }), stat('Sondages', activity.polls), stat('Salons vocaux', activity.voiceChannels), stat('Déploiements réussis', deployTotals.success ?? 0, { tone: 'success' }), stat('Déploiements en échec', deployTotals.failed ?? 0, { tone: deployTotals.failed ? 'danger' : 'neutral', }), ), activity.environments.length > 0 && h( 'div', { class: 'stack' }, h('h3', { text: 'Dernier déploiement par environnement' }), table( [ { label: 'Environnement', key: 'environment' }, { label: 'Dépôt', key: 'repository' }, { label: 'Version', render: (row) => row.release_tag ?? '—' }, { label: 'État', render: (row) => badge(row.status, TONE[row.status] ?? 'neutral') }, { label: 'Quand', className: 'nowrap', render: (row) => formatRelative(row.finished_at) }, ], activity.environments, ), ), ); } function systemCard(database, features) { return card( 'Système', { advanced: true, subtitle: database.path }, grid( stat('Taille de la base', formatBytes(database.sizeBytes)), stat('Migrations', database.migrations.length), stat('Webhooks', features.webhooks ? 'Activés' : 'Désactivés', { tone: features.webhooks ? 'success' : 'neutral', }), stat('Gitea', features.gitea ? 'Connecté' : 'Non configuré', { tone: features.gitea ? 'success' : 'warning', }), ), table( [ { label: 'Table', key: 'name' }, { label: 'Lignes', className: 'num', key: 'count' }, ], Object.entries(database.tables) .map(([name, count]) => ({ name, count })) .sort((a, b) => b.count - a.count), ), ); } export function render(host, { state }) { autoRefresh(() => render(host, { state }), { topics: ['tick', 'mutation', 'deployments', 'gitea', 'discord', 'community'], }); return renderAsync( host, () => api.get('/api/overview'), (data) => h( 'div', { class: 'stack' }, h( 'div', { class: 'page-head' }, h( 'div', {}, h('h1', { text: 'Vue d’ensemble' }), h('p', { class: 'muted', text: `Bonjour ${state.me.displayName ?? ''} — voici l’état de FWS en un coup d’œil.`, }), ), ), botCard(data.bot), guildCard(data.bot.guild), giteaCard(data.gitea), activityCard(data.activity), systemCard(data.database, data.features), card( 'Derniers événements', { advanced: true }, table( [ { label: 'Événement', render: (event) => describeEvent(event.type) }, { label: 'Type', className: 'advanced-only', key: 'type' }, { label: 'Auteur', render: (event) => (event.actor_id ? `<@${event.actor_id}>` : '—') }, { label: 'Quand', className: 'nowrap', render: (event) => formatRelative(event.created_at) }, ], data.events, ), ), ), ); }