From 0566e6639efdccf25dd4a0fe9ab9476a71d173c5 Mon Sep 17 00:00:00 2001 From: Josseline Perdomo Date: Aug 19 2021 20:27:16 +0000 Subject: Refactor typing and added properties to be used in the components Signed-off-by: Josseline Perdomo --- diff --git a/contributor_trends/dashboard/datasets.py b/contributor_trends/dashboard/datasets.py index 09a6b00..b13520e 100644 --- a/contributor_trends/dashboard/datasets.py +++ b/contributor_trends/dashboard/datasets.py @@ -1,20 +1,22 @@ import abc -from datetime import datetime from dataclasses import dataclass +from datetime import datetime from enum import Enum -from typing import Tuple, Union +from typing import Tuple, Union, NoReturn from urllib.parse import urljoin import pandas as pd import requests -from contributor_trends.utils.exceptions import InvalidDatasetFormat +from contributor_trends.dashboard.utils.exceptions import InvalidDatasetFormat + +__all__ = ["Dataset", "ActiveContributors"] @dataclass class DatasetMeta: filepath: str - updated_at: datetime + updated_at: Union[datetime, str] def __post_init__(self): if type(self.updated_at) == str: @@ -29,13 +31,10 @@ class ReportFrequency(Enum): class Dataset(abc.ABC): - def __init__(self) -> None: - self._source = "" - self._meta = DatasetMeta(filepath="", updated_at=datetime.min) - self._data = pd.DataFrame() - - def __str__(self) -> str: - return f"Last updated: {self.last_updated}" + def __init__(self) -> NoReturn: + self._source: str = "" + self._meta: DatasetMeta = DatasetMeta(filepath="", updated_at=datetime.min) + self._data: pd.DataFrame = pd.DataFrame() @property def id(self) -> str: @@ -46,6 +45,14 @@ class Dataset(abc.ABC): raise NotImplementedError @property + def last_updated(self) -> datetime: + return self._meta.updated_at + + @property + def description(self) -> str: + return "" + + @property def data(self) -> pd.DataFrame: return self._data @@ -57,10 +64,6 @@ class Dataset(abc.ABC): def y(self) -> Union[pd.Series, str]: raise NotImplementedError - @property - def last_updated(self) -> datetime: - return self._meta.updated_at - @abc.abstractmethod def _prepare_data(self, dataframe: pd.DataFrame) -> pd.DataFrame: # It should be implemented by the inheritance classes @@ -86,7 +89,7 @@ class Dataset(abc.ABC): return updated_status, meta - def load(self, source: str) -> None: + def load(self, source: str) -> NoReturn: self._source = source meta_updated, meta = self._get_meta() @@ -96,7 +99,7 @@ class Dataset(abc.ABC): self._meta = meta self._data = self._get_data() - def update(self): + def update(self) -> NoReturn: # TODO: Add logging to check when there is an update request meta_updated, meta = self._get_meta() if meta_updated: @@ -104,8 +107,10 @@ class Dataset(abc.ABC): self._data = self._get_data() +# TODO: Maybe add a config file (json or yaml) to setup static fields in the dataset presentation +# (like titles, and descriptions) class ActiveContributors(Dataset): - def __init__(self): + def __init__(self) -> NoReturn: super().__init__() self.report_frequency = ReportFrequency.WEEKLY.value @@ -118,6 +123,14 @@ class ActiveContributors(Dataset): return "Active Contributors" @property + def description(self) -> str: + return ( + "Stacked graph of contributors with measured activity each week — and at least 13 weeks total in the last " + "year. “Old school” contributors have been active for longer than two years; new contributors, less than " + "one. Blue line shows all contributors active this week regardless of amount of other activity." + ) + + @property def x(self) -> Union[pd.Index, pd.Series, str]: return self._data.index @@ -125,13 +138,10 @@ class ActiveContributors(Dataset): def y(self) -> Union[pd.Series, str]: return "ActiveUsers" - def load(self, base_url: str) -> None: + def load(self, base_url: str) -> NoReturn: endpoint = urljoin(base_url, "reports/contributors") super(ActiveContributors, self).load(endpoint) def _prepare_data(self, dataframe: pd.DataFrame) -> pd.DataFrame: dataframe.Date = pd.to_datetime(dataframe.Date, format="%m/%d/%y") return dataframe.resample(self.report_frequency, on="Date").sum() - - -active_contributors = ActiveContributors()