Skip to content

Commit aad8195

Browse files
committed
format md table, add portal to top pages plot
1 parent 472c2df commit aad8195

File tree

6 files changed

+64
-74
lines changed

6 files changed

+64
-74
lines changed

.github/workflows/get-metrics.py

+36-39
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44

55
import cartopy
6-
import google
76
import matplotlib.cm as cm
87
import matplotlib.colors as colors
98
import matplotlib.pyplot as plt
@@ -32,28 +31,20 @@
3231
'universe_domain': 'googleapis.com',
3332
}
3433

35-
# with open('cisl-vast-pythia-606cf5ac2303.json') as json_file:
36-
# credentials_dict = json.load(json_file)
37-
38-
try:
39-
client = BetaAnalyticsDataClient.from_service_account_info(credentials_dict)
40-
except google.auth.exceptions.MalformedError as e:
41-
print('Malformed Error:', e)
42-
print(len(PRIVATE_KEY))
34+
client = BetaAnalyticsDataClient.from_service_account_info(credentials_dict)
4335

4436

4537
def _format_rounding(value):
4638
return f'{round(value / 1000, 1):.1f}K'
4739

4840

49-
def run_total_users_report(property_id):
41+
def _run_total_users_report(property_id):
5042
request = RunReportRequest(
5143
property=f'properties/{property_id}',
5244
dimensions=[],
5345
metrics=[Metric(name='activeUsers')],
5446
date_ranges=[DateRange(start_date='2020-03-31', end_date='today')],
5547
)
56-
5748
response = client.run_report(request)
5849

5950
total_users = 0
@@ -63,14 +54,22 @@ def run_total_users_report(property_id):
6354
return _format_rounding(total_users)
6455

6556

57+
def get_total_users(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID):
58+
metrics_dict = {}
59+
metrics_dict['Portal'] = _run_total_users_report(PORTAL_ID)
60+
metrics_dict['Foundations'] = _run_total_users_report(FOUNDATIONS_ID)
61+
metrics_dict['Cookbooks'] = _run_total_users_report(COOKBOOKS_ID)
62+
with open('portal/metrics/user_metrics.json', 'w') as outfile:
63+
json.dump(metrics_dict, outfile)
64+
65+
6666
def _run_top_pages_report(property_id):
6767
request = RunReportRequest(
6868
property=f'properties/{property_id}',
6969
dimensions=[Dimension(name='pageTitle')],
7070
date_ranges=[DateRange(start_date='2020-03-31', end_date='today')],
7171
metrics=[Metric(name='screenPageViews')],
7272
)
73-
7473
response = client.run_report(request)
7574

7675
page_views = {}
@@ -80,12 +79,18 @@ def _run_top_pages_report(property_id):
8079
page_views[page] = views
8180

8281
top_10_pages = sorted(page_views.items(), key=lambda item: item[1], reverse=True)[:10]
83-
top_10_pages_dict = {page: views for page, views in top_10_pages}
82+
return {page: views for page, views in top_10_pages}
8483

85-
return top_10_pages_dict
8684

85+
def plot_top_pages(portal_id, foundations_id, cookbooks_id):
86+
portal_page_views = _run_top_pages_report(portal_id)
87+
portal_pages = []
88+
portal_sorted = {k: v for k, v in sorted(portal_page_views.items(), key=lambda item: item[1])}
89+
portal_views = portal_sorted.values()
90+
for key in portal_sorted:
91+
newkey = key.split('—')[0]
92+
portal_pages.append(newkey)
8793

88-
def plot_top_pages(foundations_id, cookbooks_id):
8994
foundations_page_views = _run_top_pages_report(foundations_id)
9095
foundations_pages = []
9196
foundations_sorted = {k: v for k, v in sorted(foundations_page_views.items(), key=lambda item: item[1])}
@@ -102,28 +107,32 @@ def plot_top_pages(foundations_id, cookbooks_id):
102107
newkey = key.split('—')[0]
103108
cookbooks_pages.insert(0, newkey)
104109

105-
pages = cookbooks_pages + foundations_pages
110+
pages = cookbooks_pages + foundations_pages + portal_pages
106111

107-
fig, ax = plt.subplots()
108-
plt.title('Most Popular Pages')
112+
fig, ax = plt.subplots(figsize=(10, 8))
113+
plt.title('All-Time Top Pages')
109114

110-
views_max = int(math.ceil(max(foundations_views) / 10000.0)) * 10000
115+
views_max = int(math.ceil(max(portal_views) / 10000.0)) * 10000
111116
ax.set_xlim([0, views_max])
112117

113118
y = np.arange(10)
114119
y2 = np.arange(11, 21)
115-
y3 = np.append(y, y2)
120+
y3 = np.arange(22, 32)
121+
y4 = np.append(y, y2)
122+
y4 = np.append(y4, y3)
116123

117-
bar1 = ax.barh(y2, foundations_views, align='center', label='Foundations', color='royalblue')
118-
bar2 = ax.barh(y, cookbooks_views, align='center', label='Cookbooks', color='indianred')
124+
bar1 = ax.barh(y3, portal_views, align='center', label='Portal', color='purple')
125+
bar2 = ax.barh(y2, foundations_views, align='center', label='Foundations', color='royalblue')
126+
bar3 = ax.barh(y, cookbooks_views, align='center', label='Cookbooks', color='indianred')
119127

120-
ax.set_yticks(y3, labels=pages)
128+
ax.set_yticks(y4, labels=pages)
121129

122130
ax.bar_label(bar1, fmt=_format_rounding)
123131
ax.bar_label(bar2, fmt=_format_rounding)
132+
ax.bar_label(bar3, fmt=_format_rounding)
124133

125134
plt.legend()
126-
plt.savefig('portal/metrics/bypage.png', bbox_inches='tight')
135+
plt.savefig('portal/metrics/toppages.png', bbox_inches='tight')
127136

128137

129138
def _run_usersXcountry_report(foundations_id):
@@ -133,7 +142,6 @@ def _run_usersXcountry_report(foundations_id):
133142
metrics=[Metric(name='activeUsers')],
134143
date_ranges=[DateRange(start_date='2020-03-31', end_date='today')],
135144
)
136-
137145
response = client.run_report(request)
138146

139147
user_by_country = {}
@@ -205,18 +213,7 @@ def plot_usersXcountry(foundations_id):
205213
plt.savefig('portal/metrics/bycountry.png', bbox_inches='tight')
206214

207215

208-
def get_metrics():
209-
metrics_dict = {}
210-
metrics_dict['Portal'] = run_total_users_report(str(PORTAL_ID))
211-
metrics_dict['Foundations'] = run_total_users_report(str(FOUNDATIONS_ID))
212-
metrics_dict['Cookbooks'] = run_total_users_report(str(COOKBOOKS_ID))
213-
with open('portal/metrics/user_metrics.json', 'w') as outfile:
214-
json.dump(metrics_dict, outfile)
215-
216-
plot_top_pages(str(FOUNDATIONS_ID), str(COOKBOOKS_ID))
217-
218-
plot_usersXcountry(str(FOUNDATIONS_ID))
219-
220-
221216
if __name__ == '__main__':
222-
get_metrics()
217+
get_total_users(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID)
218+
plot_top_pages(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID)
219+
plot_usersXcountry(str(FOUNDATIONS_ID))

.github/workflows/write-metrics-md.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,32 @@
22
import json
33

44

5-
def process_user_data(user_data_file, bar_plot_file, map_plot_file, markdown_file):
6-
"""
7-
Reads user data from a JSON file and writes it to a markdown file.
8-
9-
Args:
10-
user_data_file: Path to the JSON file containing user data.
11-
markdown_file: Path to the output markdown file.
12-
"""
13-
now = datetime.datetime.now()
14-
15-
with open(user_data_file, 'r') as f:
5+
def process_user_data(json_file, top_pages, map, md_file):
6+
with open(json_file, 'r') as f:
167
user_data = json.load(f)
178

18-
# Write processed data to markdown file
19-
with open(markdown_file, 'w') as f:
9+
with open(md_file, 'w') as f:
2010
f.write('# Metrics \n\n')
21-
f.write(f'Last Updated: {now}\n\n')
22-
f.write('Total Users:\n\n')
23-
for key in user_data:
24-
f.write(f'{key}: {(user_data[key])}\n')
25-
f.write('\n')
26-
f.write(f'![Top Pages]({bar_plot_file})')
11+
f.write(f'Last Updated: {datetime.datetime.now()}\n\n')
12+
13+
headers = '| Project | Views |'
14+
separator = '| ' + ' | '.join(['-----'] * 2) + ' |'
15+
rows = []
16+
for key in user_data.keys():
17+
rows.append('| ' + key + ' | ' + user_data[key] + ' |')
18+
table = '\n'.join([headers, separator] + rows)
19+
f.write(table)
2720
f.write('\n\n')
28-
f.write(f'![Users by Country]({map_plot_file})')
21+
22+
f.write(f'![Top Pages]({top_pages})')
23+
f.write(f'![Users by Country]({map})')
2924
f.write('\n')
3025
f.close()
3126

3227

3328
if __name__ == '__main__':
34-
user_data_file = 'portal/metrics/user_metrics.json'
35-
bar_plot_file = 'metrics/bypage.png'
36-
map_plot_file = 'metrics/bycountry.png'
37-
markdown_file = 'portal/metrics.md'
38-
process_user_data(user_data_file, bar_plot_file, map_plot_file, markdown_file)
29+
json_file = 'portal/metrics/user_metrics.json'
30+
top_pages = 'metrics/toppages.png'
31+
map = 'metrics/bycountry.png'
32+
md_file = 'portal/metrics.md'
33+
process_user_data(json_file, top_pages, map, md_file)

portal/metrics.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Metrics
22

3-
Last Updated: 2024-03-18 22:35:09.405196
3+
Last Updated: 2024-03-19 09:15:00.008673
44

5-
Total Users:
5+
| Project | Views |
6+
| ----- | ----- |
7+
| Portal | 12.3K |
8+
| Foundations | 47.0K |
9+
| Cookbooks | 6.2K |
610

7-
Portal: 46.9K
8-
Foundations: 46.9K
9-
Cookbooks: 6.2K
10-
11-
![Top Pages](metrics/bypage.png)
12-
13-
![Users by Country](metrics/bycountry.png)
11+
![Top Pages](metrics/toppages.png)![Users by Country](metrics/bycountry.png)

portal/metrics/bycountry.png

-2 Bytes
Loading

portal/metrics/toppages.png

112 KB
Loading

portal/metrics/user_metrics.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Portal": "46.9K", "Foundations": "46.9K", "Cookbooks": "6.2K"}
1+
{"Portal": "12.3K", "Foundations": "47.0K", "Cookbooks": "6.2K"}

0 commit comments

Comments
 (0)