|
| 1 | +defmodule NervesHub.Insights do |
| 2 | + @moduledoc """ |
| 3 | + Queries for Product Insights. |
| 4 | +
|
| 5 | + Some useful reads, which may inspire future improvements: |
| 6 | + - https://danschultzer.com/posts/normalize-chart-data-using-ecto-postgresql |
| 7 | + - https://danschultzer.com/posts/echarts-phoenix-liveview |
| 8 | + """ |
| 9 | + import Ecto.Query |
| 10 | + |
| 11 | + alias NervesHub.Devices.Device |
| 12 | + alias NervesHub.Devices.DeviceConnection |
| 13 | + alias NervesHub.Products.Product |
| 14 | + |
| 15 | + alias NervesHub.Repo |
| 16 | + |
| 17 | + @doc """ |
| 18 | + Count of currently connected Devices, scoped to an individual Product. |
| 19 | + """ |
| 20 | + @spec connected_count(product :: NervesHub.Products.Product.t()) :: integer() |
| 21 | + def connected_count(product) do |
| 22 | + Device |
| 23 | + |> join(:left, [d], lc in assoc(d, :latest_connection), as: :latest_connection) |
| 24 | + |> where([d], d.product_id == ^product.id) |
| 25 | + |> where([_, latest_connection: lc], lc.status == :connected) |
| 26 | + |> Repo.aggregate(:count) |
| 27 | + end |
| 28 | + |
| 29 | + @doc """ |
| 30 | + Count of connected Devices over the last 24 hours, in 1 minute intervals, scoped to an individual Product. |
| 31 | + """ |
| 32 | + @spec connected_device_counts_last_24_hours(product :: Product.t()) :: list() |
| 33 | + def connected_device_counts_last_24_hours(product) do |
| 34 | + device_counts_query = |
| 35 | + DeviceConnection |
| 36 | + |> select([dc], %{ |
| 37 | + device_count: count(dc.id, :distinct) |
| 38 | + }) |
| 39 | + |> where([dc], dc.product_id == ^product.id) |
| 40 | + |> where( |
| 41 | + [dc], |
| 42 | + fragment( |
| 43 | + "? BETWEEN DATE_BIN('1 minute'::interval, ?, 'epoch') AND DATE_BIN('1 minute'::interval, ?, 'epoch') + '1 minute'::interval", |
| 44 | + dc.established_at, |
| 45 | + parent_as(:series).timestamp, |
| 46 | + parent_as(:series).timestamp |
| 47 | + ) or |
| 48 | + fragment( |
| 49 | + "? BETWEEN DATE_BIN('1 minute'::interval, ?, 'epoch') AND DATE_BIN('1 minute'::interval, ?, 'epoch') + '1 minute'::interval", |
| 50 | + dc.disconnected_at, |
| 51 | + parent_as(:series).timestamp, |
| 52 | + parent_as(:series).timestamp |
| 53 | + ) or |
| 54 | + (fragment( |
| 55 | + "? < DATE_BIN('1 minute'::interval, ?, 'epoch')", |
| 56 | + dc.established_at, |
| 57 | + parent_as(:series).timestamp |
| 58 | + ) and |
| 59 | + fragment( |
| 60 | + "? > DATE_BIN('1 minute'::interval, ?, 'epoch') + '1 minute'::interval", |
| 61 | + dc.disconnected_at, |
| 62 | + parent_as(:series).timestamp |
| 63 | + )) or |
| 64 | + (fragment( |
| 65 | + "? < DATE_BIN('1 minute'::interval, ?, 'epoch')", |
| 66 | + dc.established_at, |
| 67 | + parent_as(:series).timestamp |
| 68 | + ) and is_nil(dc.disconnected_at)) |
| 69 | + ) |
| 70 | + |> group_by([dc], dc.product_id) |
| 71 | + |
| 72 | + from(fragment("GENERATE_SERIES(NOW() - '1 hour'::interval, NOW(), '1 minute'::interval)"), |
| 73 | + as: :series |
| 74 | + ) |
| 75 | + |> join(:left_lateral, [], subquery(device_counts_query), on: true, as: :counts) |
| 76 | + |> select([gs, counts: dc], %{ |
| 77 | + timestamp: |
| 78 | + fragment("DATE_BIN('1 minute'::interval, ?, 'epoch')", gs) |> selected_as(:timestamp), |
| 79 | + count: coalesce(dc.device_count, 0) |
| 80 | + }) |
| 81 | + |> order_by([series: gs], asc: gs.timestamp) |
| 82 | + |> Repo.all() |
| 83 | + end |
| 84 | + |
| 85 | + @doc """ |
| 86 | + Count of devices where the health status isn't 'healthy' or 'unknown', scoped to an individual Product. |
| 87 | + """ |
| 88 | + @spec not_healthy_devices_count(product :: NervesHub.Products.Product.t()) :: integer() |
| 89 | + def not_healthy_devices_count(product) do |
| 90 | + Device |
| 91 | + |> join(:left, [d], lc in assoc(d, :latest_health), as: :latest_health) |
| 92 | + |> where([d], d.product_id == ^product.id) |
| 93 | + |> where([_, latest_health: lh], lh.status in [:warning, :unhealthy]) |
| 94 | + |> Repo.aggregate(:count) |
| 95 | + end |
| 96 | + |
| 97 | + @doc """ |
| 98 | + Count of devices which haven't connected in the last 24 hours, scoped to an individual Product. |
| 99 | + """ |
| 100 | + @spec not_recently_seen_count(product :: NervesHub.Products.Product.t()) :: integer() |
| 101 | + def not_recently_seen_count(product) do |
| 102 | + Device |
| 103 | + |> join(:left, [d], lc in assoc(d, :latest_connection), as: :latest_connection) |
| 104 | + |> where([d], d.product_id == ^product.id) |
| 105 | + |> where([_, latest_connection: lc], lc.status == :disconnected) |
| 106 | + |> where([_, latest_connection: lc], lc.disconnected_at < ago(24, "hour")) |
| 107 | + |> Repo.aggregate(:count) |
| 108 | + end |
| 109 | + |
| 110 | + @doc """ |
| 111 | + Count of different firmware versions run on connected devices, scoped to an individual Product. |
| 112 | + """ |
| 113 | + @spec active_firmware_versions_count(product :: NervesHub.Products.Product.t()) :: integer() |
| 114 | + def active_firmware_versions_count(product) do |
| 115 | + Device |
| 116 | + |> select([d], %{version: d.firmware_metadata["uuid"]}) |
| 117 | + |> distinct(true) |
| 118 | + |> where([d], d.product_id == ^product.id) |
| 119 | + |> Repo.aggregate(:count) |
| 120 | + end |
| 121 | + |
| 122 | + @doc """ |
| 123 | + List of unique firmware versions run on connected devices, scoped to an individual Product. |
| 124 | + """ |
| 125 | + @spec active_firmware_versions(product :: NervesHub.Products.Product.t()) :: integer() |
| 126 | + def active_firmware_versions(product) do |
| 127 | + Device |
| 128 | + |> select([d], %{version: d.firmware_metadata["uuid"]}) |
| 129 | + |> distinct(true) |
| 130 | + |> where([d], d.product_id == ^product.id) |
| 131 | + |> Repo.all() |
| 132 | + end |
| 133 | + |
| 134 | + @doc """ |
| 135 | + Count of devices which have reconnected more than 5 times in the last 24 hours, scoped to an individual Product. |
| 136 | + """ |
| 137 | + @spec high_reconnect_count(product :: NervesHub.Products.Product.t()) :: integer() |
| 138 | + def high_reconnect_count(product) do |
| 139 | + Device |
| 140 | + |> join(:left, [d], lc in assoc(d, :device_connections), as: :device_connections) |
| 141 | + |> where([d], d.product_id == ^product.id) |
| 142 | + |> where([_, device_connections: dc], dc.established_at > ago(24, "hour")) |
| 143 | + |> group_by([d], d.id) |
| 144 | + |> having([d, device_connections: dc], count(dc.id) > 5) |
| 145 | + |> subquery() |
| 146 | + |> Repo.aggregate(:count) |
| 147 | + end |
| 148 | +end |
0 commit comments