forked from sangonzal/repository-traffic-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (55 loc) · 2.58 KB
/
main.py
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
from github import Github
import os
import pandas as pd
import matplotlib.pyplot as plt
repo_name = os.environ["GITHUB_REPOSITORY"]
github = Github(os.environ["TRAFFIC_ACTION_TOKEN"])
print("Repository name: ", repo_name)
repo = github.get_repo(repo_name)
workplace_path = "{}/{}".format(os.environ["GITHUB_WORKSPACE"], "traffic")
if not os.path.exists(workplace_path):
os.makedirs(workplace_path)
print("Workplace path: ", workplace_path)
views_path = "{}/{}".format(workplace_path, "views.csv")
clones_path = "{}/{}".format(workplace_path, "clones.csv")
plots_path = "{}/{}".format(workplace_path, "plots.png")
def main():
# Traffic stats
traffic = repo.get_views_traffic()
traffic_dict = {}
for view in traffic['views']:
traffic_dict[view.timestamp] = {"total_views": view.count, "unique_views": view.uniques}
try:
old_traffic_data = pd.read_csv(views_path, index_col="_date", parse_dates=["_date"]).to_dict(orient="index")
updated_dict = {**old_traffic_data, **traffic_dict}
traffic_frame = pd.DataFrame.from_dict(data=updated_dict, orient="index", columns=["total_views", "unique_views"])
except:
traffic_frame = pd.DataFrame.from_dict(data=traffic_dict, orient="index", columns=["total_views", "unique_views"])
traffic_frame.index.name = "_date"
traffic_frame.to_csv(views_path)
# Clones stats
clones = repo.get_clones_traffic()
clones_dict = {}
for view in clones['clones']:
clones_dict[view.timestamp] = {"total_clones": view.count, "unique_clones": view.uniques}
try:
old_clone_data = pd.read_csv(clones_path, index_col="_date", parse_dates=["_date"]).to_dict(orient="index")
updated_clones_dict = {**old_clone_data, **clones_dict}
clones_frame = pd.DataFrame.from_dict(data=updated_clones_dict, orient="index", columns=["total_clones", "unique_clones"])
except:
clones_frame = pd.DataFrame.from_dict(data=clones_dict, orient="index", columns=["total_clones", "unique_clones"])
clones_frame.index.name = "_date"
clones_frame.to_csv(clones_path)
# Plots
fig, axes = plt.subplots(nrows=2)
fig.tight_layout(h_pad=6)
# Consider letting users configure plots
# traffic_weekly = traffic_frame.resample("W", label="left").sum().tail(12)
# clones_weekly = clones_frame.resample("W", label="left").sum().tail(12)
if not traffic_frame.empty:
traffic_frame.tail(30).plot(ax=axes[0])
if not clones_frame.empty:
clones_frame.tail(30).plot(ax=axes[1])
plt.savefig(plots_path)
if __name__ == "__main__":
main()