Commit fde3cd6
authored
Feature: Sandboxes Monitoring - 1 - API/State Management Layer (#137)
Introduces the backend infrastructure and data layer for real-time
monitoring capabilities of team sandbox usage at scale. This establishes
the foundation for metrics collection, processing, and serving.
## Key Features
**Team Metrics API**
- New `/api/teams/[teamId]/metrics` endpoint for historical metrics
- Request validation with 31-day maximum range and future date
protection
- Structured response with metrics array and calculated step intervals
- Support for custom time ranges with proper aggregation
**Server Actions & Data Processing**
- `getTeamMetrics()` - Fetches and processes team sandbox metrics
- `getTeamTierLimits()` - Retrieves tier-specific concurrent sandbox
limits
- Request-level caching with `getNowMemo()` for deduplication
- Memoized functions for performance optimization
**Data Transformation Utilities**
- Time range calculation and step size determination
- Metrics aggregation and gap-filling algorithms
- Support for various time intervals (5s to 15min steps based on range)
- Timezone-aware formatting utilities
**Type System & Validation**
- `ClientTeamMetrics` type for standardized metrics format
- Request/response schemas with Zod validation
- OpenAPI specification updates for new endpoints
- Enhanced `sandboxes.types.ts` with monitoring-specific types
## Architecture Overview
### Metrics Data Pipeline
```mermaid
graph TB
subgraph "Data Sources"
DB[(PostgreSQL<br/>team_metrics table)]
TierDB[(tier_limits table)]
end
subgraph "Server Actions"
GetMetrics["getTeamMetrics()<br/>Input:<br/>- teamId<br/>- startDate (timestamp)<br/>- endDate (timestamp)<br/><br/>Output:<br/>TeamMetricsResponse {<br/> metrics: ClientTeamMetric[]<br/> step: number<br/>}"]
GetTierLimits["getTeamTierLimits()<br/>Input:<br/>- teamId<br/><br/>Output:<br/>{ concurrentInstances: number }"]
end
subgraph "Data Transformation"
ServerTransform["Server Transform<br/>- Calculate step size<br/>- Aggregate by interval<br/>- Format timestamps"]
ClientTransform["Client Transform<br/>fillMetricsWithZeros()<br/>- Fill gaps in data<br/>- Ensure continuous timeline<br/><br/>transformMetricsToLineData()<br/>- Convert to chart format<br/>- x: timestamp<br/>- y: value"]
end
subgraph "Data Structures"
RawMetric["Raw DB Record<br/>{<br/> timestamp: Date<br/> concurrent_sandboxes: number<br/> started_sandboxes: number<br/> team_id: string<br/>}"]
ClientMetric["ClientTeamMetric<br/>{<br/> timestamp: number<br/> concurrentSandboxes: number<br/> startedSandboxes: number<br/>}"]
ChartData["LineSeries<br/>{<br/> name: string<br/> data: {<br/> x: number | Date<br/> y: number<br/> }[]<br/>}"]
end
DB --> RawMetric
RawMetric --> GetMetrics
GetMetrics --> ServerTransform
ServerTransform --> ClientMetric
ClientMetric --> ClientTransform
ClientTransform --> ChartData
TierDB --> GetTierLimits
style DB fill:#e8f5e9
style ClientMetric fill:#fff9c4
style ChartData fill:#e1f5fe
```
## Technical Implementation
**Architecture Decisions**
- Server-only utilities marked with `'server-only'` directive
- Proper separation of client/server type definitions
- Request validation at API boundary with detailed error messages
- Configurable mock data support for development/testing
**Key Files Added**
- `src/app/api/teams/[teamId]/metrics/` - API endpoint and types
- `src/server/sandboxes/get-team-metrics.ts` - Core metrics fetching
logic
- `src/server/team/get-team-tier-limits.ts` - Tier limits functionality
- `src/lib/utils/timeframe.ts` - Time calculation utilities
- `src/lib/utils/formatting.ts` - Formatting utilities (400+ lines)
- `src/configs/intervals.ts`, `src/configs/keys.ts` - Configuration
constants
**Dependencies Added**
- `echarts` - Chart library foundation
- `date-fns-tz` - Timezone handling
- `deepmerge` - Configuration merging
- `sonner` - Toast notifications
---
*This PR provides the complete backend foundation for sandbox
monitoring. The API endpoints are functional and can be tested
independently. No UI components are included - this is purely the data
layer that subsequent PRs will build upon.*1 parent d43ce38 commit fde3cd6
File tree
26 files changed
+1939
-433
lines changed- spec
- src
- app/api/teams/[teamId]
- metrics
- sandboxes/metrics
- configs
- lib
- clients/logger
- utils
- server
- sandboxes
- team
- templates
- types
26 files changed
+1939
-433
lines changedLarge diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | | - | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
| |||
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
56 | | - | |
| 58 | + | |
57 | 59 | | |
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
| |||
84 | 87 | | |
85 | 88 | | |
86 | 89 | | |
| 90 | + | |
87 | 91 | | |
88 | 92 | | |
89 | 93 | | |
90 | 94 | | |
91 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
92 | 100 | | |
93 | 101 | | |
94 | 102 | | |
| |||
101 | 109 | | |
102 | 110 | | |
103 | 111 | | |
104 | | - | |
| 112 | + | |
105 | 113 | | |
106 | 114 | | |
107 | 115 | | |
108 | 116 | | |
109 | 117 | | |
110 | 118 | | |
111 | | - | |
| 119 | + | |
112 | 120 | | |
113 | 121 | | |
114 | 122 | | |
| |||
122 | 130 | | |
123 | 131 | | |
124 | 132 | | |
| 133 | + | |
125 | 134 | | |
126 | 135 | | |
127 | 136 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
461 | 461 | | |
462 | 462 | | |
463 | 463 | | |
464 | | - | |
| 464 | + | |
465 | 465 | | |
466 | 466 | | |
467 | 467 | | |
| |||
608 | 608 | | |
609 | 609 | | |
610 | 610 | | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
611 | 677 | | |
612 | 678 | | |
613 | 679 | | |
| |||
617 | 683 | | |
618 | 684 | | |
619 | 685 | | |
| 686 | + | |
| 687 | + | |
620 | 688 | | |
621 | 689 | | |
622 | 690 | | |
| |||
670 | 738 | | |
671 | 739 | | |
672 | 740 | | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
673 | 752 | | |
674 | 753 | | |
675 | 754 | | |
| |||
705 | 784 | | |
706 | 785 | | |
707 | 786 | | |
708 | | - | |
709 | | - | |
| 787 | + | |
710 | 788 | | |
711 | 789 | | |
712 | 790 | | |
| |||
799 | 877 | | |
800 | 878 | | |
801 | 879 | | |
| 880 | + | |
| 881 | + | |
802 | 882 | | |
803 | 883 | | |
804 | 884 | | |
| |||
816 | 896 | | |
817 | 897 | | |
818 | 898 | | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
819 | 903 | | |
820 | 904 | | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
821 | 908 | | |
822 | 909 | | |
823 | 910 | | |
| |||
845 | 932 | | |
846 | 933 | | |
847 | 934 | | |
| 935 | + | |
| 936 | + | |
848 | 937 | | |
849 | 938 | | |
850 | 939 | | |
| |||
864 | 953 | | |
865 | 954 | | |
866 | 955 | | |
867 | | - | |
| 956 | + | |
868 | 957 | | |
869 | 958 | | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
870 | 966 | | |
871 | 967 | | |
872 | 968 | | |
| |||
1214 | 1310 | | |
1215 | 1311 | | |
1216 | 1312 | | |
1217 | | - | |
1218 | | - | |
| 1313 | + | |
1219 | 1314 | | |
1220 | 1315 | | |
1221 | 1316 | | |
| |||
1656 | 1751 | | |
1657 | 1752 | | |
1658 | 1753 | | |
| 1754 | + | |
1659 | 1755 | | |
1660 | 1756 | | |
1661 | 1757 | | |
| |||
1799 | 1895 | | |
1800 | 1896 | | |
1801 | 1897 | | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
1802 | 1904 | | |
1803 | 1905 | | |
1804 | 1906 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
0 commit comments