Add typing to get_components function and refactor

count_components to accept filter_conditions
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-11-13 12:30:23 -03:00
commit 805ec1570d
2 changed files with 28 additions and 18 deletions

View file

@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import List, Optional from typing import Any, Dict, List, Optional
from uuid import UUID from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException, Query from fastapi import APIRouter, Depends, HTTPException, Query
@ -11,6 +11,7 @@ from langflow.services.deps import get_settings_service, get_store_service
from langflow.services.store.schema import ( from langflow.services.store.schema import (
ComponentResponse, ComponentResponse,
DownloadComponentResponse, DownloadComponentResponse,
ListComponentResponse,
ListComponentResponseModel, ListComponentResponseModel,
StoreComponentCreate, StoreComponentCreate,
TagResponse, TagResponse,
@ -89,9 +90,11 @@ def get_components(
): ):
try: try:
with user_data_context(api_key=store_api_Key, store_service=store_service): with user_data_context(api_key=store_api_Key, store_service=store_service):
filter_conditions: List[Dict[str, Any]] = []
result: List[ListComponentResponse] = []
authorized = False
try: try:
authorized = False result, filter_conditions = store_service.query_components(
result = store_service.query_components(
api_key=store_api_Key, api_key=store_api_Key,
page=page, page=page,
limit=limit, limit=limit,
@ -108,16 +111,19 @@ def get_components(
"You are not authorized to access this public resource" "You are not authorized to access this public resource"
) )
try: try:
comp_count = store_service.count_components( if result:
api_key=store_api_Key, comp_count = store_service.count_components(
filter_by_user=filter_by_user, api_key=store_api_Key,
is_component=is_component, filter_by_user=filter_by_user,
) filter_conditions=filter_conditions,
)
else:
comp_count = 0
except Exception: except Exception:
#! This should be removed once we fix the bug #! This should be removed once we fix the bug
comp_count = 0 comp_count = 0
if store_api_Key: if store_api_Key and result:
# Now, from the result, we need to get the components # Now, from the result, we need to get the components
# the user likes and set the liked_by_user to True # the user likes and set the liked_by_user to True
try: try:

View file

@ -1,6 +1,6 @@
import json import json
from datetime import datetime from datetime import datetime
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple
from uuid import UUID from uuid import UUID
import httpx import httpx
@ -144,8 +144,11 @@ class StoreService(Service):
filter_conditions.append({"is_component": {"_eq": is_component}}) filter_conditions.append({"is_component": {"_eq": is_component}})
if tags: if tags:
# params["filter[tags][_in]"] = ",".join(tags)
filter_conditions.append({"tags": {"tags_id": {"name": {"_in": tags}}}}) filter_conditions.append({"tags": {"tags_id": {"name": {"_in": tags}}}})
# tags_filter = {"tags": {"_or": []}}
# for tag in tags:
# tags_filter["tags"]["_or"].append({"tags_id": {"name": {"_eq": tag}}})
# filter_conditions.append(tags_filter)
if date_from: if date_from:
# params["filter[date_updated][_gte]"] = date_from.isoformat() # params["filter[date_updated][_gte]"] = date_from.isoformat()
@ -183,10 +186,10 @@ class StoreService(Service):
self, self,
api_key: Optional[str] = None, api_key: Optional[str] = None,
filter_by_user: bool = False, filter_by_user: bool = False,
is_component: Optional[bool] = None, filter_conditions: Optional[List[Dict[str, Any]]] = None,
) -> int: ) -> int:
params = {"aggregate": json.dumps({"count": "*"})} params = {"aggregate": json.dumps({"count": "*"})}
filter_conditions = [] filter_conditions = [] if filter_conditions is None else filter_conditions
if filter_by_user: if filter_by_user:
params["deep"] = json.dumps( params["deep"] = json.dumps(
{ {
@ -198,9 +201,6 @@ class StoreService(Service):
else: else:
filter_conditions.append({"status": {"_in": ["public", "Public"]}}) filter_conditions.append({"status": {"_in": ["public", "Public"]}})
if is_component is not None:
filter_conditions.append({"is_component": {"_eq": is_component}})
if filter_conditions: if filter_conditions:
params["filter"] = json.dumps({"_and": filter_conditions}) params["filter"] = json.dumps({"_and": filter_conditions})
@ -229,7 +229,7 @@ class StoreService(Service):
fields: Optional[List[str]] = None, fields: Optional[List[str]] = None,
is_component: Optional[bool] = None, is_component: Optional[bool] = None,
filter_by_user: bool = False, filter_by_user: bool = False,
) -> Union[List[ListComponentResponse], List[Dict[str, int]]]: ) -> Tuple[List[ListComponentResponse], List[Dict[str, Any]]]:
params = {"page": page, "limit": limit} params = {"page": page, "limit": limit}
# ?aggregate[count]=likes # ?aggregate[count]=likes
params["fields"] = ",".join(fields) if fields else ",".join(self.default_fields) params["fields"] = ",".join(fields) if fields else ",".join(self.default_fields)
@ -248,6 +248,10 @@ class StoreService(Service):
if tags: if tags:
filter_conditions.append({"tags": {"tags_id": {"name": {"_in": tags}}}}) filter_conditions.append({"tags": {"tags_id": {"name": {"_in": tags}}}})
# tags_filter = {"tags": {"_or": []}}
# for tag in tags:
# tags_filter["tags"]["_or"].append({"tags_id": {"name": {"_eq": tag}}})
# filter_conditions.append(tags_filter)
if is_component is not None: if is_component is not None:
filter_conditions.append({"is_component": {"_eq": is_component}}) filter_conditions.append({"is_component": {"_eq": is_component}})
@ -276,7 +280,7 @@ class StoreService(Service):
# for component in results_objects: # for component in results_objects:
# if component.tags: # if component.tags:
# component.tags = [tags_id.tags_id for tags_id in component.tags] # component.tags = [tags_id.tags_id for tags_id in component.tags]
return results_objects return results_objects, filter_conditions
def get_liked_by_user_components( def get_liked_by_user_components(
self, component_ids: List[UUID], api_key: str self, component_ids: List[UUID], api_key: str