diff options
| author | Pinapelz <yukais@pinapelz.com> | 2024-10-09 15:37:10 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2024-10-09 15:40:24 -0700 |
| commit | cd81899aa4aff98f1b98bdaa6031115a7da6366a (patch) | |
| tree | bb5eeeea3294882ac12250dc38498378a8643904 | |
| parent | 8799ac2deb38d6bf654b54aaae3c8780de5d5a83 (diff) | |
add route to get 1 day, 7 day, and 30 day difference
| -rw-r--r-- | app.py | 22 |
1 files changed, 19 insertions, 3 deletions
@@ -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/<channel_name>/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/<channel_name>") 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) |
