Skip to content

Commit

Permalink
Merge pull request #1 from SaM-92/dev_b
Browse files Browse the repository at this point in the history
Enhanced Error Handling for EirGrid API Downtime Notifications
  • Loading branch information
SaM-92 authored Feb 20, 2024
2 parents 33ab9ff + 316ed02 commit b73b2e1
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 100 deletions.
50 changes: 35 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,45 @@ async def energy_api_func(update: Update, context: CallbackContext):
# )

chat_id = update.effective_chat.id
# Initialize variables to None or a suitable default value

if selected_option_user == "Carbon intensity":
df_carbon_forecast_indexed = None
co2_stats_prior_day = None
df_carbon_intensity_recent = None

# Proceed with your existing logic here...
df_carbon_forecast_indexed = carbon_api_forecast()
co2_stats_prior_day, df_carbon_intensity_recent = carbon_api_intensity()
df_ = status_classification(df_carbon_forecast_indexed, co2_stats_prior_day)
# data analysis & adding category per hours
summary_text, df_with_trend = find_optimized_relative_periods(df_)
today_date = df_with_trend.index[0].strftime("%d/%m/%Y")
eu_summary_text = optimize_categorize_periods(df_with_trend)
quantile_summary_text, _ = find_optimized_relative_periods(
df_with_trend
) # Generate this based on your DataFrame

prompt = create_combined_gpt_prompt(
today_date, eu_summary_text, quantile_summary_text
)
gpt_recom = opt_gpt_summarise(prompt)
await update.message.reply_text(gpt_recom)
await send_co2_intensity_plot(update, context, df_with_trend)
# Check if either API call failed
if (
df_carbon_forecast_indexed is None
or co2_stats_prior_day is None
or df_carbon_intensity_recent is None
):
await update.message.reply_html(
f"Sorry, {user_first_name} 😔. We're currently unable to retrieve the necessary data due to issues with the <a href='https://www.smartgriddashboard.com'>EirGrid website</a> 🌐. Please try again later. We appreciate your understanding 🙏."
)

return # Exit the function early since we can't proceed without the data
else:
# df_carbon_forecast_indexed = carbon_api_forecast()
# co2_stats_prior_day, df_carbon_intensity_recent = carbon_api_intensity()
df_ = status_classification(df_carbon_forecast_indexed, co2_stats_prior_day)
# data analysis & adding category per hours
summary_text, df_with_trend = find_optimized_relative_periods(df_)
today_date = df_with_trend.index[0].strftime("%d/%m/%Y")
eu_summary_text = optimize_categorize_periods(df_with_trend)
quantile_summary_text, _ = find_optimized_relative_periods(
df_with_trend
) # Generate this based on your DataFrame

prompt = create_combined_gpt_prompt(
today_date, eu_summary_text, quantile_summary_text
)
gpt_recom = opt_gpt_summarise(prompt)
await update.message.reply_text(gpt_recom)
await send_co2_intensity_plot(update, context, df_with_trend)

