From cd81899aa4aff98f1b98bdaa6031115a7da6366a Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Wed, 9 Oct 2024 15:37:10 -0700 Subject: add route to get 1 day, 7 day, and 30 day difference --- app.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 924c1b3..1b249e1 100644 --- a/app.py +++ b/app.py @@ -83,8 +83,8 @@ def get_channel_milestones(channel_name): initial_milestone = 10000 current_milestone = initial_milestone query = """ - SELECT subscriber_count, MIN(timestamp) - FROM subscriber_data_historical + SELECT subscriber_count, MIN(timestamp) + FROM subscriber_data_historical WHERE name = %s GROUP BY subscriber_count ORDER BY subscriber_count ASC @@ -101,6 +101,22 @@ def get_channel_milestones(channel_name): current_milestone += milestone_increment return jsonify({"milestones": milestones, "dates": dates}) +@app.route("/api/subscribers//past_diff") +def get_past_records(channel_name): + server = create_database_connection() + query = "SELECT * FROM subscriber_data_historical WHERE name = %s ORDER BY timestamp DESC" + data = server.execute_query(query, (channel_name,)) + if len(data) == 0: + return jsonify({"diff_1d": None, "diff_7d": None, "diff_30d": None}) + latest_sub_count = data[0][4] + sub_count_1d_ago = next((row[4] for row in data if (datetime.datetime.now() - row[5]).days >= 1), None) + sub_count_7d_ago = next((row[4] for row in data if (datetime.datetime.now() - row[5]).days >= 7), None) + sub_count_30d_ago = next((row[4] for row in data if (datetime.datetime.now() - row[5]).days >= 30), None) + diff_1d = latest_sub_count - sub_count_1d_ago if sub_count_1d_ago is not None else None + diff_7d = latest_sub_count - sub_count_7d_ago if sub_count_7d_ago is not None else None + diff_30d = latest_sub_count - sub_count_30d_ago if sub_count_30d_ago is not None else None + return jsonify({"diff_1d": diff_1d, "diff_7d": diff_7d, "diff_30d": diff_30d}) + @app.route("/api/channel/") def get_channel_information(channel_name): @@ -165,4 +181,4 @@ def not_found(error): return jsonify(error=str(error)), 404 if __name__ == "__main__": - app.run(debug=True) \ No newline at end of file + app.run(debug=True) -- cgit v1.2.3