import time
import random

# Placeholder for a real dashboard implementation (e.g., using Flask, Django, or a dedicated dashboarding library)
# This example demonstrates how to display current rate limits and recent adjustment history.

class RateLimitDashboard:
    def __init__(self):
        self.rate_limits = {}
        self.adjustment_history = []

    def update_rate_limit(self, resource_id, new_limit):
        self.rate_limits[resource_id] = new_limit

    def log_adjustment(self, resource_id, previous_limit, new_limit, trigger_metric, threshold, reason):
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
        adjustment = {
            'timestamp': timestamp,
            'resource_id': resource_id,
            'previous_limit': previous_limit,
            'new_limit': new_limit,
            'trigger_metric': trigger_metric,
            'threshold': threshold,
            'reason': reason
        }
        self.adjustment_history.append(adjustment)
        # Keep only the last 10 adjustments for brevity
        self.adjustment_history = self.adjustment_history[-10:]

    def display_dashboard(self):
        print("--- Rate Limit Dashboard ---")
        print("Current Rate Limits:")
        for resource_id, limit in self.rate_limits.items():
            print(f"  {resource_id}: {limit}")

        print("\nRecent Adjustment History:")
        for adjustment in self.adjustment_history:
            print(f"  [{adjustment['timestamp']}] {adjustment['resource_id']}: {adjustment['previous_limit']} -> {adjustment['new_limit']} (Trigger: {adjustment['trigger_metric']} > {adjustment['threshold']}, Reason: {adjustment['reason']})")

# Example Usage (replace with actual integration with rate limiting logic)
if __name__ == '__main__':
    dashboard = RateLimitDashboard()

    # Simulate initial rate limits
    dashboard.update_rate_limit('user_api', 100)
    dashboard.update_rate_limit('image_processing', 50)
    dashboard.display_dashboard()

    # Simulate rate limit adjustments
    dashboard.log_adjustment(
        resource_id='user_api',
        previous_limit=100,
        new_limit=75,
        trigger_metric='request_latency',
        threshold=0.5,
        reason='Request latency exceeded 0.5 seconds'
    )
    dashboard.log_adjustment(
        resource_id='image_processing',
        previous_limit=50,
        new_limit=25,
        trigger_metric='queue_size',
        threshold=200,
        reason='Image processing queue size exceeded 200'
    )
    dashboard.update_rate_limit('user_api', 75)
    dashboard.update_rate_limit('image_processing', 25)
    dashboard.display_dashboard()

    # Simulate more adjustments with random values
    for i in range(5):
        resource_id = random.choice(['user_api', 'image_processing'])
        previous_limit = dashboard.rate_limits.get(resource_id, 50)
        new_limit = max(1, int(previous_limit * (1 + random.uniform(-0.2, 0.2)))) # Change by up to 20%
        trigger_metric = random.choice(['cpu_usage', 'memory_usage', 'request_latency', 'queue_size'])
        threshold = random.uniform(0.7, 1.0)
        reason = f'Simulated adjustment {i+1}'

        dashboard.log_adjustment(
            resource_id=resource_id,
            previous_limit=previous_limit,
            new_limit=new_limit,
            trigger_metric=trigger_metric,
            threshold=threshold,
            reason=reason
        )
        dashboard.update_rate_limit(resource_id, new_limit)
        dashboard.display_dashboard()
        time.sleep(1) # Simulate some time passing
