|
| 1 | +"""Schema definitions for usecase dashboards.""" |
| 2 | + |
| 3 | +from typing import List, Optional |
| 4 | + |
| 5 | +import strawberry |
| 6 | +import strawberry_django |
| 7 | +from strawberry.types import Info |
| 8 | +from strawberry_django.pagination import OffsetPaginationInput |
| 9 | + |
| 10 | +from api.models import UseCase, UseCaseDashboard |
| 11 | +from api.schema.base_mutation import BaseMutation |
| 12 | +from api.types.type_usecase_dashboard import ( |
| 13 | + TypeUseCaseDashboard, |
| 14 | + UseCaseDashboardFilter, |
| 15 | + UseCaseDashboardOrder, |
| 16 | +) |
| 17 | +from api.utils.graphql_telemetry import trace_resolver |
| 18 | +from authorization.permissions import IsAuthenticated |
| 19 | + |
| 20 | + |
| 21 | +@strawberry.input |
| 22 | +class UseCaseDashboardInput: |
| 23 | + """Input type for usecase dashboard creation.""" |
| 24 | + |
| 25 | + name: str |
| 26 | + link: str |
| 27 | + |
| 28 | + |
| 29 | +@strawberry.input |
| 30 | +class AddUseCaseDashboardsInput: |
| 31 | + """Input for adding multiple dashboards to a usecase.""" |
| 32 | + |
| 33 | + usecase_id: int |
| 34 | + dashboards: List[UseCaseDashboardInput] |
| 35 | + |
| 36 | + |
| 37 | +@strawberry.type |
| 38 | +class UseCaseDashboardMutationResponse: |
| 39 | + """Response type for usecase dashboard mutations.""" |
| 40 | + |
| 41 | + dashboards: Optional[List[TypeUseCaseDashboard]] = None |
| 42 | + success: bool = False |
| 43 | + message: str = "" |
| 44 | + |
| 45 | + |
| 46 | +class Mutation: |
| 47 | + """UseCaseDashboard mutations.""" |
| 48 | + |
| 49 | + @BaseMutation.mutation( |
| 50 | + permission_classes=[IsAuthenticated], |
| 51 | + trace_name="add_usecase_dashboards", |
| 52 | + trace_attributes={"component": "usecase_dashboard"}, |
| 53 | + track_activity={ |
| 54 | + "verb": "created", |
| 55 | + "get_data": lambda result, **kwargs: { |
| 56 | + "usecase_id": str(kwargs.get("input").usecase_id), |
| 57 | + "dashboard_count": len(result), |
| 58 | + }, |
| 59 | + }, |
| 60 | + ) |
| 61 | + def add_usecase_dashboards( |
| 62 | + self, info: Info, input: AddUseCaseDashboardsInput |
| 63 | + ) -> UseCaseDashboardMutationResponse: |
| 64 | + """Add multiple dashboards to a usecase.""" |
| 65 | + try: |
| 66 | + # Check if usecase exists |
| 67 | + usecase = UseCase.objects.get(id=input.usecase_id) |
| 68 | + |
| 69 | + # Create dashboards |
| 70 | + created_dashboards = [] |
| 71 | + for dashboard_input in input.dashboards: |
| 72 | + dashboard = UseCaseDashboard.objects.create( |
| 73 | + name=dashboard_input.name, |
| 74 | + link=dashboard_input.link, |
| 75 | + usecase=usecase, |
| 76 | + ) |
| 77 | + created_dashboards.append(dashboard) |
| 78 | + |
| 79 | + return UseCaseDashboardMutationResponse( |
| 80 | + dashboards=[ |
| 81 | + TypeUseCaseDashboard.from_django(d) for d in created_dashboards |
| 82 | + ], |
| 83 | + success=True, |
| 84 | + message=f"Added {len(created_dashboards)} dashboards to usecase", |
| 85 | + ) |
| 86 | + except UseCase.DoesNotExist: |
| 87 | + raise Exception(f"Usecase with ID {input.usecase_id} does not exist") |
| 88 | + except Exception as e: |
| 89 | + raise Exception(f"Failed to add dashboards: {str(e)}") |
| 90 | + |
| 91 | + @BaseMutation.mutation( |
| 92 | + permission_classes=[IsAuthenticated], |
| 93 | + trace_name="delete_usecase_dashboard", |
| 94 | + trace_attributes={"component": "usecase_dashboard"}, |
| 95 | + track_activity={ |
| 96 | + "verb": "deleted", |
| 97 | + "get_data": lambda result, **kwargs: { |
| 98 | + "dashboard_id": str(kwargs.get("id")), |
| 99 | + }, |
| 100 | + }, |
| 101 | + ) |
| 102 | + def delete_usecase_dashboard( |
| 103 | + self, info: Info, id: int |
| 104 | + ) -> UseCaseDashboardMutationResponse: |
| 105 | + """Delete a usecase dashboard.""" |
| 106 | + try: |
| 107 | + dashboard = UseCaseDashboard.objects.get(id=id) |
| 108 | + dashboard.delete() |
| 109 | + return UseCaseDashboardMutationResponse( |
| 110 | + success=True, |
| 111 | + message=f"Dashboard with ID {id} deleted successfully", |
| 112 | + ) |
| 113 | + except UseCaseDashboard.DoesNotExist: |
| 114 | + raise Exception(f"Dashboard with ID {id} does not exist") |
| 115 | + except Exception as e: |
| 116 | + raise Exception(f"Failed to delete dashboard: {str(e)}") |
| 117 | + |
| 118 | + |
| 119 | +class Query: |
| 120 | + """UseCaseDashboard queries.""" |
| 121 | + |
| 122 | + @strawberry_django.field( |
| 123 | + filters=UseCaseDashboardFilter, |
| 124 | + pagination=True, |
| 125 | + order=UseCaseDashboardOrder, |
| 126 | + ) |
| 127 | + @trace_resolver( |
| 128 | + name="get_usecase_dashboards_all", attributes={"component": "usecase_dashboard"} |
| 129 | + ) |
| 130 | + def usecase_dashboards_all( |
| 131 | + self, |
| 132 | + info: Info, |
| 133 | + filters: UseCaseDashboardFilter = strawberry.UNSET, |
| 134 | + pagination: OffsetPaginationInput = strawberry.UNSET, |
| 135 | + order: UseCaseDashboardOrder = strawberry.UNSET, |
| 136 | + ) -> list[TypeUseCaseDashboard]: |
| 137 | + """Get all usecase dashboards.""" |
| 138 | + queryset = UseCaseDashboard.objects.all() |
| 139 | + |
| 140 | + if filters is not strawberry.UNSET: |
| 141 | + queryset = strawberry_django.filters.apply(filters, queryset, info) |
| 142 | + |
| 143 | + if order is not strawberry.UNSET: |
| 144 | + queryset = strawberry_django.ordering.apply(order, queryset, info) |
| 145 | + |
| 146 | + dashboard_instances = list(queryset) |
| 147 | + |
| 148 | + if pagination is not strawberry.UNSET: |
| 149 | + offset = pagination.offset if pagination.offset is not None else 0 |
| 150 | + limit = pagination.limit |
| 151 | + |
| 152 | + if limit is not None: |
| 153 | + dashboard_instances = dashboard_instances[offset : offset + limit] |
| 154 | + else: |
| 155 | + dashboard_instances = dashboard_instances[offset:] |
| 156 | + |
| 157 | + return [ |
| 158 | + TypeUseCaseDashboard.from_django(instance) |
| 159 | + for instance in dashboard_instances |
| 160 | + ] |
| 161 | + |
| 162 | + @strawberry_django.field |
| 163 | + @trace_resolver( |
| 164 | + name="get_usecase_dashboards", attributes={"component": "usecase_dashboard"} |
| 165 | + ) |
| 166 | + def usecase_dashboards( |
| 167 | + self, info: Info, usecase_id: int |
| 168 | + ) -> list[TypeUseCaseDashboard]: |
| 169 | + """Get dashboards for a specific usecase.""" |
| 170 | + try: |
| 171 | + # Check if usecase exists |
| 172 | + usecase = UseCase.objects.get(id=usecase_id) |
| 173 | + |
| 174 | + # Get dashboards for the usecase |
| 175 | + dashboards = UseCaseDashboard.objects.filter(usecase=usecase) |
| 176 | + |
| 177 | + return [ |
| 178 | + TypeUseCaseDashboard.from_django(dashboard) for dashboard in dashboards |
| 179 | + ] |
| 180 | + except UseCase.DoesNotExist: |
| 181 | + raise Exception(f"Usecase with ID {usecase_id} does not exist") |
| 182 | + except Exception as e: |
| 183 | + raise Exception(f"Failed to get usecase dashboards: {str(e)}") |
0 commit comments