ALwrity LinkedIn Writer: Billing Dashboard: Compact View, Billing Overview, System Health Indicator, Cost Breakdown, Usage Trends, Usage Alerts, Comprehensive API Breakdown

This commit is contained in:
ajaysi
2025-09-11 11:09:10 +05:30
parent b156298e82
commit 1b65a9487b
84 changed files with 10143 additions and 156 deletions

View File

@@ -0,0 +1,287 @@
import { z } from 'zod';
// Core data structures for billing and usage tracking
export interface DashboardData {
current_usage: UsageStats;
trends: UsageTrends;
limits: SubscriptionLimits;
alerts: UsageAlert[];
projections: CostProjections;
summary: UsageSummary;
}
export interface UsageStats {
billing_period: string;
usage_status: 'active' | 'warning' | 'limit_reached';
total_calls: number;
total_tokens: number;
total_cost: number;
avg_response_time: number;
error_rate: number;
limits: SubscriptionLimits;
provider_breakdown: ProviderBreakdown;
alerts: UsageAlert[];
usage_percentages: UsagePercentages;
last_updated: string;
}
export interface ProviderBreakdown {
gemini: ProviderUsage;
openai: ProviderUsage;
anthropic: ProviderUsage;
mistral: ProviderUsage;
tavily: ProviderUsage;
serper: ProviderUsage;
metaphor: ProviderUsage;
firecrawl: ProviderUsage;
stability: ProviderUsage;
}
export interface ProviderUsage {
calls: number;
tokens: number;
cost: number;
}
export interface SubscriptionLimits {
plan_name: string;
tier: 'free' | 'basic' | 'pro' | 'enterprise';
limits: {
gemini_calls: number;
openai_calls: number;
anthropic_calls: number;
mistral_calls: number;
tavily_calls: number;
serper_calls: number;
metaphor_calls: number;
firecrawl_calls: number;
stability_calls: number;
gemini_tokens: number;
openai_tokens: number;
anthropic_tokens: number;
mistral_tokens: number;
monthly_cost: number;
};
features: string[];
}
export interface UsageTrends {
periods: string[];
total_calls: number[];
total_cost: number[];
total_tokens: number[];
provider_trends: {
[key: string]: {
calls: number[];
cost: number[];
tokens: number[];
};
};
}
export interface UsageAlert {
id: number;
type: string;
threshold_percentage: number;
provider?: string;
title: string;
message: string;
severity: 'info' | 'warning' | 'error';
is_sent: boolean;
sent_at?: string;
is_read: boolean;
read_at?: string;
billing_period: string;
created_at: string;
}
export interface CostProjections {
projected_monthly_cost: number;
cost_limit: number;
projected_usage_percentage: number;
}
export interface UsageSummary {
total_api_calls_this_month: number;
total_cost_this_month: number;
usage_status: string;
unread_alerts: number;
}
export interface SubscriptionPlan {
id: number;
name: string;
tier: 'free' | 'basic' | 'pro' | 'enterprise';
price_monthly: number;
price_yearly: number;
description: string;
features: string[];
limits: {
gemini_calls: number;
openai_calls: number;
anthropic_calls: number;
mistral_calls: number;
tavily_calls: number;
serper_calls: number;
metaphor_calls: number;
firecrawl_calls: number;
stability_calls: number;
gemini_tokens: number;
openai_tokens: number;
anthropic_tokens: number;
mistral_tokens: number;
monthly_cost: number;
};
}
export interface APIPricing {
provider: string;
model_name: string;
cost_per_input_token: number;
cost_per_output_token: number;
cost_per_request: number;
cost_per_search: number;
cost_per_image: number;
cost_per_page: number;
description: string;
effective_date: string;
}
export interface UsagePercentages {
gemini_calls: number;
openai_calls: number;
anthropic_calls: number;
mistral_calls: number;
tavily_calls: number;
serper_calls: number;
metaphor_calls: number;
firecrawl_calls: number;
stability_calls: number;
cost: number;
}
// Zod validation schemas
export const UsagePercentagesSchema = z.object({
gemini_calls: z.number(),
openai_calls: z.number(),
anthropic_calls: z.number(),
mistral_calls: z.number(),
tavily_calls: z.number(),
serper_calls: z.number(),
metaphor_calls: z.number(),
firecrawl_calls: z.number(),
stability_calls: z.number(),
cost: z.number(),
});
export const ProviderUsageSchema = z.object({
calls: z.number(),
tokens: z.number(),
cost: z.number(),
});
export const ProviderBreakdownSchema = z.object({
gemini: ProviderUsageSchema,
openai: ProviderUsageSchema,
anthropic: ProviderUsageSchema,
mistral: ProviderUsageSchema,
tavily: ProviderUsageSchema,
serper: ProviderUsageSchema,
metaphor: ProviderUsageSchema,
firecrawl: ProviderUsageSchema,
stability: ProviderUsageSchema,
});
export const SubscriptionLimitsSchema = z.object({
plan_name: z.string(),
tier: z.enum(['free', 'basic', 'pro', 'enterprise']),
limits: z.object({
gemini_calls: z.number(),
openai_calls: z.number(),
anthropic_calls: z.number(),
mistral_calls: z.number(),
tavily_calls: z.number(),
serper_calls: z.number(),
metaphor_calls: z.number(),
firecrawl_calls: z.number(),
stability_calls: z.number(),
gemini_tokens: z.number(),
openai_tokens: z.number(),
anthropic_tokens: z.number(),
mistral_tokens: z.number(),
monthly_cost: z.number(),
}),
features: z.array(z.string()),
});
export const UsageAlertSchema = z.object({
id: z.number(),
type: z.string(),
threshold_percentage: z.number(),
provider: z.string().optional(),
title: z.string(),
message: z.string(),
severity: z.enum(['info', 'warning', 'error']),
is_sent: z.boolean(),
sent_at: z.string().optional(),
is_read: z.boolean(),
read_at: z.string().optional(),
billing_period: z.string(),
created_at: z.string(),
});
export const UsageStatsSchema = z.object({
billing_period: z.string(),
usage_status: z.enum(['active', 'warning', 'limit_reached']),
total_calls: z.number(),
total_tokens: z.number(),
total_cost: z.number(),
avg_response_time: z.number(),
error_rate: z.number(),
limits: SubscriptionLimitsSchema,
provider_breakdown: ProviderBreakdownSchema,
alerts: z.array(UsageAlertSchema),
usage_percentages: UsagePercentagesSchema,
last_updated: z.string(),
});
export const DashboardDataSchema = z.object({
current_usage: UsageStatsSchema,
trends: z.object({
periods: z.array(z.string()),
total_calls: z.array(z.number()),
total_cost: z.array(z.number()),
total_tokens: z.array(z.number()),
provider_trends: z.record(z.object({
calls: z.array(z.number()),
cost: z.array(z.number()),
tokens: z.array(z.number()),
})),
}),
limits: SubscriptionLimitsSchema,
alerts: z.array(UsageAlertSchema),
projections: z.object({
projected_monthly_cost: z.number(),
cost_limit: z.number(),
projected_usage_percentage: z.number(),
}),
summary: z.object({
total_api_calls_this_month: z.number(),
total_cost_this_month: z.number(),
usage_status: z.string(),
unread_alerts: z.number(),
}),
});
// API Response types
export interface BillingAPIResponse<T> {
success: boolean;
data: T;
error?: string;
}
export interface UsageAPIResponse extends BillingAPIResponse<UsageStats> {}
export interface DashboardAPIResponse extends BillingAPIResponse<DashboardData> {}
export interface PlansAPIResponse extends BillingAPIResponse<{ plans: SubscriptionPlan[]; total: number }> {}
export interface PricingAPIResponse extends BillingAPIResponse<{ pricing: APIPricing[]; total: number }> {}
export interface AlertsAPIResponse extends BillingAPIResponse<{ alerts: UsageAlert[]; total: number; unread_count: number }> {}

