-
Notifications
You must be signed in to change notification settings - Fork 6
[POC] Provide a metric dashboard #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NathanVss
wants to merge
4
commits into
main
Choose a base branch
from
feat/metrics-dashboard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
47eb2d1
✨(backend) expose operator metrics dashboard API
NathanVss 09d30ce
✨(frontend) add operator metrics dashboard
NathanVss acc1684
✨(backend) add create_demo_operator command
NathanVss a87de80
✨(frontend) add desktop left panel with primary navigation
NathanVss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| """Metrics API viewsets.""" | ||
| """ | ||
| API endpoints for Metrics model. | ||
| """ | ||
|
|
||
| from django.db.models import Avg, Sum | ||
|
|
||
| from rest_framework import status, viewsets | ||
| from rest_framework.settings import api_settings | ||
|
|
||
| from core import models | ||
| from core.authentication import ExternalManagementApiKeyAuthentication | ||
|
|
||
| from .. import permissions, serializers as core_serializers | ||
|
|
||
| from rest_framework import serializers | ||
| from rest_framework.response import Response | ||
|
|
@@ -62,3 +75,161 @@ def get(self, request): | |
| "results": results, | ||
| } | ||
| ) | ||
|
|
||
| class OperatorMetricsViewSet(viewsets.ViewSet): | ||
| """ViewSet for Metrics model nested under Operator. | ||
|
|
||
| GET /api/v1.0/operators/<operator_id>/metrics/ | ||
| Return the list of metrics for the given operator based on filters. | ||
| Supports filtering by key, service, organizations, accounts, account_type. | ||
| Supports aggregation via agg=sum|avg query param. | ||
|
|
||
| Required query params: | ||
| - key: Metric key to filter on (single value) | ||
| - service: Service ID to filter on (single value) | ||
|
|
||
| Optional query params: | ||
| - organizations: Comma-separated organization IDs. Defaults to all orgs the operator has access to. | ||
| - accounts: Comma-separated account IDs. If omitted, returns all accounts (including null). | ||
| - account_type: Filter by account type (e.g., "user", "mailbox"). | ||
| - agg: Aggregation type (sum|avg). If provided, returns aggregated value. | ||
| - group_by: Group results by 'organization'. Returns sum per organization. | ||
| """ | ||
|
|
||
| authentication_classes = [ | ||
| ExternalManagementApiKeyAuthentication, | ||
| ] + list(api_settings.DEFAULT_AUTHENTICATION_CLASSES) | ||
| permission_classes = [ | ||
| permissions.IsAuthenticatedWithAnyMethod, | ||
| permissions.OperatorAccessPermission, | ||
| ] | ||
|
|
||
| def _get_operator_organizations(self, operator_id): | ||
| """Get all organization IDs that the operator has access to.""" | ||
| return models.Organization.objects.filter( | ||
| operator_roles__operator_id=operator_id | ||
| ).values_list("id", flat=True) | ||
|
|
||
| def _parse_comma_separated_ids(self, param_value): | ||
| """Parse comma-separated IDs from query param.""" | ||
| if not param_value: | ||
| return None | ||
| return [id.strip() for id in param_value.split(",") if id.strip()] | ||
|
|
||
| def list(self, request, operator_id=None): | ||
| """List metrics with filtering and optional aggregation.""" | ||
| # Validate required params | ||
| key = request.query_params.get("key") | ||
| service_id = request.query_params.get("service") | ||
|
|
||
| if not key: | ||
| return Response( | ||
| {"error": "Query parameter 'key' is required."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| if not service_id: | ||
| return Response( | ||
| {"error": "Query parameter 'service' is required."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| # Validate service exists | ||
| try: | ||
| service = models.Service.objects.get(id=service_id) | ||
| except models.Service.DoesNotExist: | ||
| return Response( | ||
| {"error": f"Service with id '{service_id}' not found."}, | ||
| status=status.HTTP_404_NOT_FOUND, | ||
| ) | ||
|
|
||
| # Get allowed organizations for this operator (convert UUIDs to strings for comparison) | ||
| allowed_org_ids = set( | ||
| str(org_id) for org_id in self._get_operator_organizations(operator_id) | ||
| ) | ||
|
|
||
| # Parse optional organization filter | ||
| org_ids_param = request.query_params.get("organizations") | ||
| if org_ids_param: | ||
| requested_org_ids = set(self._parse_comma_separated_ids(org_ids_param)) | ||
| # Filter to only allowed organizations | ||
| org_ids = list(requested_org_ids & allowed_org_ids) | ||
| if not org_ids: | ||
| return Response( | ||
| {"error": "No valid organizations found for the given IDs."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
| else: | ||
| # Default to all organizations the operator has access to | ||
| org_ids = list(allowed_org_ids) | ||
|
|
||
| # Build base queryset | ||
| queryset = models.Metric.objects.filter( | ||
| key=key, | ||
| service=service, | ||
| organization_id__in=org_ids, | ||
| ) | ||
|
|
||
| # Filter by account_type if provided | ||
| account_type = request.query_params.get("account_type") | ||
| if account_type: | ||
| queryset = queryset.filter(account__type=account_type) | ||
|
|
||
| # Filter by accounts if provided | ||
| accounts_param = request.query_params.get("accounts") | ||
| if accounts_param: | ||
| account_ids = self._parse_comma_separated_ids(accounts_param) | ||
| queryset = queryset.filter(account_id__in=account_ids) | ||
|
|
||
|
Comment on lines
+179
to
+183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate At Line 182, malformed IDs can bubble into DB errors instead of returning a controlled 400 response. Proposed fix+import uuid
...
accounts_param = request.query_params.get("accounts")
if accounts_param:
- account_ids = self._parse_comma_separated_ids(accounts_param)
+ account_ids = self._parse_comma_separated_ids(accounts_param)
+ try:
+ account_ids = [str(uuid.UUID(v)) for v in account_ids]
+ except (TypeError, ValueError):
+ return Response(
+ {"error": "Query parameter 'accounts' must contain valid UUIDs."},
+ status=status.HTTP_400_BAD_REQUEST,
+ )
queryset = queryset.filter(account_id__in=account_ids)🤖 Prompt for AI Agents |
||
| # Handle aggregation | ||
| agg = request.query_params.get("agg") | ||
| if agg: | ||
| if agg not in ("sum", "avg"): | ||
| return Response( | ||
| {"error": "Query parameter 'agg' must be 'sum' or 'avg'."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| if agg == "sum": | ||
| result = queryset.aggregate(value=Sum("value")) | ||
| else: # avg | ||
| result = queryset.aggregate(value=Avg("value")) | ||
|
|
||
| serializer = core_serializers.AggregatedMetricSerializer( | ||
| { | ||
| "key": key, | ||
| "service_id": service_id, | ||
| "aggregation": agg, | ||
| "value": result["value"] or 0, | ||
| "count": queryset.count(), | ||
| } | ||
| ) | ||
| return Response(serializer.data) | ||
|
|
||
| # Handle group_by parameter | ||
| group_by = request.query_params.get("group_by") | ||
| if group_by == "organization": | ||
| # Group by organization and sum values | ||
| grouped = ( | ||
| queryset.values("organization__id", "organization__name") | ||
| .annotate(value=Sum("value")) | ||
| .order_by("organization__name") | ||
| ) | ||
| results = [ | ||
| { | ||
| "organization": { | ||
| "id": str(item["organization__id"]), | ||
| "name": item["organization__name"], | ||
| }, | ||
| "value": str(item["value"]), | ||
| } | ||
| for item in grouped | ||
| ] | ||
| return Response({"results": results, "grouped_by": "organization"}) | ||
|
|
||
| # Return list of metrics | ||
| queryset = queryset.select_related("account", "organization").order_by( | ||
| "organization__name", "account__email" | ||
| ) | ||
| serializer = core_serializers.MetricSerializer(queryset, many=True) | ||
| return Response({"results": serializer.data}) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enforce that
servicebelongs to the operator scope.Current validation only checks existence. At Line 139, a service outside the operator perimeter is accepted, which weakens endpoint scoping.
Proposed fix
🤖 Prompt for AI Agents