Commit d19807cb authored by Peter van der meulen's avatar Peter van der meulen

Clean up reporting to make space for more reports 2/2

parent 9f38d87e
Pipeline #148 passed with stage
in 1 minute and 38 seconds
......@@ -56,8 +56,107 @@ defmodule AuroraBot.Report.Live do
"""
def run! do
report = create!()
Store.to_files(report)
Slack.run!(report)
Store.live(report)
send_slack_message(report)
:ok
end
defp send_slack_message(report) do
lowest_cloud_coverage = report.clouds |> Map.values() |> Enum.min()
in_time = Timex.compare(Timex.now(:utc) |> DateTime.to_time(), ~T[18:00:00.000000]) >= 0
if report.kp >= 5 && lowest_cloud_coverage < 50 && in_time do
send_message_or_ignore_report(report)
else
:ok
end
end
defp send_message_or_ignore_report(report) do
report_kp = report.kp
case Cachex.get!(:base, "slack-report") do
nil -> send_message(report)
x when x > report_kp -> send_message(report)
_ -> :ok
end
end
defp send_message(report) do
true = Cachex.put!(:base, "slack-report", report.kp, ttl: :timer.hours(3))
Slack.send!(
"LIVE. \n New live data from IRF in Kiruna suggests that northern lights" <>
" can be seen in Sweden.\n The calculated KP is #{report.kp}.",
[
message_attachments_probability(report.chances),
message_attachments_weather(report.clouds)
]
)
end
defp message_attachments_weather(clouds) do
%{
"title" => "Weather",
"pretext" => "This is how the clouds look like at these locations.",
"title_link" => "https://aurora.picodevelopment.nl",
"color" => "good",
fields: [
%{
title: "Härnösand",
value: "#{clouds.harnosand}% cloud coverage",
short: true
},
%{
title: "Östersund",
value: "#{clouds.ostersund}% cloud coverage",
short: true
},
%{
title: "Sundsvall",
value: "#{clouds.sundsvall}% cloud coverage",
short: true
},
%{
title: "Umeå",
value: "#{clouds.umea}% cloud coverage",
short: true
}
]
}
end
defp message_attachments_probability(chances) do
%{
"title" => "Chances",
"pretext" =>
"Below a small overview of chance to see northern lights in " <>
"different monitored locations. " <>
"The precision of these chances will imporove over time.",
"title_link" => "https://aurora.picodevelopment.nl",
"color" => "good",
fields: [
%{
title: "Härnösand",
value: "#{chances.harnosand}% chance",
short: true
},
%{
title: "Östersund",
value: "#{chances.ostersund}% chance",
short: true
},
%{
title: "Sundsvall",
value: "#{chances.sundsvall}% chance",
short: true
},
%{
title: "Umeå",
value: "#{chances.umea}% chance",
short: true
}
]
}
end
end
defmodule AuroraBot.Report.Slack do
# https://bitbucket.org/mittmedia/newsletter-service-legacy/src/master/app/models/slack_bot.rb
def run!(report) do
lowest_cloud_coverage = report.clouds |> Map.values() |> Enum.min()
{_, {current_hour, _, _}} = Timex.now(:utc) |> Timex.to_erl()
if report.kp > 5 && lowest_cloud_coverage < 50 && current_hour in 20..23 do
send_or_ignore_report(report)
else
:ok
end
end
def send_or_ignore_report(report) do
report_kp = report.kp
case Cachex.get!(:base, "slack-report") do
nil ->
start_run(report)
x when x > report_kp ->
start_run(report)
_ ->
:ok
end
end
def start_run(report) do
true = Cachex.put!(:base, "slack-report", report.kp, ttl: :timer.hours(3))
report |> message_from_report() |> send_message()
end
defp message_from_report(report) do
%{
def send!(text, attachments) do
send_message!(%{
"token" => Application.get_env(:aurora_bot, :slack)[:token],
"channel" => Application.get_env(:aurora_bot, :slack)[:channel],
"text" =>
"LIVE. \n New live data from IRF in Kiruna suggests that northern lights can be seen in Sweden.\n The calculated KP is #{
report.kp
}.",
"text" => text,
"as_user" => true,
"attachments" => Jason.encode!(report_attachments(report))
}
end
defp report_attachments(report) do
[report_attachments_probability(report.chances), report_attachments_weather(report.clouds)]
end
defp report_attachments_weather(clouds) do
%{
"title" => "Weather",
"pretext" => "This is how the clouds look like at these locations.",
"title_link" => "https://aurora.picodevelopment.nl",
"color" => "good",
fields: [
%{
title: "Härnösand",
value: "#{clouds.harnosand}% cloud coverage",
short: true
},
%{
title: "Östersund",
value: "#{clouds.ostersund}% cloud coverage",
short: true
},
%{
title: "Sundsvall",
value: "#{clouds.sundsvall}% cloud coverage",
short: true
},
%{
title: "Umeå",
value: "#{clouds.umea}% cloud coverage",
short: true
}
]
}
end
defp report_attachments_probability(chances) do
%{
"title" => "Chances",
"pretext" =>
"Below a small overview of chance to see northern lights in " <>
"different monitored locations. " <>
"The precision of these chances will imporove over time.",
"title_link" => "https://aurora.picodevelopment.nl",
"color" => "good",
fields: [
%{
title: "Härnösand",
value: "#{chances.harnosand}% chance",
short: true
},
%{
title: "Östersund",
value: "#{chances.ostersund}% chance",
short: true
},
%{
title: "Sundsvall",
value: "#{chances.sundsvall}% chance",
short: true
},
%{
title: "Umeå",
value: "#{chances.umea}% chance",
short: true
}
]
}
"attachments" => Jason.encode!(attachments)
})
end
defp send_message(message) do
defp send_message!(message) do
case HTTPoison.get!(
"https://slack.com/api/chat.postMessage",
[],
......
defmodule AuroraBot.Report.Store do
def to_files(report) do
def live(report) do
json_report = Jason.encode!(report, pretty: true)
File.write!("public/aurora_data.json", json_report)
File.write!("public/kp.txt", Integer.to_string(report.kp))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment