## PRD: Automated Downscale Trigger Pattern Analysis

This PRD focuses on automatically analyzing the reasons behind AIVA's downscale events to identify common patterns and provide insights for optimization.

### GPM-064: Aggregate Downscale Trigger Counts
**Goal:** To create an aggregate view of downscale trigger counts for each trigger reason.
**Acceptance Criteria:**
- A new table or data structure exists to store the count of each downscale trigger reason.
- The `Display Downscale Trigger Reason` functionality (GPM-063) correctly increments the corresponding trigger reason count.
- A new API endpoint exists to retrieve the aggregated downscale trigger counts.
**Files:** `aiva/analytics/downscale_analysis.py`, `aiva/api/analytics_api.py`
**Spawns:** Visualize the aggregated downscale trigger counts on a dashboard.

### GPM-065: Implement Time-Based Trigger Count Aggregation
**Goal:** To aggregate downscale trigger counts over different time intervals (e.g., hourly, daily, weekly).
**Acceptance Criteria:**
- The system can aggregate downscale trigger counts for hourly, daily, and weekly intervals.
- The API endpoint for retrieving aggregated counts accepts a parameter specifying the desired time interval.
- The API returns aggregated counts for the specified time interval.
**Files:** `aiva/analytics/downscale_analysis.py`, `aiva/api/analytics_api.py`
**Spawns:** Allow filtering aggregated downscale trigger counts by specific cameras or regions.

### GPM-066: Define Common Downscale Patterns
**Goal:** To define a set of common downscale patterns based on combinations of trigger reasons and time intervals.
**Acceptance Criteria:**
- A configuration file or database table defines a set of common downscale patterns (e.g., "High CPU usage during peak hours").
- Each pattern includes a combination of trigger reasons and time intervals.
- The system can load and interpret these patterns.
**Files:** `aiva/config/downscale_patterns.yaml`, `aiva/analytics/pattern_detection.py`
**Spawns:** Detect instances of defined downscale patterns in real-time.

### GPM-067: Detect Downscale Patterns in Aggregated Data
**Goal:** To detect instances of the defined downscale patterns in the aggregated downscale trigger counts.
**Acceptance Criteria:**
- A function or module exists to analyze the aggregated downscale trigger counts and identify occurrences of the defined patterns.
- The system logs detected pattern occurrences, including the timestamp and relevant trigger reasons.
- The pattern detection logic is configurable and can be updated without code changes.
**Files:** `aiva/analytics/pattern_detection.py`
**Spawns:** Alert users when specific downscale patterns are detected.

### GPM-068: Report Most Frequent Downscale Pattern
**Goal:** To identify and report the most frequently occurring downscale pattern.
**Acceptance Criteria:**
- The system tracks the frequency of each detected downscale pattern.
- An API endpoint exists to retrieve the most frequent downscale pattern and its frequency.
- The API returns the pattern name and its count.
**Files:** `aiva/analytics/pattern_detection.py`, `aiva/api/analytics_api.py`
**Spawns:** Provide recommendations for addressing the most frequent downscale pattern.
