Skip to content

Commit

Permalink
[Enhancement] Improve FRED Search (#6785)
Browse files Browse the repository at this point in the history
* make fred search better

* lint

* more lint

* and more lint

* and more lint

* recapture cassettes
  • Loading branch information
deeleeramone authored Oct 17, 2024
1 parent a207c3e commit ad634e2
Show file tree
Hide file tree
Showing 12 changed files with 670 additions and 2,401 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
date as dateType,
datetime,
)
from typing import Optional, Union
from typing import Optional

from dateutil import parser
from pydantic import Field, field_validator
from pydantic import Field

from openbb_core.provider.abstract.data import Data
from openbb_core.provider.abstract.query_params import QueryParams
Expand All @@ -22,14 +21,22 @@ class SearchQueryParams(QueryParams):
class SearchData(Data):
"""FRED Search Data."""

release_id: Optional[Union[str, int]] = Field(
release_id: Optional[str] = Field(
default=None,
description="The release ID for queries.",
)
series_id: Optional[str] = Field(
default=None,
description="The series ID for the item in the release.",
)
series_group: Optional[str] = Field(
default=None,
description="The series group ID of the series. This value is used to query for regional data.",
)
region_type: Optional[str] = Field(
default=None,
description="The region type of the series.",
)
name: Optional[str] = Field(
default=None,
description="The name of the release.",
Expand Down Expand Up @@ -72,6 +79,22 @@ class SearchData(Data):
default=None,
description="The datetime of the last update to the data.",
)
popularity: Optional[int] = Field(
default=None,
description="Popularity of the series",
)
group_popularity: Optional[int] = Field(
default=None,
description="Group popularity of the release",
)
realtime_start: Optional[dateType] = Field(
default=None,
description="The realtime start date of the series.",
)
realtime_end: Optional[dateType] = Field(
default=None,
description="The realtime end date of the series.",
)
notes: Optional[str] = Field(
default=None, description="Description of the release."
)
Expand All @@ -80,9 +103,3 @@ class SearchData(Data):
default=None,
)
url: Optional[str] = Field(default=None, description="URL to the release.")

