|
1 | 1 | import json
|
2 | 2 | import logging
|
3 |
| -from typing import Optional |
| 3 | +from typing import Dict, List, Optional |
4 | 4 |
|
5 |
| -from fastapi import APIRouter, Depends, Query, Request |
| 5 | +from fastapi import APIRouter, Depends, HTTPException, Query, Request |
| 6 | +from pydantic import BaseModel, Field |
6 | 7 |
|
| 8 | +from feast.api.registry.rest.feature_views import _extract_feature_view_from_any |
7 | 9 | from feast.api.registry.rest.rest_utils import (
|
8 | 10 | get_pagination_params,
|
9 | 11 | get_sorting_params,
|
|
13 | 15 | from feast.protos.feast.registry import RegistryServer_pb2
|
14 | 16 |
|
15 | 17 |
|
| 18 | +class FeatureViewInfo(BaseModel): |
| 19 | + """Feature view information in popular tags response.""" |
| 20 | + |
| 21 | + name: str = Field(..., description="Name of the feature view") |
| 22 | + project: str = Field(..., description="Project name of the feature view") |
| 23 | + |
| 24 | + |
| 25 | +class PopularTagInfo(BaseModel): |
| 26 | + """Popular tag information with associated feature views.""" |
| 27 | + |
| 28 | + tag_key: str = Field(..., description="Tag key") |
| 29 | + tag_value: str = Field(..., description="Tag value") |
| 30 | + feature_views: List[FeatureViewInfo] = Field( |
| 31 | + ..., description="List of feature views with this tag" |
| 32 | + ) |
| 33 | + total_feature_views: int = Field( |
| 34 | + ..., description="Total number of feature views with this tag" |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +class PopularTagsMetadata(BaseModel): |
| 39 | + """Metadata for popular tags response.""" |
| 40 | + |
| 41 | + totalFeatureViews: int = Field( |
| 42 | + ..., description="Total number of feature views processed" |
| 43 | + ) |
| 44 | + totalTags: int = Field(..., description="Total number of unique tags found") |
| 45 | + limit: int = Field(..., description="Number of popular tags requested") |
| 46 | + |
| 47 | + |
| 48 | +class PopularTagsResponse(BaseModel): |
| 49 | + """Response model for popular tags endpoint.""" |
| 50 | + |
| 51 | + popular_tags: List[PopularTagInfo] = Field( |
| 52 | + ..., description="List of popular tags with their associated feature views" |
| 53 | + ) |
| 54 | + metadata: PopularTagsMetadata = Field( |
| 55 | + ..., description="Metadata about the response" |
| 56 | + ) |
| 57 | + |
| 58 | + |
16 | 59 | def get_metrics_router(grpc_handler, server=None) -> APIRouter:
|
17 | 60 | logger = logging.getLogger(__name__)
|
18 | 61 | router = APIRouter()
|
@@ -96,6 +139,155 @@ def count_resources_for_project(project_name: str):
|
96 | 139 | total_counts[k] += counts[k]
|
97 | 140 | return {"total": total_counts, "perProject": all_counts}
|
98 | 141 |
|
| 142 | + @router.get( |
| 143 | + "/metrics/popular_tags", tags=["Metrics"], response_model=PopularTagsResponse |
| 144 | + ) |
| 145 | + async def popular_tags( |
| 146 | + project: Optional[str] = Query( |
| 147 | + None, |
| 148 | + description="Project name for popular tags (optional, returns all projects if not specified)", |
| 149 | + ), |
| 150 | + limit: int = Query(4, description="Number of popular tags to return"), |
| 151 | + allow_cache: bool = Query(default=True), |
| 152 | + ): |
| 153 | + """ |
| 154 | + Discover Feature Views by popular tags. Returns the most popular tags |
| 155 | + (tags assigned to maximum number of feature views) with their associated feature views. |
| 156 | + If no project is specified, returns popular tags across all projects. |
| 157 | + """ |
| 158 | + |
| 159 | + def build_tag_collection( |
| 160 | + feature_views: List[Dict], |
| 161 | + ) -> Dict[str, Dict[str, List[Dict]]]: |
| 162 | + """Build a collection of tags grouped by tag key and tag value.""" |
| 163 | + tag_collection: Dict[str, Dict[str, List[Dict]]] = {} |
| 164 | + |
| 165 | + for fv in feature_views: |
| 166 | + tags = fv.get("spec", {}).get("tags", {}) |
| 167 | + if not tags: |
| 168 | + continue |
| 169 | + |
| 170 | + for tag_key, tag_value in tags.items(): |
| 171 | + if tag_key not in tag_collection: |
| 172 | + tag_collection[tag_key] = {} |
| 173 | + |
| 174 | + if tag_value not in tag_collection[tag_key]: |
| 175 | + tag_collection[tag_key][tag_value] = [] |
| 176 | + |
| 177 | + tag_collection[tag_key][tag_value].append(fv) |
| 178 | + |
| 179 | + return tag_collection |
| 180 | + |
| 181 | + def find_most_popular_tags( |
| 182 | + tag_collection: Dict[str, Dict[str, List[Dict]]], |
| 183 | + ) -> List[Dict]: |
| 184 | + """Find the most popular tags based on total feature view count.""" |
| 185 | + tag_popularity = [] |
| 186 | + |
| 187 | + for tag_key, tag_values_map in tag_collection.items(): |
| 188 | + for tag_value, fv_entries in tag_values_map.items(): |
| 189 | + total_feature_views = len(fv_entries) |
| 190 | + tag_popularity.append( |
| 191 | + { |
| 192 | + "tag_key": tag_key, |
| 193 | + "tag_value": tag_value, |
| 194 | + "feature_views": fv_entries, |
| 195 | + "total_feature_views": total_feature_views, |
| 196 | + } |
| 197 | + ) |
| 198 | + |
| 199 | + return sorted( |
| 200 | + tag_popularity, |
| 201 | + key=lambda x: (x["total_feature_views"], x["tag_key"]), |
| 202 | + reverse=True, |
| 203 | + ) |
| 204 | + |
| 205 | + def get_feature_views_for_project(project_name: str) -> List[Dict]: |
| 206 | + """Get feature views for a specific project.""" |
| 207 | + req = RegistryServer_pb2.ListAllFeatureViewsRequest( |
| 208 | + project=project_name, |
| 209 | + allow_cache=allow_cache, |
| 210 | + ) |
| 211 | + response = grpc_call(grpc_handler.ListAllFeatureViews, req) |
| 212 | + any_feature_views = response.get("featureViews", []) |
| 213 | + feature_views = [] |
| 214 | + for any_feature_view in any_feature_views: |
| 215 | + feature_view = _extract_feature_view_from_any(any_feature_view) |
| 216 | + if feature_view: |
| 217 | + feature_view["project"] = project_name |
| 218 | + feature_views.append(feature_view) |
| 219 | + return feature_views |
| 220 | + |
| 221 | + try: |
| 222 | + if project: |
| 223 | + feature_views = get_feature_views_for_project(project) |
| 224 | + else: |
| 225 | + projects_resp = grpc_call( |
| 226 | + grpc_handler.ListProjects, |
| 227 | + RegistryServer_pb2.ListProjectsRequest(allow_cache=allow_cache), |
| 228 | + ) |
| 229 | + projects = projects_resp.get("projects", []) |
| 230 | + feature_views = [] |
| 231 | + for project_info in projects: |
| 232 | + project_name = project_info["spec"]["name"] |
| 233 | + project_feature_views = get_feature_views_for_project(project_name) |
| 234 | + feature_views.extend(project_feature_views) |
| 235 | + |
| 236 | + if not feature_views: |
| 237 | + return PopularTagsResponse( |
| 238 | + popular_tags=[], |
| 239 | + metadata=PopularTagsMetadata( |
| 240 | + totalFeatureViews=0, |
| 241 | + totalTags=0, |
| 242 | + limit=limit, |
| 243 | + ), |
| 244 | + ) |
| 245 | + |
| 246 | + tag_collection = build_tag_collection(feature_views) |
| 247 | + |
| 248 | + if not tag_collection: |
| 249 | + return PopularTagsResponse( |
| 250 | + popular_tags=[], |
| 251 | + metadata=PopularTagsMetadata( |
| 252 | + totalFeatureViews=len(feature_views), |
| 253 | + totalTags=0, |
| 254 | + limit=limit, |
| 255 | + ), |
| 256 | + ) |
| 257 | + popular_tags = find_most_popular_tags(tag_collection) |
| 258 | + top_popular_tags = popular_tags[:limit] |
| 259 | + formatted_tags = [] |
| 260 | + for tag_info in top_popular_tags: |
| 261 | + feature_view_infos = [ |
| 262 | + FeatureViewInfo( |
| 263 | + name=fv.get("spec", {}).get("name", "unknown"), |
| 264 | + project=fv.get("project", "unknown"), |
| 265 | + ) |
| 266 | + for fv in tag_info["feature_views"] |
| 267 | + ] |
| 268 | + |
| 269 | + formatted_tag = PopularTagInfo( |
| 270 | + tag_key=tag_info["tag_key"], |
| 271 | + tag_value=tag_info["tag_value"], |
| 272 | + feature_views=feature_view_infos, |
| 273 | + total_feature_views=tag_info["total_feature_views"], |
| 274 | + ) |
| 275 | + formatted_tags.append(formatted_tag) |
| 276 | + |
| 277 | + return PopularTagsResponse( |
| 278 | + popular_tags=formatted_tags, |
| 279 | + metadata=PopularTagsMetadata( |
| 280 | + totalFeatureViews=len(feature_views), |
| 281 | + totalTags=len(popular_tags), |
| 282 | + limit=limit, |
| 283 | + ), |
| 284 | + ) |
| 285 | + except Exception as e: |
| 286 | + raise HTTPException( |
| 287 | + status_code=500, |
| 288 | + detail=f"Failed to generate popular tags: {str(e)}", |
| 289 | + ) |
| 290 | + |
99 | 291 | @router.get("/metrics/recently_visited", tags=["Metrics"])
|
100 | 292 | async def recently_visited(
|
101 | 293 | request: Request,
|
|
0 commit comments