5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-16 18:26:21 +00:00

feat: refactorizar mailer para que use menos recursors

y soporte varias urls
This commit is contained in:
f 2023-02-08 18:55:05 -03:00
parent f15bcf453b
commit b091b3212a
4 changed files with 73 additions and 37 deletions

View file

@ -32,7 +32,7 @@ class DeployJob < ApplicationJob
status: deploy_locally, status: deploy_locally,
seconds: deploy_local.build_stats.last.seconds, seconds: deploy_local.build_stats.last.seconds,
size: deploy_local.size, size: deploy_local.size,
url: deploy_local.url urls: [deploy_local.url]
} }
} }
@ -74,7 +74,7 @@ class DeployJob < ApplicationJob
status: status, status: status,
seconds: build_stat.try(:seconds) || 0, seconds: build_stat.try(:seconds) || 0,
size: d.size, size: d.size,
url: d.url urls: d.respond_to?(:urls) ? d.urls : [d.url]
} }
end end
end end

View file

@ -8,21 +8,64 @@
# TODO: Agregar firma GPG y header Autocrypt # TODO: Agregar firma GPG y header Autocrypt
# TODO: Cifrar con GPG si le usuarie nos dio su llave # TODO: Cifrar con GPG si le usuarie nos dio su llave
class DeployMailer < ApplicationMailer class DeployMailer < ApplicationMailer
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::DateHelper
# rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/AbcSize
def deployed(which_ones) def deployed(deploys)
@usuarie = Usuarie.find(params[:usuarie]) usuarie = Usuarie.find(params[:usuarie])
@site = @usuarie.sites.find(params[:site]) site = usuarie.sites.find(params[:site])
@deploys = which_ones subject = t('.subject', site: site.name)
@deploy_local = @site.deploys.find_by(type: 'DeployLocal') hostname = site.hostname
@hi = t('.hi')
@explanation = t('.explanation', fqdn: hostname)
@help = t('.help')
@headers = %w[type status url seconds size].map do |header|
t(".th.#{header}")
end
@table = deploys.each_pair.map do |deploy, value|
{
title: t(".#{deploy}.title"),
status: t(".#{deploy}.#{value[:status] ? 'success' : 'error'}"),
urls: value[:urls],
seconds: {
human: distance_of_time_in_words(value[:seconds].seconds),
machine: "PT#{value[:seconds]}S"
},
size: number_to_human_size(value[:size], precision: 2)
}
end
@terminal_table = Terminal::Table.new do |t|
t << @headers
t.add_separator
@table.each do |row|
row[:urls].each do |url|
t << (row.map do |k, v|
case k
when :seconds then v[:human]
when :urls then url
else v
end
end)
end
end
end
# Informamos a cada quien en su idioma y damos una dirección de # Informamos a cada quien en su idioma y damos una dirección de
# respuesta porque a veces les usuaries nos escriben # respuesta porque a veces les usuaries nos escriben
I18n.with_locale(@usuarie.lang) do I18n.with_locale(usuarie.lang) do
mail(to: @usuarie.email, mail(to: usuarie.email, reply_to: "sutty@#{Site.domain}", subject: subject)
reply_to: "sutty@#{Site.domain}",
subject: I18n.t('deploy_mailer.deployed.subject',
site: @site.name))
end end
end end
# rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/AbcSize
private
def t(key, **args)
I18n.t("deploy_mailer.deployed#{key}", **args)
end
end end

View file

@ -1,24 +1,21 @@
%h1= t('.hi') %h1= @hi
= sanitize_markdown t('.explanation', fqdn: @deploy_local.site.hostname), = sanitize_markdown @explanation, tags: %w[p a strong em]
tags: %w[p a strong em]
%table %table
%thead %thead
%tr %tr
%th= t('.th.type') - @headers.each do |header|
%th= t('.th.status') %th= header
%th= t('.th.url')
%th= t('.th.seconds')
%th= t('.th.size')
%tbody %tbody
- @deploys.each_pair do |deploy, value| - @table.each do |row|
%tr - row[:urls].each do |url|
%td= t(".#{deploy}.title") %tr
%td= value[:status] ? t(".#{deploy}.success") : t(".#{deploy}.error") %td= row[:title]
%td= link_to value[:url], value[:url] %td= row[:status]
%td %td= link_to url, url
%time{ datetime: "PT#{value[:seconds]}S" }= distance_of_time_in_words value[:seconds].seconds %td
%td= number_to_human_size value[:size], precision: 2 %time{ datetime: row[:seconds][:machine] }= row[:seconds][:human]
%td= row[:size]
= sanitize_markdown t('.help'), tags: %w[p a strong em] = sanitize_markdown @help, tags: %w[p a strong em]

View file

@ -1,11 +1,7 @@
= '# ' + t('.hi') = "# #{@hi}"
\ \
= t('.explanation', fqdn: @deploy_local.site.hostname) = @explanation
\ \
= Terminal::Table.new do |table| = @terminal_table
- table << [t('.th.type'), t('.th.status'), t('.th.url'), t('.th.seconds'), t('.th.size')]
- table.add_separator
- @deploys.each_pair do |deploy, value|
- table << [t(".#{deploy}.title"), value[:status] ? t(".#{deploy}.success") : t(".#{deploy}.error"), value[:url], distance_of_time_in_words(value[:seconds].seconds), number_to_human_size(value[:size], precision: 2)]
\ \
= t('.help') = @help