summaryrefslogtreecommitdiff
path: root/lib/silmataivas/weather_poller.ex
blob: b42b1847333c9645120ee6c1f2cb394bf18c995c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
defmodule Silmataivas.WeatherPoller do
  require Logger
  alias Silmataivas.{Users, Notifications.NtfyNotifier}

  @api_url "https://api.openweathermap.org/data/2.5/forecast"
  # Check forecasts within the next 24 hours
  @alert_window_hours 24

  def check_all do
    Logger.info("🔄 Checking weather forecast for all users...")

    Users.list_users_with_locations()
    |> Enum.each(&check_user_weather/1)
  end

  def check_user_weather(%{user_id: user_id, location: %{latitude: lat, longitude: lon}} = _user) do
    case fetch_forecast(lat, lon) do
      {:ok, forecasts} ->
        case find_first_alert_entry(forecasts) do
          nil -> :ok
          entry -> NtfyNotifier.send_alert(user_id, entry)
        end

      {:error, reason} ->
        Logger.error("❌ Error fetching forecast for user #{user_id}: #{inspect(reason)}")
    end
  end

  # Add this clause to handle users with missing location data
  def check_user_weather(%{user_id: user_id} = user) do
    Logger.warning(
      "⚠️ User #{user_id} has missing or incomplete location data: #{inspect(user)}",
      []
    )

    :ok
  end

  # Add a catch-all clause to handle unexpected data formats
  def check_user_weather(invalid_user) do
    Logger.error("❌ Invalid user data structure: #{inspect(invalid_user)}")
    :ok
  end

  defp fetch_forecast(lat, lon) do
    api_key = Application.fetch_env!(:silmataivas, :openweathermap_api_key)

    Req.get(
      url: @api_url,
      params: [
        lat: lat,
        lon: lon,
        units: "metric",
        appid: api_key
      ]
    )
    |> case do
      {:ok, %{status: 200, body: %{"list" => forecast_list}}} ->
        {:ok, forecast_list}

      {:ok, %{status: code, body: body}} ->
        {:error, {code, body}}

      error ->
        error
    end
  end

  defp dangerous_conditions?(
         %{
           "main" => %{"temp" => temp},
           "wind" => %{"speed" => speed},
           "dt_txt" => time_str
         } = entry
       ) do
    rain_mm = get_in(entry, ["rain", "3h"]) || 0.0
    wind_kmh = speed * 3.6

    cond do
      wind_kmh > 80 -> log_reason("Wind", wind_kmh, time_str)
      rain_mm > 40 -> log_reason("Rain", rain_mm, time_str)
      temp < 0 -> log_reason("Temperature", temp, time_str)
      true -> false
    end
  end

  defp find_first_alert_entry(forecast_list) do
    now = DateTime.utc_now()

    forecast_list
    |> Enum.take_while(fn %{"dt" => ts} ->
      forecast_time = DateTime.from_unix!(ts)
      DateTime.diff(forecast_time, now, :hour) <= @alert_window_hours
    end)
    |> Enum.find(&dangerous_conditions?/1)
  end

  defp log_reason(type, value, time_str) do
    Logger.info("🚨 #{type} threshold exceeded: #{value} at #{time_str}")
    true
  end
end