Skip to content

Commit

Permalink
Merge pull request #1110 from linea-it/subscription-fixes-05
Browse files Browse the repository at this point in the history
Subscription fixes 05
  • Loading branch information
rcboufleur authored Nov 29, 2024
2 parents 6476b26 + d9b63b1 commit 0e8c5fe
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 90 deletions.
44 changes: 22 additions & 22 deletions backend/newsletter/events_send_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ def get_context_data(self, csv_name):
tmp_path = Path(settings.DATA_TMP_DIR).joinpath(path)
# print(tmp_path)

file_csv = os.path.join(
tmp_path, csv_name
) # + "_results_filter_newsletter.csv")
# print(file_csv, file_csv)
file_csv = os.path.join(tmp_path, csv_name)

self.log.info("file_csv %s", file_csv)

Expand Down Expand Up @@ -89,7 +86,8 @@ def exec_send_mail(self):
)

self.log.info("Attachment found: %s", attachment.filename)
data = self.get_context_data(attachment.filename)[0:10]
complete_data = self.get_context_data(attachment.filename)
number_of_events = len(complete_data)

# Validate data structure
required_keys = [
Expand All @@ -101,6 +99,19 @@ def exec_send_mail(self):
"gaia_magnitude",
"id",
]

# Limit data to the first 10 rows
data = complete_data.head(10)[required_keys]
data["date_time"] = pd.to_datetime(
data["date_time"]
).dt.strftime("%Y-%m-%d %H:%M")
# data["date_time"] = data["date_time"].apply(
# lambda dt: dt.replace("T", " ").rstrip("Z")
# )

# Convert to JSON
json_data = data.to_dict(orient="records")

if not all(key in data for key in required_keys):
self.log.error(
"Invalid data structure returned for %s",
Expand All @@ -113,30 +124,19 @@ def exec_send_mail(self):
# print(attachment.filename)

# Recorte da data
start_str = arquivo.split("_")[-5]
end_str = arquivo.split("_")[-4]
start_str = arquivo.split("_")[-4]
end_str = arquivo.split("_")[-3]
# Extract YYYY-MM-DD
date_start = (
f"{start_str[:4]}-{start_str[4:6]}-{start_str[6:8]}"
)
date_end = f"{end_str[:4]}-{end_str[4:6]}-{end_str[6:8]}"
date_time = [
dt.replace("T", " ").rstrip("Z")
for dt in data["date_time"]
]
date_start = datetime.strptime(start_str, "%Y%m%d%H%M%S")
date_end = datetime.strptime(end_str, "%Y%m%d%H%M%S")

context = [
event_filter.filter_name,
date_start,
date_end,
date_time,
data["name"],
data["velocity"],
data["closest_approach"],
data["closest_approach_uncertainty_km"],
data["gaia_magnitude"],
number_of_events,
link,
data["id"],
json_data,
]

# Send email with events
Expand Down
28 changes: 9 additions & 19 deletions backend/newsletter/newsletter_send_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,25 @@ def send_events_mail(self, subscription: Subscription, email, context):
Contem alguns eventos de predição de ocultações encontrados,
de acordo com as preferencias do usuario.
"""
# print(context[5], context[6])
data = {
"date": context[3],
"name": context[4],
"velocity": context[5],
"closest_approach": context[6],
"closest_approach_uncertainty_km": context[7],
"gaia_magnitude": context[8],
"id": context[10],
}
transformed_data = [
dict(zip(data.keys(), values)) for values in zip(*data.values())
]

html_content = render_to_string(
"results.html",
{
"host": settings.SITE_URL,
"subscriber": email,
"filter_name": context[0],
"date_start": context[1],
"date_end": context[2],
"link": context[9],
"data": transformed_data,
"date_start": context[1].strftime("%B %d, %Y, at %H:%M (UTC)"),
"date_end": context[2].strftime("%B %d, %Y, at %H:%M (UTC)"),
"number_of_events": context[3],
"link": context[4],
"data": context[5],
},
)

time_period = (
context[1].strftime("%b %d") + " to " + context[2].strftime("%b %d, %y")
)
self.send_newsletter_email(
f"Upcoming Occultation Predictions for '{context[0]}' ({context[1]} to {context[2]})",
f"Upcoming Occultation Predictions for '{context[0]}' ({time_period})",
html_content,
email,
)
Expand Down
42 changes: 21 additions & 21 deletions backend/newsletter/process_event_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,22 @@ def process_subscription_filter(self, filter_set, output_path, force_run=False):
).process_date
else:
# se ainda não existir é definido como a data e hora atual
latest_processing_date = now
latest_processing_date = None

self.log.info("Latest processing date: %s", latest_processing_date)

# definir as datas inicial e final para o processamento a partir da ultima data de processamento
# caso mensal processa até 7 dias antes do final do mes
if filter_set["frequency"] == 1:
date_start = (latest_processing_date + relativedelta(months=1)).replace(
date_start = (now + relativedelta(months=1)).replace(
day=1, hour=0, minute=0, second=0, microsecond=0
)
allow_process = (
True if (date_start - latest_processing_date).days <= 7 else False
)
allow_process = True if (date_start - now).days <= 7 else False
already_processed = (
True if (now - latest_processing_date).days < 7 else False
latest_processing_date is not None
and (date_start - latest_processing_date).days < 7
)

if force_run:
date_end = (
date_start + relativedelta(months=1) - relativedelta(microseconds=1)
Expand All @@ -282,14 +282,15 @@ def process_subscription_filter(self, filter_set, output_path, force_run=False):
# caso semanal, processa até 3 dias antes do final da semana
# sempre considera o proximo domingo como inicio da semana
if filter_set["frequency"] == 2:
date_start = (
latest_processing_date + relativedelta(weekday=SU(+1))
).replace(hour=0, minute=0, second=0, microsecond=0)
date_start = (now + relativedelta(weekday=SU(+1))).replace(
hour=0, minute=0, second=0, microsecond=0
)
allow_process = (
True if (date_start - latest_processing_date).days <= 3 else False
True if (date_start - now).days <= 3 else False
) # verifica se deve ser processado
already_processed = (
True if (now - latest_processing_date).days < 3 else False
latest_processing_date is not None
and (date_start - latest_processing_date).days < 3
)
if force_run:
date_end = (
Expand All @@ -309,14 +310,15 @@ def process_subscription_filter(self, filter_set, output_path, force_run=False):

# caso diario, processa no intervalo de 24 horas
if filter_set["frequency"] == 3:
date_start = (latest_processing_date + relativedelta(days=1)).replace(
date_start = (now + relativedelta(days=1)).replace(
hour=0, minute=0, second=0, microsecond=0
)
allow_process = (
True if (date_start - latest_processing_date).seconds < 86400 else False
)

allow_process = True if (date_start - now).seconds < 86400 else False

already_processed = (
True if (now - latest_processing_date).seconds < 43200 else False
latest_processing_date is not None
and (date_start - latest_processing_date).seconds < 86400
)
if force_run:
date_end = (
Expand Down Expand Up @@ -352,7 +354,7 @@ def process_subscription_filter(self, filter_set, output_path, force_run=False):
return None
else:
filename = (
"_".join(filter_set["filter_name"].strip().lower().split())
str(filter_set["id"])
+ "_"
+ str(record.id)
+ "_"
Expand Down Expand Up @@ -427,9 +429,7 @@ def create_csv(self, filter_results, tmp_path, filename, submission_id):
inplace=True,
)

csv_file = os.path.join(
tmp_path, filename + "_results_filter_newsletter.csv"
)
csv_file = os.path.join(tmp_path, filename + "_subscription_results.csv")
df.to_csv(csv_file, sep=";", header=True, index=False)
self.log.info("An archive was created with the Results.")

Expand All @@ -445,7 +445,7 @@ def create_csv(self, filter_results, tmp_path, filename, submission_id):

record = Attachment(
submission_id=Submission.objects.get(pk=submission_id),
filename=filename + "_results_filter_newsletter.csv",
filename=filename + "_subscription_results.csv",
size=size,
)
self.log.info("Updating status of stored file...")
Expand Down
8 changes: 8 additions & 0 deletions backend/newsletter/templates/base.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
letter-spacing: 8px; /* Adjust the value as needed */
}
.clickable-row {
transition: background-color 0.5s; /* Smooth color transition */
}
.clickable-row:hover {
background-color: #c0d6ef;
}
</style>
</head>

Expand Down
8 changes: 8 additions & 0 deletions backend/newsletter/templates/email_base.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@
text-align: center;
margin: 20px 0;
}
.clickable-row {
transition: background-color 0.5s; /* Smooth color transition */
}
.clickable-row:hover {
background-color: #c0d6ef;
}
</style>
</head>

Expand Down
55 changes: 30 additions & 25 deletions backend/newsletter/templates/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,55 @@
<td style="padding: 20px;">
<p style="font-size: 14px; color: #5a5c5f; line-height: 1.6;">
Dear {{ subscriber }},<br><br>
We are pleased to provide your customized occultation predictions for the period spanning
<b>{{ date_start }}</b> to <b>{{ date_end }}</b> based on your filter criteria <b>{{ filter_name }}</b>.
Below is a list with partial results. A CSV file is available with detailed information about all events.

We are pleased to provide your customized occultation predictions based on your specified filter criteria,
<b>{{filter_name}}</b>, for the period from <b>{{date_start}}</b>, to <b>{{date_end}}</b>. The results
include a total of <b>{{number_of_events}}</b> prediction events. Below, you will find a preview
of the results, and a detailed CSV file containing all event data is available for your convenience.
</p>
</td>
</tr>
<tr>
<td>
<!-- Centered and Rounded Table -->
<div
style="margin: 0 auto; max-width: 90%; background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);">
<table width="100%" style="border-collapse: collapse; margin: 20px 0;">
<thead style="background-color: #2a5ca1; color: #fff;">
style="margin: 0 auto; max-width: 90%; background-color: #ffffff; border-radius: 10px; overflow: hidden; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);">
<table width="100%" style="border-collapse: collapse; margin: 0px 0;">
<thead style="background-color: #0076BC; color: #fff;">
<tr>
<th style="padding: 8px; font-size: 12px; text-align: center;">Date & Time (UTC)</th>
<th style="padding: 8px; font-size: 12px; text-align: center;">Object Identifier</th>
<th style="padding: 8px; font-size: 12px; text-align: center;">Velocity (km/s)</th>
<th style="padding: 8px; font-size: 12px; text-align: center;">Closest Approach (arcsec)</th>
<th style="padding: 8px; font-size: 12px; text-align: center;">Uncertainty (km)</th>
<th style="padding: 8px; font-size: 12px; text-align: center;">Gaia Magnitude</th>
<th style="padding: 5px; font-size: 12px; text-align: center;">Date Time (UTC)</th>
<th style="padding: 5px; font-size: 12px; text-align: center;">Object Identifier</th>
<th style="padding: 5px; font-size: 12px; text-align: center;">Velocity (km/s)</th>
<th style="padding: 5px; font-size: 12px; text-align: center;">Closest Approach (arcsec)</th>
<th style="padding: 5px; font-size: 12px; text-align: center;">Uncertainty (km)</th>
<th style="padding: 5px; font-size: 12px; text-align: center;">Gaia Magnitude</th>
<th style="padding: 5px; font-size: 12px; text-align: center;"></th>
</tr>
</thead>
<tbody style="background-color: #ffffff; color: #0c0c0c;">
{% for row in data %}
<tr>
<td style="padding: 8px; font-size: 12px; text-align: center;">
{{ row.date|default:"-" }}
<tr class="clickable-row">
<td style="padding: 10px; font-size: 12px; text-align: center;">
{{ row.date_time|default:"-" }}
</td>
<td style="padding: 8px; font-size: 12px; text-align: center;">
<a href="{{ host }}/prediction-event-detail/{{ row.id }}" style="color: #0076BC;">
{{row.name|default:"-" }}</a>
<td style="padding: 6px; font-size: 12px; text-align: center;">
{{row.name|default:"-" }}
</td>
<td style="padding: 8px; font-size: 12px; text-align: center;">
<td style="padding: 6px; font-size: 12px; text-align: center;">
{% if row.velocity %}
{{ row.velocity|floatformat:3 }}
{{ row.velocity|floatformat:1 }}
{% else %}
-
{% endif %}
</td>
<td style="padding: 8px; font-size: 12px; text-align: center;">
<td style="padding: 6px; font-size: 12px; text-align: center;">
{% if row.closest_approach %}
{{ row.closest_approach|floatformat:3 }}
{% else %}
-
{% endif %}
</td>
<td style="padding: 8px; font-size: 12px; text-align: center;">
<td style="padding: 6px; font-size: 12px; text-align: center;">
{% if row.closest_approach_uncertainty_km %}
{% if row.closest_approach_uncertainty_km > 12500 %}
Very High
Expand All @@ -62,20 +64,23 @@
-
{% endif %}
</td>
<td style="padding: 8px; font-size: 12px; text-align: center;">
<td style="padding: 6px; font-size: 12px; text-align: center;">
{% if row.gaia_magnitude %}
{{ row.gaia_magnitude|floatformat:3 }}
{{ row.gaia_magnitude|floatformat:1 }}
{% else %}
-
{% endif %}
</td>
<td style="padding: 8px; font-size: 11px; text-align: center;">
<a href='{{ host }}/prediction-event-detail/{{ row.id }}'>[URL]</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- Download Link -->
<div style="text-align: center; margin: 20px 0;">
<div style="text-align: center; margin: 30px 0;">
<a href="{{ link }}" style="
display: inline-block;
background-color: #0076BC;
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion backend/newsletter/templates/unsubscribe_banner.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<hr style="margin: 10px 0; border: 0; border-top: 1px solid #dfe0e1;">
<p style="font-size: 12px; color: #5a5c5f; text-align: center; margin: 5px 0;">
If you wish to unsubscribe or manage your subscription preferences,
<a href={{host}}/login/ style="color:#fb101095;" target="_blank">click here</a>.
<a href={{host}}/newsletter_settings/ style="color:#fb101095;" target="_blank">click here</a>.
</p>
<hr style="margin: 10px 0; border: 0; border-top: 1px solid #dfe0e1;">
</td>
Expand Down
8 changes: 6 additions & 2 deletions compose/testing/docker-compose-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ services:
- ${TNO_PORT:-8060}:8080
volumes:
- ${TNO_PROJ_DIR:-.}/nginx_proxy.conf:/etc/nginx/conf.d/default.conf
- ${TNO_PROJ_DIR:-.}/data/public:/var/www/data
#- ${TNO_PROJ_DIR:-.}/data/public:/var/www/data
- ${TNO_PROJ_DIR:-.}/data:/var/www/data
- ${TNO_PROJ_DIR:-.}/data/maps:/var/www/data/maps
- ${TNO_PROJ_DIR:-.}/data/tmp:/var/www/data/tmp
# Diretório de mapas compartilhado com ambiente de produção.
#- /lustre/t1/apps/app.tno/tno_prod/data/maps:/var/www/data/maps
# - ${TNO_PROJ_DIR:-.}/logs/:/var/log/nginx/
# - ${TNO_PROJ_DIR:-.}/data/database_subset:/var/www/data/database_subset
- /lustre/t1/apps/app.tno/data_sample:/var/www/data/database_subset
depends_on:
- backend
- rabbit
Expand Down

0 comments on commit 0e8c5fe

Please sign in to comment.