moved most api stuff to web-api and built out a better monitoring solution for web-app

This commit is contained in:
Boki 2025-06-23 09:01:29 -04:00
parent fbff428e90
commit da1c52a841
45 changed files with 2986 additions and 312 deletions

View file

@ -1,5 +1,5 @@
import { DataTable } from '@/components/ui';
import { ColumnDef } from '@tanstack/react-table';
import type { ColumnDef } from '@tanstack/react-table';
import React from 'react';
interface PortfolioItem {

View file

@ -1,8 +1,8 @@
import { Dialog, DialogContent, DialogHeader, DialogTitle, Button } from '@/components/ui';
import { Button, Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui';
import { useCallback } from 'react';
import { CreateExchangeRequest, AddExchangeDialogProps } from '../types';
import { validateExchangeForm } from '../utils/validation';
import { useFormValidation } from '../hooks/useFormValidation';
import type { AddExchangeDialogProps, CreateExchangeRequest } from '../types';
import { validateExchangeForm } from '../utils/validation';
const initialFormData: CreateExchangeRequest = {
code: '',

View file

@ -1,7 +1,7 @@
import { Dialog, DialogContent, DialogHeader, DialogTitle, Button } from '@/components/ui';
import { useCallback, useEffect, useState } from 'react';
import { useExchanges } from '../hooks/useExchanges';
import { CreateProviderMappingRequest } from '../types';
import type { CreateProviderMappingRequest } from '../types/index';
interface AddProviderMappingDialogProps {
isOpen: boolean;

View file

@ -1,216 +0,0 @@
import { Dialog, Transition } from '@headlessui/react';
import { XMarkIcon } from '@heroicons/react/24/outline';
import React, { useState } from 'react';
import { AddSourceRequest } from '../types';
interface AddSourceDialogProps {
isOpen: boolean;
onClose: () => void;
onAddSource: (request: AddSourceRequest) => Promise<void>;
exchangeId: string;
exchangeName: string;
}
export function AddSourceDialog({
isOpen,
onClose,
onAddSource,
exchangeName,
}: AddSourceDialogProps) {
const [source, setSource] = useState('');
const [sourceCode, setSourceCode] = useState('');
const [id, setId] = useState('');
const [name, setName] = useState('');
const [code, setCode] = useState('');
const [aliases, setAliases] = useState('');
const [loading, setLoading] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!source || !sourceCode || !id || !name || !code) {return;}
setLoading(true);
try {
await onAddSource({
source,
source_code: sourceCode,
mapping: {
id,
name,
code,
aliases: aliases
.split(',')
.map(a => a.trim())
.filter(Boolean),
},
});
// Reset form
setSource('');
setSourceCode('');
setId('');
setName('');
setCode('');
setAliases('');
} catch (_error) {
// TODO: Implement proper error handling/toast notification
// eslint-disable-next-line no-console
console.error('Error adding source:', _error);
} finally {
setLoading(false);
}
};
return (
<Transition appear show={isOpen} as={React.Fragment}>
<Dialog as="div" className="relative z-50" onClose={onClose}>
<Transition.Child
as={React.Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 bg-black bg-opacity-25" />
</Transition.Child>
<div className="fixed inset-0 overflow-y-auto">
<div className="flex min-h-full items-center justify-center p-4 text-center">
<Transition.Child
as={React.Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0 scale-95"
enterTo="opacity-100 scale-100"
leave="ease-in duration-200"
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95"
>
<Dialog.Panel className="w-full max-w-md transform overflow-hidden rounded-lg bg-background border border-border p-6 text-left align-middle shadow-xl transition-all">
<div className="flex items-center justify-between mb-4">
<Dialog.Title className="text-lg font-medium text-text-primary">
Add Source to {exchangeName}
</Dialog.Title>
<button
onClick={onClose}
className="text-text-muted hover:text-text-primary transition-colors"
>
<XMarkIcon className="h-5 w-5" />
</button>
</div>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label className="block text-sm font-medium text-text-primary mb-1">
Source Provider
</label>
<select
value={source}
onChange={e => setSource(e.target.value)}
className="w-full bg-surface border border-border rounded px-3 py-2 text-text-primary focus:ring-1 focus:ring-primary-500 focus:border-primary-500"
required
>
<option value="">Select a source</option>
<option value="ib">Interactive Brokers</option>
<option value="alpaca">Alpaca</option>
<option value="polygon">Polygon</option>
<option value="yahoo">Yahoo Finance</option>
<option value="alpha_vantage">Alpha Vantage</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-text-primary mb-1">
Source Code
</label>
<input
type="text"
value={sourceCode}
onChange={e => setSourceCode(e.target.value)}
className="w-full bg-surface border border-border rounded px-3 py-2 text-text-primary focus:ring-1 focus:ring-primary-500 focus:border-primary-500"
placeholder="e.g., IB, ALP, POLY"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-text-primary mb-1">
Source ID
</label>
<input
type="text"
value={id}
onChange={e => setId(e.target.value)}
className="w-full bg-surface border border-border rounded px-3 py-2 text-text-primary focus:ring-1 focus:ring-primary-500 focus:border-primary-500"
placeholder="e.g., NYSE, NASDAQ"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-text-primary mb-1">
Source Name
</label>
<input
type="text"
value={name}
onChange={e => setName(e.target.value)}
className="w-full bg-surface border border-border rounded px-3 py-2 text-text-primary focus:ring-1 focus:ring-primary-500 focus:border-primary-500"
placeholder="e.g., New York Stock Exchange"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-text-primary mb-1">
Source Code
</label>
<input
type="text"
value={code}
onChange={e => setCode(e.target.value)}
className="w-full bg-surface border border-border rounded px-3 py-2 text-text-primary focus:ring-1 focus:ring-primary-500 focus:border-primary-500"
placeholder="e.g., NYSE"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-text-primary mb-1">
Aliases (comma-separated)
</label>
<input
type="text"
value={aliases}
onChange={e => setAliases(e.target.value)}
className="w-full bg-surface border border-border rounded px-3 py-2 text-text-primary focus:ring-1 focus:ring-primary-500 focus:border-primary-500"
placeholder="e.g., NYSE, New York, Big Board"
/>
</div>
<div className="flex justify-end space-x-3 pt-4">
<button
type="button"
onClick={onClose}
className="px-4 py-2 border border-border text-text-secondary hover:text-text-primary hover:bg-surface-secondary rounded transition-colors"
>
Cancel
</button>
<button
type="submit"
disabled={loading || !source || !sourceCode || !id || !name || !code}
className="px-4 py-2 bg-primary-500 text-white rounded hover:bg-primary-600 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
{loading ? 'Adding...' : 'Add Source'}
</button>
</div>
</form>
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</Dialog>
</Transition>
);
}

View file

@ -1,12 +1,12 @@
import { DataTable } from '@/components/ui';
import { PlusIcon, TrashIcon } from '@heroicons/react/24/outline';
import { ColumnDef } from '@tanstack/react-table';
import type { ColumnDef } from '@tanstack/react-table';
import { useCallback, useMemo, useState } from 'react';
import { useExchanges } from '../hooks/useExchanges';
import { Exchange, EditingCell, AddProviderMappingDialogState, DeleteDialogState } from '../types';
import type { AddProviderMappingDialogState, DeleteDialogState, EditingCell, Exchange } from '../types';
import { formatDate, formatProviderMapping, getProviderMappingColor, sortProviderMappings } from '../utils/formatters';
import { AddProviderMappingDialog } from './AddProviderMappingDialog';
import { DeleteExchangeDialog } from './DeleteExchangeDialog';
import { sortProviderMappings, getProviderMappingColor, formatProviderMapping, formatDate } from '../utils/formatters';
export function ExchangesTable() {
const {
@ -235,7 +235,7 @@ export function ExchangesTable() {
cell: ({ getValue, row }) => {
const totalMappings = parseInt(getValue() as string) || 0;
const activeMappings = parseInt(row.original.active_mapping_count) || 0;
const _verifiedMappings = parseInt(row.original.verified_mapping_count) || 0;
// const _verifiedMappings = parseInt(row.original.verified_mapping_count) || 0;
// Get provider mappings directly from the exchange data
const mappings = row.original.provider_mappings || [];
@ -329,7 +329,7 @@ export function ExchangesTable() {
<h3 className="text-danger font-medium mb-2">Error Loading Exchanges</h3>
<p className="text-text-secondary text-sm">{error}</p>
<p className="text-text-muted text-xs mt-2">
Make sure the web-api service is running on localhost:4000
Make sure the web-api service is running on localhost:2003
</p>
</div>
);

View file

@ -1,4 +1,4 @@
export { AddSourceDialog } from './AddSourceDialog';
export { AddProviderMappingDialog } from './AddProviderMappingDialog';
export { AddExchangeDialog } from './AddExchangeDialog';
export { DeleteExchangeDialog } from './DeleteExchangeDialog';

View file

@ -1,6 +1,6 @@
import { useCallback, useEffect, useState } from 'react';
import { exchangeApi } from '../services/exchangeApi';
import {
import type {
CreateExchangeRequest,
CreateProviderMappingRequest,
Exchange,
@ -10,7 +10,7 @@ import {
ProviderMapping,
UpdateExchangeRequest,
UpdateProviderMappingRequest,
} from '../types';
} from '../types/index';
export function useExchanges() {
const [exchanges, setExchanges] = useState<Exchange[]>([]);

View file

@ -1,5 +1,5 @@
import { useCallback, useState } from 'react';
import { FormErrors } from '../types';
import type { FormErrors } from '../types';
export function useFormValidation<T>(initialData: T, validateFn: (data: T) => FormErrors) {
const [formData, setFormData] = useState<T>(initialData);

View file

@ -1,4 +1,4 @@
import {
import type {
ApiResponse,
CreateExchangeRequest,
CreateProviderMappingRequest,
@ -9,13 +9,13 @@ import {
ProviderMapping,
UpdateExchangeRequest,
UpdateProviderMappingRequest,
} from '../types';
} from '../types/index';
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000/api';
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:2003';
class ExchangeApiService {
private async request<T>(endpoint: string, options?: RequestInit): Promise<ApiResponse<T>> {
const url = `${API_BASE_URL}${endpoint}`;
const url = `${API_BASE_URL}/api${endpoint}`;
const response = await fetch(url, {
headers: {

View file

@ -1,7 +1,154 @@
// Re-export all types from organized files
export * from './api.types';
export * from './request.types';
export * from './component.types';
// API Response types
export interface ApiResponse<T = unknown> {
success: boolean;
data?: T;
error?: string;
message?: string;
total?: number;
}
// Base entity types
export interface BaseEntity {
id: string;
created_at: string;
updated_at: string;
}
export interface ProviderMapping extends BaseEntity {
provider: string;
provider_exchange_code: string;
provider_exchange_name: string;
master_exchange_id: string;
country_code: string | null;
currency: string | null;
confidence: number;
active: boolean;
verified: boolean;
auto_mapped: boolean;
master_exchange_code?: string;
master_exchange_name?: string;
master_exchange_active?: boolean;
}
export interface Exchange extends BaseEntity {
code: string;
name: string;
country: string;
currency: string;
active: boolean;
visible: boolean;
provider_mapping_count: string;
active_mapping_count: string;
verified_mapping_count: string;
providers: string | null;
provider_mappings: ProviderMapping[];
}
export interface ExchangeDetails {
exchange: Exchange;
provider_mappings: ProviderMapping[];
}
export interface ProviderExchange {
provider_exchange_code: string;
provider_exchange_name: string;
country_code: string | null;
currency: string | null;
symbol_count: number | null;
}
export interface ExchangeStats {
total_exchanges: string;
active_exchanges: string;
countries: string;
currencies: string;
total_provider_mappings: string;
active_provider_mappings: string;
verified_provider_mappings: string;
providers: string;
}
// Request types for API calls
export interface CreateExchangeRequest {
code: string;
name: string;
country: string;
currency: string;
active?: boolean;
}
export interface UpdateExchangeRequest {
name?: string;
active?: boolean;
visible?: boolean;
country?: string;
currency?: string;
}
export interface CreateProviderMappingRequest {
provider: string;
provider_exchange_code: string;
provider_exchange_name?: string;
master_exchange_id: string;
country_code?: string;
currency?: string;
confidence?: number;
active?: boolean;
verified?: boolean;
}
export interface UpdateProviderMappingRequest {
active?: boolean;
verified?: boolean;
confidence?: number;
master_exchange_id?: string;
}
// Component-specific types
export interface EditingCell {
id: string;
field: string;
}
export interface AddProviderMappingDialogState {
exchangeId: string;
exchangeName: string;
}
export interface DeleteDialogState {
exchangeId: string;
exchangeName: string;
providerMappingCount: number;
}
export interface FormErrors {
[key: string]: string;
}
// Dialog props interfaces
export interface BaseDialogProps {
isOpen: boolean;
onClose: () => void;
}
export interface AddExchangeDialogProps extends BaseDialogProps {
onCreateExchange: (request: CreateExchangeRequest) => Promise<void>;
}
export interface AddProviderMappingDialogProps extends BaseDialogProps {
exchangeId: string;
exchangeName: string;
onCreateMapping: (
request: CreateProviderMappingRequest
) => Promise<unknown>;
}
export interface DeleteExchangeDialogProps extends BaseDialogProps {
exchangeId: string;
exchangeName: string;
providerMappingCount: number;
onConfirmDelete: (exchangeId: string) => Promise<boolean>;
}
// Legacy compatibility - can be removed later
export type ExchangesApiResponse<T = unknown> = import('./api.types').ApiResponse<T>;
export type ExchangesApiResponse<T = unknown> = ApiResponse<T>;

View file

@ -1,4 +1,4 @@
import { ProviderMapping } from '../types';
import type { ProviderMapping } from '../types';
export function formatDate(dateString: string): string {
return new Date(dateString).toLocaleDateString();

View file

@ -1,4 +1,4 @@
import { FormErrors } from '../types';
import type { FormErrors } from '../types';
export function validateExchangeForm(data: {
code: string;

View file

@ -0,0 +1,104 @@
/**
* System Monitoring Page
*/
import React, { useState } from 'react';
import { useSystemHealth, useCacheStats, useQueueStats, useDatabaseStats } from './hooks';
import { SystemHealthCard, CacheStatsCard, QueueStatsTable, DatabaseStatsGrid } from './components';
export function MonitoringPage() {
const [refreshInterval, setRefreshInterval] = useState(5000); // 5 seconds default
const { data: health, loading: healthLoading, error: healthError } = useSystemHealth(refreshInterval);
const { data: cache, loading: cacheLoading, error: cacheError } = useCacheStats(refreshInterval);
const { data: queues, loading: queuesLoading, error: queuesError } = useQueueStats(refreshInterval);
const { data: databases, loading: dbLoading, error: dbError } = useDatabaseStats(refreshInterval);
const handleRefreshIntervalChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
setRefreshInterval(Number(e.target.value));
};
if (healthLoading || cacheLoading || queuesLoading || dbLoading) {
return (
<div className="flex items-center justify-center h-screen">
<div className="text-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto"></div>
<p className="mt-4 text-gray-600">Loading monitoring data...</p>
</div>
</div>
);
}
const hasErrors = healthError || cacheError || queuesError || dbError;
return (
<div className="p-6">
<div className="mb-6">
<div className="flex items-center justify-between">
<h1 className="text-2xl font-bold">System Monitoring</h1>
<div className="flex items-center space-x-4">
<div className="flex items-center space-x-2">
<label htmlFor="refresh-interval" className="text-sm text-gray-600">
Refresh interval:
</label>
<select
id="refresh-interval"
value={refreshInterval}
onChange={handleRefreshIntervalChange}
className="text-sm border rounded px-2 py-1"
>
<option value={0}>Manual</option>
<option value={5000}>5 seconds</option>
<option value={10000}>10 seconds</option>
<option value={30000}>30 seconds</option>
<option value={60000}>1 minute</option>
</select>
</div>
</div>
</div>
</div>
{hasErrors && (
<div className="mb-6 p-4 bg-red-50 border border-red-200 rounded-lg">
<h3 className="font-semibold text-red-800 mb-2">Errors occurred while fetching data:</h3>
<ul className="list-disc list-inside text-sm text-red-700 space-y-1">
{healthError && <li>System Health: {healthError}</li>}
{cacheError && <li>Cache Stats: {cacheError}</li>}
{queuesError && <li>Queue Stats: {queuesError}</li>}
{dbError && <li>Database Stats: {dbError}</li>}
</ul>
</div>
)}
<div className="space-y-6">
{/* System Health */}
{health && (
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
<div className="lg:col-span-1">
<SystemHealthCard health={health} />
</div>
<div className="lg:col-span-2">
{cache && <CacheStatsCard stats={cache} />}
</div>
</div>
)}
{/* Database Stats */}
{databases && databases.length > 0 && (
<div>
<h2 className="text-xl font-semibold mb-4">Database Connections</h2>
<DatabaseStatsGrid databases={databases} />
</div>
)}
{/* Queue Stats */}
{queues && queues.length > 0 && (
<div>
<h2 className="text-xl font-semibold mb-4">Queue Status</h2>
<QueueStatsTable queues={queues} />
</div>
)}
</div>
</div>
);
}

View file

@ -0,0 +1,96 @@
/**
* Cache Statistics Card Component
*/
import React from 'react';
import { Card } from '../../../components/ui/Card';
import type { CacheStats } from '../types';
interface CacheStatsCardProps {
stats: CacheStats;
}
export function CacheStatsCard({ stats }: CacheStatsCardProps) {
const formatBytes = (bytes: number) => {
const mb = bytes / 1024 / 1024;
return mb.toFixed(2) + ' MB';
};
const hitRate = stats.stats && (stats.stats.hits + stats.stats.misses) > 0
? (stats.stats.hits / (stats.stats.hits + stats.stats.misses) * 100).toFixed(1)
: '0';
return (
<Card className="p-6">
<div className="flex items-center justify-between mb-4">
<h3 className="text-lg font-semibold">Cache (Dragonfly)</h3>
<span className={`px-2 py-1 rounded text-xs font-medium ${
stats.connected ? 'text-green-600 bg-green-100' : 'text-red-600 bg-red-100'
}`}>
{stats.connected ? 'Connected' : 'Disconnected'}
</span>
</div>
{stats.connected ? (
<div className="space-y-4">
{stats.memoryUsage && (
<div className="grid grid-cols-2 gap-4">
<div>
<div className="text-sm text-gray-600">Memory Used</div>
<div className="text-xl font-semibold">{formatBytes(stats.memoryUsage.used)}</div>
</div>
<div>
<div className="text-sm text-gray-600">Peak Memory</div>
<div className="text-xl font-semibold">{formatBytes(stats.memoryUsage.peak)}</div>
</div>
</div>
)}
{stats.stats && (
<>
<div className="grid grid-cols-2 gap-4">
<div>
<div className="text-sm text-gray-600">Hit Rate</div>
<div className="text-xl font-semibold">{hitRate}%</div>
</div>
<div>
<div className="text-sm text-gray-600">Total Keys</div>
<div className="text-xl font-semibold">{stats.stats.keys.toLocaleString()}</div>
</div>
</div>
<div className="grid grid-cols-2 gap-4 text-sm">
<div>
<span className="text-gray-600">Hits:</span> {stats.stats.hits.toLocaleString()}
</div>
<div>
<span className="text-gray-600">Misses:</span> {stats.stats.misses.toLocaleString()}
</div>
{stats.stats.evictedKeys !== undefined && (
<div>
<span className="text-gray-600">Evicted:</span> {stats.stats.evictedKeys.toLocaleString()}
</div>
)}
{stats.stats.expiredKeys !== undefined && (
<div>
<span className="text-gray-600">Expired:</span> {stats.stats.expiredKeys.toLocaleString()}
</div>
)}
</div>
</>
)}
{stats.uptime && (
<div className="text-sm text-gray-600">
Uptime: {Math.floor(stats.uptime / 3600)}h {Math.floor((stats.uptime % 3600) / 60)}m
</div>
)}
</div>
) : (
<div className="text-center py-8 text-gray-500">
Cache service is not available
</div>
)}
</Card>
);
}

View file

@ -0,0 +1,104 @@
/**
* Database Statistics Grid Component
*/
import React from 'react';
import { Card } from '../../../components/ui/Card';
import type { DatabaseStats } from '../types';
interface DatabaseStatsGridProps {
databases: DatabaseStats[];
}
export function DatabaseStatsGrid({ databases }: DatabaseStatsGridProps) {
const getDbIcon = (type: string) => {
switch (type) {
case 'postgres':
return '🐘';
case 'mongodb':
return '🍃';
case 'questdb':
return '⚡';
default:
return '💾';
}
};
return (
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
{databases.map((db) => (
<Card key={db.type} className="p-6">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center space-x-2">
<span className="text-2xl">{getDbIcon(db.type)}</span>
<h4 className="font-semibold">{db.name}</h4>
</div>
<span className={`px-2 py-1 rounded text-xs font-medium ${
db.connected ? 'text-green-600 bg-green-100' : 'text-red-600 bg-red-100'
}`}>
{db.connected ? 'Connected' : 'Disconnected'}
</span>
</div>
{db.connected ? (
<div className="space-y-3">
{db.latency !== undefined && (
<div>
<div className="text-sm text-gray-600">Latency</div>
<div className="text-lg font-semibold">{db.latency}ms</div>
</div>
)}
{db.pool && (
<div>
<div className="text-sm text-gray-600 mb-1">Connection Pool</div>
<div className="grid grid-cols-2 gap-2 text-sm">
<div>
<span className="text-gray-600">Active:</span> {db.pool.active}
</div>
<div>
<span className="text-gray-600">Idle:</span> {db.pool.idle}
</div>
<div>
<span className="text-gray-600">Size:</span> {db.pool.size}
</div>
<div>
<span className="text-gray-600">Max:</span> {db.pool.max}
</div>
</div>
{db.pool.max > 0 && (
<div className="mt-2">
<div className="w-full bg-gray-200 rounded-full h-2">
<div
className="bg-blue-600 h-2 rounded-full"
style={{ width: `${(db.pool.size / db.pool.max) * 100}%` }}
/>
</div>
<div className="text-xs text-gray-500 mt-1">
{((db.pool.size / db.pool.max) * 100).toFixed(0)}% utilized
</div>
</div>
)}
</div>
)}
{db.type === 'mongodb' && db.stats && (
<div className="text-xs text-gray-600">
<div>Version: {db.stats.version}</div>
{db.stats.connections && (
<div>Connections: {db.stats.connections.current}/{db.stats.connections.available}</div>
)}
</div>
)}
</div>
) : (
<div className="text-center py-4 text-gray-500">
Database is not available
</div>
)}
</Card>
))}
</div>
);
}

View file

@ -0,0 +1,77 @@
/**
* Queue Statistics Table Component
*/
import React from 'react';
import { Card } from '../../../components/ui/Card';
import type { QueueStats } from '../types';
interface QueueStatsTableProps {
queues: QueueStats[];
}
export function QueueStatsTable({ queues }: QueueStatsTableProps) {
const totalJobs = (queue: QueueStats) => {
const { jobs } = queue;
return jobs.waiting + jobs.active + jobs.completed + jobs.failed + jobs.delayed + jobs.paused;
};
return (
<Card className="p-6">
<h3 className="text-lg font-semibold mb-4">Queue Statistics</h3>
{queues.length > 0 ? (
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead>
<tr className="border-b">
<th className="text-left py-2 px-3">Queue</th>
<th className="text-center py-2 px-3">Status</th>
<th className="text-right py-2 px-3">Waiting</th>
<th className="text-right py-2 px-3">Active</th>
<th className="text-right py-2 px-3">Completed</th>
<th className="text-right py-2 px-3">Failed</th>
<th className="text-right py-2 px-3">Delayed</th>
<th className="text-right py-2 px-3">Total</th>
</tr>
</thead>
<tbody>
{queues.map((queue) => (
<tr key={queue.name} className="border-b hover:bg-gray-50">
<td className="py-2 px-3 font-medium">{queue.name}</td>
<td className="py-2 px-3 text-center">
<span className={`inline-block w-2 h-2 rounded-full ${
queue.connected ? 'bg-green-500' : 'bg-red-500'
}`} />
</td>
<td className="py-2 px-3 text-right">{queue.jobs.waiting.toLocaleString()}</td>
<td className="py-2 px-3 text-right">
{queue.jobs.active > 0 ? (
<span className="text-blue-600 font-medium">{queue.jobs.active}</span>
) : (
queue.jobs.active
)}
</td>
<td className="py-2 px-3 text-right">{queue.jobs.completed.toLocaleString()}</td>
<td className="py-2 px-3 text-right">
{queue.jobs.failed > 0 ? (
<span className="text-red-600 font-medium">{queue.jobs.failed}</span>
) : (
queue.jobs.failed
)}
</td>
<td className="py-2 px-3 text-right">{queue.jobs.delayed.toLocaleString()}</td>
<td className="py-2 px-3 text-right font-medium">{totalJobs(queue).toLocaleString()}</td>
</tr>
))}
</tbody>
</table>
</div>
) : (
<div className="text-center py-8 text-gray-500">
No queue data available
</div>
)}
</Card>
);
}

View file

@ -0,0 +1,87 @@
/**
* System Health Card Component
*/
import React from 'react';
import { Card } from '../../../components/ui/Card';
import type { SystemHealth } from '../types';
interface SystemHealthCardProps {
health: SystemHealth;
}
export function SystemHealthCard({ health }: SystemHealthCardProps) {
const statusColor = {
healthy: 'text-green-600 bg-green-100',
degraded: 'text-yellow-600 bg-yellow-100',
unhealthy: 'text-red-600 bg-red-100',
}[health.status];
const formatUptime = (ms: number) => {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (days > 0) return `${days}d ${hours % 24}h`;
if (hours > 0) return `${hours}h ${minutes % 60}m`;
if (minutes > 0) return `${minutes}m ${seconds % 60}s`;
return `${seconds}s`;
};
const formatBytes = (bytes: number) => {
const gb = bytes / 1024 / 1024 / 1024;
return gb.toFixed(2) + ' GB';
};
return (
<Card className="p-6">
<div className="flex items-center justify-between mb-4">
<h3 className="text-lg font-semibold">System Health</h3>
<span className={`px-3 py-1 rounded-full text-sm font-medium ${statusColor}`}>
{health.status.toUpperCase()}
</span>
</div>
<div className="space-y-4">
<div>
<div className="text-sm text-gray-600">Uptime</div>
<div className="text-2xl font-semibold">{formatUptime(health.uptime)}</div>
</div>
<div>
<div className="text-sm text-gray-600">Memory Usage</div>
<div className="mt-1">
<div className="flex justify-between text-sm">
<span>{formatBytes(health.memory.used)} / {formatBytes(health.memory.total)}</span>
<span>{health.memory.percentage.toFixed(1)}%</span>
</div>
<div className="mt-1 w-full bg-gray-200 rounded-full h-2">
<div
className="bg-blue-600 h-2 rounded-full"
style={{ width: `${health.memory.percentage}%` }}
/>
</div>
</div>
</div>
{health.errors && health.errors.length > 0 && (
<div>
<div className="text-sm text-gray-600 mb-2">Issues</div>
<ul className="space-y-1">
{health.errors.map((error, index) => (
<li key={index} className="text-sm text-red-600">
{error}
</li>
))}
</ul>
</div>
)}
<div className="text-xs text-gray-500">
Last updated: {new Date(health.timestamp).toLocaleTimeString()}
</div>
</div>
</Card>
);
}

View file

@ -0,0 +1,8 @@
/**
* Monitoring components exports
*/
export { SystemHealthCard } from './SystemHealthCard';
export { CacheStatsCard } from './CacheStatsCard';
export { QueueStatsTable } from './QueueStatsTable';
export { DatabaseStatsGrid } from './DatabaseStatsGrid';

View file

@ -0,0 +1,5 @@
/**
* Monitoring hooks exports
*/
export * from './useMonitoring';

View file

@ -0,0 +1,123 @@
/**
* Custom hook for monitoring data
*/
import { useState, useEffect, useCallback } from 'react';
import { monitoringApi } from '../services/monitoringApi';
import type { SystemHealth, CacheStats, QueueStats, DatabaseStats } from '../types';
export function useSystemHealth(refreshInterval: number = 5000) {
const [data, setData] = useState<SystemHealth | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchData = useCallback(async () => {
try {
const health = await monitoringApi.getSystemHealth();
setData(health);
setError(null);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch system health');
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchData();
if (refreshInterval > 0) {
const interval = setInterval(fetchData, refreshInterval);
return () => clearInterval(interval);
}
}, [fetchData, refreshInterval]);
return { data, loading, error, refetch: fetchData };
}
export function useCacheStats(refreshInterval: number = 5000) {
const [data, setData] = useState<CacheStats | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchData = useCallback(async () => {
try {
const stats = await monitoringApi.getCacheStats();
setData(stats);
setError(null);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch cache stats');
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchData();
if (refreshInterval > 0) {
const interval = setInterval(fetchData, refreshInterval);
return () => clearInterval(interval);
}
}, [fetchData, refreshInterval]);
return { data, loading, error, refetch: fetchData };
}
export function useQueueStats(refreshInterval: number = 5000) {
const [data, setData] = useState<QueueStats[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchData = useCallback(async () => {
try {
const result = await monitoringApi.getQueueStats();
setData(result.queues);
setError(null);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch queue stats');
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchData();
if (refreshInterval > 0) {
const interval = setInterval(fetchData, refreshInterval);
return () => clearInterval(interval);
}
}, [fetchData, refreshInterval]);
return { data, loading, error, refetch: fetchData };
}
export function useDatabaseStats(refreshInterval: number = 5000) {
const [data, setData] = useState<DatabaseStats[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchData = useCallback(async () => {
try {
const result = await monitoringApi.getDatabaseStats();
setData(result.databases);
setError(null);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch database stats');
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchData();
if (refreshInterval > 0) {
const interval = setInterval(fetchData, refreshInterval);
return () => clearInterval(interval);
}
}, [fetchData, refreshInterval]);
return { data, loading, error, refetch: fetchData };
}

View file

@ -0,0 +1,8 @@
/**
* Monitoring feature exports
*/
export { MonitoringPage } from './MonitoringPage';
export * from './types';
export * from './hooks/useMonitoring';
export * from './services/monitoringApi';

View file

@ -0,0 +1,87 @@
/**
* Monitoring API Service
*/
import type { SystemHealth, CacheStats, QueueStats, DatabaseStats } from '../types';
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:2003';
const MONITORING_BASE = `${API_BASE_URL}/api/system/monitoring`;
export const monitoringApi = {
/**
* Get overall system health
*/
async getSystemHealth(): Promise<SystemHealth> {
const response = await fetch(MONITORING_BASE);
if (!response.ok) {
throw new Error(`Failed to fetch system health: ${response.statusText}`);
}
return response.json();
},
/**
* Get cache statistics
*/
async getCacheStats(): Promise<CacheStats> {
const response = await fetch(`${MONITORING_BASE}/cache`);
if (!response.ok) {
throw new Error(`Failed to fetch cache stats: ${response.statusText}`);
}
return response.json();
},
/**
* Get queue statistics
*/
async getQueueStats(): Promise<{ queues: QueueStats[] }> {
const response = await fetch(`${MONITORING_BASE}/queues`);
if (!response.ok) {
throw new Error(`Failed to fetch queue stats: ${response.statusText}`);
}
return response.json();
},
/**
* Get specific queue statistics
*/
async getQueueStatsByName(name: string): Promise<QueueStats> {
const response = await fetch(`${MONITORING_BASE}/queues/${name}`);
if (!response.ok) {
throw new Error(`Failed to fetch queue ${name} stats: ${response.statusText}`);
}
return response.json();
},
/**
* Get database statistics
*/
async getDatabaseStats(): Promise<{ databases: DatabaseStats[] }> {
const response = await fetch(`${MONITORING_BASE}/databases`);
if (!response.ok) {
throw new Error(`Failed to fetch database stats: ${response.statusText}`);
}
return response.json();
},
/**
* Get specific database statistics
*/
async getDatabaseStatsByType(type: 'postgres' | 'mongodb' | 'questdb'): Promise<DatabaseStats> {
const response = await fetch(`${MONITORING_BASE}/databases/${type}`);
if (!response.ok) {
throw new Error(`Failed to fetch ${type} stats: ${response.statusText}`);
}
return response.json();
},
/**
* Get detailed cache info
*/
async getCacheInfo(): Promise<{ parsed: CacheStats; raw: string }> {
const response = await fetch(`${MONITORING_BASE}/cache/info`);
if (!response.ok) {
throw new Error(`Failed to fetch cache info: ${response.statusText}`);
}
return response.json();
},
};

View file

@ -0,0 +1,80 @@
/**
* Monitoring types for system health and metrics
*/
export interface CacheStats {
provider: string;
connected: boolean;
uptime?: number;
memoryUsage?: {
used: number;
peak: number;
total?: number;
};
stats?: {
hits: number;
misses: number;
keys: number;
evictedKeys?: number;
expiredKeys?: number;
};
info?: Record<string, any>;
}
export interface QueueStats {
name: string;
connected: boolean;
jobs: {
waiting: number;
active: number;
completed: number;
failed: number;
delayed: number;
paused: number;
};
workers?: {
count: number;
concurrency: number;
};
throughput?: {
processed: number;
failed: number;
avgProcessingTime?: number;
};
}
export interface DatabaseStats {
type: 'postgres' | 'mongodb' | 'questdb';
name: string;
connected: boolean;
latency?: number;
pool?: {
size: number;
active: number;
idle: number;
waiting?: number;
max: number;
};
stats?: Record<string, any>;
}
export interface SystemHealth {
status: 'healthy' | 'degraded' | 'unhealthy';
timestamp: string;
uptime: number;
memory: {
used: number;
total: number;
percentage: number;
};
cpu?: {
usage: number;
loadAverage?: number[];
};
services: {
cache: CacheStats;
queues: QueueStats[];
databases: DatabaseStats[];
};
errors?: string[];
}

View file

@ -0,0 +1,406 @@
import { useState, useEffect } from 'react';
import {
ArrowPathIcon,
CircleStackIcon,
CloudArrowDownIcon,
ExclamationTriangleIcon,
CheckCircleIcon,
ClockIcon,
} from '@heroicons/react/24/outline';
import { usePipeline } from './hooks/usePipeline';
import type { PipelineOperation } from './types';
const operations: PipelineOperation[] = [
// Symbol operations
{
id: 'sync-qm-symbols',
name: 'Sync QM Symbols',
description: 'Sync symbols from QuestionsAndMethods API',
endpoint: '/symbols',
method: 'POST',
category: 'sync',
},
{
id: 'sync-provider-symbols',
name: 'Sync Provider Symbols',
description: 'Sync symbols from a specific provider',
endpoint: '/symbols/:provider',
method: 'POST',
category: 'sync',
params: { provider: 'yahoo' }, // Default provider
},
// Exchange operations
{
id: 'sync-qm-exchanges',
name: 'Sync QM Exchanges',
description: 'Sync exchanges from QuestionsAndMethods API',
endpoint: '/exchanges',
method: 'POST',
category: 'sync',
},
{
id: 'sync-all-exchanges',
name: 'Sync All Exchanges',
description: 'Sync all exchanges with optional clear',
endpoint: '/exchanges/all',
method: 'POST',
category: 'sync',
},
// Provider mapping operations
{
id: 'sync-qm-provider-mappings',
name: 'Sync QM Provider Mappings',
description: 'Sync provider mappings from QuestionsAndMethods',
endpoint: '/provider-mappings/qm',
method: 'POST',
category: 'sync',
},
{
id: 'sync-ib-exchanges',
name: 'Sync IB Exchanges',
description: 'Sync exchanges from Interactive Brokers',
endpoint: '/provider-mappings/ib',
method: 'POST',
category: 'sync',
},
// Maintenance operations
{
id: 'clear-postgresql',
name: 'Clear PostgreSQL Data',
description: 'Clear exchange and provider mapping data',
endpoint: '/clear/postgresql',
method: 'POST',
category: 'maintenance',
dangerous: true,
},
];
export function PipelinePage() {
const {
loading,
error,
lastJobResult,
syncQMSymbols,
syncProviderSymbols,
syncQMExchanges,
syncAllExchanges,
syncQMProviderMappings,
syncIBExchanges,
clearPostgreSQLData,
getExchangeStats,
getProviderMappingStats,
} = usePipeline();
const [selectedProvider, setSelectedProvider] = useState('yahoo');
const [clearFirst, setClearFirst] = useState(false);
const [clearDataType, setClearDataType] = useState<'all' | 'exchanges' | 'provider_mappings'>('all');
const [stats, setStats] = useState<{ exchanges?: any; providerMappings?: any }>({});
// Load stats on mount
useEffect(() => {
loadStats();
}, []);
const loadStats = async () => {
const [exchangeStats, mappingStats] = await Promise.all([
getExchangeStats(),
getProviderMappingStats(),
]);
setStats({
exchanges: exchangeStats,
providerMappings: mappingStats,
});
};
const handleOperation = async (op: PipelineOperation) => {
switch (op.id) {
case 'sync-qm-symbols':
await syncQMSymbols();
break;
case 'sync-provider-symbols':
await syncProviderSymbols(selectedProvider);
break;
case 'sync-qm-exchanges':
await syncQMExchanges();
break;
case 'sync-all-exchanges':
await syncAllExchanges(clearFirst);
break;
case 'sync-qm-provider-mappings':
await syncQMProviderMappings();
break;
case 'sync-ib-exchanges':
await syncIBExchanges();
break;
case 'clear-postgresql':
if (confirm(`Are you sure you want to clear ${clearDataType} data? This action cannot be undone.`)) {
await clearPostgreSQLData(clearDataType);
}
break;
}
// Reload stats after operation
await loadStats();
};
const getCategoryIcon = (category: string) => {
switch (category) {
case 'sync':
return <CloudArrowDownIcon className="h-5 w-5" />;
case 'maintenance':
return <ExclamationTriangleIcon className="h-5 w-5" />;
default:
return <CircleStackIcon className="h-5 w-5" />;
}
};
const getCategoryColor = (category: string) => {
switch (category) {
case 'sync':
return 'text-primary-400';
case 'maintenance':
return 'text-warning';
default:
return 'text-text-secondary';
}
};
return (
<div className="p-6 max-w-7xl mx-auto">
<div className="mb-8">
<h1 className="text-2xl font-bold text-text-primary mb-2">Data Pipeline Management</h1>
<p className="text-text-secondary">
Manage data synchronization and maintenance operations
</p>
</div>
{/* Stats Overview */}
{(stats.exchanges || stats.providerMappings) && (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 mb-8">
{stats.exchanges && (
<div className="bg-surface-secondary p-4 rounded-lg border border-border">
<h3 className="text-lg font-semibold text-text-primary mb-3">Exchange Statistics</h3>
<div className="space-y-2 text-sm">
<div className="flex justify-between">
<span className="text-text-secondary">Total Exchanges:</span>
<span className="text-text-primary font-medium">{stats.exchanges.totalExchanges}</span>
</div>
<div className="flex justify-between">
<span className="text-text-secondary">Active Exchanges:</span>
<span className="text-success font-medium">{stats.exchanges.activeExchanges}</span>
</div>
<div className="flex justify-between">
<span className="text-text-secondary">Total Provider Mappings:</span>
<span className="text-text-primary font-medium">{stats.exchanges.totalProviderMappings}</span>
</div>
<div className="flex justify-between">
<span className="text-text-secondary">Active Mappings:</span>
<span className="text-success font-medium">{stats.exchanges.activeProviderMappings}</span>
</div>
</div>
</div>
)}
{stats.providerMappings && (
<div className="bg-surface-secondary p-4 rounded-lg border border-border">
<h3 className="text-lg font-semibold text-text-primary mb-3">Provider Mapping Statistics</h3>
<div className="space-y-2 text-sm">
<div className="flex justify-between">
<span className="text-text-secondary">Coverage:</span>
<span className="text-primary-400 font-medium">
{stats.providerMappings.coveragePercentage?.toFixed(1)}%
</span>
</div>
<div className="flex justify-between">
<span className="text-text-secondary">Verified Mappings:</span>
<span className="text-success font-medium">{stats.providerMappings.verifiedMappings}</span>
</div>
<div className="flex justify-between">
<span className="text-text-secondary">Auto-mapped:</span>
<span className="text-text-primary font-medium">{stats.providerMappings.autoMappedCount}</span>
</div>
{stats.providerMappings.mappingsByProvider && (
<div className="mt-2 pt-2 border-t border-border">
<span className="text-text-secondary text-xs">By Provider:</span>
<div className="mt-1 flex flex-wrap gap-2">
{Object.entries(stats.providerMappings.mappingsByProvider).map(([provider, count]) => (
<span key={provider} className="text-xs bg-surface px-2 py-1 rounded">
{provider}: {count}
</span>
))}
</div>
</div>
)}
</div>
</div>
)}
</div>
)}
{/* Status Messages */}
{error && (
<div className="mb-6 p-4 bg-danger/10 border border-danger/20 rounded-lg">
<div className="flex items-center gap-2">
<ExclamationTriangleIcon className="h-5 w-5 text-danger" />
<span className="text-danger">{error}</span>
</div>
</div>
)}
{lastJobResult && (
<div className={`mb-6 p-4 rounded-lg border ${
lastJobResult.success
? 'bg-success/10 border-success/20'
: 'bg-danger/10 border-danger/20'
}`}>
<div className="flex items-center gap-2">
{lastJobResult.success ? (
<CheckCircleIcon className="h-5 w-5 text-success" />
) : (
<ExclamationTriangleIcon className="h-5 w-5 text-danger" />
)}
<span className={lastJobResult.success ? 'text-success' : 'text-danger'}>
{lastJobResult.message || lastJobResult.error}
</span>
{lastJobResult.jobId && (
<span className="text-text-muted text-xs ml-auto">
Job ID: {lastJobResult.jobId}
</span>
)}
</div>
</div>
)}
{/* Operations Grid */}
<div className="space-y-6">
{/* Sync Operations */}
<div>
<h2 className="text-lg font-semibold text-text-primary mb-4 flex items-center gap-2">
<CloudArrowDownIcon className="h-5 w-5 text-primary-400" />
Sync Operations
</h2>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{operations.filter(op => op.category === 'sync').map(op => (
<div
key={op.id}
className="bg-surface-secondary p-4 rounded-lg border border-border hover:border-primary-500/50 transition-colors"
>
<div className="flex items-start justify-between mb-2">
<h3 className="font-medium text-text-primary">{op.name}</h3>
<div className={getCategoryColor(op.category)}>
{getCategoryIcon(op.category)}
</div>
</div>
<p className="text-sm text-text-secondary mb-3">{op.description}</p>
{/* Special inputs for specific operations */}
{op.id === 'sync-provider-symbols' && (
<div className="mb-3">
<label className="block text-xs text-text-muted mb-1">Provider</label>
<select
value={selectedProvider}
onChange={e => setSelectedProvider(e.target.value)}
className="w-full px-2 py-1 text-sm bg-surface border border-border rounded focus:ring-1 focus:ring-primary-500 focus:border-primary-500"
>
<option value="yahoo">Yahoo</option>
<option value="qm">QuestionsAndMethods</option>
<option value="ib">Interactive Brokers</option>
</select>
</div>
)}
{op.id === 'sync-all-exchanges' && (
<div className="mb-3">
<label className="flex items-center gap-2 text-sm">
<input
type="checkbox"
checked={clearFirst}
onChange={e => setClearFirst(e.target.checked)}
className="rounded border-border text-primary-500 focus:ring-primary-500"
/>
<span className="text-text-secondary">Clear existing data first</span>
</label>
</div>
)}
<button
onClick={() => handleOperation(op)}
disabled={loading}
className="w-full px-3 py-2 bg-primary-500/20 text-primary-400 rounded text-sm font-medium hover:bg-primary-500/30 disabled:opacity-50 disabled:cursor-not-allowed transition-colors flex items-center justify-center gap-2"
>
{loading ? (
<>
<ArrowPathIcon className="h-4 w-4 animate-spin" />
Processing...
</>
) : (
<>
<CloudArrowDownIcon className="h-4 w-4" />
Execute
</>
)}
</button>
</div>
))}
</div>
</div>
{/* Maintenance Operations */}
<div>
<h2 className="text-lg font-semibold text-text-primary mb-4 flex items-center gap-2">
<ExclamationTriangleIcon className="h-5 w-5 text-warning" />
Maintenance Operations
</h2>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{operations.filter(op => op.category === 'maintenance').map(op => (
<div
key={op.id}
className="bg-surface-secondary p-4 rounded-lg border border-warning/20 hover:border-warning/50 transition-colors"
>
<div className="flex items-start justify-between mb-2">
<h3 className="font-medium text-text-primary">{op.name}</h3>
<div className={getCategoryColor(op.category)}>
{getCategoryIcon(op.category)}
</div>
</div>
<p className="text-sm text-text-secondary mb-3">{op.description}</p>
{op.id === 'clear-postgresql' && (
<div className="mb-3">
<label className="block text-xs text-text-muted mb-1">Data Type</label>
<select
value={clearDataType}
onChange={e => setClearDataType(e.target.value as any)}
className="w-full px-2 py-1 text-sm bg-surface border border-border rounded focus:ring-1 focus:ring-warning focus:border-warning"
>
<option value="all">All Data</option>
<option value="exchanges">Exchanges Only</option>
<option value="provider_mappings">Provider Mappings Only</option>
</select>
</div>
)}
<button
onClick={() => handleOperation(op)}
disabled={loading}
className="w-full px-3 py-2 bg-warning/20 text-warning rounded text-sm font-medium hover:bg-warning/30 disabled:opacity-50 disabled:cursor-not-allowed transition-colors flex items-center justify-center gap-2"
>
{loading ? (
<>
<ArrowPathIcon className="h-4 w-4 animate-spin" />
Processing...
</>
) : (
<>
<ExclamationTriangleIcon className="h-4 w-4" />
Execute
</>
)}
</button>
</div>
))}
</div>
</div>
</div>
</div>
);
}

View file

@ -0,0 +1,159 @@
import { useCallback, useState } from 'react';
import { pipelineApi } from '../services/pipelineApi';
import type {
DataClearType,
ExchangeStats,
PipelineJobResult,
ProviderMappingStats,
} from '../types';
export function usePipeline() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [lastJobResult, setLastJobResult] = useState<PipelineJobResult | null>(null);
const executeOperation = useCallback(async (
operation: () => Promise<PipelineJobResult>
): Promise<boolean> => {
try {
setLoading(true);
setError(null);
const result = await operation();
setLastJobResult(result);
if (!result.success) {
setError(result.error || 'Operation failed');
return false;
}
return true;
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
setError(errorMessage);
setLastJobResult({ success: false, error: errorMessage });
return false;
} finally {
setLoading(false);
}
}, []);
// Symbol sync operations
const syncQMSymbols = useCallback(
() => executeOperation(() => pipelineApi.syncQMSymbols()),
[executeOperation]
);
const syncProviderSymbols = useCallback(
(provider: string) => executeOperation(() => pipelineApi.syncProviderSymbols(provider)),
[executeOperation]
);
// Exchange sync operations
const syncQMExchanges = useCallback(
() => executeOperation(() => pipelineApi.syncQMExchanges()),
[executeOperation]
);
const syncAllExchanges = useCallback(
(clearFirst: boolean = false) =>
executeOperation(() => pipelineApi.syncAllExchanges(clearFirst)),
[executeOperation]
);
// Provider mapping sync operations
const syncQMProviderMappings = useCallback(
() => executeOperation(() => pipelineApi.syncQMProviderMappings()),
[executeOperation]
);
const syncIBExchanges = useCallback(
() => executeOperation(() => pipelineApi.syncIBExchanges()),
[executeOperation]
);
// Maintenance operations
const clearPostgreSQLData = useCallback(
(dataType: DataClearType = 'all') =>
executeOperation(() => pipelineApi.clearPostgreSQLData(dataType)),
[executeOperation]
);
// Status and stats operations
const getSyncStatus = useCallback(async () => {
try {
setLoading(true);
setError(null);
const result = await pipelineApi.getSyncStatus();
return result;
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Failed to get sync status';
setError(errorMessage);
return null;
} finally {
setLoading(false);
}
}, []);
const getExchangeStats = useCallback(async (): Promise<ExchangeStats | null> => {
try {
setLoading(true);
setError(null);
const result = await pipelineApi.getExchangeStats();
if (result.success && result.data) {
return result.data as ExchangeStats;
}
setError(result.error || 'Failed to get exchange stats');
return null;
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Failed to get exchange stats';
setError(errorMessage);
return null;
} finally {
setLoading(false);
}
}, []);
const getProviderMappingStats = useCallback(async (): Promise<ProviderMappingStats | null> => {
try {
setLoading(true);
setError(null);
const result = await pipelineApi.getProviderMappingStats();
if (result.success && result.data) {
return result.data as ProviderMappingStats;
}
setError(result.error || 'Failed to get provider mapping stats');
return null;
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Failed to get provider mapping stats';
setError(errorMessage);
return null;
} finally {
setLoading(false);
}
}, []);
return {
// State
loading,
error,
lastJobResult,
// Symbol operations
syncQMSymbols,
syncProviderSymbols,
// Exchange operations
syncQMExchanges,
syncAllExchanges,
// Provider mapping operations
syncQMProviderMappings,
syncIBExchanges,
// Maintenance operations
clearPostgreSQLData,
// Status and stats operations
getSyncStatus,
getExchangeStats,
getProviderMappingStats,
};
}

View file

@ -0,0 +1,3 @@
export { PipelinePage } from './PipelinePage';
export * from './hooks/usePipeline';
export * from './types';

View file

@ -0,0 +1,82 @@
import type {
DataClearType,
PipelineJobResult,
PipelineStatsResult,
} from '../types';
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:2003';
class PipelineApiService {
private async request<T = any>(
endpoint: string,
options?: RequestInit
): Promise<T> {
const url = `${API_BASE_URL}/pipeline${endpoint}`;
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
...options?.headers,
},
...options,
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || `HTTP ${response.status}: ${response.statusText}`);
}
return data;
}
// Symbol sync operations
async syncQMSymbols(): Promise<PipelineJobResult> {
return this.request<PipelineJobResult>('/symbols', { method: 'POST' });
}
async syncProviderSymbols(provider: string): Promise<PipelineJobResult> {
return this.request<PipelineJobResult>(`/symbols/${provider}`, { method: 'POST' });
}
// Exchange sync operations
async syncQMExchanges(): Promise<PipelineJobResult> {
return this.request<PipelineJobResult>('/exchanges', { method: 'POST' });
}
async syncAllExchanges(clearFirst: boolean = false): Promise<PipelineJobResult> {
const params = clearFirst ? '?clear=true' : '';
return this.request<PipelineJobResult>(`/exchanges/all${params}`, { method: 'POST' });
}
// Provider mapping sync operations
async syncQMProviderMappings(): Promise<PipelineJobResult> {
return this.request<PipelineJobResult>('/provider-mappings/qm', { method: 'POST' });
}
async syncIBExchanges(): Promise<PipelineJobResult> {
return this.request<PipelineJobResult>('/provider-mappings/ib', { method: 'POST' });
}
// Status and maintenance operations
async getSyncStatus(): Promise<PipelineJobResult> {
return this.request<PipelineJobResult>('/status');
}
async clearPostgreSQLData(dataType: DataClearType = 'all'): Promise<PipelineJobResult> {
const params = `?type=${dataType}`;
return this.request<PipelineJobResult>(`/clear/postgresql${params}`, { method: 'POST' });
}
// Statistics operations
async getExchangeStats(): Promise<PipelineStatsResult> {
return this.request<PipelineStatsResult>('/stats/exchanges');
}
async getProviderMappingStats(): Promise<PipelineStatsResult> {
return this.request<PipelineStatsResult>('/stats/provider-mappings');
}
}
// Export singleton instance
export const pipelineApi = new PipelineApiService();

View file

@ -0,0 +1,58 @@
// Pipeline API types
export interface PipelineJobResult {
success: boolean;
jobId?: string;
message?: string;
error?: string;
data?: any;
}
export interface PipelineStatsResult {
success: boolean;
data?: any;
error?: string;
}
export interface ExchangeStats {
totalExchanges: number;
activeExchanges: number;
totalProviderMappings: number;
activeProviderMappings: number;
verifiedProviderMappings: number;
providers: string[];
}
export interface ProviderMappingStats {
totalMappings: number;
activeMappings: number;
verifiedMappings: number;
autoMappedCount: number;
mappingsByProvider: Record<string, number>;
coveragePercentage: number;
}
export interface SyncStatus {
lastSync?: {
symbols?: string;
exchanges?: string;
providerMappings?: string;
};
pendingJobs?: number;
activeJobs?: number;
completedJobs?: number;
failedJobs?: number;
}
export type DataClearType = 'exchanges' | 'provider_mappings' | 'all';
export interface PipelineOperation {
id: string;
name: string;
description: string;
endpoint: string;
method: 'GET' | 'POST';
category: 'sync' | 'stats' | 'maintenance';
dangerous?: boolean;
params?: Record<string, any>;
}