agnes-the-ai-analyst/app/web/templates/admin_tables.html
ZdenekSrotyr 1824b9dd9c
feat(admin): #108 M1 — BigQuery table registration in UI + CLI (#119)
Issue #108 Milestone 1. Adds BigQuery table registration via /admin/tables UI and `da admin register-table` CLI without hand-editing table_registry. POST /api/admin/register-table/precheck for round-trip validation. --dry-run flag on CLI. Audit-log entries on register/update/unregister. PUT /api/admin/registry/{id} now preserves registered_at (closes #130).
2026-04-29 13:18:31 +02:00

1629 lines
63 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table Management - {{ config.INSTANCE_NAME }}</title>
{% if not config.THEME_FONT_URL %}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
{% endif %}
<link rel="stylesheet" href="{{ url_for('static', filename='style-custom.css') }}">
<style>
:root {
/* Colors - Design System */
--primary: #0073D1;
--primary-light: rgba(0, 115, 209, 0.1);
--text-primary: #1A253C;
--text-secondary: #6B7280;
--background: #F5F7FA;
--surface: #FFFFFF;
--border: #E5E7EB;
--border-light: #F3F4F6;
--success: #10B77F;
--success-light: rgba(16, 183, 127, 0.1);
--warning: #F59F0A;
--error: #EA580C;
--error-light: rgba(234, 88, 12, 0.1);
/* Typography */
--font-primary: 'Inter', system-ui, sans-serif;
--font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
/* Shadows */
--shadow-sm: rgba(0, 0, 0, 0.05) 0px 1px 2px 0px;
--shadow-md: rgba(0, 0, 0, 0.1) 0px 4px 6px -1px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-primary);
font-size: 14px;
color: var(--text-primary);
background: var(--background);
line-height: 1.5;
}
/* ── Header (dashboard-style) ── */
.header {
background: var(--surface);
border-bottom: 1px solid var(--border);
padding: 0 32px;
height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
position: sticky;
top: 0;
z-index: 100;
}
.header-left {
display: flex;
align-items: center;
gap: 16px;
}
.header-back {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border-radius: 6px;
color: var(--text-secondary);
text-decoration: none;
transition: all 0.15s ease;
}
.header-back:hover {
background: var(--border-light);
color: var(--text-primary);
}
.header-logo-group {
display: flex;
flex-direction: column;
justify-content: center;
gap: 2px;
}
.header-logo svg {
display: block;
}
.header-subtitle {
font-size: 11px;
font-weight: 500;
color: var(--text-secondary);
letter-spacing: 0.4px;
text-transform: uppercase;
margin-top: 2px;
}
.header-right {
font-size: 12px;
color: var(--text-secondary);
}
/* ── Page Title ── */
.page-title {
max-width: 1000px;
margin: 0 auto;
padding: 32px 24px 24px;
}
.page-title h1 {
font-size: 24px;
font-weight: 600;
color: var(--text-primary);
margin-bottom: 4px;
}
.page-title p {
font-size: 14px;
color: var(--text-secondary);
}
/* ── Content Layout ── */
.content {
max-width: 1000px;
margin: 0 auto;
padding: 0 24px 32px;
display: flex;
flex-direction: column;
gap: 24px;
}
/* ── Panel (shared card style) ── */
.panel {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 12px;
overflow: hidden;
box-shadow: var(--shadow-sm);
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px 24px;
border-bottom: 1px solid var(--border-light);
}
.panel-header-left {
display: flex;
align-items: center;
gap: 12px;
}
.panel-header-icon {
width: 36px;
height: 36px;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
background: var(--primary-light);
}
.panel-title {
font-size: 16px;
font-weight: 600;
color: var(--text-primary);
}
.panel-subtitle {
font-size: 13px;
color: var(--text-secondary);
margin-top: 1px;
}
.panel-body {
padding: 20px 24px;
}
.panel-body-empty {
padding: 40px 24px;
text-align: center;
color: var(--text-secondary);
font-size: 13px;
}
/* ── Buttons ── */
.btn {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 8px 16px;
border: none;
border-radius: 8px;
font-family: var(--font-primary);
font-size: 13px;
font-weight: 500;
cursor: pointer;
transition: all 0.15s ease;
white-space: nowrap;
}
.btn-primary {
background: var(--primary);
color: white;
}
.btn-primary:hover {
background: #005FA8;
}
.btn-primary:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.btn-secondary {
background: var(--border-light);
color: var(--text-primary);
}
.btn-secondary:hover {
background: var(--border);
}
.btn-danger {
background: var(--error-light);
color: var(--error);
}
.btn-danger:hover {
background: rgba(234, 88, 12, 0.2);
}
.btn-sm {
padding: 5px 10px;
font-size: 12px;
border-radius: 6px;
}
.btn-icon {
width: 28px;
height: 28px;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
border-radius: 6px;
background: none;
border: none;
cursor: pointer;
color: var(--text-secondary);
transition: all 0.15s ease;
}
.btn-icon:hover {
background: var(--border-light);
color: var(--text-primary);
}
.btn-icon.danger:hover {
background: var(--error-light);
color: var(--error);
}
/* ── Badges ── */
.badge {
flex-shrink: 0;
font-size: 11px;
font-weight: 500;
border-radius: 6px;
padding: 3px 8px;
white-space: nowrap;
}
.badge-registered {
color: #065F46;
background: #D1FAE5;
}
.badge-available {
color: var(--primary);
background: var(--primary-light);
}
/* ── Spinner ── */
.spinner {
display: inline-block;
width: 18px;
height: 18px;
border: 2px solid var(--border);
border-top-color: var(--primary);
border-radius: 50%;
animation: spin 0.6s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner-lg {
width: 32px;
height: 32px;
border-width: 3px;
}
/* ── Loading state ── */
.loading-state {
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
padding: 40px 24px;
color: var(--text-secondary);
font-size: 13px;
}
/* ── Notification toast ── */
.toast {
position: fixed;
top: 84px;
right: 24px;
z-index: 200;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
box-shadow: var(--shadow-md);
padding: 12px 16px;
display: flex;
align-items: center;
gap: 10px;
font-size: 13px;
transform: translateX(120%);
transition: transform 0.3s ease;
max-width: 360px;
}
.toast.visible {
transform: translateX(0);
}
.toast-success {
border-left: 3px solid var(--success);
}
.toast-error {
border-left: 3px solid var(--error);
}
.toast-icon {
width: 20px;
height: 20px;
flex-shrink: 0;
}
/* ── Bucket accordion ── */
.bucket-group {
border-top: 1px solid var(--border-light);
}
.bucket-group:first-child {
border-top: none;
}
.bucket-trigger {
width: 100%;
display: flex;
align-items: center;
gap: 10px;
padding: 12px 24px;
background: none;
border: none;
cursor: pointer;
font-family: var(--font-primary);
font-size: 14px;
font-weight: 500;
color: var(--text-primary);
text-align: left;
transition: background 0.1s ease;
}
.bucket-trigger:hover {
background: var(--border-light);
}
.bucket-chevron {
width: 16px;
height: 16px;
color: var(--text-secondary);
transition: transform 0.2s ease;
flex-shrink: 0;
}
.bucket-trigger.expanded .bucket-chevron {
transform: rotate(90deg);
}
.bucket-count {
font-size: 11px;
font-weight: 500;
color: var(--text-secondary);
background: var(--border-light);
padding: 1px 7px;
border-radius: 9999px;
margin-left: auto;
}
.bucket-content {
display: none;
}
.bucket-content.expanded {
display: block;
}
/* ── Table row (discovery results) ── */
.table-item {
display: flex;
align-items: center;
padding: 10px 24px 10px 50px;
border-top: 1px solid var(--border-light);
gap: 12px;
transition: background 0.1s ease;
}
.table-item:hover {
background: rgba(243, 244, 246, 0.5);
}
.table-item-info {
flex: 1;
min-width: 0;
}
.table-item-name {
font-weight: 500;
font-family: var(--font-mono);
font-size: 13px;
color: var(--text-primary);
}
.table-item-meta {
font-size: 12px;
color: var(--text-secondary);
margin-top: 1px;
display: flex;
gap: 12px;
flex-wrap: wrap;
}
.table-item-actions {
display: flex;
align-items: center;
gap: 8px;
flex-shrink: 0;
}
/* ── Registry table ── */
.registry-table {
width: 100%;
border-collapse: collapse;
}
.registry-table th {
text-align: left;
font-size: 11px;
font-weight: 600;
color: var(--text-secondary);
text-transform: uppercase;
letter-spacing: 0.4px;
padding: 10px 16px;
border-bottom: 1px solid var(--border);
background: var(--background);
}
.registry-table td {
padding: 12px 16px;
font-size: 13px;
border-bottom: 1px solid var(--border-light);
vertical-align: middle;
}
.registry-table tr:last-child td {
border-bottom: none;
}
.registry-table tr:hover td {
background: rgba(243, 244, 246, 0.5);
}
.registry-table .col-id {
font-family: var(--font-mono);
font-size: 12px;
max-width: 280px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.registry-table .col-strategy {
white-space: nowrap;
}
.registry-table .col-actions {
width: 80px;
text-align: right;
}
.strategy-badge {
font-size: 11px;
font-weight: 500;
padding: 2px 8px;
border-radius: 4px;
background: var(--border-light);
color: var(--text-secondary);
}
/* ── Modal overlay ── */
.modal-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 1000;
padding: 40px 24px;
overflow-y: auto;
}
.modal-overlay.active {
display: flex;
align-items: flex-start;
justify-content: center;
}
.modal {
max-width: 560px;
width: 100%;
background: var(--surface);
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
overflow: hidden;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 24px;
border-bottom: 1px solid var(--border);
background: var(--background);
}
.modal-header h2 {
font-size: 18px;
font-weight: 600;
}
.modal-close {
width: 32px;
height: 32px;
border: none;
background: none;
cursor: pointer;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
color: var(--text-secondary);
transition: all 0.15s;
}
.modal-close:hover {
background: var(--border-light);
color: var(--text-primary);
}
.modal-body {
padding: 24px;
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: 10px;
padding: 16px 24px;
border-top: 1px solid var(--border);
background: var(--background);
}
/* ── Form ── */
.form-group {
margin-bottom: 18px;
}
.form-group:last-child {
margin-bottom: 0;
}
.form-label {
display: block;
font-size: 13px;
font-weight: 500;
color: var(--text-primary);
margin-bottom: 6px;
}
.form-label .optional {
font-weight: 400;
color: var(--text-secondary);
font-size: 12px;
}
.form-input,
.form-select,
.form-textarea {
width: 100%;
padding: 8px 12px;
border: 1px solid var(--border);
border-radius: 8px;
font-family: var(--font-primary);
font-size: 13px;
color: var(--text-primary);
background: var(--surface);
transition: border-color 0.15s;
}
.form-input:focus,
.form-select:focus,
.form-textarea:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px rgba(0, 115, 209, 0.1);
}
.form-input[readonly] {
background: var(--border-light);
color: var(--text-secondary);
cursor: not-allowed;
}
.form-textarea {
min-height: 80px;
resize: vertical;
}
.form-select {
cursor: pointer;
appearance: none;
background-image: url("data:image/svg+xml,%3Csvg width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236B7280' stroke-width='2' stroke-linecap='round' stroke-linejoin='round' xmlns='http://www.w3.org/2000/svg'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: right 12px center;
padding-right: 32px;
}
.form-hint {
font-size: 12px;
color: var(--text-secondary);
margin-top: 4px;
}
/* ── Footer ── */
.footer {
text-align: center;
padding: 24px;
color: var(--text-secondary);
font-size: 12px;
}
.footer a {
color: var(--primary);
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
/* ── Responsive ── */
@media (max-width: 640px) {
.header {
padding: 0 16px;
}
.page-title {
padding: 24px 16px 16px;
}
.content {
padding: 0 16px 24px;
}
.panel-header {
flex-direction: column;
align-items: flex-start;
gap: 12px;
}
.table-item {
padding-left: 24px;
flex-wrap: wrap;
}
.table-item-meta {
flex-direction: column;
gap: 4px;
}
.registry-table .col-id {
max-width: 160px;
}
.modal {
margin: 16px;
}
}
</style>
{% include '_theme.html' %}
</head>
<body>
<!-- ═══════════════ HEADER ═══════════════ -->
{% include '_app_header.html' %}
<!-- ═══════════════ PAGE TITLE ═══════════════ -->
<div class="page-title">
<h1>Table Management</h1>
<p>Discover, register, and manage data tables from your source</p>
</div>
<!-- ═══════════════ CONTENT ═══════════════ -->
<div class="content">
{% if data_source_type == 'bigquery' %}
<!-- ── BigQuery Register Panel (M1: no discovery, manual entry) ── -->
<div class="panel" data-test="bq-register-panel">
<div class="panel-header">
<div class="panel-header-left">
<div class="panel-header-icon">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#0073D1" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="12" y1="5" x2="12" y2="19"/>
<line x1="5" y1="12" x2="19" y2="12"/>
</svg>
</div>
<div>
<div class="panel-title">Register BigQuery Table</div>
<div class="panel-subtitle">Manually register a BQ table or view as a remote DuckDB view</div>
</div>
</div>
<button class="btn btn-primary" onclick="openBigQueryRegisterModal()">
Register BigQuery table
</button>
</div>
<div class="panel-body-empty">
BigQuery dataset/table discovery lands in Milestone 2 of issue #108. For now, enter the dataset + table by hand.
</div>
</div>
{% else %}
<!-- ── Discovery Panel (Keboola etc.) ── -->
<div class="panel">
<div class="panel-header">
<div class="panel-header-left">
<div class="panel-header-icon">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#0073D1" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="11" cy="11" r="8"/>
<line x1="21" y1="21" x2="16.65" y2="16.65"/>
</svg>
</div>
<div>
<div class="panel-title">Discover Tables</div>
<div class="panel-subtitle">Scan your data source for available tables</div>
</div>
</div>
<button class="btn btn-primary" id="discoverBtn" onclick="discoverTables()">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="11" cy="11" r="8"/>
<line x1="21" y1="21" x2="16.65" y2="16.65"/>
</svg>
Discover tables from source
</button>
</div>
<div id="discoveryResults">
<div class="panel-body-empty" id="discoveryEmpty">
Click "Discover tables from source" to scan for available tables
</div>
</div>
</div>
{% endif %}
<!-- ── Registry Panel ── -->
<div class="panel">
<div class="panel-header">
<div class="panel-header-left">
<div class="panel-header-icon" style="background: var(--success-light);">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#10B77F" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
<polyline points="14 2 14 8 20 8"/>
<line x1="16" y1="13" x2="8" y2="13"/>
<line x1="16" y1="17" x2="8" y2="17"/>
<polyline points="10 9 9 9 8 9"/>
</svg>
</div>
<div>
<div class="panel-title">Registered Tables</div>
<div class="panel-subtitle" id="registrySubtitle">Tables currently configured for sync</div>
</div>
</div>
<button class="btn btn-secondary btn-sm" onclick="loadRegistry()">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="23 4 23 10 17 10"/>
<path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/>
</svg>
Refresh
</button>
</div>
<div id="registryContent">
<div class="loading-state" id="registryLoading">
<div class="spinner spinner-lg"></div>
<span>Loading registry...</span>
</div>
</div>
</div>
</div>
<!-- ═══════════════ REGISTRATION MODAL ═══════════════ -->
<!--
The form is branched server-side on data_source_type so the JS
doesn't have to round-trip /api/admin/server-config to learn the
source type. data-source-type carries the value down to the JS so
the submit handler picks the right payload shape.
-->
<div class="modal-overlay" id="registerModal" data-source-type="{{ data_source_type }}">
<div class="modal">
<div class="modal-header">
<h2>Register Table</h2>
<button class="modal-close" onclick="closeRegisterModal()">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="18" y1="6" x2="6" y2="18"/>
<line x1="6" y1="6" x2="18" y2="18"/>
</svg>
</button>
</div>
<div class="modal-body">
{% if data_source_type == 'bigquery' %}
{# BigQuery path — registers a remote view that queries BQ directly. #}
<div class="form-group">
<label class="form-label" for="bqDataset">Dataset</label>
<input type="text" class="form-input" id="bqDataset" placeholder="e.g. analytics">
<div class="form-hint">BigQuery dataset name (no project prefix — that's read from instance.yaml)</div>
</div>
<div class="form-group">
<label class="form-label" for="bqSourceTable">Source Table</label>
<input type="text" class="form-input" id="bqSourceTable" placeholder="e.g. orders">
<div class="form-hint">BigQuery table or view name within the dataset</div>
</div>
<div class="form-group">
<label class="form-label" for="bqViewName">View Name</label>
<input type="text" class="form-input" id="bqViewName" placeholder="defaults to source table">
<div class="form-hint">DuckDB view name analysts will use; defaults to the source table</div>
</div>
<div class="form-group">
<label class="form-label" for="bqDescription">Description <span class="optional">(optional)</span></label>
<textarea class="form-textarea" id="bqDescription" placeholder="Brief description of the table contents..."></textarea>
</div>
<div class="form-group">
<label class="form-label" for="bqFolder">Folder <span class="optional">(optional)</span></label>
<input type="text" class="form-input" id="bqFolder" placeholder="e.g. crm, finance, marketing">
<div class="form-hint">Logical grouping for catalog organization</div>
</div>
<div class="form-group">
<label class="form-label" for="bqSyncSchedule">Sync Schedule <span class="optional">(optional)</span></label>
<input type="text" class="form-input" id="bqSyncSchedule" placeholder="e.g. 0 6 * * *">
<div class="form-hint">Cron expression. Note: scheduler does not yet evaluate this — see #79; addressed in M3 of #108.</div>
</div>
<div class="form-group" id="bqPrecheckSummary" style="display:none;">
<div class="form-label">Source check</div>
<div class="form-hint" id="bqPrecheckSummaryText"></div>
</div>
{% else %}
{# Keboola (default) path — fields populated by Discover-tables prefill. #}
<div class="form-group">
<label class="form-label" for="regTableId">Table ID</label>
<input type="text" class="form-input" id="regTableId" readonly>
</div>
<div class="form-group">
<label class="form-label" for="regTableName">Table Name</label>
<input type="text" class="form-input" id="regTableName" readonly>
<div class="form-hint">Derived from the source table identifier</div>
</div>
<div class="form-group">
<label class="form-label" for="regBucket">Bucket</label>
<input type="text" class="form-input" id="regBucket" readonly>
<div class="form-hint">Source bucket (Keboola: in.c-foo)</div>
</div>
{# Hidden: the Keboola storage table identifier (the part after the
bucket prefix in `t.id`, e.g. `company` for `in.c-sfdc.company`).
`regTableName` is the human-friendly display name and is NOT
safe to send as `source_table` — see review IMPORTANT 5 in
PR #119. #}
<input type="hidden" id="regSourceTable" value="">
<div class="form-group">
<label class="form-label" for="regStrategy">Sync Strategy</label>
<select class="form-select" id="regStrategy">
<option value="full_refresh">Full Refresh</option>
<option value="incremental">Incremental</option>
<option value="partitioned">Partitioned</option>
</select>
<div class="form-hint">How data should be synchronized from the source</div>
</div>
<div class="form-group">
<label class="form-label" for="regPrimaryKey">Primary Key</label>
<input type="text" class="form-input" id="regPrimaryKey" placeholder="e.g. id">
<div class="form-hint">Comma-separated list of primary key columns</div>
</div>
<div class="form-group">
<label class="form-label" for="regDescription">Description <span class="optional">(optional)</span></label>
<textarea class="form-textarea" id="regDescription" placeholder="Brief description of the table contents..."></textarea>
</div>
<div class="form-group">
<label class="form-label" for="regFolder">Folder <span class="optional">(optional)</span></label>
<input type="text" class="form-input" id="regFolder" placeholder="e.g. crm, finance, marketing">
<div class="form-hint">Logical grouping for catalog organization</div>
</div>
{% endif %}
</div>
<div class="modal-footer">
<button class="btn btn-secondary" onclick="closeRegisterModal()">Cancel</button>
<button class="btn btn-primary" id="registerSubmitBtn" onclick="registerTable()">
Register Table
</button>
</div>
</div>
</div>
<!-- ═══════════════ EDIT MODAL ═══════════════ -->
<div class="modal-overlay" id="editModal">
<div class="modal">
<div class="modal-header">
<h2>Edit Table</h2>
<button class="modal-close" onclick="closeEditModal()">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="18" y1="6" x2="6" y2="18"/>
<line x1="6" y1="6" x2="18" y2="18"/>
</svg>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label class="form-label" for="editTableId">Table ID</label>
<input type="text" class="form-input" id="editTableId" readonly>
</div>
<div class="form-group">
<label class="form-label" for="editStrategy">Sync Strategy</label>
<select class="form-select" id="editStrategy">
<option value="full_refresh">Full Refresh</option>
<option value="incremental">Incremental</option>
<option value="partitioned">Partitioned</option>
</select>
</div>
<div class="form-group">
<label class="form-label" for="editPrimaryKey">Primary Key</label>
<input type="text" class="form-input" id="editPrimaryKey" placeholder="e.g. id">
</div>
<div class="form-group">
<label class="form-label" for="editDescription">Description <span class="optional">(optional)</span></label>
<textarea class="form-textarea" id="editDescription" placeholder="Brief description of the table contents..."></textarea>
</div>
<div class="form-group">
<label class="form-label" for="editDataset">Dataset Group <span class="optional">(optional)</span></label>
<input type="text" class="form-input" id="editDataset" placeholder="e.g. crm, finance, marketing">
</div>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" onclick="closeEditModal()">Cancel</button>
<button class="btn btn-primary" id="editSubmitBtn" onclick="saveTableEdit()">
Save Changes
</button>
</div>
</div>
</div>
<!-- ═══════════════ TOAST ═══════════════ -->
<div class="toast" id="toast">
<div class="toast-icon" id="toastIcon"></div>
<span id="toastMessage"></span>
</div>
<!-- ═══════════════ FOOTER ═══════════════ -->
<footer class="footer">
<a href="{{ url_for('dashboard') }}">Back to Dashboard</a>
</footer>
<script>
/* ═══════════════════════════════════════════════════════════════
Admin Tables - JavaScript
═══════════════════════════════════════════════════════════════ */
// State
let discoveryData = null;
let registryData = null;
let registryVersion = null;
let currentEditTableId = null;
// ── Toast notification ──────────────────────────────────────
function showToast(message, type) {
var toast = document.getElementById('toast');
var icon = document.getElementById('toastIcon');
var msg = document.getElementById('toastMessage');
toast.className = 'toast toast-' + type;
msg.textContent = message;
if (type === 'success') {
icon.innerHTML = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#10B77F" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>';
} else {
icon.innerHTML = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#EA580C" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>';
}
// Show
requestAnimationFrame(function() {
toast.classList.add('visible');
});
// Hide after 4 seconds
setTimeout(function() {
toast.classList.remove('visible');
}, 4000);
}
// ── Format helpers ──────────────────────────────────────────
function formatNumber(n) {
if (n == null) return '-';
if (n >= 1000000) return (n / 1000000).toFixed(1) + 'M';
if (n >= 1000) return n.toLocaleString();
return String(n);
}
function formatSize(bytes) {
if (bytes == null || bytes === 0) return '-';
if (bytes >= 1073741824) return (bytes / 1073741824).toFixed(1) + ' GB';
if (bytes >= 1048576) return (bytes / 1048576).toFixed(1) + ' MB';
if (bytes >= 1024) return (bytes / 1024).toFixed(1) + ' KB';
return bytes + ' B';
}
function escapeHtml(str) {
if (!str) return '';
var div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
// ── Discovery ───────────────────────────────────────────────
function discoverTables() {
var btn = document.getElementById('discoverBtn');
var resultsEl = document.getElementById('discoveryResults');
// Loading state
btn.disabled = true;
btn.innerHTML = '<span class="spinner"></span> Discovering...';
resultsEl.innerHTML = '<div class="loading-state"><div class="spinner spinner-lg"></div><span>Scanning data source for tables...</span></div>';
fetch('/api/admin/discover-tables')
.then(function(r) {
if (!r.ok) return r.json().then(function(d) { throw new Error(d.error || 'Discovery failed'); });
return r.json();
})
.then(function(data) {
discoveryData = data;
// renderDiscoveryResults handles the per-source shape and
// surfaces its own success toast (count derived from the
// actual flat tables list, not whatever `data.total` says).
renderDiscoveryResults(data);
})
.catch(function(err) {
resultsEl.innerHTML = '<div class="panel-body-empty" style="color: var(--error);">Discovery failed: ' + escapeHtml(err.message) + '</div>';
showToast('Discovery failed: ' + err.message, 'error');
})
.finally(function() {
btn.disabled = false;
btn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg> Discover tables from source';
});
}
function renderDiscoveryResults(data) {
var el = document.getElementById('discoveryResults');
// The discovery API returns a flat {tables: [...]} list; group on
// the client by `bucket_id` so the rendered accordion still shows
// bucket → tables. Fall back to the legacy `data.buckets` shape
// if a future endpoint emits it pre-grouped.
var buckets;
if (data.buckets && data.buckets.length) {
buckets = data.buckets;
} else if (data.tables && data.tables.length) {
var byBucket = {};
data.tables.forEach(function(t) {
var bid = t.bucket_id || '(ungrouped)';
if (!byBucket[bid]) {
byBucket[bid] = {
bucket_id: bid,
bucket_name: t.bucket_name || bid,
tables: [],
};
}
byBucket[bid].tables.push(t);
});
buckets = Object.keys(byBucket).sort().map(function(k) { return byBucket[k]; });
} else {
buckets = [];
}
if (!buckets.length) {
el.innerHTML = '<div class="panel-body-empty">No tables found in data source</div>';
return;
}
var html = '';
var totalTables = 0;
buckets.forEach(function(bucket) {
var registeredCount = bucket.tables.filter(function(t) { return t.is_registered; }).length;
totalTables += bucket.tables.length;
html += '<div class="bucket-group">';
html += '<button class="bucket-trigger expanded" onclick="toggleBucket(this)">';
html += '<svg class="bucket-chevron" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>';
html += escapeHtml(bucket.bucket_name || bucket.bucket_id);
html += '<span class="bucket-count">' + bucket.tables.length + ' tables';
if (registeredCount > 0) html += ' / ' + registeredCount + ' registered';
html += '</span>';
html += '</button>';
html += '<div class="bucket-content expanded">';
bucket.tables.forEach(function(table) {
html += '<div class="table-item">';
html += '<div class="table-item-info">';
html += '<div class="table-item-name">' + escapeHtml(table.name) + '</div>';
html += '<div class="table-item-meta">';
if (table.columns != null) html += '<span>' + table.columns + ' columns</span>';
if (table.row_count != null) html += '<span>' + formatNumber(table.row_count) + ' rows</span>';
if (table.size_bytes != null) html += '<span>' + formatSize(table.size_bytes) + '</span>';
html += '</div>';
html += '</div>';
html += '<div class="table-item-actions">';
if (table.is_registered) {
html += '<span class="badge badge-registered">Registered</span>';
} else {
html += '<span class="badge badge-available">Available</span>';
html += '<button class="btn btn-primary btn-sm" onclick=\'openRegisterModal(' + JSON.stringify(table).replace(/'/g, "\\'") + ')\'>Register</button>';
}
html += '</div>';
html += '</div>';
});
html += '</div>';
html += '</div>';
});
el.innerHTML = html;
// Surface the discovery summary in the toast (count is whatever
// discovery returned, not whatever the API claimed in `total`).
showToast('Found ' + totalTables + ' tables in ' + buckets.length + ' buckets', 'success');
}
function toggleBucket(trigger) {
var content = trigger.nextElementSibling;
var isExpanded = trigger.classList.contains('expanded');
if (isExpanded) {
trigger.classList.remove('expanded');
content.classList.remove('expanded');
} else {
trigger.classList.add('expanded');
content.classList.add('expanded');
}
}
// ── Registration Modal ──────────────────────────────────────
// Server-rendered marker so the JS knows whether to drive the Keboola
// form or the BigQuery form. Single source of truth — stays in sync
// with the Jinja branch above.
var DATA_SOURCE_TYPE = document.getElementById('registerModal').dataset.sourceType || 'keboola';
function openRegisterModal(table) {
if (DATA_SOURCE_TYPE === 'bigquery') {
// BQ uses a manual-entry form (no discovery panel for BQ in M1).
// `table` may be partially populated by a future M2 prefill —
// tolerate either an empty call or a {bucket, source_table, ...}
// shape from a hypothetical future prefill.
table = table || {};
document.getElementById('bqDataset').value = table.bucket || '';
document.getElementById('bqSourceTable').value = table.source_table || table.name || '';
document.getElementById('bqViewName').value = table.name || '';
document.getElementById('bqDescription').value = '';
document.getElementById('bqFolder').value = '';
document.getElementById('bqSyncSchedule').value = '';
var summary = document.getElementById('bqPrecheckSummary');
if (summary) summary.style.display = 'none';
} else {
// Keboola path — fields populated from the discovery click.
// `table.id` from KeboolaClient.discover_all_tables() is the
// full storage identifier (e.g. `in.c-sfdc.company`); strip the
// bucket prefix to get the bare source table name (`company`)
// that the extractor expects in `kbc."{bucket}"."{source_table}"`.
// `table.name` is the human-friendly display name — NOT safe to
// use as the storage identifier. See review IMPORTANT 5 in #119.
var bucketId = table.bucket_id || (table.bucket && table.bucket.id) || '';
var fullId = table.id || '';
var bareSourceTable = '';
if (fullId && bucketId && fullId.indexOf(bucketId + '.') === 0) {
bareSourceTable = fullId.substring(bucketId.length + 1);
} else if (fullId.indexOf('.') >= 0) {
bareSourceTable = fullId.substring(fullId.lastIndexOf('.') + 1);
} else {
bareSourceTable = fullId || table.name || '';
}
document.getElementById('regTableId').value = fullId;
document.getElementById('regTableName').value = table.name || '';
document.getElementById('regBucket').value = bucketId;
document.getElementById('regSourceTable').value = bareSourceTable;
document.getElementById('regStrategy').value = 'full_refresh';
document.getElementById('regPrimaryKey').value = (table.primary_key || []).join(', ');
document.getElementById('regDescription').value = '';
document.getElementById('regFolder').value = '';
}
document.getElementById('registerSubmitBtn').disabled = false;
document.getElementById('registerSubmitBtn').textContent = 'Register Table';
// Reset BQ button handler back to the precheck step in case the
// operator left it on "Register" from a previous open.
document.getElementById('registerSubmitBtn').onclick = registerTable;
document.getElementById('registerModal').classList.add('active');
}
function closeRegisterModal() {
document.getElementById('registerModal').classList.remove('active');
}
function _buildKeboolaPayload() {
var pk = document.getElementById('regPrimaryKey').value.trim();
var primaryKey = pk ? pk.split(',').map(function(s) { return s.trim(); }).filter(Boolean) : [];
// `regSourceTable` carries the bare Keboola storage identifier
// (e.g. `company`), populated from the discovery row's `t.id`
// by openRegisterModal. `regTableName` is the display name and
// can include spaces or non-ASCII chars — never send it as
// source_table. See review IMPORTANT 5 in #119.
var sourceTable = (document.getElementById('regSourceTable').value || '').trim();
if (!sourceTable) {
// Manual-entry fallback (no discovery row clicked) — fall
// back to the display name. Validators on the server will
// reject it if it's not a safe identifier.
sourceTable = document.getElementById('regTableName').value;
}
return {
// RegisterTableRequest contract: name + source_type + bucket +
// source_table; no `id`, no `version`, no `dataset` (the field
// is `bucket`). Pre-fix the modal posted those phantom fields
// and the API silently dropped them.
name: document.getElementById('regTableName').value,
source_type: 'keboola',
bucket: document.getElementById('regBucket').value,
source_table: sourceTable,
query_mode: 'local',
sync_strategy: document.getElementById('regStrategy').value,
primary_key: primaryKey,
description: document.getElementById('regDescription').value.trim() || null,
folder: document.getElementById('regFolder').value.trim() || null,
};
}
function _buildBigQueryPayload() {
var dataset = document.getElementById('bqDataset').value.trim();
var sourceTable = document.getElementById('bqSourceTable').value.trim();
var viewName = document.getElementById('bqViewName').value.trim() || sourceTable;
return {
name: viewName,
source_type: 'bigquery',
bucket: dataset,
source_table: sourceTable,
// The server forces these for BQ rows, but we set them explicitly
// so the network log + audit-log entry reflect the operator's
// intent rather than a server-side mutation.
query_mode: 'remote',
profile_after_sync: false,
description: document.getElementById('bqDescription').value.trim() || null,
folder: document.getElementById('bqFolder').value.trim() || null,
sync_schedule: document.getElementById('bqSyncSchedule').value.trim() || null,
};
}
function registerTable() {
var btn = document.getElementById('registerSubmitBtn');
btn.disabled = true;
if (DATA_SOURCE_TYPE === 'bigquery') {
_registerBigQueryTable(btn);
} else {
_registerKeboolaTable(btn);
}
}
function _registerKeboolaTable(btn) {
btn.textContent = 'Registering...';
var payload = _buildKeboolaPayload();
fetch('/api/admin/register-table', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})
.then(function(r) {
if (!r.ok) return r.json().then(function(d) { throw new Error(d.detail || d.error || 'Registration failed'); });
return r.json();
})
.then(function(data) {
closeRegisterModal();
showToast('Table registered successfully', 'success');
loadRegistry();
if (discoveryData) discoverTables();
})
.catch(function(err) {
showToast('Registration failed: ' + err.message, 'error');
})
.finally(function() {
btn.disabled = false;
btn.textContent = 'Register Table';
});
}
function _registerBigQueryTable(btn) {
// Step 1 of two-step flow: precheck only. After precheck succeeds
// we surface row count / size / column count in the modal AND swap
// the primary button to "Register" — operator must explicitly
// click it to fire the actual register call. Pre-fix this chained
// precheck → register in a single promise, so the operator never
// got to review the summary before the row was committed (review
// IMPORTANT 4 in #119).
btn.textContent = 'Checking source...';
var payload = _buildBigQueryPayload();
var summary = document.getElementById('bqPrecheckSummary');
var summaryText = document.getElementById('bqPrecheckSummaryText');
fetch('/api/admin/register-table/precheck', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})
.then(function(r) {
if (!r.ok) return r.json().then(function(d) { throw new Error(d.detail || 'Precheck failed'); });
return r.json();
})
.then(function(data) {
// Surface the source-side metrics so the operator can sanity-
// check the dataset/table before committing the row.
var t = data.table || {};
if (summary && summaryText) {
summary.style.display = '';
summaryText.textContent =
formatNumber(t.rows) + ' rows, ' +
formatSize(t.size_bytes) + ', ' +
(t.column_count != null ? t.column_count : (t.columns || []).length) + ' columns';
}
// Swap the primary button from "Register Table" (which fired
// the precheck) to "Register" (which fires the actual register
// call). Reusing the same modal means the operator can still
// hit Cancel or close without committing.
btn.disabled = false;
btn.textContent = 'Register';
btn.onclick = function() { _confirmRegisterBigQueryTable(btn, payload); };
})
.catch(function(err) {
showToast('Precheck failed: ' + err.message, 'error');
btn.disabled = false;
btn.textContent = 'Register Table';
});
}
function _confirmRegisterBigQueryTable(btn, payload) {
// Step 2 of the two-step BQ flow: actually POST /register-table.
// Reached only after the operator has reviewed the precheck
// summary in the modal and clicked the (relabeled) Register button.
btn.disabled = true;
btn.textContent = 'Registering...';
fetch('/api/admin/register-table', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})
.then(function(r) {
// 200 (synchronous materialize) and 202 (BG materialize) are
// success; 500 with a `rebuild_failed` body is the new failure
// mode where the row is in the registry but the view wasn't
// created. Surface the error verbatim so the operator can fix
// it (typically a missing project in the overlay or auth
// missing bigquery.metadata.get).
return r.json().then(function(d) {
if (!r.ok) {
var msg = d.detail || d.message || 'Registration failed';
if (d.errors && d.errors.length) {
msg += ' (' + d.errors.length + ' error' + (d.errors.length > 1 ? 's' : '') + ')';
}
throw new Error(msg);
}
closeRegisterModal();
if (r.status === 202) {
showToast('Registration accepted, materializing in background', 'success');
} else {
showToast('Queryable as ' + (d.view_name || d.id || payload.name), 'success');
}
loadRegistry();
});
})
.catch(function(err) {
showToast('Registration failed: ' + err.message, 'error');
})
.finally(function() {
// Restore the modal's default button state — the operator may
// close + reopen the modal, and openRegisterModal also resets
// onclick back to registerTable.
btn.disabled = false;
btn.textContent = 'Register Table';
btn.onclick = registerTable;
});
}
// BQ-only: surface a "Register" button outside the discovery panel,
// since Discovery doesn't run for BQ in M1.
function openBigQueryRegisterModal() {
openRegisterModal({});
}
// ── Edit Modal ──────────────────────────────────────────────
function openEditModal(table) {
currentEditTableId = table.id;
document.getElementById('editTableId').value = table.id || '';
document.getElementById('editStrategy').value = table.sync_strategy || 'full_refresh';
document.getElementById('editPrimaryKey').value = (table.primary_key || []).join(', ');
document.getElementById('editDescription').value = table.description || '';
document.getElementById('editDataset').value = table.dataset || '';
document.getElementById('editSubmitBtn').disabled = false;
document.getElementById('editModal').classList.add('active');
}
function closeEditModal() {
document.getElementById('editModal').classList.remove('active');
currentEditTableId = null;
}
function saveTableEdit() {
if (!currentEditTableId) return;
var btn = document.getElementById('editSubmitBtn');
btn.disabled = true;
btn.textContent = 'Saving...';
var pk = document.getElementById('editPrimaryKey').value.trim();
var primaryKey = pk ? pk.split(',').map(function(s) { return s.trim(); }).filter(Boolean) : [];
// UpdateTableRequest contract: no `dataset` field (it's `folder` for
// catalog grouping in the registry), no `version` (the API has no
// optimistic-concurrency layer). Pre-fix the modal posted `dataset`
// + `version` and the API silently dropped them, so the operator's
// dataset edit looked saved but never persisted.
var payload = {
sync_strategy: document.getElementById('editStrategy').value,
primary_key: primaryKey,
description: document.getElementById('editDescription').value.trim() || null,
folder: document.getElementById('editDataset').value.trim() || null,
};
fetch('/api/admin/registry/' + encodeURIComponent(currentEditTableId), {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})
.then(function(r) {
if (!r.ok) return r.json().then(function(d) { throw new Error(d.detail || d.error || 'Update failed'); });
return r.json();
})
.then(function(data) {
closeEditModal();
showToast('Table updated successfully', 'success');
loadRegistry();
})
.catch(function(err) {
showToast('Update failed: ' + err.message, 'error');
})
.finally(function() {
btn.disabled = false;
btn.textContent = 'Save Changes';
});
}
// ── Registry ────────────────────────────────────────────────
function loadRegistry() {
var el = document.getElementById('registryContent');
el.innerHTML = '<div class="loading-state"><div class="spinner spinner-lg"></div><span>Loading registry...</span></div>';
fetch('/api/admin/registry')
.then(function(r) {
if (!r.ok) return r.json().then(function(d) { throw new Error(d.error || 'Load failed'); });
return r.json();
})
.then(function(data) {
registryData = data;
registryVersion = data.version;
renderRegistry(data);
})
.catch(function(err) {
el.innerHTML = '<div class="panel-body-empty" style="color: var(--error);">Failed to load registry: ' + escapeHtml(err.message) + '</div>';
});
}
function renderRegistry(data) {
var el = document.getElementById('registryContent');
var tables = data.tables || [];
var subtitle = document.getElementById('registrySubtitle');
subtitle.textContent = tables.length + ' table' + (tables.length !== 1 ? 's' : '') + ' configured for sync';
if (tables.length === 0) {
el.innerHTML = '<div class="panel-body-empty">No tables registered yet. Use the discovery panel above to find and register tables.</div>';
return;
}
var html = '<table class="registry-table">';
html += '<thead><tr>';
html += '<th>Table ID</th>';
html += '<th>Strategy</th>';
html += '<th>Primary Key</th>';
html += '<th>Description</th>';
html += '<th class="col-actions">Actions</th>';
html += '</tr></thead>';
html += '<tbody>';
tables.forEach(function(table) {
html += '<tr>';
html += '<td class="col-id" title="' + escapeHtml(table.id) + '">' + escapeHtml(table.id) + '</td>';
html += '<td class="col-strategy"><span class="strategy-badge">' + escapeHtml(table.sync_strategy || 'full_refresh') + '</span></td>';
html += '<td>' + escapeHtml((table.primary_key || []).join(', ') || '-') + '</td>';
html += '<td>' + escapeHtml(table.description || '-') + '</td>';
html += '<td class="col-actions">';
html += '<button class="btn-icon" title="Edit" onclick=\'openEditModal(' + JSON.stringify(table).replace(/'/g, "\\'") + ')\'>';
html += '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>';
html += '</button>';
html += '<button class="btn-icon danger" title="Delete" onclick="deleteTable(\'' + escapeHtml(table.id).replace(/'/g, "\\'") + '\')">';
html += '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>';
html += '</button>';
html += '</td>';
html += '</tr>';
});
html += '</tbody></table>';
el.innerHTML = html;
}
function deleteTable(tableId) {
if (!confirm('Are you sure you want to unregister "' + tableId + '"?\n\nThis will remove it from sync and clean up user subscriptions.')) {
return;
}
// DELETE doesn't need a body — the API has no `version` field.
fetch('/api/admin/registry/' + encodeURIComponent(tableId), {
method: 'DELETE',
})
.then(function(r) {
// 204 No Content has an empty body, so don't .json() it.
if (r.status === 204) return null;
if (!r.ok) return r.json().then(function(d) { throw new Error(d.detail || d.error || 'Delete failed'); });
return r.json();
})
.then(function() {
showToast('Table unregistered successfully', 'success');
loadRegistry();
if (discoveryData) discoverTables();
})
.catch(function(err) {
showToast('Delete failed: ' + err.message, 'error');
});
}
// ── Modal close on overlay click ────────────────────────────
document.getElementById('registerModal').addEventListener('click', function(e) {
if (e.target === this) closeRegisterModal();
});
document.getElementById('editModal').addEventListener('click', function(e) {
if (e.target === this) closeEditModal();
});
// Close modals on Escape key
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
closeRegisterModal();
closeEditModal();
}
});
// ── Initialize ──────────────────────────────────────────────
loadRegistry();
</script>
{% include "_version_badge.html" %}
</body>
</html>