From 19fb2a818519fdf28c123307f521f45d0fbcec6c Mon Sep 17 00:00:00 2001 From: spla Date: Wed, 12 Apr 2023 20:25:10 +0200 Subject: [PATCH] Added new graph, users and mau by weeks --- app/libraries/database.py | 20 +++++++++++++- app/libraries/graphit.py | 56 ++++++++++++++++++++++++++++++++++++++- fediverse.py | 8 ++++-- 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/app/libraries/database.py b/app/libraries/database.py index 8326574..1a50b2a 100644 --- a/app/libraries/database.py +++ b/app/libraries/database.py @@ -853,6 +853,12 @@ class Database(): global_mau = [] + global_week = [] + + global_week_users = [] + + global_week_mau = [] + conn = None try: @@ -887,6 +893,18 @@ class Database(): global_mau.append(row[3]) + cur.execute("select distinct on (date_trunc('week', datetime)) datetime::TIMESTAMP::DATE, total_users, total_mau from totals group by date_trunc('week', datetime), datetime") + + rows = cur.fetchall() + + for row in rows: + + global_week.append(row[0]) + + global_week_users.append(row[1]) + + global_week_mau.append(row[2]) + cur.close() except (Exception, psycopg2.DatabaseError) as error: @@ -899,7 +917,7 @@ class Database(): conn.close() - return (servers_plots, users_plots, mau_plots, global_day, global_servers, global_users, global_mau) + return (servers_plots, users_plots, mau_plots, global_day, global_servers, global_users, global_mau, global_week, global_week_users, global_week_mau) def get_server_data(self, server): diff --git a/app/libraries/graphit.py b/app/libraries/graphit.py index 76a70b3..9201261 100644 --- a/app/libraries/graphit.py +++ b/app/libraries/graphit.py @@ -24,7 +24,21 @@ class Graphit(): name = 'fediverse data graph library' - def __init__(self, servers=None, servers_max=None, users=None, users_max=None, mau=None, mau_max=None, global_day=None, global_servers=None, global_users=None, global_mau=None): + def __init__(self, + servers=None, + servers_max=None, + users=None, + users_max=None, + mau=None, + mau_max=None, + global_day=None, + global_servers=None, + global_users=None, + global_mau=None, + global_week=None, + global_week_users=None, + global_week_mau=None + ): self.servers = servers self.servers_max = servers_max @@ -37,6 +51,10 @@ class Graphit(): self.global_users = global_users self.global_mau = global_mau + self.global_week = global_week + self.global_week_users = global_week_users + self.global_week_mau = global_week_mau + def graph(self): # generate graphs @@ -172,3 +190,39 @@ class Graphit(): plt.savefig('app/graphs/global_mau.png') plt.close() + + def generate_weeks(self): + + df = pd.DataFrame( + {'week': np.array(self.global_week), + 'week_users': np.array(self.global_week_users), + 'week_mau': np.array(self.global_week_mau)}) + + df['week'] = pd.to_datetime(df['week']) + + # weeks + + fig, ax = plt.subplots() + + ax.plot(df.week, df.week_users, label='users', color='green') + + ax.plot(df.week, df.week_mau, label='users', color='blue') + + ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}')) + + plt.tick_params(rotation=45) + + ax.set_title("fediverse's weekly users & MAU") + + ax.set_xlabel('week') + + ax.set_ylabel('users') + + ax.grid(visible=True) + + plt.savefig('app/graphs/global_weeks.png') + + plt.close() + + + diff --git a/fediverse.py b/fediverse.py index 8be7bab..3649dda 100644 --- a/fediverse.py +++ b/fediverse.py @@ -73,10 +73,12 @@ if __name__ == '__main__': # get plots - graph.servers, graph.users, graph.mau, graph.global_day, graph.global_servers, graph.global_users, graph.global_mau = db.get_plots() + graph.servers, graph.users, graph.mau, graph.global_day, graph.global_servers, graph.global_users, graph.global_mau, graph.global_week, graph.global_week_users, graph.global_week_mau = db.get_plots() graph.generate() + graph.generate_weeks() + ############################################################################### # P O S T ! @@ -142,4 +144,6 @@ if __name__ == '__main__': mau_image_id = mastodon.media_post('app/graphs/global_mau.png', "image/png", description='mau graph').id - mastodon.status_post(post_text, in_reply_to_id=None, media_ids={servers_image_id, users_image_id, mau_image_id}) + weeks_image_id = mastodon.media_post('app/graphs/global_weeks.png', "image/png", description='weeks graph').id + + mastodon.status_post(post_text, in_reply_to_id=None, media_ids={servers_image_id, users_image_id, mau_image_id, weeks_image_id})