summaryrefslogtreecommitdiffstats
path: root/openbb_platform/providers/oecd/openbb_oecd/models/unemployment.py
blob: 236513430d2b932d7e3e8968dd4d464afd3d1e50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""OECD Unemployment Data."""

# pylint: disable=unused-argument

from datetime import date
from io import StringIO
from typing import Any, Dict, List, Literal, Optional
from warnings import warn

from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.standard_models.unemployment import (
    UnemploymentData,
    UnemploymentQueryParams,
)
from openbb_core.provider.utils.descriptions import QUERY_DESCRIPTIONS
from openbb_core.provider.utils.errors import EmptyDataError
from openbb_core.provider.utils.helpers import check_item, make_request
from openbb_oecd.utils import helpers
from openbb_oecd.utils.constants import (
    CODE_TO_COUNTRY_UNEMPLOYMENT,
    COUNTRY_TO_CODE_UNEMPLOYMENT,
)
from pandas import read_csv
from pydantic import Field, field_validator

countries = tuple(CODE_TO_COUNTRY_UNEMPLOYMENT.values()) + ("all",)
CountriesList = sorted(list(countries))  # type: ignore
AGES = [
    "total",
    "15-24",
    "25-54",
    "55-64",
    "15-64",
    "15-74",
]
AgesLiteral = Literal[
    "total",
    "15-24",
    "25-54",
    "55-64",
    "15-64",
    "15-74",
]


class OECDUnemploymentQueryParams(UnemploymentQueryParams):
    """OECD Unemployment Query.

    Source: https://data-explorer.oecd.org/?lc=en
    """

    __json_schema_extra__ = {"country": ["multiple_items_allowed"]}

    country: str = Field(
        description=QUERY_DESCRIPTIONS.get("country", ""),
        default="united_states",
        choices=CountriesList,
    )
    sex: Literal["total", "male", "female"] = Field(
        description="Sex to get unemployment for.",
        default="total",
        json_schema_extra={"choices": ["total", "male", "female"]},
    )
    age: Literal[AgesLiteral] = Field(
        description="Age group to get unemployment for. Total indicates 15 years or over",
        default="total",
        json_schema_extra={"choices": AGES},
    )
    seasonal_adjustment: bool = Field(
        description="Whether to get seasonally adjusted unemployment. Defaults to False.",
        default=False,
    )

    @field_validator("country", mode="before", check_fields=False)
    @classmethod
    def validate_country(cls, c):
        """Validate country."""
        result: List = []
        values = c.replace(" ", "_").split(",")
        for v in values:
            if v.upper() in CODE_TO_COUNTRY_UNEMPLOYMENT:
                result.append(CODE_TO_COUNTRY_UNEMPLOYMENT.get(v.upper()))
                continue
            try:
                check_item(v.lower(), CountriesList)
            except Exception as e:
                if len(values) == 1:
                    raise e from e
                warn(f"Invalid country: {v}. Skipping...")
                continue
            result.append(v.lower())
        if result:
            return ",".join(result)
        raise OpenBBError(f"No valid country found. -> {values}")


class OECDUnemploymentData(UnemploymentData):
    """OECD Unemployment Data."""


class OECDUnemploymentFetcher(
    Fetcher[OECDUnemploymentQueryParams, List[OECDUnemploymentData]]
):
    """Transform the query, extract and transform the data from the OECD endpoints."""

    @staticmethod
    def transform_query(params: Dict[str, Any]) -> OECDUnemploymentQueryParams:
        """Transform the query."""
        transformed_params = params.copy()
        if transformed_params["start_date"] is None:
            transformed_params["start_date"] = (
                date(2010, 1, 1)
                if transformed_params.get("country") == "all"
                else date(1950, 1, 1)
            )
        if transformed_params["end_date"] is None:
            transformed_params["end_date"] = date(date.today().year, 12, 31)

        return OECDUnemploymentQueryParams(**transformed_params)

    @staticmethod
    def extract_data(
        query: OECDUnemploymentQueryParams,
        credentials: Optional[Dict[str, str]],
        **kwargs: Any,
    ) -> List[Dict]:
        """Return the raw data from the OECD endpoint."""
        sex = {"total": "_T", "male": "M", "female": "F"}[query.sex]
        frequency = query.frequency[0].upper()
        age = {
            "total": "Y_GE15",
            "15-24": "Y15T24",
            "15-64": "Y15T64",
            "15-74": "Y15T74",
            "25-54": "Y25T54",
            "55-64": "Y55T64",
        }[query.age]
        seasonal_adjustment = "Y" if query.seasonal_adjustment else "N"

        def country_string(input_str: str):
            if input_str == "all":
                return ""
            _countries = input_str.split(",")
            return "+".join(
                [COUNTRY_TO_CODE_UNEMPLOYMENT[country] for country in _countries]
            )

        country = country_string(query.country)
        start_date = query.start_date.strftime("%Y-%m") if query.start_date else ""
        end_date = query.end_date.strftime("%Y-%m") if query.end_date else ""
        url = (
            "https://sdmx.oecd.org/public/rest/data/OECD.SDD.TPS,DSD_LFS@DF_IALFS_UNE_M,1.0/"
            + f"{country}..._Z.{seasonal_adjustment}.{sex}.{age}..{frequency}"
            + f"?startPeriod={start_date}&endPeriod={end_date}"
            + "&dimensionAtObservation=TIME_PERIOD&detail=dataonly"
        )
        headers = {"Accept": "application/vnd.sdmx.data+csv; charset=utf-8"}
        response = make_request(url, headers=headers, timeout=20)
        if response.status_code != 200:
            raise OpenBBError(f"Error: {response.status_code} -> {response.text}")
        df = read_csv(StringIO(response.text)).get(
            ["REF_AREA", "TIME_PERIOD", "OBS_VALUE"]
        )
        if df.empty:
            raise EmptyDataError()
        df = df.rename(
            columns={"REF_AREA": "country", "TIME_PERIOD": "date", "OBS_VALUE": "value"}
        )
        df["value"] = df["value"].astype(float) / 100
        df["country"] = df["country"].map(CODE_TO_COUNTRY_UNEMPLOYMENT)
        df["date"] = df["date"].apply(helpers.oecd_date_to_python_date)
        df = (
            df.query("value.notnull()")
            .set_index(["date", "country"])
            .sort_index()
            .reset_index()
        )
        df = df[(df["date"] <= query.end_date) & (df["date"] >= query.start_date)]

        # in column "country" if NaN replace with "all"
        df["country"] = df["country"].fillna("all")

        return df.to_dict(orient="records")

    @staticmethod
    def transform_data(
        query: OECDUnemploymentQueryParams, data: List[Dict], **kwargs: Any
    ) -> List[OECDUnemploymentData]:
        """Transform the data from the OECD endpoint."""
        return [OECDUnemploymentData.model_validate(d) for d in data]