Skip to content

Latest commit

 

History

History
367 lines (295 loc) · 10.7 KB

File metadata and controls

367 lines (295 loc) · 10.7 KB

Rate Limit Dashboard - Implementation Guide

Overview

A production-ready client-facing rate limit status and usage dashboard built with React and TypeScript. The component displays real-time API rate limit monitoring with visual indicators, endpoint usage breakdowns, and automated alerts.

What Was Created

Component Files

  1. src/components/RateLimitDashboard.tsx (473 lines)

    • Main React component with full functionality
    • TypeScript with strong typing
    • Includes all sub-components (ProgressBar, StatusBadge, Alert, EndpointUsageRow)
    • Helper functions for calculations and formatting
    • Mock data generator for demo mode
    • State management with React hooks
  2. src/pages/rate-limits.tsx (16 lines)

    • Standalone page for the dashboard
    • Accessible at /rate-limits route
    • Integrated with Docusaurus Layout
  3. src/css/custom.css (630 lines added)

    • Complete styling for all components
    • Responsive design (desktop, tablet, mobile)
    • Accessible color contrasts
    • Smooth animations and transitions
    • Dark mode support via CSS variables
  4. RATE_LIMIT_DASHBOARD.md (301 lines)

    • Complete component documentation
    • API integration guide
    • Configuration options
    • Usage examples
    • Development notes
  5. docusaurus.config.ts (updated)

    • Added rate-limits route to navbar

Features

Display & Visualization

  • Overall Usage Card: Progress bar with percentage, used/limit/remaining stats
  • Plan Details Card: Current tier, reset time, countdown timer
  • Endpoint Usage Table: Per-endpoint request counts with status colors
  • Status Indicators: OK (green), Warning (orange), Critical (red)
  • Real-time Updates: Auto-refresh every 30 seconds (configurable)

User Interactions

  • Manual refresh button
  • Auto-refresh toggle checkbox
  • Last updated timestamp
  • Navigation links to documentation and support

Smart Alerts

  • Critical (90%+ usage): "Your requests may be throttled soon"
  • Warning (70-90% usage): "Consider optimizing your API usage"
  • OK (<70% usage): "Your rate limit usage is healthy"

Help & Guidance

  • Built-in tips for optimization
  • Links to API documentation
  • Links to support channels
  • Best practices for rate limit management

Data Requirements

Expected API Endpoint: /api/rate-limit-status

GET /api/rate-limit-status
Authorization: Bearer {api_token}
Content-Type: application/json

Response:
{
  "tier": "Pro",
  "requestsLimit": 5000,
  "requestsUsed": 1250,
  "requestsRemaining": 3750,
  "resetTime": "2024-07-30T15:30:00Z",
  "resetTimestamp": 1722352200000,
  "percentageUsed": 25,
  "endpoints": [
    {
      "path": "/api/transactions",
      "method": "GET",
      "requestsUsed": 500,
      "limit": 1000
    },
    // ... more endpoints
  ]
}

How to Enable Production Mode

  1. Open src/components/RateLimitDashboard.tsx
  2. Find the line: const DEMO_MODE = true;
  3. Change to: const DEMO_MODE = false;
  4. Rebuild: npm run build or npm start

The component will then fetch from your real API endpoint.

Configuration

Polling Interval

Change the auto-refresh interval (in milliseconds):

const POLLING_INTERVAL = 30000; // 30 seconds

API Endpoint

The component expects the endpoint at /api/rate-limit-status. If your endpoint differs, modify the fetch URL in the fetchStatus function.

Authentication

The component uses localStorage token:

'Authorization': `Bearer ${localStorage.getItem('api_token')}`

Modify this to use your authentication mechanism.

Component Architecture

State Management

  • status: Current rate limit data
  • loading: Fetch state
  • error: Error message if fetch fails
  • alerts: Array of alert messages
  • autoRefresh: Toggle for auto-polling
  • lastUpdated: Timestamp of last fetch

Effects & Hooks

  • useCallback: Optimized fetch function
  • useEffect: Initialization and polling setup
  • useState: All state management

Sub-Components

  • ProgressBar: Reusable progress visualization
  • StatusBadge: Health status indicator
  • Alert: Alert message display
  • EndpointUsageRow: Table row with endpoint data

Styling Approach

CSS Variables (Using Docusaurus Theme)

  • --ifm-color-primary: Main accent color
  • --ifm-font-color-base: Text color
  • --ifm-background-surface-color: Surface color
  • --ifm-color-emphasis-*: Emphasis levels

Color Scheme

  • OK Status: #49cc90 (green)
  • Warning Status: #fca130 (orange)
  • Critical Status: #f93e3e (red)

Responsive Breakpoints

  • Desktop: Full layout, side-by-side cards
  • Tablet (≤768px): Single column, condensed table
  • Mobile (≤480px): Stack layout, minimal padding

Performance Optimizations

  1. Memoization: useCallback for fetch function
  2. Debouncing: Not needed here but structure allows it
  3. Polling: Configurable interval, stops if component unmounts
  4. Error Boundaries: Graceful error handling

Accessibility Features

  • Semantic HTML: Proper heading hierarchy, button elements
  • ARIA Labels: On interactive elements and dynamic regions
  • Color Contrast: WCAG AA compliant
  • Keyboard Navigation: Full support
  • Screen Reader Support: Semantic structure, clear labels

Testing

