feat(StockDataService): Add unified StockDataService and BaseProviderService with new stockdata interfaces, provider integrations, tests and README updates
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@fin.cx/opendata',
|
||||
version: '3.1.0',
|
||||
version: '3.2.0',
|
||||
description: 'A comprehensive TypeScript library for accessing business data and real-time financial information. Features include German company data management with MongoDB integration, JSONL bulk processing, automated Handelsregister interactions, and real-time stock market data from multiple providers.'
|
||||
}
|
||||
|
||||
296
ts/stocks/classes.baseproviderservice.ts
Normal file
296
ts/stocks/classes.baseproviderservice.ts
Normal file
@@ -0,0 +1,296 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
|
||||
/**
|
||||
* Base provider entry for tracking provider state
|
||||
*/
|
||||
export interface IBaseProviderEntry<TProvider> {
|
||||
provider: TProvider;
|
||||
config: IBaseProviderConfig;
|
||||
lastError?: Error;
|
||||
lastErrorTime?: Date;
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base provider configuration
|
||||
*/
|
||||
export interface IBaseProviderConfig {
|
||||
enabled: boolean;
|
||||
priority: number;
|
||||
timeout?: number;
|
||||
retryAttempts?: number;
|
||||
retryDelay?: number;
|
||||
cacheTTL?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base provider interface
|
||||
*/
|
||||
export interface IBaseProvider {
|
||||
name: string;
|
||||
priority: number;
|
||||
isAvailable(): Promise<boolean>;
|
||||
readonly requiresAuth: boolean;
|
||||
readonly rateLimit?: {
|
||||
requestsPerMinute: number;
|
||||
requestsPerDay?: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache entry for any data type
|
||||
*/
|
||||
export interface IBaseCacheEntry<TData> {
|
||||
data: TData;
|
||||
timestamp: Date;
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base service for managing data providers with caching
|
||||
* Shared logic extracted from StockPriceService and FundamentalsService
|
||||
*/
|
||||
export abstract class BaseProviderService<TProvider extends IBaseProvider, TData> {
|
||||
protected providers = new Map<string, IBaseProviderEntry<TProvider>>();
|
||||
protected cache = new Map<string, IBaseCacheEntry<TData>>();
|
||||
protected logger = console;
|
||||
|
||||
protected cacheConfig = {
|
||||
ttl: 60000, // Default 60 seconds
|
||||
maxEntries: 10000
|
||||
};
|
||||
|
||||
constructor(cacheConfig?: { ttl?: number; maxEntries?: number }) {
|
||||
if (cacheConfig) {
|
||||
this.cacheConfig = { ...this.cacheConfig, ...cacheConfig };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a provider
|
||||
*/
|
||||
public register(provider: TProvider, config?: Partial<IBaseProviderConfig>): void {
|
||||
const defaultConfig: IBaseProviderConfig = {
|
||||
enabled: true,
|
||||
priority: provider.priority,
|
||||
timeout: 30000,
|
||||
retryAttempts: 2,
|
||||
retryDelay: 1000,
|
||||
cacheTTL: this.cacheConfig.ttl
|
||||
};
|
||||
|
||||
const mergedConfig = { ...defaultConfig, ...config };
|
||||
|
||||
this.providers.set(provider.name, {
|
||||
provider,
|
||||
config: mergedConfig,
|
||||
successCount: 0,
|
||||
errorCount: 0
|
||||
});
|
||||
|
||||
console.log(`Registered provider: ${provider.name}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a provider
|
||||
*/
|
||||
public unregister(providerName: string): void {
|
||||
this.providers.delete(providerName);
|
||||
console.log(`Unregistered provider: ${providerName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific provider by name
|
||||
*/
|
||||
public getProvider(name: string): TProvider | undefined {
|
||||
return this.providers.get(name)?.provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered providers
|
||||
*/
|
||||
public getAllProviders(): TProvider[] {
|
||||
return Array.from(this.providers.values()).map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enabled providers sorted by priority
|
||||
*/
|
||||
public getEnabledProviders(): TProvider[] {
|
||||
return Array.from(this.providers.values())
|
||||
.filter(entry => entry.config.enabled)
|
||||
.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0))
|
||||
.map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check health of all providers
|
||||
*/
|
||||
public async checkProvidersHealth(): Promise<Map<string, boolean>> {
|
||||
const health = new Map<string, boolean>();
|
||||
|
||||
for (const [name, entry] of this.providers) {
|
||||
if (!entry.config.enabled) {
|
||||
health.set(name, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const isAvailable = await entry.provider.isAvailable();
|
||||
health.set(name, isAvailable);
|
||||
} catch (error) {
|
||||
health.set(name, false);
|
||||
console.error(`Health check failed for ${name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider statistics
|
||||
*/
|
||||
public getProviderStats(): Map<
|
||||
string,
|
||||
{
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
lastError?: string;
|
||||
lastErrorTime?: Date;
|
||||
}
|
||||
> {
|
||||
const stats = new Map();
|
||||
|
||||
for (const [name, entry] of this.providers) {
|
||||
stats.set(name, {
|
||||
successCount: entry.successCount,
|
||||
errorCount: entry.errorCount,
|
||||
lastError: entry.lastError?.message,
|
||||
lastErrorTime: entry.lastErrorTime
|
||||
});
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all cached data
|
||||
*/
|
||||
public clearCache(): void {
|
||||
this.cache.clear();
|
||||
console.log('Cache cleared');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cache TTL
|
||||
*/
|
||||
public setCacheTTL(ttl: number): void {
|
||||
this.cacheConfig.ttl = ttl;
|
||||
console.log(`Cache TTL set to ${ttl}ms`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache statistics
|
||||
*/
|
||||
public getCacheStats(): {
|
||||
size: number;
|
||||
maxEntries: number;
|
||||
ttl: number;
|
||||
} {
|
||||
return {
|
||||
size: this.cache.size,
|
||||
maxEntries: this.cacheConfig.maxEntries,
|
||||
ttl: this.cacheConfig.ttl
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch with retry logic
|
||||
*/
|
||||
protected async fetchWithRetry<T>(
|
||||
fetchFn: () => Promise<T>,
|
||||
config: IBaseProviderConfig
|
||||
): Promise<T> {
|
||||
const maxAttempts = config.retryAttempts || 1;
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
return await fetchFn();
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
if (attempt < maxAttempts) {
|
||||
const delay = (config.retryDelay || 1000) * attempt;
|
||||
console.log(`Retry attempt ${attempt} after ${delay}ms`);
|
||||
await plugins.smartdelay.delayFor(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError || new Error('Unknown error during fetch');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get from cache if not expired
|
||||
*/
|
||||
protected getFromCache(key: string): TData | null {
|
||||
const entry = this.cache.get(key);
|
||||
|
||||
if (!entry) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if cache entry has expired
|
||||
const age = Date.now() - entry.timestamp.getTime();
|
||||
if (entry.ttl !== Infinity && age > entry.ttl) {
|
||||
this.cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to cache with TTL
|
||||
*/
|
||||
protected addToCache(key: string, data: TData, ttl?: number): void {
|
||||
// Enforce max entries limit
|
||||
if (this.cache.size >= this.cacheConfig.maxEntries) {
|
||||
// Remove oldest entry
|
||||
const oldestKey = this.cache.keys().next().value;
|
||||
if (oldestKey) {
|
||||
this.cache.delete(oldestKey);
|
||||
}
|
||||
}
|
||||
|
||||
this.cache.set(key, {
|
||||
data,
|
||||
timestamp: new Date(),
|
||||
ttl: ttl || this.cacheConfig.ttl
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Track successful fetch for provider
|
||||
*/
|
||||
protected trackSuccess(providerName: string): void {
|
||||
const entry = this.providers.get(providerName);
|
||||
if (entry) {
|
||||
entry.successCount++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Track failed fetch for provider
|
||||
*/
|
||||
protected trackError(providerName: string, error: Error): void {
|
||||
const entry = this.providers.get(providerName);
|
||||
if (entry) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error;
|
||||
entry.lastErrorTime = new Date();
|
||||
}
|
||||
}
|
||||
}
|
||||
647
ts/stocks/classes.stockdataservice.ts
Normal file
647
ts/stocks/classes.stockdataservice.ts
Normal file
@@ -0,0 +1,647 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import type { IStockProvider, IProviderConfig } from './interfaces/provider.js';
|
||||
import type { IFundamentalsProvider, IFundamentalsProviderConfig, IStockFundamentals } from './interfaces/fundamentals.js';
|
||||
import type { IStockPrice, IStockDataRequest as IPriceRequest } from './interfaces/stockprice.js';
|
||||
import type { IStockData, IStockDataServiceConfig, ICompleteStockDataRequest, ICompleteStockDataBatchRequest } from './interfaces/stockdata.js';
|
||||
|
||||
interface IProviderEntry<T> {
|
||||
provider: T;
|
||||
config: IProviderConfig | IFundamentalsProviderConfig;
|
||||
lastError?: Error;
|
||||
lastErrorTime?: Date;
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
}
|
||||
|
||||
interface ICacheEntry<T> {
|
||||
data: T;
|
||||
timestamp: Date;
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unified service for managing both stock prices and fundamentals
|
||||
* Provides automatic enrichment and convenient combined data access
|
||||
*/
|
||||
export class StockDataService {
|
||||
private priceProviders = new Map<string, IProviderEntry<IStockProvider>>();
|
||||
private fundamentalsProviders = new Map<string, IProviderEntry<IFundamentalsProvider>>();
|
||||
|
||||
private priceCache = new Map<string, ICacheEntry<IStockPrice | IStockPrice[]>>();
|
||||
private fundamentalsCache = new Map<string, ICacheEntry<IStockFundamentals | IStockFundamentals[]>>();
|
||||
|
||||
private logger = console;
|
||||
|
||||
private config: Required<IStockDataServiceConfig> = {
|
||||
cache: {
|
||||
priceTTL: 24 * 60 * 60 * 1000, // 24 hours
|
||||
fundamentalsTTL: 90 * 24 * 60 * 60 * 1000, // 90 days
|
||||
maxEntries: 10000
|
||||
},
|
||||
timeout: {
|
||||
price: 10000, // 10 seconds
|
||||
fundamentals: 30000 // 30 seconds
|
||||
}
|
||||
};
|
||||
|
||||
constructor(config?: IStockDataServiceConfig) {
|
||||
if (config) {
|
||||
this.config = {
|
||||
cache: { ...this.config.cache, ...config.cache },
|
||||
timeout: { ...this.config.timeout, ...config.timeout }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Provider Management ==========
|
||||
|
||||
/**
|
||||
* Register a price provider
|
||||
*/
|
||||
public registerPriceProvider(provider: IStockProvider, config?: IProviderConfig): void {
|
||||
const defaultConfig: IProviderConfig = {
|
||||
enabled: true,
|
||||
priority: provider.priority,
|
||||
timeout: this.config.timeout.price,
|
||||
retryAttempts: 2,
|
||||
retryDelay: 1000
|
||||
};
|
||||
|
||||
const mergedConfig = { ...defaultConfig, ...config };
|
||||
|
||||
this.priceProviders.set(provider.name, {
|
||||
provider,
|
||||
config: mergedConfig,
|
||||
successCount: 0,
|
||||
errorCount: 0
|
||||
});
|
||||
|
||||
console.log(`Registered price provider: ${provider.name}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a fundamentals provider
|
||||
*/
|
||||
public registerFundamentalsProvider(
|
||||
provider: IFundamentalsProvider,
|
||||
config?: IFundamentalsProviderConfig
|
||||
): void {
|
||||
const defaultConfig: IFundamentalsProviderConfig = {
|
||||
enabled: true,
|
||||
priority: provider.priority,
|
||||
timeout: this.config.timeout.fundamentals,
|
||||
retryAttempts: 2,
|
||||
retryDelay: 1000,
|
||||
cacheTTL: this.config.cache.fundamentalsTTL
|
||||
};
|
||||
|
||||
const mergedConfig = { ...defaultConfig, ...config };
|
||||
|
||||
this.fundamentalsProviders.set(provider.name, {
|
||||
provider,
|
||||
config: mergedConfig,
|
||||
successCount: 0,
|
||||
errorCount: 0
|
||||
});
|
||||
|
||||
console.log(`Registered fundamentals provider: ${provider.name}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a price provider
|
||||
*/
|
||||
public unregisterPriceProvider(providerName: string): void {
|
||||
this.priceProviders.delete(providerName);
|
||||
console.log(`Unregistered price provider: ${providerName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a fundamentals provider
|
||||
*/
|
||||
public unregisterFundamentalsProvider(providerName: string): void {
|
||||
this.fundamentalsProviders.delete(providerName);
|
||||
console.log(`Unregistered fundamentals provider: ${providerName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered price providers
|
||||
*/
|
||||
public getPriceProviders(): IStockProvider[] {
|
||||
return Array.from(this.priceProviders.values()).map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered fundamentals providers
|
||||
*/
|
||||
public getFundamentalsProviders(): IFundamentalsProvider[] {
|
||||
return Array.from(this.fundamentalsProviders.values()).map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enabled price providers sorted by priority
|
||||
*/
|
||||
private getEnabledPriceProviders(): IStockProvider[] {
|
||||
return Array.from(this.priceProviders.values())
|
||||
.filter(entry => entry.config.enabled)
|
||||
.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0))
|
||||
.map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enabled fundamentals providers sorted by priority
|
||||
*/
|
||||
private getEnabledFundamentalsProviders(): IFundamentalsProvider[] {
|
||||
return Array.from(this.fundamentalsProviders.values())
|
||||
.filter(entry => entry.config.enabled)
|
||||
.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0))
|
||||
.map(entry => entry.provider);
|
||||
}
|
||||
|
||||
// ========== Data Fetching Methods ==========
|
||||
|
||||
/**
|
||||
* Get current price for a single ticker
|
||||
*/
|
||||
public async getPrice(ticker: string): Promise<IStockPrice> {
|
||||
const cacheKey = `price:${ticker}`;
|
||||
const cached = this.getFromCache(this.priceCache, cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for price: ${ticker}`);
|
||||
return cached as IStockPrice;
|
||||
}
|
||||
|
||||
const providers = this.getEnabledPriceProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No price providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.priceProviders.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData({ type: 'current', ticker }),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
const price = result as IStockPrice;
|
||||
this.addToCache(this.priceCache, cacheKey, price, this.config.cache.priceTTL);
|
||||
|
||||
console.log(`Successfully fetched price for ${ticker} from ${provider.name}`);
|
||||
return price;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(`Provider ${provider.name} failed for ${ticker}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch price for ${ticker} from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current prices for multiple tickers
|
||||
*/
|
||||
public async getPrices(tickers: string[]): Promise<IStockPrice[]> {
|
||||
const cacheKey = `prices:${tickers.sort().join(',')}`;
|
||||
const cached = this.getFromCache(this.priceCache, cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for prices: ${tickers.length} tickers`);
|
||||
return cached as IStockPrice[];
|
||||
}
|
||||
|
||||
const providers = this.getEnabledPriceProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No price providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.priceProviders.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData({ type: 'batch', tickers }),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
const prices = result as IStockPrice[];
|
||||
this.addToCache(this.priceCache, cacheKey, prices, this.config.cache.priceTTL);
|
||||
|
||||
console.log(`Successfully fetched ${prices.length} prices from ${provider.name}`);
|
||||
return prices;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(`Provider ${provider.name} failed for batch prices: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch prices for ${tickers.length} tickers from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fundamentals for a single ticker
|
||||
*/
|
||||
public async getFundamentals(ticker: string): Promise<IStockFundamentals> {
|
||||
const cacheKey = `fundamentals:${ticker}`;
|
||||
const cached = this.getFromCache(this.fundamentalsCache, cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for fundamentals: ${ticker}`);
|
||||
return cached as IStockFundamentals;
|
||||
}
|
||||
|
||||
const providers = this.getEnabledFundamentalsProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No fundamentals providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.fundamentalsProviders.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData({ type: 'fundamentals-current', ticker }),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
const fundamentals = result as IStockFundamentals;
|
||||
const ttl = (entry.config as IFundamentalsProviderConfig).cacheTTL || this.config.cache.fundamentalsTTL;
|
||||
this.addToCache(this.fundamentalsCache, cacheKey, fundamentals, ttl);
|
||||
|
||||
console.log(`Successfully fetched fundamentals for ${ticker} from ${provider.name}`);
|
||||
return fundamentals;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(`Provider ${provider.name} failed for ${ticker} fundamentals: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch fundamentals for ${ticker} from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fundamentals for multiple tickers
|
||||
*/
|
||||
public async getBatchFundamentals(tickers: string[]): Promise<IStockFundamentals[]> {
|
||||
const cacheKey = `fundamentals-batch:${tickers.sort().join(',')}`;
|
||||
const cached = this.getFromCache(this.fundamentalsCache, cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for batch fundamentals: ${tickers.length} tickers`);
|
||||
return cached as IStockFundamentals[];
|
||||
}
|
||||
|
||||
const providers = this.getEnabledFundamentalsProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No fundamentals providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.fundamentalsProviders.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData({ type: 'fundamentals-batch', tickers }),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
const fundamentals = result as IStockFundamentals[];
|
||||
const ttl = (entry.config as IFundamentalsProviderConfig).cacheTTL || this.config.cache.fundamentalsTTL;
|
||||
this.addToCache(this.fundamentalsCache, cacheKey, fundamentals, ttl);
|
||||
|
||||
console.log(`Successfully fetched ${fundamentals.length} fundamentals from ${provider.name}`);
|
||||
return fundamentals;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(`Provider ${provider.name} failed for batch fundamentals: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch fundamentals for ${tickers.length} tickers from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* ✨ Get complete stock data (price + fundamentals) with automatic enrichment
|
||||
*/
|
||||
public async getStockData(request: string | ICompleteStockDataRequest): Promise<IStockData> {
|
||||
const normalizedRequest = typeof request === 'string'
|
||||
? { ticker: request, includeFundamentals: true, enrichFundamentals: true }
|
||||
: { includeFundamentals: true, enrichFundamentals: true, ...request };
|
||||
|
||||
const price = await this.getPrice(normalizedRequest.ticker);
|
||||
|
||||
let fundamentals: IStockFundamentals | undefined;
|
||||
|
||||
if (normalizedRequest.includeFundamentals) {
|
||||
try {
|
||||
fundamentals = await this.getFundamentals(normalizedRequest.ticker);
|
||||
|
||||
// Enrich fundamentals with price calculations
|
||||
if (normalizedRequest.enrichFundamentals && fundamentals) {
|
||||
fundamentals = this.enrichWithPrice(fundamentals, price.price);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Failed to fetch fundamentals for ${normalizedRequest.ticker}: ${error.message}`);
|
||||
// Continue without fundamentals
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ticker: normalizedRequest.ticker,
|
||||
price,
|
||||
fundamentals,
|
||||
fetchedAt: new Date()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* ✨ Get complete stock data for multiple tickers with automatic enrichment
|
||||
*/
|
||||
public async getBatchStockData(request: string[] | ICompleteStockDataBatchRequest): Promise<IStockData[]> {
|
||||
const normalizedRequest = Array.isArray(request)
|
||||
? { tickers: request, includeFundamentals: true, enrichFundamentals: true }
|
||||
: { includeFundamentals: true, enrichFundamentals: true, ...request };
|
||||
|
||||
const prices = await this.getPrices(normalizedRequest.tickers);
|
||||
const priceMap = new Map(prices.map(p => [p.ticker, p]));
|
||||
|
||||
let fundamentalsMap = new Map<string, IStockFundamentals>();
|
||||
|
||||
if (normalizedRequest.includeFundamentals) {
|
||||
try {
|
||||
const fundamentals = await this.getBatchFundamentals(normalizedRequest.tickers);
|
||||
|
||||
// Enrich with prices if requested
|
||||
if (normalizedRequest.enrichFundamentals) {
|
||||
for (const fund of fundamentals) {
|
||||
const price = priceMap.get(fund.ticker);
|
||||
if (price) {
|
||||
fundamentalsMap.set(fund.ticker, this.enrichWithPrice(fund, price.price));
|
||||
} else {
|
||||
fundamentalsMap.set(fund.ticker, fund);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fundamentalsMap = new Map(fundamentals.map(f => [f.ticker, f]));
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Failed to fetch batch fundamentals: ${error.message}`);
|
||||
// Continue without fundamentals
|
||||
}
|
||||
}
|
||||
|
||||
return normalizedRequest.tickers.map(ticker => ({
|
||||
ticker,
|
||||
price: priceMap.get(ticker)!,
|
||||
fundamentals: fundamentalsMap.get(ticker),
|
||||
fetchedAt: new Date()
|
||||
}));
|
||||
}
|
||||
|
||||
// ========== Helper Methods ==========
|
||||
|
||||
/**
|
||||
* Enrich fundamentals with calculated metrics using current price
|
||||
*/
|
||||
private enrichWithPrice(fundamentals: IStockFundamentals, price: number): IStockFundamentals {
|
||||
const enriched = { ...fundamentals };
|
||||
|
||||
// Calculate market cap: price × shares outstanding
|
||||
if (fundamentals.sharesOutstanding) {
|
||||
enriched.marketCap = price * fundamentals.sharesOutstanding;
|
||||
}
|
||||
|
||||
// Calculate P/E ratio: price / EPS
|
||||
if (fundamentals.earningsPerShareDiluted && fundamentals.earningsPerShareDiluted > 0) {
|
||||
enriched.priceToEarnings = price / fundamentals.earningsPerShareDiluted;
|
||||
}
|
||||
|
||||
// Calculate price-to-book: market cap / stockholders equity
|
||||
if (enriched.marketCap && fundamentals.stockholdersEquity && fundamentals.stockholdersEquity > 0) {
|
||||
enriched.priceToBook = enriched.marketCap / fundamentals.stockholdersEquity;
|
||||
}
|
||||
|
||||
return enriched;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch with retry logic
|
||||
*/
|
||||
private async fetchWithRetry<T>(
|
||||
fetchFn: () => Promise<T>,
|
||||
config: IProviderConfig | IFundamentalsProviderConfig
|
||||
): Promise<T> {
|
||||
const maxAttempts = config.retryAttempts || 1;
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
return await fetchFn();
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
if (attempt < maxAttempts) {
|
||||
const delay = (config.retryDelay || 1000) * attempt;
|
||||
console.log(`Retry attempt ${attempt} after ${delay}ms`);
|
||||
await plugins.smartdelay.delayFor(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError || new Error('Unknown error during fetch');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get from cache if not expired
|
||||
*/
|
||||
private getFromCache<T>(cache: Map<string, ICacheEntry<T>>, key: string): T | null {
|
||||
const entry = cache.get(key);
|
||||
|
||||
if (!entry) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if cache entry has expired
|
||||
const age = Date.now() - entry.timestamp.getTime();
|
||||
if (entry.ttl !== Infinity && age > entry.ttl) {
|
||||
cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to cache with TTL
|
||||
*/
|
||||
private addToCache<T>(cache: Map<string, ICacheEntry<T>>, key: string, data: T, ttl: number): void {
|
||||
// Enforce max entries limit
|
||||
if (cache.size >= this.config.cache.maxEntries) {
|
||||
// Remove oldest entry
|
||||
const oldestKey = cache.keys().next().value;
|
||||
if (oldestKey) {
|
||||
cache.delete(oldestKey);
|
||||
}
|
||||
}
|
||||
|
||||
cache.set(key, {
|
||||
data,
|
||||
timestamp: new Date(),
|
||||
ttl
|
||||
});
|
||||
}
|
||||
|
||||
// ========== Health & Statistics ==========
|
||||
|
||||
/**
|
||||
* Check health of all providers (both price and fundamentals)
|
||||
*/
|
||||
public async checkProvidersHealth(): Promise<Map<string, boolean>> {
|
||||
const health = new Map<string, boolean>();
|
||||
|
||||
// Check price providers
|
||||
for (const [name, entry] of this.priceProviders) {
|
||||
if (!entry.config.enabled) {
|
||||
health.set(`${name} (price)`, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const isAvailable = await entry.provider.isAvailable();
|
||||
health.set(`${name} (price)`, isAvailable);
|
||||
} catch (error) {
|
||||
health.set(`${name} (price)`, false);
|
||||
console.error(`Health check failed for ${name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Check fundamentals providers
|
||||
for (const [name, entry] of this.fundamentalsProviders) {
|
||||
if (!entry.config.enabled) {
|
||||
health.set(`${name} (fundamentals)`, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const isAvailable = await entry.provider.isAvailable();
|
||||
health.set(`${name} (fundamentals)`, isAvailable);
|
||||
} catch (error) {
|
||||
health.set(`${name} (fundamentals)`, false);
|
||||
console.error(`Health check failed for ${name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get statistics for all providers
|
||||
*/
|
||||
public getProviderStats(): Map<
|
||||
string,
|
||||
{
|
||||
type: 'price' | 'fundamentals';
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
lastError?: string;
|
||||
lastErrorTime?: Date;
|
||||
}
|
||||
> {
|
||||
const stats = new Map();
|
||||
|
||||
// Price provider stats
|
||||
for (const [name, entry] of this.priceProviders) {
|
||||
stats.set(name, {
|
||||
type: 'price',
|
||||
successCount: entry.successCount,
|
||||
errorCount: entry.errorCount,
|
||||
lastError: entry.lastError?.message,
|
||||
lastErrorTime: entry.lastErrorTime
|
||||
});
|
||||
}
|
||||
|
||||
// Fundamentals provider stats
|
||||
for (const [name, entry] of this.fundamentalsProviders) {
|
||||
stats.set(name, {
|
||||
type: 'fundamentals',
|
||||
successCount: entry.successCount,
|
||||
errorCount: entry.errorCount,
|
||||
lastError: entry.lastError?.message,
|
||||
lastErrorTime: entry.lastErrorTime
|
||||
});
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all caches
|
||||
*/
|
||||
public clearCache(): void {
|
||||
this.priceCache.clear();
|
||||
this.fundamentalsCache.clear();
|
||||
console.log('All caches cleared');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache statistics
|
||||
*/
|
||||
public getCacheStats(): {
|
||||
priceCache: { size: number; ttl: number };
|
||||
fundamentalsCache: { size: number; ttl: number };
|
||||
maxEntries: number;
|
||||
} {
|
||||
return {
|
||||
priceCache: {
|
||||
size: this.priceCache.size,
|
||||
ttl: this.config.cache.priceTTL
|
||||
},
|
||||
fundamentalsCache: {
|
||||
size: this.fundamentalsCache.size,
|
||||
ttl: this.config.cache.fundamentalsTTL
|
||||
},
|
||||
maxEntries: this.config.cache.maxEntries
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,15 @@
|
||||
export * from './interfaces/stockprice.js';
|
||||
export * from './interfaces/provider.js';
|
||||
export * from './interfaces/fundamentals.js';
|
||||
export * from './interfaces/stockdata.js';
|
||||
|
||||
// Export main services
|
||||
export * from './classes.stockservice.js';
|
||||
export * from './classes.fundamentalsservice.js';
|
||||
export * from './classes.stockdataservice.js'; // ✨ New unified service
|
||||
|
||||
// Export base service (for advanced use cases)
|
||||
export * from './classes.baseproviderservice.js';
|
||||
|
||||
// Export providers
|
||||
export * from './providers/provider.yahoo.js';
|
||||
|
||||
65
ts/stocks/interfaces/stockdata.ts
Normal file
65
ts/stocks/interfaces/stockdata.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { IStockPrice } from './stockprice.js';
|
||||
import type { IStockFundamentals } from './fundamentals.js';
|
||||
|
||||
/**
|
||||
* Combined stock data with price and fundamentals
|
||||
* All calculated metrics (market cap, P/E, P/B) are automatically included
|
||||
*/
|
||||
export interface IStockData {
|
||||
/** Stock ticker symbol */
|
||||
ticker: string;
|
||||
|
||||
/** Price information */
|
||||
price: IStockPrice;
|
||||
|
||||
/** Fundamental data (optional - may not be available for all stocks) */
|
||||
fundamentals?: IStockFundamentals;
|
||||
|
||||
/** When this combined data was fetched */
|
||||
fetchedAt: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for StockDataService
|
||||
*/
|
||||
export interface IStockDataServiceConfig {
|
||||
/** Cache configuration */
|
||||
cache?: {
|
||||
/** TTL for price data (default: 24 hours) */
|
||||
priceTTL?: number;
|
||||
/** TTL for fundamentals data (default: 90 days) */
|
||||
fundamentalsTTL?: number;
|
||||
/** Max cache entries (default: 10000) */
|
||||
maxEntries?: number;
|
||||
};
|
||||
|
||||
/** Provider timeouts */
|
||||
timeout?: {
|
||||
/** Timeout for price providers (default: 10000ms) */
|
||||
price?: number;
|
||||
/** Timeout for fundamentals providers (default: 30000ms) */
|
||||
fundamentals?: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Request type for getting complete stock data
|
||||
*/
|
||||
export interface ICompleteStockDataRequest {
|
||||
ticker: string;
|
||||
/** Whether to include fundamentals (default: true) */
|
||||
includeFundamentals?: boolean;
|
||||
/** Whether to enrich fundamentals with price calculations (default: true) */
|
||||
enrichFundamentals?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch request for multiple stocks
|
||||
*/
|
||||
export interface ICompleteStockDataBatchRequest {
|
||||
tickers: string[];
|
||||
/** Whether to include fundamentals (default: true) */
|
||||
includeFundamentals?: boolean;
|
||||
/** Whether to enrich fundamentals with price calculations (default: true) */
|
||||
enrichFundamentals?: boolean;
|
||||
}
|
||||
Reference in New Issue
Block a user