diff options
| author | Pinapelz <yukais@pinapelz.com> | 2023-11-22 22:15:05 -0800 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2023-11-22 22:15:05 -0800 |
| commit | 25d0eb723bf40e16d4c8b26804f009385188f594 (patch) | |
| tree | 0f4dcc5b22ad186eccae68ed23663fe1775248d0 | |
| parent | 77a0b69d9a0dd755a0a59a4c1dc3f3d045327e89 (diff) | |
handle overflow on proejection calculations
- In case for new debuts where there is not enough data
| -rw-r--r-- | backend/app.py | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/backend/app.py b/backend/app.py index 7f38585..7e8deae 100644 --- a/backend/app.py +++ b/backend/app.py @@ -89,17 +89,22 @@ def get_channel_information(channel_name): df = pandas.DataFrame(data=data) df['dates'] = pandas.to_datetime(df['dates']) df.set_index('dates', inplace=True) - model = LinearRegression() - X = np.array(range(len(df))).reshape(-1, 1) - y = df['subscribers'] - model.fit(X, y) - next_milestone = find_next_milestone(current_subscriber_count) - days_until_next_milestone = (next_milestone - model.intercept_) / model.coef_ - next_milestone_date = (df.index[0] + pandas.Timedelta(days=int(days_until_next_milestone))).date() - time_until_next_milestone = (next_milestone_date - datetime.datetime.now().date()).days - channel_data["next_milestone_date"] = str(next_milestone_date) - channel_data["days_until_next_milestone"] = str(time_until_next_milestone) - channel_data["next_milestone"] = str(next_milestone) + try: + model = LinearRegression() + X = np.array(range(len(df))).reshape(-1, 1) + y = df['subscribers'] + model.fit(X, y) + next_milestone = find_next_milestone(current_subscriber_count) + days_until_next_milestone = (next_milestone - model.intercept_) / model.coef_ + next_milestone_date = (df.index[0] + pandas.Timedelta(days=int(days_until_next_milestone))).date() + time_until_next_milestone = (next_milestone_date - datetime.datetime.now().date()).days + channel_data["next_milestone_date"] = str(next_milestone_date) + channel_data["days_until_next_milestone"] = str(time_until_next_milestone) + channel_data["next_milestone"] = str(next_milestone) + except OverflowError: + channel_data["next_milestone_date"] = "N/A" + channel_data["days_until_next_milestone"] = "N/A" + channel_data["next_milestone"] = "N/A" return jsonify(channel_data) @app.errorhandler(404) |
