|
| 1 | +defmodule TeslaMate.DataHealth do |
| 2 | + @moduledoc """ |
| 3 | + Read-only checks for TeslaMate data that may need manual review. |
| 4 | +
|
| 5 | + The report intentionally does not repair, close or delete records. It only |
| 6 | + surfaces drives and charging sessions that have remained open past a bounded |
| 7 | + age so a user can inspect them before choosing a maintenance action. Long-running |
| 8 | + does not by itself mean the data is corrupt. |
| 9 | + """ |
| 10 | + |
| 11 | + import Ecto.Query, warn: false |
| 12 | + |
| 13 | + alias TeslaMate.Log.{Car, ChargingProcess, Drive} |
| 14 | + alias TeslaMate.Repo |
| 15 | + |
| 16 | + @default_open_after_seconds 2 * 24 * 60 * 60 |
| 17 | + @default_limit 200 |
| 18 | + |
| 19 | + defmodule Finding do |
| 20 | + @moduledoc "A read-only data-health finding backed by database records." |
| 21 | + |
| 22 | + @enforce_keys [ |
| 23 | + :id, |
| 24 | + :code, |
| 25 | + :entity_type, |
| 26 | + :entity_id, |
| 27 | + :car_id, |
| 28 | + :car_name, |
| 29 | + :started_at |
| 30 | + ] |
| 31 | + defstruct @enforce_keys |
| 32 | + end |
| 33 | + |
| 34 | + defmodule Report do |
| 35 | + @moduledoc "A read-only data-health report." |
| 36 | + |
| 37 | + @enforce_keys [ |
| 38 | + :checked_at, |
| 39 | + :open_after_seconds, |
| 40 | + :findings, |
| 41 | + :truncated?, |
| 42 | + :read_only? |
| 43 | + ] |
| 44 | + defstruct @enforce_keys |
| 45 | + end |
| 46 | + |
| 47 | + @doc """ |
| 48 | + Returns long-running open drives and charging sessions without changing the database. |
| 49 | +
|
| 50 | + Options are accepted to make the age boundary and result cap deterministic in |
| 51 | + tests. A record is long-running only when it started strictly before the cutoff. |
| 52 | + """ |
| 53 | + def report(opts \\ []) do |
| 54 | + checked_at = Keyword.get(opts, :now, DateTime.utc_now()) |
| 55 | + |
| 56 | + open_after_seconds = |
| 57 | + positive_integer_option!(opts, :open_after_seconds, @default_open_after_seconds) |
| 58 | + |
| 59 | + limit = positive_integer_option!(opts, :limit, @default_limit) |
| 60 | + cutoff = DateTime.add(checked_at, -open_after_seconds, :second) |
| 61 | + |
| 62 | + candidates = |
| 63 | + cutoff |
| 64 | + |> long_running_candidates(limit + 1) |
| 65 | + |> Enum.sort_by(fn candidate -> |
| 66 | + { |
| 67 | + DateTime.to_unix(candidate.started_at, :microsecond), |
| 68 | + candidate.entity_type, |
| 69 | + candidate.entity_id |
| 70 | + } |
| 71 | + end) |
| 72 | + |
| 73 | + truncated? = length(candidates) > limit |
| 74 | + selected = Enum.take(candidates, limit) |
| 75 | + |
| 76 | + findings = |
| 77 | + Enum.map(selected, fn candidate -> |
| 78 | + %Finding{ |
| 79 | + id: "#{candidate.entity_type}:#{candidate.entity_id}", |
| 80 | + code: finding_code(candidate.entity_type), |
| 81 | + entity_type: candidate.entity_type, |
| 82 | + entity_id: candidate.entity_id, |
| 83 | + car_id: candidate.car_id, |
| 84 | + car_name: candidate.car_name, |
| 85 | + started_at: candidate.started_at |
| 86 | + } |
| 87 | + end) |
| 88 | + |
| 89 | + %Report{ |
| 90 | + checked_at: checked_at, |
| 91 | + open_after_seconds: open_after_seconds, |
| 92 | + findings: findings, |
| 93 | + truncated?: truncated?, |
| 94 | + read_only?: true |
| 95 | + } |
| 96 | + end |
| 97 | + |
| 98 | + defp positive_integer_option!(opts, key, default) do |
| 99 | + case Keyword.get(opts, key, default) do |
| 100 | + value when is_integer(value) and value > 0 -> value |
| 101 | + value -> raise ArgumentError, "#{key} must be a positive integer, got: #{inspect(value)}" |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + defp long_running_candidates(cutoff, limit) do |
| 106 | + long_running_drives(cutoff, limit) ++ long_running_charging_processes(cutoff, limit) |
| 107 | + end |
| 108 | + |
| 109 | + defp long_running_drives(cutoff, limit) do |
| 110 | + Drive |
| 111 | + |> join(:inner, [drive], car in Car, on: car.id == drive.car_id) |
| 112 | + |> where([drive], is_nil(drive.end_date) and drive.start_date < ^cutoff) |
| 113 | + |> order_by([drive], asc: drive.start_date, asc: drive.id) |
| 114 | + |> limit(^limit) |
| 115 | + |> select([drive, car], %{ |
| 116 | + entity_type: :drive, |
| 117 | + entity_id: drive.id, |
| 118 | + car_id: car.id, |
| 119 | + car_name: car.name, |
| 120 | + started_at: drive.start_date |
| 121 | + }) |
| 122 | + |> Repo.all() |
| 123 | + end |
| 124 | + |
| 125 | + defp long_running_charging_processes(cutoff, limit) do |
| 126 | + ChargingProcess |
| 127 | + |> join(:inner, [charging_process], car in Car, on: car.id == charging_process.car_id) |
| 128 | + |> where( |
| 129 | + [charging_process], |
| 130 | + is_nil(charging_process.end_date) and charging_process.start_date < ^cutoff |
| 131 | + ) |
| 132 | + |> order_by([charging_process], asc: charging_process.start_date, asc: charging_process.id) |
| 133 | + |> limit(^limit) |
| 134 | + |> select([charging_process, car], %{ |
| 135 | + entity_type: :charging_process, |
| 136 | + entity_id: charging_process.id, |
| 137 | + car_id: car.id, |
| 138 | + car_name: car.name, |
| 139 | + started_at: charging_process.start_date |
| 140 | + }) |
| 141 | + |> Repo.all() |
| 142 | + end |
| 143 | + |
| 144 | + defp finding_code(:drive), do: :long_running_open_drive |
| 145 | + |
| 146 | + defp finding_code(:charging_process), |
| 147 | + do: :long_running_open_charging_process |
| 148 | +end |
0 commit comments