|
| 1 | +from fastapi import APIRouter, Depends, Query |
| 2 | + |
| 3 | +from feast.api.registry.rest.rest_utils import ( |
| 4 | + create_grpc_pagination_params, |
| 5 | + create_grpc_sorting_params, |
| 6 | + get_object_relationships, |
| 7 | + get_pagination_params, |
| 8 | + get_relationships_for_objects, |
| 9 | + get_sorting_params, |
| 10 | + grpc_call, |
| 11 | + paginate_and_sort, |
| 12 | + parse_tags, |
| 13 | +) |
| 14 | +from feast.registry_server import RegistryServer_pb2 |
| 15 | + |
| 16 | + |
| 17 | +def get_label_view_router(grpc_handler) -> APIRouter: |
| 18 | + router = APIRouter() |
| 19 | + |
| 20 | + @router.get("/label_views") |
| 21 | + def list_label_views( |
| 22 | + project: str = Query(...), |
| 23 | + allow_cache: bool = Query(default=True), |
| 24 | + include_relationships: bool = Query( |
| 25 | + False, description="Include relationships for each label view" |
| 26 | + ), |
| 27 | + tags: dict = Depends(parse_tags), |
| 28 | + pagination_params: dict = Depends(get_pagination_params), |
| 29 | + sorting_params: dict = Depends(get_sorting_params), |
| 30 | + ): |
| 31 | + req = RegistryServer_pb2.ListLabelViewsRequest( |
| 32 | + project=project, |
| 33 | + allow_cache=allow_cache, |
| 34 | + tags=tags, |
| 35 | + pagination=create_grpc_pagination_params(pagination_params), |
| 36 | + sorting=create_grpc_sorting_params(sorting_params), |
| 37 | + ) |
| 38 | + response = grpc_call(grpc_handler.ListLabelViews, req) |
| 39 | + label_views = response.get("labelViews", []) |
| 40 | + |
| 41 | + for lv in label_views: |
| 42 | + lv["type"] = "labelView" |
| 43 | + |
| 44 | + result = { |
| 45 | + "featureViews": label_views, |
| 46 | + "pagination": response.get("pagination", {}), |
| 47 | + } |
| 48 | + |
| 49 | + if include_relationships: |
| 50 | + relationships = get_relationships_for_objects( |
| 51 | + grpc_handler, label_views, "featureView", project, allow_cache |
| 52 | + ) |
| 53 | + result["relationships"] = relationships |
| 54 | + |
| 55 | + return result |
| 56 | + |
| 57 | + @router.get("/label_views/all") |
| 58 | + def list_label_views_all( |
| 59 | + allow_cache: bool = Query(default=True), |
| 60 | + page: int = Query(1, ge=1), |
| 61 | + limit: int = Query(50, ge=1, le=100), |
| 62 | + sort_by: str = Query(None), |
| 63 | + sort_order: str = Query("asc"), |
| 64 | + include_relationships: bool = Query( |
| 65 | + False, description="Include relationships for each label view" |
| 66 | + ), |
| 67 | + ): |
| 68 | + projects_resp = grpc_call( |
| 69 | + grpc_handler.ListProjects, |
| 70 | + RegistryServer_pb2.ListProjectsRequest(allow_cache=allow_cache), |
| 71 | + ) |
| 72 | + projects = projects_resp.get("projects", []) |
| 73 | + all_label_views = [] |
| 74 | + for project in projects: |
| 75 | + project_name = project["spec"]["name"] |
| 76 | + req = RegistryServer_pb2.ListLabelViewsRequest( |
| 77 | + project=project_name, |
| 78 | + allow_cache=allow_cache, |
| 79 | + ) |
| 80 | + response = grpc_call(grpc_handler.ListLabelViews, req) |
| 81 | + label_views = response.get("labelViews", []) |
| 82 | + for lv in label_views: |
| 83 | + lv["type"] = "labelView" |
| 84 | + lv["project"] = project_name |
| 85 | + all_label_views.extend(label_views) |
| 86 | + |
| 87 | + paged_label_views, pagination = paginate_and_sort( |
| 88 | + all_label_views, page, limit, sort_by, sort_order |
| 89 | + ) |
| 90 | + result = { |
| 91 | + "featureViews": paged_label_views, |
| 92 | + "pagination": pagination, |
| 93 | + } |
| 94 | + if include_relationships: |
| 95 | + relationships_map = {} |
| 96 | + for project in projects: |
| 97 | + project_name = project["spec"]["name"] |
| 98 | + project_lvs = [ |
| 99 | + lv for lv in all_label_views if lv.get("project") == project_name |
| 100 | + ] |
| 101 | + rels = get_relationships_for_objects( |
| 102 | + grpc_handler, |
| 103 | + project_lvs, |
| 104 | + "featureView", |
| 105 | + project_name, |
| 106 | + allow_cache, |
| 107 | + ) |
| 108 | + relationships_map.update(rels) |
| 109 | + result["relationships"] = relationships_map |
| 110 | + return result |
| 111 | + |
| 112 | + @router.get("/label_views/{name}") |
| 113 | + def get_label_view( |
| 114 | + name: str, |
| 115 | + project: str = Query(...), |
| 116 | + include_relationships: bool = Query( |
| 117 | + False, description="Include relationships for this label view" |
| 118 | + ), |
| 119 | + allow_cache: bool = Query(True), |
| 120 | + ): |
| 121 | + req = RegistryServer_pb2.GetLabelViewRequest( |
| 122 | + name=name, |
| 123 | + project=project, |
| 124 | + allow_cache=allow_cache, |
| 125 | + ) |
| 126 | + response = grpc_call(grpc_handler.GetLabelView, req) |
| 127 | + response["type"] = "labelView" |
| 128 | + |
| 129 | + if include_relationships: |
| 130 | + relationships = get_object_relationships( |
| 131 | + grpc_handler, "featureView", name, project, allow_cache |
| 132 | + ) |
| 133 | + response["relationships"] = relationships |
| 134 | + |
| 135 | + return response |
| 136 | + |
| 137 | + return router |
0 commit comments