From fde7956b658e615cc10d83f7cf2f3994f7d6f383 Mon Sep 17 00:00:00 2001 From: Lucas Tavano Date: Fri, 6 Nov 2026 11:08:17 -0300 Subject: [PATCH 1/3] Show installed commit and update status in settings Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + assets/css/app.css | 11 ++ config/runtime.exs | 3 + lib/local_web_monitorex/update_status.ex | 162 ++++++++++++++++++ .../components/layouts.ex | 11 ++ .../components/layouts/root.html.heex | 1 + .../components/update_status_panel.ex | 45 +++++ .../live/dashboard_live.ex | 9 +- .../update_status_test.exs | 121 +++++++++++++ .../live/dashboard_live_update_test.exs | 99 +++++++++++ 10 files changed, 462 insertions(+), 1 deletion(-) create mode 100644 lib/local_web_monitorex/update_status.ex create mode 100644 lib/local_web_monitorex_web/components/update_status_panel.ex create mode 100644 test/local_web_monitorex/update_status_test.exs create mode 100644 test/local_web_monitorex_web/live/dashboard_live_update_test.exs diff --git a/.gitignore b/.gitignore index aa4eb4f..eb1d8d9 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ npm-debug.log /dist/ .DS_Store /.playwright-mcp/ +/REVISION diff --git a/assets/css/app.css b/assets/css/app.css index 998f365..88296bf 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -232,6 +232,13 @@ a { color:inherit; text-decoration:none; } .settings-range-fields > span { color:#87948a; padding-bottom:11px; } .settings-range-save { width:100%; height:41px; margin-top:13px; border:0; border-radius:5px; background:#20382f; color:#f4f8ea; font-size:12px; font-weight:700; } .settings-range-save:hover { background:#335943; } +.settings-updates { border-top:1px solid #d9e1d7; margin-top:38px; padding-top:29px; } +.settings-updates h3 { margin:0 0 8px; font-size:18px; letter-spacing:-.045em; } +.settings-updates-summary { margin:0; color:#355c41; font-size:12px; font-weight:700; line-height:1.5; } +.settings-updates-summary.failed { color:#9b3b2c; } +.settings-updates dl { display:grid; grid-template-columns:auto minmax(0,1fr); gap:6px 14px; margin:14px 0 0; font-size:12px; } +.settings-updates dt { color:#6d7b70; } +.settings-updates dd { margin:0; min-width:0; overflow-wrap:anywhere; } .settings-error,.settings-success { margin:11px 0 0; font-size:12px; line-height:1.5; } .settings-error { color:#a54334; } .settings-success { color:#357344; } @@ -252,6 +259,10 @@ html[data-theme="dark"] .app-notice.error { background:#432924; border-color:#8b html[data-theme="dark"] .brand-mark span { box-shadow:0 0 0 4px #0b1812; } html[data-theme="dark"] .eyebrow,html[data-theme="dark"] .section-index { color:#a2c5a4; } html[data-theme="dark"] .page-head p,html[data-theme="dark"] .settings-panel > p,html[data-theme="dark"] .settings-theme p,html[data-theme="dark"] .settings-range > p { color:#adbfaf; } +html[data-theme="dark"] .settings-updates { border-color:#3d5443; } +html[data-theme="dark"] .settings-updates dt { color:#adbfaf; } +html[data-theme="dark"] .settings-updates-summary { color:#b7ddb1; } +html[data-theme="dark"] .settings-updates-summary.failed { color:#ffb4a5; } html[data-theme="dark"] .scan-status { color:#b7ddb1; } html[data-theme="dark"] .pulse-dot { box-shadow:0 0 0 4px #24402a; } html[data-theme="dark"] .refresh-button,html[data-theme="dark"] .settings-shortcut { background:#1c2c22; border-color:#476050; color:#dcebdc; } diff --git a/config/runtime.exs b/config/runtime.exs index e9500f9..4959a72 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -75,3 +75,6 @@ if config_env() == :prod do config :local_web_monitorex, LocalWebMonitorexWeb.Endpoint, secret_key_base: secret_key_base end + +config :local_web_monitorex, + update_status_file: System.get_env("LOCALWEBMONITOREX_UPDATE_STATUS") diff --git a/lib/local_web_monitorex/update_status.ex b/lib/local_web_monitorex/update_status.ex new file mode 100644 index 0000000..c8a2c91 --- /dev/null +++ b/lib/local_web_monitorex/update_status.ex @@ -0,0 +1,162 @@ +defmodule LocalWebMonitorex.UpdateStatus do + @moduledoc """ + Reads what the macOS updater last recorded about this installation. + + The updater writes `key=value` lines to a status file. The running copy's + own commit comes from its `REVISION` file, so a check made for another + installed commit is reported as pending instead of being trusted. + """ + + @reasons %{ + "fetch_failed" => :fetch_failed, + "build_failed" => :build_failed, + "smoke_failed" => :smoke_failed, + "switch_failed" => :switch_failed, + "restart_failed" => :restart_failed, + "installed_revision_missing" => :installed_revision_missing + } + + @failure_text %{ + fetch_failed: "could not reach GitHub", + build_failed: "the new version did not build", + smoke_failed: "the new version did not start", + switch_failed: "could not switch to the new version", + restart_failed: "the new version did not stay up, so the previous one was restored", + installed_revision_missing: "the installed commit is unknown", + unknown: "see the updater log" + } + + @type state :: + :unconfigured + | :pending + | :up_to_date + | :diverged + | {:behind, pos_integer()} + | {:failed, atom()} + + @type t :: %__MODULE__{ + installed: String.t() | nil, + available: String.t() | nil, + checked_at: DateTime.t() | nil, + state: state() + } + + defstruct installed: nil, available: nil, checked_at: nil, state: :unconfigured + + @spec status_path() :: String.t() | nil + def status_path, do: Application.get_env(:local_web_monitorex, :update_status_file) + + @spec revision_path() :: String.t() + def revision_path do + Application.get_env(:local_web_monitorex, :revision_file, Path.join(File.cwd!(), "REVISION")) + end + + @spec installed_revision(String.t()) :: String.t() | nil + def installed_revision(path \\ revision_path()) do + with {:ok, content} <- File.read(path), + sha when byte_size(sha) == 40 <- String.trim(content), + true <- hex?(sha) do + sha + else + _ -> nil + end + end + + @spec read(String.t() | nil, String.t()) :: t() + def read(status_path \\ status_path(), revision_path \\ revision_path()) do + installed = installed_revision(revision_path) + status = %__MODULE__{installed: installed} + + case read_fields(status_path) do + :unconfigured -> status + :missing -> %{status | state: :pending} + {:ok, fields} -> from_fields(status, fields) + end + end + + @spec summary(t()) :: String.t() + def summary(%__MODULE__{state: :up_to_date}), do: "Up to date with main" + def summary(%__MODULE__{state: {:behind, 1}}), do: "Behind main by 1 commit" + def summary(%__MODULE__{state: {:behind, count}}), do: "Behind main by #{count} commits" + def summary(%__MODULE__{state: :pending}), do: "Waiting for the first update check" + def summary(%__MODULE__{state: :unconfigured}), do: "Automatic updates are not set up" + + def summary(%__MODULE__{state: :diverged}), + do: "Installed commit is not on main; automatic updates are paused" + + def summary(%__MODULE__{state: {:failed, reason}}), + do: "Update failed: #{Map.fetch!(@failure_text, reason)}" + + @spec short_sha(String.t() | nil) :: String.t() + def short_sha(nil), do: "unknown" + def short_sha(sha), do: String.slice(sha, 0, 7) + + defp read_fields(nil), do: :unconfigured + + defp read_fields(path) do + case File.read(path) do + {:ok, content} -> {:ok, parse_fields(content)} + {:error, _reason} -> :missing + end + end + + defp parse_fields(content) do + content + |> String.split("\n", trim: true) + |> Enum.flat_map(&parse_line/1) + |> Map.new() + end + + defp parse_line(line) do + case String.split(line, "=", parts: 2) do + [key, value] -> [{key, String.trim(value)}] + _ -> [] + end + end + + defp from_fields(%{installed: installed} = status, %{"installed_sha" => installed} = fields) + when is_binary(installed) do + case state(fields) do + {:ok, state} -> + %{ + status + | state: state, + available: fields["available_sha"], + checked_at: checked_at(fields) + } + + :error -> + %{status | state: :pending} + end + end + + defp from_fields(status, _fields), do: %{status | state: :pending} + + defp state(%{"phase" => "up_to_date"}), do: {:ok, :up_to_date} + defp state(%{"phase" => "diverged"}), do: {:ok, :diverged} + defp state(%{"phase" => "failed"} = fields), do: {:ok, {:failed, reason(fields["reason"])}} + + defp state(%{"phase" => "behind", "behind" => behind}) do + case Integer.parse(behind) do + {count, ""} when count > 0 -> {:ok, {:behind, count}} + _ -> :error + end + end + + defp state(_fields), do: :error + + defp reason(value), do: Map.get(@reasons, value, :unknown) + + defp checked_at(%{"checked_at" => value}) do + with {seconds, ""} <- Integer.parse(value), + {:ok, datetime} <- DateTime.from_unix(seconds) do + datetime + else + _ -> nil + end + end + + defp checked_at(_fields), do: nil + + defp hex?(value), do: String.match?(value, ~r/\A[0-9a-f]+\z/) +end diff --git a/lib/local_web_monitorex_web/components/layouts.ex b/lib/local_web_monitorex_web/components/layouts.ex index eb015d4..e25fc05 100644 --- a/lib/local_web_monitorex_web/components/layouts.ex +++ b/lib/local_web_monitorex_web/components/layouts.ex @@ -3,5 +3,16 @@ defmodule LocalWebMonitorexWeb.Layouts do use LocalWebMonitorexWeb, :html + alias LocalWebMonitorex.UpdateStatus + embed_templates "layouts/*" + + @doc "Names the served commit so the updater can confirm which version answered." + def revision_meta(assigns) do + assigns = assign(assigns, :revision, UpdateStatus.installed_revision()) + + ~H""" + + """ + end end diff --git a/lib/local_web_monitorex_web/components/layouts/root.html.heex b/lib/local_web_monitorex_web/components/layouts/root.html.heex index 57a1398..818b187 100644 --- a/lib/local_web_monitorex_web/components/layouts/root.html.heex +++ b/lib/local_web_monitorex_web/components/layouts/root.html.heex @@ -5,6 +5,7 @@ + <.revision_meta />