Manual Testing Checklist

  • Component loads with mock data
  • Progress bar updates correctly
  • Status badges show appropriate colors
  • Alerts display based on usage threshold
  • Manual refresh button works
  • Auto-refresh toggle enables/disables polling
  • Last updated timestamp updates
  • Responsive design works on mobile
  • No console errors
  • All links work

Demo Mode Testing

The component comes with realistic demo data that cycles through various usage scenarios:

npm start  # Start dev server
# Navigate to http://localhost:3001/proxypay/rate-limits

Integration Steps

1. If Already Implemented

The dashboard is already integrated! You can:

  • View at /rate-limits in your docs site
  • Access from navbar: "Rate Limits" link

2. To Use in Other Pages

import RateLimitDashboard from '@site/src/components/RateLimitDashboard';

export default function MyPage() {
  return (
    <Layout>
      <RateLimitDashboard />
    </Layout>
  );
}

3. To Embed in Dashboard

// In your application's dashboard component
import RateLimitDashboard from '@site/src/components/RateLimitDashboard';

export default function Dashboard() {
  return (
    <div className="dashboard-grid">
      <RateLimitDashboard />
    </div>
  );
}

Backend Integration

Node.js/Express Example

app.get('/api/rate-limit-status', authenticateToken, (req, res) => {
  const userId = req.user.id;
  const rateLimit = getRateLimitForUser(userId);
  
  res.json({
    tier: rateLimit.tier,
    requestsLimit: rateLimit.limit,
    requestsUsed: rateLimit.used,
    requestsRemaining: rateLimit.limit - rateLimit.used,
    resetTime: new Date(rateLimit.resetAt).toISOString(),
    resetTimestamp: rateLimit.resetAt.getTime(),
    percentageUsed: Math.round((rateLimit.used / rateLimit.limit) * 100),
    endpoints: getEndpointUsage(userId),
  });
});

Python/FastAPI Example

@app.get("/api/rate-limit-status")
async def get_rate_limit_status(current_user: User = Depends(get_current_user)):
    rate_limit = db.get_rate_limit(current_user.id)
    
    return {
        "tier": rate_limit.tier,
        "requestsLimit": rate_limit.limit,
        "requestsUsed": rate_limit.used,
        "requestsRemaining": rate_limit.limit - rate_limit.used,
        "resetTime": rate_limit.reset_at.isoformat(),
        "resetTimestamp": int(rate_limit.reset_at.timestamp() * 1000),
        "percentageUsed": round((rate_limit.used / rate_limit.limit) * 100),
        "endpoints": get_endpoint_usage(current_user.id),
    }

Troubleshooting

Component doesn't load

  • Check browser console for errors
  • Verify DEMO_MODE is set correctly
  • Ensure all files are in correct directories

API calls fail in production

  • Verify authentication token is in localStorage
  • Check CORS headers if API on different domain
  • Verify endpoint URL matches backend

Styling looks broken

  • Clear cache: rm -rf .docusaurus node_modules/.cache
  • Rebuild CSS: npm run build
  • Check CSS is linked in Layout component

Auto-refresh not working

  • Check polling interval value
  • Verify useEffect cleanup is called
  • Check browser DevTools network tab

Future Enhancement Ideas

  1. Historical Trends: Graph showing usage over time
  2. Export Data: Download usage reports (CSV/PDF)
  3. Predictions: Estimate when limit will be exhausted
  4. Webhooks: Subscribe to rate limit events
  5. Custom Thresholds: User-configurable alert levels
  6. API Key Usage: Break down by API key
  7. Usage Forecasting: Based on historical patterns
  8. Billing Integration: Show cost impact of usage

Support & Documentation

  • Full documentation: See RATE_LIMIT_DASHBOARD.md
  • Component code: src/components/RateLimitDashboard.tsx
  • Styles: src/css/custom.css (Rate Limit section)
  • Page: src/pages/rate-limits.tsx
  • Config: docusaurus.config.ts

Files Modified/Created

src/
├── components/
│   ├── RateLimitDashboard.tsx      [NEW - 473 lines]
│   └── ApiReference.tsx            [unchanged]
├── pages/
│   ├── rate-limits.tsx             [NEW - 16 lines]
│   ├── index.tsx                   [unchanged]
│   └── api.tsx                     [unchanged]
└── css/
    └── custom.css                  [MODIFIED - 630 lines added]

Root files:
├── docusaurus.config.ts            [MODIFIED - navbar updated]
├── RATE_LIMIT_DASHBOARD.md         [NEW - 301 lines]
└── RATE_LIMIT_DASHBOARD_IMPL.md    [This file]

Quick Start

  1. View Dashboard

    npm start
    # Navigate to http://localhost:3001/proxypay/rate-limits
  2. Test Production Integration

    • Edit src/components/RateLimitDashboard.tsx
    • Change const DEMO_MODE = false;
    • Ensure backend provides /api/rate-limit-status
  3. Customize Styling

    • Edit src/css/custom.css
    • Search for "Rate Limit Dashboard" section
    • Modify colors, spacing, fonts as needed

Summary

The Rate Limit Dashboard is a complete, production-ready component that:

✅ Displays real-time rate limit status
✅ Shows endpoint-level usage breakdown
✅ Provides smart alerts and recommendations
✅ Auto-refreshes with user control
✅ Fully responsive on all devices
✅ Accessible to all users
✅ Works in demo mode out-of-the-box
✅ Integrates with your backend API
✅ Well-documented and maintainable
✅ No external UI library dependencies

Ready for production deployment!