@field_validator("last_updated", mode="before", check_fields=False)
@classmethod
def date_validate(cls, v):
"""Validate datetime format."""
return parser.isoparse(v) if v else None
31 changes: 11 additions & 20 deletions openbb_platform/extensions/economy/integration/test_economy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,26 +285,13 @@ def test_economy_balance_of_payments(params, headers):
[
(
{
"query": None,
"is_release": False,
"release_id": 15,
"offset": 0,
"limit": 1000,
"filter_variable": "frequency",
"filter_value": "Monthly",
"tag_names": "nsa",
"exclude_tag_names": None,
"series_id": None,
"provider": "fred",
}
),
(
{
"query": "GDP",
"is_release": True,
"query": "GDP*",
"search_type": "series_id",
"release_id": None,
"offset": 0,
"limit": 1000,
"limit": 10,
"order_by": "observation_end",
"sort_order": "desc",
"filter_variable": None,
"filter_value": None,
"tag_names": None,
Expand All @@ -316,10 +303,12 @@ def test_economy_balance_of_payments(params, headers):
(
{
"query": None,
"is_release": False,
"search_type": "release",
"release_id": None,
"offset": None,
"limit": None,
"order_by": "observation_end",
"sort_order": "desc",
"filter_variable": None,
"filter_value": None,
"tag_names": None,
Expand All @@ -331,10 +320,12 @@ def test_economy_balance_of_payments(params, headers):
(
{
"query": None,
"is_release": False,
"search_type": "full_text",
"release_id": None,
"offset": None,
"limit": None,
"order_by": "observation_end",
"sort_order": "desc",
"filter_variable": None,
"filter_value": None,
"tag_names": None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,13 @@ def test_economy_balance_of_payments(params, obb):
[
(
{
"query": None,
"is_release": False,
"release_id": "15",
"offset": 0,
"limit": 1000,
"filter_variable": "frequency",
"filter_value": "Monthly",
"tag_names": "nsa",
"exclude_tag_names": None,
"series_id": None,
"provider": "fred",
}
),
(
{
"query": "GDP",
"is_release": True,
"query": "GDP*",
"search_type": "series_id",
"release_id": None,
"offset": 0,
"limit": 1000,
"limit": 10,
"order_by": "observation_end",
"sort_order": "desc",
"filter_variable": None,
"filter_value": None,
"tag_names": None,
Expand All @@ -303,10 +290,12 @@ def test_economy_balance_of_payments(params, obb):
(
{
"query": None,
"is_release": False,
"search_type": "release",
"release_id": None,
"offset": None,
"limit": None,
"order_by": "observation_end",
"sort_order": "desc",
"filter_variable": None,
"filter_value": None,
"tag_names": None,
Expand All @@ -318,10 +307,12 @@ def test_economy_balance_of_payments(params, obb):
(
{
"query": None,
"is_release": False,
"search_type": "full_text",
"release_id": None,
"offset": None,
"limit": None,
"order_by": "observation_end",
"sort_order": "desc",
"filter_variable": None,
"filter_value": None,
"tag_names": None,
Expand Down
64 changes: 49 additions & 15 deletions openbb_platform/openbb/package/economy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2255,22 +2255,26 @@ def fred_search(
The search word(s).
provider : Optional[Literal['fred']]
The provider to use, by default None. If None, the priority list configured in the settings is used. Default priority: fred.
is_release : Optional[bool]
Is release? If True, other search filter variables are ignored. If no query text or release_id is supplied, this defaults to True. (provider: fred)
search_type : Literal['full_text', 'series_id', 'release']
The type of search to perform. Automatically set to 'release' when a 'release_id' is provided. (provider: fred)
release_id : Optional[Union[int, str]]
A specific release ID to target. (provider: fred)
limit : Optional[int]
The number of data entries to return. (1-1000) (provider: fred)
offset : Optional[Annotated[int, Ge(ge=0)]]
Offset the results in conjunction with limit. (provider: fred)
Offset the results in conjunction with limit. This parameter is ignored When search_type is 'release'. (provider: fred)
order_by : Literal['search_rank', 'series_id', 'title', 'units', 'frequency', 'seasonal_adjustment', 'realtime_start', 'realtime_end', 'last_updated', 'observation_start', 'observation_end', 'popularity', 'group_popularity']
Order the results by a specific attribute. The default is 'observation_end'. (provider: fred)
sort_order : Literal['asc', 'desc']
Sort the 'order_by' item in ascending or descending order. The default is 'desc'. (provider: fred)
filter_variable : Optional[Literal['frequency', 'units', 'seasonal_adjustment']]
Filter by an attribute. (provider: fred)
filter_value : Optional[str]
String value to filter the variable by. Used in conjunction with filter_variable. (provider: fred)
String value to filter the variable by. Used in conjunction with filter_variable. This parameter is ignored when search_type is 'release'. (provider: fred)
tag_names : Optional[str]
A semicolon delimited list of tag names that series match all of. Example: 'japan;imports' Multiple comma separated items allowed. (provider: fred)
A semicolon delimited list of tag names that series match all of. Example: 'japan;imports' This parameter is ignored when search_type is 'release'. Multiple comma separated items allowed. (provider: fred)
exclude_tag_names : Optional[str]
A semicolon delimited list of tag names that series match none of. Example: 'imports;services'. Requires that variable tag_names also be set to limit the number of matching series. Multiple comma separated items allowed. (provider: fred)
A semicolon delimited list of tag names that series match none of. Example: 'imports;services'. Requires that variable tag_names also be set to limit the number of matching series. This parameter is ignored when search_type is 'release'. Multiple comma separated items allowed. (provider: fred)
series_id : Optional[str]
A FRED Series ID to return series group information for. This returns the required information to query for regional data. Not all series that are in FRED have geographical data. Entering a value for series_id will override all other parameters. Multiple series_ids can be separated by commas. (provider: fred)
Expand All @@ -2290,10 +2294,14 @@ def fred_search(
FredSearch
----------
release_id : Optional[Union[int, str]]
release_id : Optional[str]
The release ID for queries.
series_id : Optional[str]
The series ID for the item in the release.
series_group : Optional[str]
The series group ID of the series. This value is used to query for regional data.
region_type : Optional[str]
The region type of the series.
name : Optional[str]
The name of the release.
title : Optional[str]
Expand All @@ -2316,20 +2324,20 @@ def fred_search(
Short form of the data seasonal adjustment.
last_updated : Optional[datetime]
The datetime of the last update to the data.
popularity : Optional[int]
Popularity of the series
group_popularity : Optional[int]
Group popularity of the release
realtime_start : Optional[date]
The realtime start date of the series.
realtime_end : Optional[date]
The realtime end date of the series.
notes : Optional[str]
Description of the release.
press_release : Optional[bool]
If the release is a press release.
url : Optional[str]
URL to the release.
popularity : Optional[int]
Popularity of the series (provider: fred)
group_popularity : Optional[int]
Group popularity of the release (provider: fred)
region_type : Optional[str]
The region type of the series. (provider: fred)
series_group : Optional[Union[int, str]]
The series group ID of the series. This value is used to query for regional data. (provider: fred)
Examples
--------
Expand All @@ -2352,6 +2360,32 @@ def fred_search(
},
extra_params=kwargs,
info={
"search_type": {
"fred": {
"multiple_items_allowed": False,
"choices": ["full_text", "series_id", "release"],
}
},
"order_by": {
"fred": {
"multiple_items_allowed": False,
"choices": [
"search_rank",
"series_id",
"title",
"units",
"frequency",
"seasonal_adjustment",
"realtime_start",
"realtime_end",
"last_updated",
"observation_start",
"observation_end",
"popularity",
"group_popularity",
],
}
},
"tag_names": {
"fred": {"multiple_items_allowed": True, "choices": None}
},
Expand Down
Loading

0 comments on commit ad634e2

Please sign in to comment.