|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from fastapi import APIRouter, Depends, Query, Request |
| 6 | + |
| 7 | +from feast.api.registry.rest.rest_utils import ( |
| 8 | + get_pagination_params, |
| 9 | + get_sorting_params, |
| 10 | + grpc_call, |
| 11 | + paginate_and_sort, |
| 12 | +) |
| 13 | +from feast.protos.feast.registry import RegistryServer_pb2 |
| 14 | + |
| 15 | + |
| 16 | +def get_metrics_router(grpc_handler, server=None) -> APIRouter: |
| 17 | + logger = logging.getLogger(__name__) |
| 18 | + router = APIRouter() |
| 19 | + |
| 20 | + @router.get("/metrics/resource_counts", tags=["Metrics"]) |
| 21 | + async def resource_counts( |
| 22 | + project: Optional[str] = Query( |
| 23 | + None, description="Project name to filter resource counts" |
| 24 | + ), |
| 25 | + ): |
| 26 | + def count_resources_for_project(project_name: str): |
| 27 | + entities = grpc_call( |
| 28 | + grpc_handler.ListEntities, |
| 29 | + RegistryServer_pb2.ListEntitiesRequest(project=project_name), |
| 30 | + ) |
| 31 | + data_sources = grpc_call( |
| 32 | + grpc_handler.ListDataSources, |
| 33 | + RegistryServer_pb2.ListDataSourcesRequest(project=project_name), |
| 34 | + ) |
| 35 | + try: |
| 36 | + saved_datasets = grpc_call( |
| 37 | + grpc_handler.ListSavedDatasets, |
| 38 | + RegistryServer_pb2.ListSavedDatasetsRequest(project=project_name), |
| 39 | + ) |
| 40 | + except Exception: |
| 41 | + saved_datasets = {"savedDatasets": []} |
| 42 | + try: |
| 43 | + features = grpc_call( |
| 44 | + grpc_handler.ListFeatures, |
| 45 | + RegistryServer_pb2.ListFeaturesRequest(project=project_name), |
| 46 | + ) |
| 47 | + except Exception: |
| 48 | + features = {"features": []} |
| 49 | + try: |
| 50 | + feature_views = grpc_call( |
| 51 | + grpc_handler.ListFeatureViews, |
| 52 | + RegistryServer_pb2.ListFeatureViewsRequest(project=project_name), |
| 53 | + ) |
| 54 | + except Exception: |
| 55 | + feature_views = {"featureViews": []} |
| 56 | + try: |
| 57 | + feature_services = grpc_call( |
| 58 | + grpc_handler.ListFeatureServices, |
| 59 | + RegistryServer_pb2.ListFeatureServicesRequest(project=project_name), |
| 60 | + ) |
| 61 | + except Exception: |
| 62 | + feature_services = {"featureServices": []} |
| 63 | + return { |
| 64 | + "entities": len(entities.get("entities", [])), |
| 65 | + "dataSources": len(data_sources.get("dataSources", [])), |
| 66 | + "savedDatasets": len(saved_datasets.get("savedDatasets", [])), |
| 67 | + "features": len(features.get("features", [])), |
| 68 | + "featureViews": len(feature_views.get("featureViews", [])), |
| 69 | + "featureServices": len(feature_services.get("featureServices", [])), |
| 70 | + } |
| 71 | + |
| 72 | + if project: |
| 73 | + counts = count_resources_for_project(project) |
| 74 | + return {"project": project, "counts": counts} |
| 75 | + else: |
| 76 | + # List all projects via gRPC |
| 77 | + projects_resp = grpc_call( |
| 78 | + grpc_handler.ListProjects, RegistryServer_pb2.ListProjectsRequest() |
| 79 | + ) |
| 80 | + all_projects = [ |
| 81 | + p["spec"]["name"] for p in projects_resp.get("projects", []) |
| 82 | + ] |
| 83 | + all_counts = {} |
| 84 | + total_counts = { |
| 85 | + "entities": 0, |
| 86 | + "dataSources": 0, |
| 87 | + "savedDatasets": 0, |
| 88 | + "features": 0, |
| 89 | + "featureViews": 0, |
| 90 | + "featureServices": 0, |
| 91 | + } |
| 92 | + for project_name in all_projects: |
| 93 | + counts = count_resources_for_project(project_name) |
| 94 | + all_counts[project_name] = counts |
| 95 | + for k in total_counts: |
| 96 | + total_counts[k] += counts[k] |
| 97 | + return {"total": total_counts, "perProject": all_counts} |
| 98 | + |
| 99 | + @router.get("/metrics/recently_visited", tags=["Metrics"]) |
| 100 | + async def recently_visited( |
| 101 | + request: Request, |
| 102 | + project: Optional[str] = Query( |
| 103 | + None, description="Project name to filter recent visits" |
| 104 | + ), |
| 105 | + object_type: Optional[str] = Query( |
| 106 | + None, |
| 107 | + alias="object", |
| 108 | + description="Object type to filter recent visits (e.g., entities, features)", |
| 109 | + ), |
| 110 | + pagination_params: dict = Depends(get_pagination_params), |
| 111 | + sorting_params: dict = Depends(get_sorting_params), |
| 112 | + ): |
| 113 | + user = None |
| 114 | + if hasattr(request.state, "user"): |
| 115 | + user = getattr(request.state, "user", None) |
| 116 | + if not user: |
| 117 | + user = "anonymous" |
| 118 | + project_val = project or (server.store.project if server else None) |
| 119 | + key = f"recently_visited_{user}" |
| 120 | + logger.info( |
| 121 | + f"[/metrics/recently_visited] Project: {project_val}, Key: {key}, Object: {object_type}" |
| 122 | + ) |
| 123 | + try: |
| 124 | + visits_json = ( |
| 125 | + server.registry.get_project_metadata(project_val, key) |
| 126 | + if server |
| 127 | + else None |
| 128 | + ) |
| 129 | + visits = json.loads(visits_json) if visits_json else [] |
| 130 | + except Exception: |
| 131 | + visits = [] |
| 132 | + if object_type: |
| 133 | + visits = [v for v in visits if v.get("object") == object_type] |
| 134 | + |
| 135 | + server_limit = getattr(server, "recent_visits_limit", 100) if server else 100 |
| 136 | + visits = visits[-server_limit:] |
| 137 | + |
| 138 | + page = pagination_params.get("page", 0) |
| 139 | + limit = pagination_params.get("limit", 0) |
| 140 | + sort_by = sorting_params.get("sort_by") |
| 141 | + sort_order = sorting_params.get("sort_order", "asc") |
| 142 | + |
| 143 | + if page == 0 and limit == 0: |
| 144 | + if sort_by: |
| 145 | + visits = sorted( |
| 146 | + visits, |
| 147 | + key=lambda x: x.get(sort_by, ""), |
| 148 | + reverse=(sort_order == "desc"), |
| 149 | + ) |
| 150 | + return {"visits": visits, "pagination": {"totalCount": len(visits)}} |
| 151 | + else: |
| 152 | + if page == 0: |
| 153 | + page = 1 |
| 154 | + if limit == 0: |
| 155 | + limit = 50 |
| 156 | + paged_visits, pagination = paginate_and_sort( |
| 157 | + visits, page, limit, sort_by, sort_order |
| 158 | + ) |
| 159 | + return { |
| 160 | + "visits": paged_visits, |
| 161 | + "pagination": pagination, |
| 162 | + } |
| 163 | + |
| 164 | + return router |
0 commit comments