else:
await update.message.reply_text(
Expand Down
180 changes: 95 additions & 85 deletions subs/energy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,49 @@ def create_url(start_datetime, end_datetime, region):
url = f"https://www.co2.smartgriddashboard.com/api/co2_fc/{start_str}/{end_str}/{region}"
return url

# Current date and time
now = datetime.datetime.now()
# Round down to the nearest half-hour
start_time = round_down_time(now)
# For end time, let's use 24 hours from now
end_time = now.replace(
hour=23, minute=59, second=59, microsecond=0
) # now + timedelta(days=1)
try:
# Current date and time
now = datetime.datetime.now()
# Round down to the nearest half-hour
start_time = round_down_time(now)
# For end time, let's use 24 hours from now
end_time = now.replace(
hour=23, minute=59, second=59, microsecond=0
) # now + timedelta(days=1)

# Define the region
region = ["ROI", "NI", "ALL"]
# Define the region
region = ["ROI", "NI", "ALL"]

# Create the URL
api_url = create_url(start_time, end_time, region[2])
# Create the URL
api_url = create_url(start_time, end_time, region[2])

# Make a request with SSL verification disabled due to existing issue from EirGrid
response = requests.get(api_url, verify=False)
# Make a request with SSL verification disabled due to existing issue from EirGrid
response = requests.get(api_url, verify=False)

Rs = response.json()
Rs = response.json()

df_carbon_forecast = pd.DataFrame(Rs["Rows"])
df_carbon_forecast = pd.DataFrame(Rs["Rows"])

# Convert 'EffectiveTime' to datetime and set as index
df_carbon_forecast["EffectiveTime"] = pd.to_datetime(
df_carbon_forecast["EffectiveTime"], format="%d-%b-%Y %H:%M:%S"
)
df_carbon_forecast_indexed = df_carbon_forecast.set_index("EffectiveTime")
# Convert 'EffectiveTime' to datetime and set as index
df_carbon_forecast["EffectiveTime"] = pd.to_datetime(
df_carbon_forecast["EffectiveTime"], format="%d-%b-%Y %H:%M:%S"
)
df_carbon_forecast_indexed = df_carbon_forecast.set_index("EffectiveTime")

last_value_index_co_forecast = df_carbon_forecast_indexed[
"Value"
].last_valid_index()
last_value_index_co_forecast = df_carbon_forecast_indexed[
"Value"
].last_valid_index()

# Select rows up to the row before the last NaN
df_carbon_forecast_indexed = df_carbon_forecast_indexed.loc[
:last_value_index_co_forecast
]
# Select rows up to the row before the last NaN
df_carbon_forecast_indexed = df_carbon_forecast_indexed.loc[
:last_value_index_co_forecast
]

return df_carbon_forecast_indexed
return df_carbon_forecast_indexed

except Exception as e:
# Return None or an error message to indicate failure
return None


def carbon_api_intensity():
Expand All @@ -74,68 +79,73 @@ def round_time(dt):
def format_date(dt):
return dt.strftime("%d-%b-%Y").lower() + "+" + dt.strftime("%H%%3A%M")

# Current date and time, rounded to the nearest 15 minutes
now = round_time(datetime.datetime.now())

# Start time (same time yesterday, rounded to the nearest 15 minutes)
yesterday = now - datetime.timedelta(days=1)
startDateTime = format_date(yesterday)

# End time (current time, rounded to the nearest 15 minutes)
endDateTime = format_date(now)

area = [
"CO2Stats",
"generationactual",
"co2emission",
"co2intensity",
"interconnection",
"SnspAll",
"frequency",
"demandactual",
"windactual",
]
region = ["ROI", "NI", "ALL"]
Rows = []

url = f"http://smartgriddashboard.eirgrid.com/DashboardService.svc/data?area={area[3]}&region={region[2]}&datefrom={startDateTime}&dateto={endDateTime}"
response = requests.get(url)
Rs = json.loads(response.text)["Rows"]
for row in Rs:
Rows.append(row)

df_carbon_intensity_day_before = pd.DataFrame(Rows)

# Convert 'EffectiveTime' to datetime and set as index
df_carbon_intensity_day_before["EffectiveTime"] = pd.to_datetime(
df_carbon_intensity_day_before["EffectiveTime"], format="%d-%b-%Y %H:%M:%S"
)
df_carbon_intensity_indexed = df_carbon_intensity_day_before.set_index(
"EffectiveTime"
)
try:
# Current date and time, rounded to the nearest 15 minutes
now = round_time(datetime.datetime.now())

# Start time (same time yesterday, rounded to the nearest 15 minutes)
yesterday = now - datetime.timedelta(days=1)
startDateTime = format_date(yesterday)

# End time (current time, rounded to the nearest 15 minutes)
endDateTime = format_date(now)

area = [
"CO2Stats",
"generationactual",
"co2emission",
"co2intensity",
"interconnection",
"SnspAll",
"frequency",
"demandactual",
"windactual",
]
region = ["ROI", "NI", "ALL"]
Rows = []

url = f"http://smartgriddashboard.eirgrid.com/DashboardService.svc/data?area={area[3]}&region={region[2]}&datefrom={startDateTime}&dateto={endDateTime}"
response = requests.get(url)
Rs = json.loads(response.text)["Rows"]
for row in Rs:
Rows.append(row)

df_carbon_intensity_day_before = pd.DataFrame(Rows)

# Convert 'EffectiveTime' to datetime and set as index
df_carbon_intensity_day_before["EffectiveTime"] = pd.to_datetime(
df_carbon_intensity_day_before["EffectiveTime"], format="%d-%b-%Y %H:%M:%S"
)
df_carbon_intensity_indexed = df_carbon_intensity_day_before.set_index(
"EffectiveTime"
)

last_value_index_co_intensity = df_carbon_intensity_indexed[
"Value"
].last_valid_index()

last_value_index_co_intensity = df_carbon_intensity_indexed[
"Value"
].last_valid_index()
# Select rows up to the row before the last NaN
df_carbon_intensity_recent = df_carbon_intensity_indexed.loc[
:last_value_index_co_intensity
]

# Select rows up to the row before the last NaN
df_carbon_intensity_recent = df_carbon_intensity_indexed.loc[
:last_value_index_co_intensity
]
df_carbon_intensity_recent["Value"] = df_carbon_intensity_recent[
"Value"
].interpolate()

df_carbon_intensity_recent["Value"] = df_carbon_intensity_recent[
"Value"
].interpolate()
# Calculate mean, min, and max
mean_val = df_carbon_intensity_recent["Value"].mean()
min_val = df_carbon_intensity_recent["Value"].min()
max_val = df_carbon_intensity_recent["Value"].max()

# Calculate mean, min, and max
mean_val = df_carbon_intensity_recent["Value"].mean()
min_val = df_carbon_intensity_recent["Value"].min()
max_val = df_carbon_intensity_recent["Value"].max()
# Create a dictionary with these values
co2_stats_prior_day = {"mean": mean_val, "min": min_val, "max": max_val}

# Create a dictionary with these values
co2_stats_prior_day = {"mean": mean_val, "min": min_val, "max": max_val}
return co2_stats_prior_day, df_carbon_intensity_recent

return co2_stats_prior_day, df_carbon_intensity_recent
except Exception as e:
# Return None or an error message to indicate failure
return None, None


def classify_status(value, min_val, max_val):
Expand Down

0 comments on commit b73b2e1

Please sign in to comment.