Skip to content

Commit 3674291

Browse files
committed
fix github views,clones stat if data not available
1 parent 2c8908d commit 3674291

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/writegithubstat/githubstat.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GithubAuth:
1212
def __init__(self, owner: str, repo: str, token: str) -> None:
1313
self._owner = owner
1414
self._repo = repo
15-
self._header = GithubAuth._get_auth_header(token)
15+
self._header = self._get_auth_header(token)
1616

1717
@staticmethod
1818
def _get_auth_header(token: str) -> dict:
@@ -55,8 +55,8 @@ def dimensions(self):
5555
def measures(self):
5656
pass
5757

58-
@staticmethod
59-
def process_stat(responses):
58+
@abstractmethod
59+
def process_stat(self, responses):
6060
pass
6161

6262

@@ -75,8 +75,7 @@ def dimensions(self):
7575
def measures(self):
7676
return ["count", "uniques"]
7777

78-
@staticmethod
79-
def process_stat(responses):
78+
def process_stat(self, responses):
8079
data = responses[0]
8180
df = pd.DataFrame(data)
8281
return df
@@ -97,8 +96,7 @@ def dimensions(self):
9796
def measures(self):
9897
return ["count", "uniques"]
9998

100-
@staticmethod
101-
def process_stat(responses):
99+
def process_stat(self, responses):
102100
data = responses[0]
103101
df = pd.DataFrame(data)
104102
return df
@@ -117,8 +115,7 @@ def dimensions(self):
117115
def measures(self):
118116
return ["stars", "forks"]
119117

120-
@staticmethod
121-
def process_stat(responses):
118+
def process_stat(self, responses):
122119
data = responses[0]
123120
stars = data["stargazers_count"]
124121
forks = data["forks_count"]
@@ -127,6 +124,10 @@ def process_stat(responses):
127124

128125

129126
class ViewsClones(GithubStatType):
127+
def __init__(self, owner: str, repo: str, date: str) -> None:
128+
super().__init__(owner, repo)
129+
self._date = date
130+
130131
@property
131132
def urls(self):
132133
return [
@@ -140,12 +141,11 @@ def dimensions(self):
140141

141142
@property
142143
def measures(self):
143-
return []
144+
return ["views_total", "views_unique", "clones_total", "clones_total"]
144145

145-
@staticmethod
146-
def process_stat(responses):
147-
views = responses[0]["views"][-1]
148-
clones = responses[1]["clones"][-1]
146+
def process_stat(self, responses):
147+
views = self._get_actual_stat(responses[0], "views")
148+
clones = self._get_actual_stat(responses[0], "clones")
149149
df = pd.DataFrame(
150150
{
151151
"views_total": [views["count"]],
@@ -156,10 +156,21 @@ def process_stat(responses):
156156
)
157157
return df
158158

159+
def _get_actual_stat(self, data, name):
160+
try:
161+
stat = data[name][-1]
162+
if not stat["timestamp"].startswith(self._date):
163+
raise ValueError(
164+
f"The views data for the date {self._date} is not available."
165+
)
166+
return stat
167+
except (KeyError, IndexError, ValueError):
168+
return {"count": 0, "uniques": 0}
169+
159170

160-
class _GithubStat:
171+
class GithubStatAPI:
161172
@staticmethod
162-
def _get_stat(stat_type: GithubStatType, auth_header: dict) -> pd.DataFrame:
173+
def get_stat(stat_type: GithubStatType, auth_header: dict) -> pd.DataFrame:
163174
responses = []
164175
for url in stat_type.urls:
165176
response = requests.get(url, headers=auth_header)
@@ -190,11 +201,10 @@ def write_stat(self, stat_type: GithubStatType, csv: Path) -> None:
190201
merged_stats.to_csv(csv, index=False)
191202

192203
def _get_stats(self, stat_type: GithubStatType) -> pd.DataFrame:
193-
stat = _GithubStat._get_stat(stat_type, self._auth.header)
204+
stat = GithubStatAPI.get_stat(stat_type, self._auth.header)
194205
if stat.empty:
195-
empty = {
196-
**{col: "-" for col in stat_type.dimensions},
197-
**{col: 0 for col in stat_type.measures},
206+
empty = {col: "-" for col in stat_type.dimensions} | {
207+
col: 0 for col in stat_type.measures
198208
}
199209
stat = pd.DataFrame([empty])
200210
stat = self._insert_metadata(stat)

0 commit comments

Comments
 (0)