Add React UI and backend enhancements for dataflow

- Add full React + Vite UI (src/pages: Sources, Rules, Mappings, Records, Import)
- Sidebar layout with source selector persisted to localStorage
- Sources: unified field table with Dedup/In-view checkboxes, CSV suggest, generate dfv view
- Rules: extract/replace function types, regex flags, input field picklist, test results
- Mappings: unmapped values with sample records, inline key/value editor, edit existing mappings
- Records: expanded row shows per-rule extraction and mapping output breakdown
- Import: drag-drop CSV, transform/reprocess buttons, import history
- Backend: add flags/function_type to rules, get_unmapped_values with samples, generate_source_view, fields endpoint, reprocess endpoint
- database/functions.sql: apply_transformations supports replace mode and flags; generate_source_view builds typed dfv views
- Server bound to 0.0.0.0, SPA fallback for client-side routing

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Paul Trowbridge 2026-03-29 00:35:33 -04:00
parent 83300d7a8e
commit eb50704ca0
23 changed files with 1822 additions and 5 deletions

5
.gitignore vendored
View File

@ -3,7 +3,12 @@
# Dependencies # Dependencies
node_modules/ node_modules/
ui/node_modules/
package-lock.json package-lock.json
ui/package-lock.json
# UI build output (generated — run `cd ui && npm run build`)
public/
# Logs # Logs
*.log *.log

View File