View File

@@ -0,0 +1,193 @@
import { z } from 'zod';
// System health and monitoring types
export interface SystemHealth {
status: 'healthy' | 'warning' | 'critical';
icon: string;
recent_requests: number;
recent_errors: number;
error_rate: number;
timestamp: string;
}
export interface APIStats {
timestamp: string;
overview: {
total_requests: number;
total_errors: number;
recent_requests: number;
recent_errors: number;
};
cache_performance: {
hits: number;
misses: number;
hit_rate: number;
};
top_endpoints: APIEndpointStats[];
recent_errors: APIError[];
system_health: {
status: 'healthy' | 'warning' | 'critical';
error_rate: number;
};
}
export interface APIEndpointStats {
endpoint: string;
count: number;
avg_time: number;
errors: number;
last_called: string | null;
cache_hit_rate: number;
}
export interface APIError {
timestamp: string;
path: string;
method: string;
status_code: number;
duration: number;
}
export interface LightweightStats {
status: 'healthy' | 'warning' | 'critical';
icon: string;
recent_requests: number;
recent_errors: number;
error_rate: number;
timestamp: string;
}
export interface CacheStats {
hits: number;
misses: number;
hit_rate: number;
total_requests: number;
}
// Performance metrics
export interface PerformanceMetrics {
response_time: {
average: number;
p95: number;
p99: number;
};
throughput: {
requests_per_second: number;
requests_per_minute: number;
};
error_rate: number;
uptime: number;
}
// External API monitoring
export interface ExternalAPIMetrics {
provider: string;
calls: number;
cost: number;
avg_response_time: number;
error_rate: number;
last_updated: string;
}
// Zod validation schemas
export const SystemHealthSchema = z.object({
status: z.enum(['healthy', 'warning', 'critical']),
icon: z.string(),
recent_requests: z.number(),
recent_errors: z.number(),
error_rate: z.number(),
timestamp: z.string(),
});
export const APIEndpointStatsSchema = z.object({
endpoint: z.string(),
count: z.number(),
avg_time: z.number(),
errors: z.number(),
last_called: z.string().nullable(),
cache_hit_rate: z.number(),
});
export const APIErrorSchema = z.object({
timestamp: z.string(),
path: z.string(),
method: z.string(),
status_code: z.number(),
duration: z.number(),
});
export const APIStatsSchema = z.object({
timestamp: z.string(),
overview: z.object({
total_requests: z.number(),
total_errors: z.number(),
recent_requests: z.number(),
recent_errors: z.number(),
}),
cache_performance: z.object({
hits: z.number(),
misses: z.number(),
hit_rate: z.number(),
}),
top_endpoints: z.array(APIEndpointStatsSchema),
recent_errors: z.array(APIErrorSchema),
system_health: z.object({
status: z.enum(['healthy', 'warning', 'critical']),
error_rate: z.number(),
}),
});
export const LightweightStatsSchema = z.object({
status: z.enum(['healthy', 'warning', 'critical']),
icon: z.string(),
recent_requests: z.number(),
recent_errors: z.number(),
error_rate: z.number(),
timestamp: z.string(),
});
export const CacheStatsSchema = z.object({
hits: z.number(),
misses: z.number(),
hit_rate: z.number(),
total_requests: z.number(),
});
export const PerformanceMetricsSchema = z.object({
response_time: z.object({
average: z.number(),
p95: z.number(),
p99: z.number(),
}),
throughput: z.object({
requests_per_second: z.number(),
requests_per_minute: z.number(),
}),
error_rate: z.number(),
uptime: z.number(),
});
export const ExternalAPIMetricsSchema = z.object({
provider: z.string(),
calls: z.number(),
cost: z.number(),
avg_response_time: z.number(),
error_rate: z.number(),
last_updated: z.string(),
});
// API Response types
export interface MonitoringAPIResponse<T> {
success: boolean;
data: T;
error?: string;
}
export interface SystemHealthAPIResponse {
status: string;
data: SystemHealth;
message?: string;
}
export interface APIStatsAPIResponse extends MonitoringAPIResponse<APIStats> {}
export interface LightweightStatsAPIResponse extends MonitoringAPIResponse<LightweightStats> {}
export interface CacheStatsAPIResponse extends MonitoringAPIResponse<CacheStats> {}