Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
Signed-off-by: Avik Basu <ab93@users.noreply.github.com>
  • Loading branch information
ab93 committed Sep 17, 2024
1 parent 1d99b4e commit 5f9f2ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
31 changes: 18 additions & 13 deletions numalogic/connectors/wavefront.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class WavefrontFetcher(DataFetcher):
url (str): Wavefront URL.
api_token (str): Wavefront API token.
Raises:
Raises
------
ValueError: If API token is not provided.
WavefrontFetcherError: If there is an error fetching data from Wavefront.
"""
Expand Down Expand Up @@ -55,7 +56,8 @@ def _format_results(res: dict) -> pd.DataFrame:
"""Validates and formats the results from the API."""
if res.get("error_type") is not None:
raise WavefrontFetcherError(
f"Error fetching data from Wavefront: {res.get('error_type')}: {res.get('error_message')}"
f"Error fetching data from Wavefront: "
f"{res.get('error_type')}: {res.get('error_message')}"
)
if res.get("timeseries") is None:
raise WavefrontFetcherError("No timeseries data found for the query")
Expand All @@ -64,8 +66,7 @@ def _format_results(res: dict) -> pd.DataFrame:
dfs.append(pd.DataFrame(ts["data"], columns=["timestamp", "value"]))
df = pd.concat(dfs)
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="s")
df = df.set_index("timestamp").sort_index()
return df
return df.set_index("timestamp").sort_index()

def fetch(
self,
Expand All @@ -79,16 +80,19 @@ def fetch(
Fetches data from Wavefront as a single metric.
Args:
metric (str): Metric to fetch. Example: 'system.cpu.usage'. Do not include the 'ts()' function.
metric (str): Metric to fetch. Example: 'system.cpu.usage'.
Do not include the 'ts()' function.
start (datetime): Start time.
filters (dict): Filters to apply to the query.
end (datetime): End time. Set to None to fetch data until now.
granularity (str): Granularity of the data. Default is 'm' (minute).
Returns:
Returns
-------
Dataframe with the fetched data in the format: timestamp (index), value (column).
Raises:
Raises
------
WavefrontFetcherError: If there is an error fetching data from Wavefront
"""
start = int(start.timestamp())
Expand All @@ -99,7 +103,7 @@ def fetch(
query = f"ts({metric}, {_filters})"
else:
query = f"ts({metric}"
LOGGER.info(f"Fetching data from Wavefront for query: {query}")
LOGGER.info("Fetching data from Wavefront for query: %s", query)
res = self._call_api(query, start, end, granularity)
return self._format_results(res.to_dict())

Expand All @@ -121,10 +125,12 @@ def raw_fetch(
end (datetime): End time. Set to None to fetch data until now.
granularity (str): Granularity of the data. Default is 'm' (minute).
Returns:
Returns
-------
Dataframe with the fetched data in the format: timestamp (index), value (column).
Raises:
Raises
------
WavefrontFetcherError:
- If there is an error fetching data from Wavefront
- If there is a key error in the query.
Expand All @@ -139,16 +145,15 @@ def raw_fetch(
... end=datetime.now(),
... )
"""

start = start.timestamp()
if end:
end = end.timestamp()

try:
query = query.format(**filters)
except KeyError as key_err:
raise WavefrontFetcherError(f"Key error in query: {key_err}")
raise WavefrontFetcherError(f"Key error in query: {key_err}") from key_err

LOGGER.info(f"Fetching data from Wavefront for query: {query}")
LOGGER.info("Fetching data from Wavefront for query: %s", query)
qres = self._call_api(query, start, granularity, end)
return self._format_results(qres.to_dict())
1 change: 0 additions & 1 deletion tests/connectors/test_wf.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def test_fetch_err_01(wavefront_fetcher, mocker):


def test_fetch_err_02(wavefront_fetcher, mocker):

mocker.patch.object(wavefront_fetcher, "_call_api", return_value=DUMMY_OUT_NO_TS)

with pytest.raises(WavefrontFetcherError):
Expand Down

0 comments on commit 5f9f2ae

Please sign in to comment.