@ -208,6 +208,47 @@ module.exports = (pool) => {
} }
}); });
// Get all known field names for a source
router.get('/:name/fields', async (req, res, next) => {
try {
const result = await pool.query(`
SELECT key, array_agg(DISTINCT origin ORDER BY origin) AS origins
FROM (
SELECT f->>'name' AS key, 'schema' AS origin
FROM sources, jsonb_array_elements(config->'fields') f
WHERE name = $1 AND config ? 'fields'
UNION ALL
SELECT jsonb_object_keys(data) AS key, 'raw' AS origin
FROM records WHERE source_name = $1
UNION ALL
SELECT output_field AS key, 'rule: ' || name AS origin
FROM rules WHERE source_name = $1
UNION ALL
SELECT jsonb_object_keys(output) AS key, 'mapping' AS origin
FROM mappings WHERE source_name = $1
) keys
GROUP BY key
ORDER BY key
`, [req.params.name]);
res.json(result.rows);
} catch (err) {
next(err);
}
});
// Generate output view
router.post('/:name/view', async (req, res, next) => {
try {
const result = await pool.query(
'SELECT generate_source_view($1) as result',
[req.params.name]
);
res.json(result.rows[0].result);
} catch (err) {
next(err);
}
});
// Reprocess all records // Reprocess all records
router.post('/:name/reprocess', async (req, res, next) => { router.post('/:name/reprocess', async (req, res, next) => {
try { try {

View File

@ -23,6 +23,10 @@ const pool = new Pool({
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
// Serve UI
const path = require('path');
app.use(express.static(path.join(__dirname, '../public')));
// Set search path for all queries // Set search path for all queries
pool.on('connect', (client) => { pool.on('connect', (client) => {
client.query('SET search_path TO dataflow, public'); client.query('SET search_path TO dataflow, public');
@ -82,13 +86,19 @@ app.use((err, req, res, next) => {
}); });
}); });
// 404 handler // SPA fallback — serve index.html for any non-API route
app.use((req, res, next) => {
if (req.path.startsWith('/api')) return next();
res.sendFile(path.join(__dirname, '../public/index.html'));
});
// 404 handler (API routes only)
app.use((req, res) => { app.use((req, res) => {
res.status(404).json({ error: 'Endpoint not found' }); res.status(404).json({ error: 'Endpoint not found' });
}); });
// Start server // Start server
app.listen(PORT, () => { app.listen(PORT, '0.0.0.0', () => {
console.log(`✓ Dataflow API listening on port ${PORT}`); console.log(`✓ Dataflow API listening on port ${PORT}`);
console.log(` Health: http://localhost:${PORT}/health`); console.log(` Health: http://localhost:${PORT}/health`);
console.log(` API: http://localhost:${PORT}/api/sources`); console.log(` API: http://localhost:${PORT}/api/sources`);

View File

@ -199,9 +199,9 @@ BEGIN
count(*) AS record_count, count(*) AS record_count,
jsonb_agg(e.raw_record ORDER BY e.raw_record) FILTER (WHERE e.raw_record IS NOT NULL) AS sample_records jsonb_agg(e.raw_record ORDER BY e.raw_record) FILTER (WHERE e.raw_record IS NOT NULL) AS sample_records
FROM ( FROM (
SELECT rule_name, output_field, extracted_value, raw_record, SELECT e2.rule_name, e2.output_field, e2.extracted_value, e2.raw_record,
row_number() OVER (PARTITION BY rule_name, extracted_value ORDER BY (SELECT NULL)) AS rn row_number() OVER (PARTITION BY e2.rule_name, e2.extracted_value ORDER BY (SELECT NULL)) AS rn
FROM extracted FROM extracted e2
) e ) e
WHERE NOT EXISTS ( WHERE NOT EXISTS (
SELECT 1 FROM dataflow.mappings m SELECT 1 FROM dataflow.mappings m
@ -237,6 +237,63 @@ $$ LANGUAGE plpgsql;
COMMENT ON FUNCTION reprocess_records IS 'Clear and reapply all transformations for a source'; COMMENT ON FUNCTION reprocess_records IS 'Clear and reapply all transformations for a source';
------------------------------------------------------
-- Function: generate_source_view
-- Build a typed flat view in dfv schema
------------------------------------------------------
CREATE OR REPLACE FUNCTION generate_source_view(p_source_name TEXT)
RETURNS JSON AS $$
DECLARE
v_config JSONB;
v_fields JSONB;
v_field JSONB;
v_cols TEXT := '';
v_sql TEXT;
v_view TEXT;
BEGIN
SELECT config INTO v_config
FROM dataflow.sources
WHERE name = p_source_name;
IF v_config IS NULL OR NOT (v_config ? 'fields') OR jsonb_array_length(v_config->'fields') = 0 THEN
RETURN json_build_object('success', false, 'error', 'No schema fields defined for this source');
END IF;
v_fields := v_config->'fields';
FOR v_field IN SELECT * FROM jsonb_array_elements(v_fields)
LOOP
IF v_cols != '' THEN v_cols := v_cols || ', '; END IF;
CASE v_field->>'type'
WHEN 'date' THEN
v_cols := v_cols || format('(transformed->>%L)::date AS %I',
v_field->>'name', v_field->>'name');
WHEN 'numeric' THEN
v_cols := v_cols || format('(transformed->>%L)::numeric AS %I',
v_field->>'name', v_field->>'name');
ELSE
v_cols := v_cols || format('transformed->>%L AS %I',
v_field->>'name', v_field->>'name');
END CASE;
END LOOP;
CREATE SCHEMA IF NOT EXISTS dfv;
v_view := 'dfv.' || quote_ident(p_source_name);
v_sql := format(
'CREATE OR REPLACE VIEW %s AS SELECT %s FROM dataflow.records WHERE source_name = %L AND transformed IS NOT NULL',
v_view, v_cols, p_source_name
);
EXECUTE v_sql;
RETURN json_build_object('success', true, 'view', v_view, 'sql', v_sql);
END;
$$ LANGUAGE plpgsql;
COMMENT ON FUNCTION generate_source_view IS 'Generate a typed flat view in dfv schema from source config.fields';
------------------------------------------------------ ------------------------------------------------------
-- Summary -- Summary
------------------------------------------------------ ------------------------------------------------------

24
ui/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

16
ui/README.md Normal file
View File

@ -0,0 +1,16 @@
# React + Vite
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Oxc](https://oxc.rs)
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/)
## React Compiler
The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).
## Expanding the ESLint configuration
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.

29
ui/eslint.config.js Normal file
View File

@ -0,0 +1,29 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import { defineConfig, globalIgnores } from 'eslint/config'
export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{js,jsx}'],
extends: [
js.configs.recommended,
reactHooks.configs.flat.recommended,
reactRefresh.configs.vite,
],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
parserOptions: {
ecmaVersion: 'latest',
ecmaFeatures: { jsx: true },
sourceType: 'module',
},
},
rules: {
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
},
},
])

13
ui/index.html Normal file
View File

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ui</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>

30
ui/package.json Normal file
View File

@ -0,0 +1,30 @@
{
"name": "ui",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.2.4",
"react-dom": "^19.2.4",
"react-router-dom": "^7.13.2"
},
"devDependencies": {
"@eslint/js": "^9.39.4",
"@tailwindcss/vite": "^4.2.2",
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^6.0.1",
"eslint": "^9.39.4",
"eslint-plugin-react-hooks": "^7.0.1",
"eslint-plugin-react-refresh": "^0.5.2",
"globals": "^17.4.0",
"tailwindcss": "^4.2.2",
"vite": "^8.0.1"
}
}

1
ui/src/App.css Normal file
View File

@ -0,0 +1 @@
/* App-level styles — layout handled by Tailwind */

87
ui/src/App.jsx Normal file
View File

@ -0,0 +1,87 @@
import { useState, useEffect } from 'react'
import { BrowserRouter, Routes, Route, NavLink, Navigate } from 'react-router-dom'
import { api } from './api'
import Sources from './pages/Sources'
import Import from './pages/Import'
import Rules from './pages/Rules'
import Mappings from './pages/Mappings'
import Records from './pages/Records'
const NAV = [
{ to: '/sources', label: 'Sources' },
{ to: '/import', label: 'Import' },
{ to: '/rules', label: 'Rules' },
{ to: '/mappings', label: 'Mappings' },
{ to: '/records', label: 'Records' },
]
export default function App() {
const [sources, setSources] = useState([])
const [source, setSource] = useState(() => localStorage.getItem('selectedSource') || '')
useEffect(() => {
api.getSources().then(s => {
setSources(s)
if (!source && s.length > 0) setSource(s[0].name)
}).catch(() => {})
}, [])
useEffect(() => {
if (source) localStorage.setItem('selectedSource', source)
}, [source])
return (
<BrowserRouter>
<div className="flex h-screen bg-gray-50">
{/* Sidebar */}
<div className="w-44 bg-white border-r border-gray-200 flex flex-col">
<div className="px-4 py-4 border-b border-gray-200">
<span className="text-sm font-semibold text-gray-800 tracking-wide uppercase">Dataflow</span>
</div>
{/* Source selector */}
<div className="px-3 py-3 border-b border-gray-200">
<label className="text-xs text-gray-500 block mb-1">Source</label>
<select
className="w-full text-sm border border-gray-200 rounded px-2 py-1 bg-white focus:outline-none focus:border-blue-400"
value={source}
onChange={e => setSource(e.target.value)}
>
{sources.length === 0 && <option value=""></option>}
{sources.map(s => <option key={s.name} value={s.name}>{s.name}</option>)}
</select>
</div>
{/* Nav */}
<nav className="flex-1 py-2">
{NAV.map(({ to, label }) => (
<NavLink
key={to}
to={to}
className={({ isActive }) =>
`block px-4 py-2 text-sm ${isActive
? 'bg-blue-50 text-blue-700 font-medium'
: 'text-gray-600 hover:bg-gray-50'}`
}
>
{label}
</NavLink>
))}
</nav>
</div>
{/* Main */}
<div className="flex-1 overflow-auto">
<Routes>
<Route path="/" element={<Navigate to="/sources" replace />} />
<Route path="/sources" element={<Sources sources={sources} setSources={setSources} setSource={setSource} />} />
<Route path="/import" element={<Import source={source} />} />
<Route path="/rules" element={<Rules source={source} />} />
<Route path="/mappings" element={<Mappings source={source} />} />
<Route path="/records" element={<Records source={source} />} />
</Routes>
</div>
</div>
</BrowserRouter>
)
}

61
ui/src/api.js Normal file
View File

@ -0,0 +1,61 @@
const BASE = '/api'
async function request(method, path, body, isFormData = false) {
const opts = { method, headers: {} }
if (body) {
if (isFormData) {
opts.body = body
} else {
opts.headers['Content-Type'] = 'application/json'
opts.body = JSON.stringify(body)
}
}
const res = await fetch(BASE + path, opts)
const data = await res.json()
if (!res.ok) throw new Error(data.error || 'Request failed')
return data
}
export const api = {
// Sources
getSources: () => request('GET', '/sources'),
getSource: (name) => request('GET', `/sources/${name}`),
createSource: (body) => request('POST', '/sources', body),
updateSource: (name, body) => request('PUT', `/sources/${name}`, body),
deleteSource: (name) => request('DELETE', `/sources/${name}`),
suggestSource: (file) => {
const fd = new FormData()
fd.append('file', file)
return request('POST', '/sources/suggest', fd, true)
},
getImportLog: (name) => request('GET', `/sources/${name}/import-log`),
getStats: (name) => request('GET', `/sources/${name}/stats`),
importCSV: (name, file) => {
const fd = new FormData()
fd.append('file', file)
return request('POST', `/sources/${name}/import`, fd, true)
},
transform: (name) => request('POST', `/sources/${name}/transform`),
reprocess: (name) => request('POST', `/sources/${name}/reprocess`),
generateView: (name) => request('POST', `/sources/${name}/view`),
getFields: (name) => request('GET', `/sources/${name}/fields`),
// Rules
getRules: (source) => request('GET', `/rules/source/${source}`),
createRule: (body) => request('POST', '/rules', body),
updateRule: (id, body) => request('PUT', `/rules/${id}`, body),
deleteRule: (id) => request('DELETE', `/rules/${id}`),
testRule: (id, limit = 20) => request('GET', `/rules/${id}/test?limit=${limit}`),
// Mappings
getMappings: (source, rule) => request('GET', `/mappings/source/${source}${rule ? `?rule_name=${rule}` : ''}`),
getUnmapped: (source, rule) => request('GET', `/mappings/source/${source}/unmapped${rule ? `?rule_name=${rule}` : ''}`),
createMapping: (body) => request('POST', '/mappings', body),
bulkMappings: (mappings) => request('POST', '/mappings/bulk', { mappings }),
updateMapping: (id, body) => request('PUT', `/mappings/${id}`, body),
deleteMapping: (id) => request('DELETE', `/mappings/${id}`),
// Records
getRecords: (source, limit = 100, offset = 0) =>
request('GET', `/records/source/${source}?limit=${limit}&offset=${offset}`),
}

BIN
ui/src/assets/hero.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

1
ui/src/assets/react.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

1
ui/src/assets/vite.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 8.5 KiB

6
ui/src/index.css Normal file
View File

@ -0,0 +1,6 @@
@import "tailwindcss";
body {
margin: 0;
font-family: system-ui, -apple-system, sans-serif;
}

10
ui/src/main.jsx Normal file
View File

@ -0,0 +1,10 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
)

168
ui/src/pages/Import.jsx Normal file
View File

@ -0,0 +1,168 @@
import { useState, useEffect, useRef } from 'react'
import { api } from '../api'
export default function Import({ source }) {
const [stats, setStats] = useState(null)
const [log, setLog] = useState([])
const [result, setResult] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState('')
const [dragOver, setDragOver] = useState(false)
const fileRef = useRef()
useEffect(() => {
if (!source) return
api.getStats(source).then(setStats).catch(() => {})
api.getImportLog(source).then(setLog).catch(() => {})
}, [source])
async function handleImport(file) {
if (!file || !source) return
setLoading(true)
setError('')
setResult(null)
try {
const res = await api.importCSV(source, file)
setResult(res)
api.getStats(source).then(setStats)
api.getImportLog(source).then(setLog)
} catch (err) {
setError(err.message)
} finally {
setLoading(false)
}
}
async function handleTransform() {
if (!source) return
setLoading(true)
try {
const res = await api.transform(source)
setResult(res)
api.getStats(source).then(setStats)
} catch (err) {
setError(err.message)
} finally {
setLoading(false)
}
}
async function handleReprocess() {
if (!confirm('Reprocess all records? This will clear and reapply all transformation rules.')) return
setLoading(true)
setResult(null)
try {
const res = await api.reprocess(source)
setResult(res)
api.getStats(source).then(setStats)
} catch (err) {
setError(err.message)
} finally {
setLoading(false)
}
}
if (!source) return <div className="p-6 text-sm text-gray-400">Select a source first.</div>
return (
<div className="p-6 max-w-2xl">
<h1 className="text-xl font-semibold text-gray-800 mb-6">Import {source}</h1>
{/* Stats */}
{stats && (
<div className="flex gap-4 mb-6">
{[
{ label: 'Total records', value: stats.total_records },
{ label: 'Transformed', value: stats.transformed_records },
{ label: 'Pending', value: stats.pending_records },
].map(({ label, value }) => (
<div key={label} className="bg-white border border-gray-200 rounded px-4 py-3 flex-1 text-center">
<div className="text-2xl font-semibold text-gray-800">{value}</div>
<div className="text-xs text-gray-400 mt-0.5">{label}</div>
</div>
))}
</div>
)}
{/* Drop zone */}
<div
className={`border-2 border-dashed rounded-lg p-8 text-center mb-4 cursor-pointer transition-colors ${
dragOver ? 'border-blue-400 bg-blue-50' : 'border-gray-200 hover:border-gray-300'
}`}
onDragOver={e => { e.preventDefault(); setDragOver(true) }}
onDragLeave={() => setDragOver(false)}
onDrop={e => { e.preventDefault(); setDragOver(false); handleImport(e.dataTransfer.files[0]) }}
onClick={() => fileRef.current?.click()}
>
<input
ref={fileRef}
type="file"
accept=".csv"
className="hidden"
onChange={e => handleImport(e.target.files[0])}
/>
{loading
? <p className="text-sm text-gray-500">Importing</p>
: <p className="text-sm text-gray-400">Drop a CSV file here, or click to browse</p>
}
</div>
{error && <p className="text-sm text-red-500 mb-3">{error}</p>}
{result && (
<div className="bg-white border border-gray-200 rounded p-4 mb-4 text-sm">
{result.success !== undefined ? (
<>
<span className="text-green-600 font-medium">{result.imported} imported</span>
<span className="text-gray-400 mx-2">·</span>
<span className="text-gray-500">{result.duplicates} duplicates skipped</span>
</>
) : (
<span className="text-green-600 font-medium">{result.transformed} records transformed</span>
)}
</div>
)}
{/* Action buttons */}
<div className="flex gap-2 mb-6">
{stats && Number(stats.pending_records) > 0 && (
<button onClick={handleTransform} disabled={loading}
className="text-sm bg-green-600 text-white px-3 py-1.5 rounded hover:bg-green-700 disabled:opacity-50">
Transform {stats.pending_records} pending records
</button>
)}
{stats && Number(stats.total_records) > 0 && (
<button onClick={handleReprocess} disabled={loading}
className="text-sm bg-orange-500 text-white px-3 py-1.5 rounded hover:bg-orange-600 disabled:opacity-50">
Reprocess all records
</button>
)}
</div>
{/* Import log */}
{log.length > 0 && (
<div>
<h2 className="text-sm font-semibold text-gray-700 mb-2">Import history</h2>
<table className="w-full text-sm">
<thead>
<tr className="text-left text-xs text-gray-400 border-b border-gray-100">
<th className="pb-1 font-medium">Date</th>
<th className="pb-1 font-medium">Imported</th>
<th className="pb-1 font-medium">Duplicates</th>
</tr>
</thead>
<tbody>
{log.map(entry => (
<tr key={entry.id} className="border-b border-gray-50">
<td className="py-1.5 text-gray-500">{new Date(entry.imported_at).toLocaleString()}</td>
<td className="py-1.5 text-gray-800">{entry.records_imported}</td>
<td className="py-1.5 text-gray-400">{entry.records_duplicate}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
)
}

346
ui/src/pages/Mappings.jsx Normal file
View File

@ -0,0 +1,346 @@
import { useState, useEffect } from 'react'
import { api } from '../api'
export default function Mappings({ source }) {
const [tab, setTab] = useState('unmapped')
const [rules, setRules] = useState([])
const [selectedRule, setSelectedRule] = useState('')
const [unmapped, setUnmapped] = useState([])
const [mapped, setMapped] = useState([])
const [drafts, setDrafts] = useState({}) // key: extracted_value => [{ key, value }]
const [saving, setSaving] = useState({})
const [sampleOpen, setSampleOpen] = useState({})
const [loading, setLoading] = useState(false)
const [editingId, setEditingId] = useState(null)
const [editDrafts, setEditDrafts] = useState({})
useEffect(() => {
if (!source) return
api.getRules(source).then(r => {
setRules(r)
if (r.length > 0 && !selectedRule) setSelectedRule(r[0].name)
}).catch(() => {})
}, [source])
useEffect(() => {
if (!source) return
setLoading(true)
const rule = selectedRule || undefined
Promise.all([
api.getUnmapped(source, rule),
tab === 'mapped' ? api.getMappings(source, rule) : Promise.resolve([])
]).then(([u, m]) => {
setUnmapped(u)
setMapped(m)
setDrafts({})
}).catch(() => {}).finally(() => setLoading(false))
}, [source, selectedRule, tab])
function getDraft(extractedValue, outputField) {
return drafts[extractedValue] || [{ key: outputField, value: '' }]
}
function updateDraftKey(extractedValue, index, newKey) {
setDrafts(d => {
const current = d[extractedValue] || [{ key: '', value: '' }]
const updated = current.map((pair, i) => i === index ? { ...pair, key: newKey } : pair)
return { ...d, [extractedValue]: updated }
})
}
function updateDraftValue(extractedValue, index, newValue) {
setDrafts(d => {
const current = d[extractedValue] || [{ key: '', value: '' }]
const updated = current.map((pair, i) => i === index ? { ...pair, value: newValue } : pair)
return { ...d, [extractedValue]: updated }
})
}
function addDraftPair(extractedValue, outputField) {
setDrafts(d => {
const current = d[extractedValue] || [{ key: outputField, value: '' }]
return { ...d, [extractedValue]: [...current, { key: '', value: '' }] }
})
}
async function saveMapping(row) {
const pairs = getDraft(row.extracted_value, row.output_field)
const output = Object.fromEntries(
pairs.filter(p => p.key && p.value).map(p => [p.key, p.value])
)
if (Object.keys(output).length === 0) return
setSaving(s => ({ ...s, [row.extracted_value]: true }))
try {
await api.createMapping({
source_name: source,
rule_name: row.rule_name,
input_value: row.extracted_value,
output
})
setUnmapped(u => u.filter(x => x.extracted_value !== row.extracted_value))
setDrafts(d => { const n = { ...d }; delete n[row.extracted_value]; return n })
} catch (err) {
alert(err.message)
} finally {
setSaving(s => ({ ...s, [row.extracted_value]: false }))
}
}
async function deleteMapping(id) {
try {
await api.deleteMapping(id)
setMapped(m => m.filter(x => x.id !== id))
} catch (err) {
alert(err.message)
}
}
function startEdit(m) {
const pairs = Object.entries(m.output).map(([key, value]) => ({ key, value }))
setEditDrafts(d => ({ ...d, [m.id]: pairs.length ? pairs : [{ key: '', value: '' }] }))
setEditingId(m.id)
}
function updateEditKey(id, index, newKey) {
setEditDrafts(d => {
const pairs = d[id].map((p, i) => i === index ? { ...p, key: newKey } : p)
return { ...d, [id]: pairs }
})
}
function updateEditValue(id, index, newValue) {
setEditDrafts(d => {
const pairs = d[id].map((p, i) => i === index ? { ...p, value: newValue } : p)
return { ...d, [id]: pairs }
})
}
function addEditPair(id) {
setEditDrafts(d => ({ ...d, [id]: [...d[id], { key: '', value: '' }] }))
}
async function saveEdit(m) {
const pairs = editDrafts[m.id] || []
const output = Object.fromEntries(pairs.filter(p => p.key && p.value).map(p => [p.key, p.value]))
if (Object.keys(output).length === 0) return
setSaving(s => ({ ...s, [m.id]: true }))
try {
const updated = await api.updateMapping(m.id, { output })
setMapped(ms => ms.map(x => x.id === m.id ? updated : x))
setEditingId(null)
} catch (err) {
alert(err.message)
} finally {
setSaving(s => ({ ...s, [m.id]: false }))
}
}
if (!source) return <div className="p-6 text-sm text-gray-400">Select a source first.</div>
return (
<div className="p-6">
<div className="flex items-center justify-between mb-4">
<h1 className="text-xl font-semibold text-gray-800">Mappings {source}</h1>
</div>
{/* Rule filter */}
<div className="flex items-center gap-3 mb-4">
<select
className="text-sm border border-gray-200 rounded px-2 py-1.5 focus:outline-none focus:border-blue-400"
value={selectedRule}
onChange={e => setSelectedRule(e.target.value)}
>
<option value="">All rules</option>
{rules.map(r => <option key={r.name} value={r.name}>{r.name}</option>)}
</select>
<div className="flex bg-gray-100 rounded p-0.5">
{['unmapped', 'mapped'].map(t => (
<button key={t} onClick={() => setTab(t)}
className={`text-sm px-3 py-1 rounded transition-colors ${
tab === t ? 'bg-white text-gray-800 shadow-sm' : 'text-gray-500'
}`}>
{t === 'unmapped' ? `Unmapped${unmapped.length ? ` (${unmapped.length})` : ''}` : 'Mapped'}
</button>
))}
</div>
</div>
{loading && <p className="text-sm text-gray-400">Loading</p>}
{/* Unmapped tab */}
{!loading && tab === 'unmapped' && (
<>
{unmapped.length === 0
? <p className="text-sm text-gray-400">No unmapped values. Run a transform first, or all values are mapped.</p>
: (
<div className="space-y-2">
{unmapped.map(row => {
const pairs = getDraft(row.extracted_value, row.output_field)
const isSaving = saving[row.extracted_value]
const sampleKey = `${row.rule_name}:${row.extracted_value}`
const samples = row.sample_records || []
return (
<div key={`${row.rule_name}:${row.extracted_value}`}
className="bg-white border border-gray-200 rounded px-4 py-3">
<div className="flex items-start gap-3">
{/* Left: value info */}
<div className="flex-1 min-w-0">
<div className="flex items-baseline gap-2">
<span className="font-mono text-sm text-gray-800">{row.extracted_value}</span>
<span className="text-xs text-gray-400">{row.record_count} records</span>
<span className="text-xs text-gray-300">· {row.rule_name}</span>
</div>
{samples.length > 0 && (
<button
className="text-xs text-blue-400 hover:text-blue-600 mt-0.5"
onClick={() => setSampleOpen(s => ({ ...s, [sampleKey]: !s[sampleKey] }))}
>
{sampleOpen[sampleKey] ? 'hide samples' : 'show samples'}
</button>
)}
{sampleOpen[sampleKey] && (
<div className="mt-2 text-xs bg-gray-50 rounded p-2 space-y-1">
{samples.slice(0, 3).map((s, i) => (
<div key={i} className="font-mono text-gray-500 truncate">
{JSON.stringify(s)}
</div>
))}
</div>
)}
</div>
{/* Right: output fields */}
<div className="flex items-center gap-2 flex-shrink-0">
<div className="space-y-1">
{pairs.map((pair, i) => (
<div key={i} className="flex gap-1">
<input
className="border border-gray-200 rounded px-2 py-1 text-xs w-24 focus:outline-none focus:border-blue-400"
value={pair.key}
placeholder="key"
onChange={e => updateDraftKey(row.extracted_value, i, e.target.value)}
/>
<input
className="border border-gray-200 rounded px-2 py-1 text-xs w-32 focus:outline-none focus:border-blue-400"
value={pair.value}
placeholder="value"
onChange={e => updateDraftValue(row.extracted_value, i, e.target.value)}
onKeyDown={e => e.key === 'Enter' && saveMapping(row)}
/>
</div>
))}
<button
className="text-xs text-gray-300 hover:text-gray-500"
onClick={() => addDraftPair(row.extracted_value, row.output_field)}
>
+ field
</button>
</div>
<button
onClick={() => saveMapping(row)}
disabled={isSaving}
className="text-xs bg-blue-600 text-white px-2 py-1 rounded hover:bg-blue-700 disabled:opacity-50 self-start"
>
{isSaving ? '…' : 'Save'}
</button>
</div>
</div>
</div>
)
})}
</div>
)
}
</>
)}
{/* Mapped tab */}
{!loading && tab === 'mapped' && (
<>
{mapped.length === 0
? <p className="text-sm text-gray-400">No mappings yet.</p>
: (
<table className="w-full text-sm bg-white border border-gray-200 rounded">
<thead>
<tr className="text-left text-xs text-gray-400 border-b border-gray-100">
<th className="px-4 py-2 font-medium">Rule</th>
<th className="px-4 py-2 font-medium">Input</th>
<th className="px-4 py-2 font-medium">Output</th>
<th className="px-4 py-2 font-medium"></th>
</tr>
</thead>
<tbody>
{mapped.map(m => (
editingId === m.id ? (
<tr key={m.id} className="border-t border-gray-50 bg-blue-50">
<td className="px-4 py-2 text-xs text-gray-400">{m.rule_name}</td>
<td className="px-4 py-2 font-mono text-gray-700">{m.input_value}</td>
<td className="px-4 py-2">
<div className="space-y-1">
{(editDrafts[m.id] || []).map((pair, i) => (
<div key={i} className="flex gap-1">
<input
className="border border-gray-200 rounded px-2 py-1 text-xs w-24 focus:outline-none focus:border-blue-400"
value={pair.key}
placeholder="key"
onChange={e => updateEditKey(m.id, i, e.target.value)}
/>
<input
className="border border-gray-200 rounded px-2 py-1 text-xs w-32 focus:outline-none focus:border-blue-400"
value={pair.value}
placeholder="value"
onChange={e => updateEditValue(m.id, i, e.target.value)}
onKeyDown={e => e.key === 'Enter' && saveEdit(m)}
/>
</div>
))}
<button
className="text-xs text-gray-300 hover:text-gray-500"
onClick={() => addEditPair(m.id)}
>+ field</button>
</div>
</td>
<td className="px-4 py-2">
<div className="flex gap-2">
<button
onClick={() => saveEdit(m)}
disabled={saving[m.id]}
className="text-xs bg-blue-600 text-white px-2 py-1 rounded hover:bg-blue-700 disabled:opacity-50"
>{saving[m.id] ? '…' : 'Save'}</button>
<button
onClick={() => setEditingId(null)}
className="text-xs text-gray-400 hover:text-gray-600"
>Cancel</button>
</div>
</td>
</tr>
) : (
<tr key={m.id} className="border-t border-gray-50 hover:bg-gray-50">
<td className="px-4 py-2 text-xs text-gray-400">{m.rule_name}</td>
<td className="px-4 py-2 font-mono text-gray-700">{m.input_value}</td>
<td className="px-4 py-2 font-mono text-xs text-gray-500">
{JSON.stringify(m.output)}
</td>
<td className="px-4 py-2">
<div className="flex gap-2">
<button onClick={() => startEdit(m)}
className="text-xs text-blue-400 hover:text-blue-600">Edit</button>
<button onClick={() => deleteMapping(m.id)}
className="text-xs text-red-400 hover:text-red-600">Delete</button>
</div>
</td>
</tr>
)
))}
</tbody>
</table>
)
}
</>
)}
</div>
)
}

195
ui/src/pages/Records.jsx Normal file
View File

@ -0,0 +1,195 @@
import { useState, useEffect } from 'react'
import { api } from '../api'
export default function Records({ source }) {
const [records, setRecords] = useState([])
const [rules, setRules] = useState([])
const [mappings, setMappings] = useState([])
const [offset, setOffset] = useState(0)
const [view, setView] = useState('transformed') // 'raw' | 'transformed'
const [expanded, setExpanded] = useState(null)
const [loading, setLoading] = useState(false)
const LIMIT = 50
useEffect(() => {
if (!source) return
setOffset(0)
load(0)
api.getRules(source).then(setRules).catch(() => {})
api.getMappings(source).then(setMappings).catch(() => {})
}, [source])
async function load(off) {
setLoading(true)
try {
const res = await api.getRecords(source, LIMIT, off)
setRecords(res)
} catch (err) {
console.error(err)
} finally {
setLoading(false)
}
}
function prev() { const o = Math.max(0, offset - LIMIT); setOffset(o); load(o) }
function next() { const o = offset + LIMIT; setOffset(o); load(o) }
if (!source) return <div className="p-6 text-sm text-gray-400">Select a source first.</div>
const displayData = (record) => view === 'raw' ? record.data : (record.transformed || record.data)
// Build a lookup: rule_name + input_value mapping output
const mappingLookup = {}
for (const m of mappings) {
mappingLookup[`${m.rule_name}::${m.input_value}`] = m.output
}
return (
<div className="p-6">
<div className="flex items-center justify-between mb-4">
<h1 className="text-xl font-semibold text-gray-800">Records {source}</h1>
<div className="flex bg-gray-100 rounded p-0.5">
{['transformed', 'raw'].map(v => (
<button key={v} onClick={() => setView(v)}
className={`text-sm px-3 py-1 rounded transition-colors ${
view === v ? 'bg-white text-gray-800 shadow-sm' : 'text-gray-500'
}`}>
{v}
</button>
))}
</div>
</div>
{loading && <p className="text-sm text-gray-400">Loading</p>}
{!loading && records.length === 0 && (
<p className="text-sm text-gray-400">No records yet. Import a CSV file first.</p>
)}
{!loading && records.length > 0 && (
<>
<div className="bg-white border border-gray-200 rounded overflow-hidden mb-4">
{(() => {
const sample = displayData(records[0]) || {}
const cols = Object.keys(sample).slice(0, 8)
return (
<table className="w-full text-sm">
<thead>
<tr className="text-left text-xs text-gray-400 border-b border-gray-100 bg-gray-50">
{cols.map(c => (
<th key={c} className="px-3 py-2 font-medium truncate max-w-32">{c}</th>
))}
<th className="px-3 py-2 font-medium w-8"></th>
</tr>
</thead>
<tbody>
{records.map(record => {
const data = displayData(record) || {}
const isExpanded = expanded === record.id
return (
<>
<tr key={record.id}
className="border-t border-gray-50 hover:bg-gray-50 cursor-pointer"
onClick={() => setExpanded(isExpanded ? null : record.id)}>
{cols.map(c => (
<td key={c} className="px-3 py-2 text-xs text-gray-600 truncate max-w-32">
{String(data[c] ?? '')}
</td>
))}
<td className="px-3 py-2 text-xs text-gray-300">
{isExpanded ? '▲' : '▼'}
</td>
</tr>
{isExpanded && (
<tr key={`${record.id}-expanded`} className="bg-gray-50 border-t border-gray-100">
<td colSpan={cols.length + 1} className="px-4 py-3 space-y-4">
{/* Transformations breakdown */}
{record.transformed && rules.length > 0 && (
<div>
<p className="text-xs font-medium text-gray-500 mb-1">Transformations</p>
<table className="w-full text-xs">
<thead>
<tr className="text-left text-gray-400 border-b border-gray-100">
<th className="pb-1 font-medium pr-4">Rule</th>
<th className="pb-1 font-medium pr-4">Input value</th>
<th className="pb-1 font-medium pr-4">Extracted</th>
<th className="pb-1 font-medium">Mapped output</th>
</tr>
</thead>
<tbody>
{rules.map(rule => {
const inputVal = record.data?.[rule.field]
const extractedVal = record.transformed?.[rule.output_field]
const mappedOutput = extractedVal != null
? mappingLookup[`${rule.name}::${extractedVal}`]
: undefined
return (
<tr key={rule.id} className="border-t border-gray-50">
<td className="py-1 pr-4 font-medium text-gray-700">{rule.name}</td>
<td className="py-1 pr-4 font-mono text-gray-500 max-w-48 truncate">
{inputVal ?? <span className="text-gray-300"></span>}
</td>
<td className="py-1 pr-4 font-mono text-gray-700">
{extractedVal != null
? extractedVal
: <span className="text-gray-300"></span>}
</td>
<td className="py-1 font-mono text-gray-500">
{mappedOutput
? Object.entries(mappedOutput).map(([k, v]) => (
<span key={k} className="inline-block mr-2">
<span className="text-gray-400">{k}:</span> {v}
</span>
))
: <span className="text-gray-300"></span>}
</td>
</tr>
)
})}
</tbody>
</table>
</div>
)}
{/* Full data dump */}
<div>
<p className="text-xs font-medium text-gray-500 mb-1">
{view === 'transformed' ? 'Transformed data' : 'Raw data'}
</p>
<pre className="text-xs text-gray-600 whitespace-pre-wrap font-mono bg-white border border-gray-100 rounded p-2">
{JSON.stringify(displayData(record), null, 2)}
</pre>
{view === 'transformed' && !record.transformed && (
<p className="text-xs text-orange-400 mt-1">Not yet transformed</p>
)}
</div>
</td>
</tr>
)}
</>
)
})}
</tbody>
</table>
)
})()}
</div>
<div className="flex items-center gap-3 text-sm text-gray-500">
<button onClick={prev} disabled={offset === 0}
className="px-3 py-1 border border-gray-200 rounded hover:bg-gray-50 disabled:opacity-40">
Prev
</button>
<span>{offset + 1}{offset + records.length}</span>
<button onClick={next} disabled={records.length < LIMIT}
className="px-3 py-1 border border-gray-200 rounded hover:bg-gray-50 disabled:opacity-40">
Next
</button>
</div>
</>
)}
</div>
)
}

286
ui/src/pages/Rules.jsx Normal file
View File

@ -0,0 +1,286 @@
import { useState, useEffect } from 'react'
import { api } from '../api'
const EMPTY_FORM = { name: '', field: '', pattern: '', output_field: '', function_type: 'extract', flags: '', sequence: 0 }
function FormPanel({ form, setForm, editing, error, loading, fields, onSubmit, onCancel }) {
return (
<div className="bg-white border border-gray-200 rounded p-4 mb-4">
<h2 className="text-sm font-semibold text-gray-700 mb-3">{editing ? 'Edit rule' : 'New rule'}</h2>
<form onSubmit={onSubmit} className="space-y-3">
<div className="grid grid-cols-2 gap-3">
<div>
<label className="text-xs text-gray-500 block mb-1">Rule name</label>
<input
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm focus:outline-none focus:border-blue-400"
value={form.name} onChange={e => setForm(f => ({ ...f, name: e.target.value }))}
placeholder="e.g. First 20"
/>
</div>
<div>
<label className="text-xs text-gray-500 block mb-1">Sequence</label>
<input
type="number"
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm focus:outline-none focus:border-blue-400"
value={form.sequence} onChange={e => setForm(f => ({ ...f, sequence: parseInt(e.target.value) || 0 }))}
/>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="text-xs text-gray-500 block mb-1">Input field</label>
{fields.length > 0 ? (
<select
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm focus:outline-none focus:border-blue-400"
value={form.field} onChange={e => setForm(f => ({ ...f, field: e.target.value }))}
>
<option value=""> select field </option>
{fields.map(f => <option key={f} value={f}>{f}</option>)}
</select>
) : (
<input
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm focus:outline-none focus:border-blue-400"
value={form.field} onChange={e => setForm(f => ({ ...f, field: e.target.value }))}
placeholder="e.g. description"
/>
)}
</div>
<div>
<label className="text-xs text-gray-500 block mb-1">Output field</label>
<input
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm focus:outline-none focus:border-blue-400"
value={form.output_field} onChange={e => setForm(f => ({ ...f, output_field: e.target.value }))}
placeholder="e.g. merchant"
/>
</div>
</div>
<div>
<label className="text-xs text-gray-500 block mb-1">Pattern (regex)</label>
<input
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm font-mono focus:outline-none focus:border-blue-400"
value={form.pattern} onChange={e => setForm(f => ({ ...f, pattern: e.target.value }))}
placeholder="e.g. .{1,20}"
/>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="text-xs text-gray-500 block mb-1">Function</label>
<select
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm focus:outline-none focus:border-blue-400"
value={form.function_type} onChange={e => setForm(f => ({ ...f, function_type: e.target.value }))}
>
<option value="extract">extract</option>
<option value="replace">replace</option>
</select>
</div>
<div>
<label className="text-xs text-gray-500 block mb-1">Flags</label>
<input
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm font-mono focus:outline-none focus:border-blue-400"
value={form.flags} onChange={e => setForm(f => ({ ...f, flags: e.target.value }))}
placeholder="e.g. i"
/>
</div>
</div>
{error && <p className="text-xs text-red-500">{error}</p>}
<div className="flex gap-2">
<button type="submit" disabled={loading}
className="text-sm bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700 disabled:opacity-50">
{loading ? 'Saving…' : (editing ? 'Save' : 'Create')}
</button>
<button type="button" onClick={onCancel}
className="text-sm text-gray-500 px-3 py-1.5 rounded hover:bg-gray-100">
Cancel
</button>
</div>
</form>
</div>
)
}
export default function Rules({ source }) {
const [rules, setRules] = useState([])
const [creating, setCreating] = useState(false)
const [editing, setEditing] = useState(null)
const [form, setForm] = useState(EMPTY_FORM)
const [testResults, setTestResults] = useState({})
const [fields, setFields] = useState([])
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
useEffect(() => {
if (!source) return
api.getRules(source).then(setRules).catch(() => {})
setTestResults({})
api.getFields(source).then(f => setFields(f.map(x => x.key))).catch(() => {})
}, [source])
function startCreate() {
setForm(EMPTY_FORM)
setEditing(null)
setCreating(true)
setError('')
}
function startEdit(rule) {
setForm({
name: rule.name,
field: rule.field,
pattern: rule.pattern,
output_field: rule.output_field,
function_type: rule.function_type || 'extract',
flags: rule.flags || '',
sequence: rule.sequence,
})
setEditing(rule.id)
setCreating(false)
setError('')
}
async function handleSubmit(e) {
e.preventDefault()
setError('')
setLoading(true)
try {
if (editing) {
await api.updateRule(editing, { ...form, source_name: source })
} else {
await api.createRule({ ...form, source_name: source })
}
const updated = await api.getRules(source)
setRules(updated)
setCreating(false)
setEditing(null)
} catch (err) {
setError(err.message)
} finally {
setLoading(false)
}
}
async function handleDelete(id) {
if (!confirm('Delete this rule and all its mappings?')) return
try {
await api.deleteRule(id)
setRules(r => r.filter(x => x.id !== id))
setTestResults(t => { const n = { ...t }; delete n[id]; return n })
} catch (err) {
alert(err.message)
}
}
async function handleTest(id) {
try {
const res = await api.testRule(id)
setTestResults(t => ({ ...t, [id]: res.results }))
} catch (err) {
alert(err.message)
}
}
async function handleToggle(rule) {
try {
await api.updateRule(rule.id, { enabled: !rule.enabled })
setRules(r => r.map(x => x.id === rule.id ? { ...x, enabled: !x.enabled } : x))
} catch (err) {
alert(err.message)
}
}
if (!source) return <div className="p-6 text-sm text-gray-400">Select a source first.</div>
return (
<div className="p-6 max-w-3xl">
<div className="flex items-center justify-between mb-6">
<h1 className="text-xl font-semibold text-gray-800">Rules {source}</h1>
<button onClick={startCreate}
className="text-sm bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700">
New rule
</button>
</div>
{creating && (
<FormPanel
form={form} setForm={setForm} editing={false}
error={error} loading={loading} fields={fields}
onSubmit={handleSubmit}
onCancel={() => { setCreating(false); setError('') }}
/>
)}
{rules.length === 0 && !creating && (
<p className="text-sm text-gray-400">No rules yet. Add a regex rule to start extracting values.</p>
)}
<div className="space-y-3">
{rules.map(rule => (
<div key={rule.id} className="bg-white border border-gray-200 rounded">
<div className="flex items-center gap-3 px-4 py-3">
<button
onClick={() => handleToggle(rule)}
className={`w-8 h-4 rounded-full transition-colors ${rule.enabled ? 'bg-blue-500' : 'bg-gray-200'}`}
title={rule.enabled ? 'Disable' : 'Enable'}
/>
<div className="flex-1 min-w-0">
<span className="font-medium text-gray-800 text-sm">{rule.name}</span>
<span className="text-gray-400 text-xs ml-2">seq {rule.sequence}</span>
<div className="text-xs text-gray-400 mt-0.5">
<span className="font-mono">{rule.field}</span>
<span className="mx-1"></span>
<span className="font-mono bg-gray-50 px-1 rounded">{rule.pattern}</span>
{rule.flags && <span className="text-blue-400 ml-1">/{rule.flags}</span>}
<span className="mx-1"></span>
<span className="font-mono">{rule.output_field}</span>
{rule.function_type === 'replace' && <span className="ml-1 text-orange-400">(replace)</span>}
</div>
</div>
<div className="flex gap-2">
<button onClick={() => handleTest(rule.id)}
className="text-xs text-blue-500 hover:text-blue-700">Test</button>
<button onClick={() => editing === rule.id ? setEditing(null) : startEdit(rule)}
className="text-xs text-gray-400 hover:text-gray-600">Edit</button>
<button onClick={() => handleDelete(rule.id)}
className="text-xs text-red-400 hover:text-red-600">Delete</button>
</div>
</div>
{editing === rule.id && (
<div className="px-4 pb-4">
<FormPanel
form={form} setForm={setForm} editing={true}
error={error} loading={loading} fields={fields}
onSubmit={handleSubmit}
onCancel={() => setEditing(null)}
/>
</div>
)}
{testResults[rule.id] && (
<div className="border-t border-gray-100 px-4 py-3">
<p className="text-xs text-gray-500 mb-2">Test results (last 20 records)</p>
<table className="w-full text-xs">
<thead>
<tr className="text-left text-gray-400">
<th className="pb-1 font-medium w-1/2">Raw value</th>
<th className="pb-1 font-medium">Extracted</th>
</tr>
</thead>
<tbody>
{testResults[rule.id].slice(0, 10).map((r, i) => (
<tr key={i} className="border-t border-gray-50">
<td className="py-1 font-mono text-gray-500 truncate max-w-0 w-1/2 pr-2">{r.raw_value}</td>
<td className={`py-1 font-mono ${r.extracted_value ? 'text-gray-800' : 'text-gray-300'}`}>
{r.extracted_value ?? '—'}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
))}
</div>
</div>
)
}

415
ui/src/pages/Sources.jsx Normal file
View File

@ -0,0 +1,415 @@
import { useState, useEffect, useRef } from 'react'
import { api } from '../api'
const FIELD_TYPES = ['text', 'numeric', 'date']
function SourceDetail({ source, onClose, onDeleted, setSources, setSource }) {
const [dedup, setDedup] = useState(source.dedup_fields?.join(', ') || '')
const [schemaFields, setSchemaFields] = useState(source.config?.fields || [])
const [stats, setStats] = useState(null)
const [saving, setSaving] = useState(false)
const [reprocessing, setReprocessing] = useState(false)
const [generating, setGenerating] = useState(false)
const [result, setResult] = useState('')
const [error, setError] = useState('')
const [viewName, setViewName] = useState(source.config?.fields?.length ? `dfv.${source.name}` : '')
const [availableFields, setAvailableFields] = useState([])
useEffect(() => {
api.getStats(source.name).then(setStats).catch(() => {})
api.getFields(source.name).then(setAvailableFields).catch(() => {})
}, [source.name])
async function handleSave(e) {
e.preventDefault()
setSaving(true)
setError('')
try {
const dedup_fields = dedup.split(',').map(s => s.trim()).filter(Boolean)
const config = { ...(source.config || {}), fields: schemaFields.filter(f => f.name) }
await api.updateSource(source.name, { dedup_fields, config })
const updated = await api.getSources()
setSources(updated)
setResult('Saved.')
} catch (err) {
setError(err.message)
} finally {
setSaving(false)
}
}
async function handleGenerateView() {
setGenerating(true)
setResult('')
setError('')
try {
// Save schema first, then generate view from the saved config
const dedup_fields = dedup.split(',').map(s => s.trim()).filter(Boolean)
const config = { ...(source.config || {}), fields: schemaFields.filter(f => f.name) }
await api.updateSource(source.name, { dedup_fields, config })
const res = await api.generateView(source.name)
if (res.success) {
setViewName(res.view)
setResult(`View created: ${res.view}`)
} else {
setError(res.error)
}
} catch (err) {
setError(err.message)
} finally {
setGenerating(false)
}
}
async function handleReprocess() {
if (!confirm(`Reprocess all records for "${source.name}"? This will clear and reapply all transformations.`)) return
setReprocessing(true)
setResult('')
setError('')
try {
const res = await api.reprocess(source.name)
setResult(`Reprocessed ${res.transformed} records.`)
api.getStats(source.name).then(setStats).catch(() => {})
} catch (err) {
setError(err.message)
} finally {
setReprocessing(false)
}
}
return (
<div className="mt-1 bg-white border border-gray-200 rounded p-4 space-y-4">
{/* Stats */}
{stats && (
<div className="flex gap-4 text-xs">
<span className="text-gray-500"><span className="font-medium text-gray-800">{stats.total_records}</span> total</span>
<span className="text-gray-500"><span className="font-medium text-gray-800">{stats.transformed_records}</span> transformed</span>
<span className="text-gray-500"><span className="font-medium text-gray-800">{stats.pending_records}</span> pending</span>
</div>
)}
{/* Unified field table */}
{availableFields.length > 0 && (
<div className="pt-2 border-t border-gray-100 space-y-2">
<table className="w-full text-xs">
<thead>
<tr className="text-left text-gray-400 border-b border-gray-100">
<th className="pb-1 font-medium">Key</th>
<th className="pb-1 font-medium">Origin</th>
<th className="pb-1 font-medium">Type</th>
<th className="pb-1 font-medium text-center">Dedup</th>
<th className="pb-1 font-medium text-center">In view</th>
</tr>
</thead>
<tbody>
{availableFields.map(f => {
const isRaw = f.origins.includes('raw')
const dedupChecked = dedup.split(',').map(s => s.trim()).includes(f.key)
const schemaEntry = schemaFields.find(sf => sf.name === f.key)
const inView = !!schemaEntry
return (
<tr key={f.key} className="border-t border-gray-50">
<td className="py-1 font-mono text-gray-700">{f.key}</td>
<td className="py-1 text-gray-400">{f.origins.join(', ')}</td>
<td className="py-1">
{inView && (
<select
className="border border-gray-200 rounded px-1 py-0.5 text-xs focus:outline-none focus:border-blue-400"
value={schemaEntry.type}
onChange={e => setSchemaFields(sf =>
sf.map(s => s.name === f.key ? { ...s, type: e.target.value } : s)
)}
>
{FIELD_TYPES.map(t => <option key={t} value={t}>{t}</option>)}
</select>
)}
</td>
<td className="py-1 text-center">
{isRaw && (
<input
type="checkbox"
checked={dedupChecked}
onChange={e => {
const current = dedup.split(',').map(s => s.trim()).filter(Boolean)
const next = e.target.checked
? [...current, f.key]
: current.filter(k => k !== f.key)
setDedup(next.join(', '))
}}
/>
)}
</td>
<td className="py-1 text-center">
<input
type="checkbox"
checked={inView}
onChange={e => {
if (e.target.checked) {
setSchemaFields(sf => [...sf, { name: f.key, type: 'text' }])
} else {
setSchemaFields(sf => sf.filter(s => s.name !== f.key))
}
}}
/>
</td>
</tr>
)
})}
</tbody>
</table>
<div className="flex items-center gap-3 pt-1">
<form onSubmit={handleSave}>
<button type="submit" disabled={saving}
className="text-sm bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700 disabled:opacity-50">
{saving ? 'Saving…' : 'Save'}
</button>
</form>
{schemaFields.length > 0 && (
<>
<button
onClick={handleGenerateView}
disabled={generating}
className="text-xs bg-green-600 text-white px-2 py-1.5 rounded hover:bg-green-700 disabled:opacity-50"
>
{generating ? 'Generating…' : 'Generate view'}
</button>
{viewName && (
<code className="text-xs bg-gray-100 px-2 py-1 rounded text-gray-600">{viewName}</code>
)}
</>
)}
</div>
</div>
)}
{/* Save button when no fields loaded yet */}
{availableFields.length === 0 && (
<form onSubmit={handleSave}>
<button type="submit" disabled={saving}
className="text-sm bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700 disabled:opacity-50">
{saving ? 'Saving…' : 'Save'}
</button>
</form>
)}
{/* Reprocess */}
<div className="flex items-center gap-3 pt-2 border-t border-gray-100">
<button
onClick={handleReprocess}
disabled={reprocessing}
className="text-sm bg-orange-500 text-white px-3 py-1.5 rounded hover:bg-orange-600 disabled:opacity-50"
>
{reprocessing ? 'Reprocessing…' : 'Reprocess all records'}
</button>
<span className="text-xs text-gray-400">Clears and reruns all transformation rules</span>
</div>
{result && <p className="text-xs text-green-600">{result}</p>}
{error && <p className="text-xs text-red-500">{error}</p>}
<div className="pt-1 border-t border-gray-100 flex justify-between">
<button onClick={onClose} className="text-xs text-gray-400 hover:text-gray-600">Close</button>
<button
onClick={() => onDeleted(source.name)}
className="text-xs text-red-400 hover:text-red-600"
>
Delete source
</button>
</div>
</div>
)
}
export default function Sources({ sources, setSources, setSource }) {
const [creating, setCreating] = useState(false)
const [expanded, setExpanded] = useState(null)
const [form, setForm] = useState({ name: '', dedup_fields: '', fields: [], schema: [] })
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
const fileRef = useRef()
async function handleSuggest(e) {
const file = e.target.files[0]
if (!file) return
try {
const suggestion = await api.suggestSource(file)
setForm(f => ({
...f,
fields: suggestion.fields,
dedup_fields: '',
schema: suggestion.fields.map(f => ({ name: f.name, type: f.type }))
}))
} catch (err) {
setError(err.message)
}
}
async function handleCreate(e) {
e.preventDefault()
setError('')
const dedup = form.dedup_fields.split(',').map(s => s.trim()).filter(Boolean)
if (!form.name || dedup.length === 0) {
setError('Name and at least one dedup field required')
return
}
setLoading(true)
try {
const config = form.schema.length > 0 ? { fields: form.schema } : {}
await api.createSource({ name: form.name, dedup_fields: dedup, config })
const updated = await api.getSources()
setSources(updated)
setSource(form.name)
setForm({ name: '', dedup_fields: '', fields: [], schema: [] })
setCreating(false)
} catch (err) {
setError(err.message)
} finally {
setLoading(false)
}
}
async function handleDeleted(name) {
if (!confirm(`Delete source "${name}" and all its data?`)) return
try {
await api.deleteSource(name)
const updated = await api.getSources()
setSources(updated)
setExpanded(null)
if (updated.length > 0) setSource(updated[0].name)
} catch (err) {
alert(err.message)
}
}
return (
<div className="p-6 max-w-2xl">
<div className="flex items-center justify-between mb-6">
<h1 className="text-xl font-semibold text-gray-800">Sources</h1>
<button
onClick={() => { setCreating(true); setError(''); setExpanded(null) }}
className="text-sm bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700"
>
New source
</button>
</div>
{sources.length === 0 && !creating && (
<p className="text-gray-500 text-sm">No sources yet. Create one to get started.</p>
)}
<div className="space-y-2">
{sources.map(s => (
<div key={s.name}>
<div
className="flex items-center justify-between bg-white border border-gray-200 rounded px-4 py-3 cursor-pointer hover:bg-gray-50"
onClick={() => setExpanded(expanded === s.name ? null : s.name)}
>
<div>
<span className="font-medium text-gray-800">{s.name}</span>
<span className="ml-3 text-xs text-gray-400">dedup: {s.dedup_fields?.join(', ')}</span>
</div>
<span className="text-xs text-gray-300">{expanded === s.name ? '▲' : '▼'}</span>
</div>
{expanded === s.name && (
<SourceDetail
source={s}
onClose={() => setExpanded(null)}
onDeleted={handleDeleted}
setSources={setSources}
setSource={setSource}
/>
)}
</div>
))}
</div>
{/* Create form */}
{creating && (
<div className="mt-6 bg-white border border-gray-200 rounded p-4">
<h2 className="text-sm font-semibold text-gray-700 mb-3">New source</h2>
<div className="mb-4">
<label className="text-xs text-gray-500 block mb-1">Upload a CSV to auto-detect fields</label>
<input type="file" accept=".csv" ref={fileRef} onChange={handleSuggest} className="text-sm text-gray-600" />
</div>
<form onSubmit={handleCreate} className="space-y-3">
<div>
<label className="text-xs text-gray-500 block mb-1">Source name</label>
<input
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm focus:outline-none focus:border-blue-400"
value={form.name}
onChange={e => setForm(f => ({ ...f, name: e.target.value }))}
placeholder="e.g. chase, dcard"
/>
</div>
{form.fields.length > 0 && (
<div>
<label className="text-xs text-gray-500 block mb-1">Detected fields check to use as dedup keys</label>
<table className="w-full text-xs">
<thead>
<tr className="text-left text-gray-400 border-b border-gray-100">
<th className="pb-1 font-medium">Field</th>
<th className="pb-1 font-medium">Type</th>
<th className="pb-1 font-medium text-center">Dedup</th>
</tr>
</thead>
<tbody>
{form.fields.map(f => (
<tr key={f.name} className="border-t border-gray-50">
<td className="py-1 font-mono text-gray-700">{f.name}</td>
<td className="py-1 text-gray-400">{f.type}</td>
<td className="py-1 text-center">
<input
type="checkbox"
checked={form.dedup_fields.split(',').map(s => s.trim()).includes(f.name)}
onChange={e => {
const current = form.dedup_fields.split(',').map(s => s.trim()).filter(Boolean)
const next = e.target.checked
? [...current, f.name]
: current.filter(n => n !== f.name)
setForm(ff => ({ ...ff, dedup_fields: next.join(', ') }))
}}
/>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
{form.fields.length === 0 && (
<div>
<label className="text-xs text-gray-500 block mb-1">Dedup fields (comma-separated)</label>
<input
className="w-full border border-gray-200 rounded px-3 py-1.5 text-sm focus:outline-none focus:border-blue-400"
value={form.dedup_fields}
onChange={e => setForm(f => ({ ...f, dedup_fields: e.target.value }))}
placeholder="e.g. date, amount, description"
/>
</div>
)}
{error && <p className="text-xs text-red-500">{error}</p>}
<div className="flex gap-2">
<button type="submit" disabled={loading}
className="text-sm bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700 disabled:opacity-50">
{loading ? 'Creating…' : 'Create'}
</button>
<button type="button"
onClick={() => { setCreating(false); setError(''); setForm({ name: '', dedup_fields: '', fields: [], schema: [] }) }}
className="text-sm text-gray-500 px-3 py-1.5 rounded hover:bg-gray-100">
Cancel
</button>
</div>
</form>
</div>
)}
</div>
)
}

15
ui/vite.config.js Normal file
View File

@ -0,0 +1,15 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [react(), tailwindcss()],
server: {
proxy: {
'/api': 'http://localhost:3020'
}
},
build: {
outDir: '../public'
}
})