import { version as discordJsVersion } from 'discord.js'; import { ACCESS_LEVELS } from '../../config/constants.js'; import { env } from '../../config/env.js'; import { getDatabase } from '../../database/index.js'; import { countByStatus, lastDeploymentPerEnvironment, } from '../../database/repositories/deploy-repository.js'; import { countByAction } from '../../database/repositories/moderation-repository.js'; import { countPolls } from '../../database/repositories/poll-repository.js'; import { countOpenTickets } from '../../database/repositories/ticket-repository.js'; import { countVoiceChannels } from '../../database/repositories/voice-repository.js'; import { isGiteaConfigured } from '../../services/gitea/gitea-client.js'; import { org } from '../../services/gitea/gitea-admin.js'; /** Aggregated landing page of the dashboard (M16-T05). */ const guildId = () => env.discord.guildId; function botSnapshot(client, startedAt) { const guild = client?.guilds?.cache?.get(guildId()) ?? null; return { online: Boolean(client?.isReady?.()), username: client?.user?.username ?? null, discriminator: client?.user?.discriminator ?? null, avatar: client?.user?.displayAvatarURL?.({ size: 128 }) ?? null, ping: client?.ws?.ping ?? null, uptimeMs: Date.now() - startedAt, nodeVersion: process.version, discordJsVersion, memoryMb: Math.round(process.memoryUsage().rss / 1024 / 1024), commands: client?.commands?.size ?? 0, guild: guild ? { id: guild.id, name: guild.name, icon: guild.iconURL?.({ size: 128 }) ?? null, memberCount: guild.memberCount, channelCount: guild.channels.cache.size, roleCount: guild.roles.cache.size, boostTier: guild.premiumTier, createdAt: guild.createdAt?.toISOString() ?? null, } : null, }; } function databaseSnapshot() { const database = getDatabase(); const tables = database .prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name") .all() .map((row) => row.name); const counts = Object.fromEntries( tables.map((table) => [table, database.prepare(`SELECT COUNT(*) AS count FROM "${table}"`).get().count]), ); const pageCount = database.prepare('PRAGMA page_count').get(); const pageSize = database.prepare('PRAGMA page_size').get(); return { path: env.database.path, tables: counts, sizeBytes: (pageCount?.page_count ?? 0) * (pageSize?.page_size ?? 0), migrations: database.prepare('SELECT name, applied_at FROM schema_migrations ORDER BY name').all(), }; } async function giteaSnapshot() { if (!isGiteaConfigured()) return { configured: false }; const [organization, repositories] = await Promise.all([ org.get().catch(() => null), org.listRepos({ limit: 50 }).catch(() => []), ]); const totals = repositories.reduce( (accumulator, repository) => ({ openIssues: accumulator.openIssues + (repository.open_issues_count ?? 0), openPulls: accumulator.openPulls + (repository.open_pr_counter ?? 0), stars: accumulator.stars + (repository.stars_count ?? 0), forks: accumulator.forks + (repository.forks_count ?? 0), sizeKb: accumulator.sizeKb + (repository.size ?? 0), }), { openIssues: 0, openPulls: 0, stars: 0, forks: 0, sizeKb: 0 }, ); return { configured: true, url: env.gitea.url, organization: organization && { name: organization.username ?? organization.name, fullName: organization.full_name, description: organization.description, website: organization.website, location: organization.location, avatar: organization.avatar_url, visibility: organization.visibility, }, repositoryCount: repositories.length, ...totals, repositories: repositories .slice() .sort((a, b) => new Date(b.updated_at) - new Date(a.updated_at)) .slice(0, 6) .map((repository) => ({ fullName: repository.full_name, description: repository.description, private: repository.private, archived: repository.archived, stars: repository.stars_count, openIssues: repository.open_issues_count, openPulls: repository.open_pr_counter, updatedAt: repository.updated_at, htmlUrl: repository.html_url, })), }; } function recentEvents(limit = 15) { return getDatabase() .prepare('SELECT id, type, actor_id, payload, created_at FROM event_logs ORDER BY id DESC LIMIT ?') .all(limit) .map((row) => ({ ...row, payload: row.payload ? JSON.parse(row.payload) : null })); } export function registerOverviewRoutes(router) { router.get( '/api/overview', async ({ context }) => { const id = guildId(); const gitea = await giteaSnapshot(); return { bot: botSnapshot(context.client, context.startedAt), gitea, database: databaseSnapshot(), activity: { openTickets: id ? countOpenTickets(id) : 0, polls: id ? countPolls(id) : 0, voiceChannels: id ? countVoiceChannels(id) : 0, moderation: id ? countByAction(id) : [], deployments: id ? countByStatus(id) : [], environments: id ? lastDeploymentPerEnvironment(id) : [], }, events: recentEvents(), features: { webhooks: env.webhook.enabled, commandDeploys: env.web.allowCommandDeploys, restart: env.web.allowRestart, gitea: isGiteaConfigured(), }, links: env.links, }; }, { access: ACCESS_LEVELS.STAFF }, ); }