Skip to content

Commit

Permalink
Add ContEx for liveview charts
Browse files Browse the repository at this point in the history
  • Loading branch information
phonnz committed Jan 10, 2025
1 parent 86f1718 commit bf96024
Show file tree
Hide file tree
Showing 6 changed files with 9,811 additions and 15 deletions.
54 changes: 51 additions & 3 deletions fullstack/lib/fullstack_web/live/public/public_transactions.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
use FullstackWeb, :live_view

import FullstackWeb.CustomComponents
alias Fullstack.Financial
alias Contex.{Sparkline}
alias Contex

@impl true
def mount(_params, session, socket) do
def mount(_params, _session, socket) do
socket =
socket
|> assign(:chart_options, %{
refresh_rate: 1000,
number_of_points: 50,
height: 400,
width: 600
})
|> assign(:process_counts, [0])
|> make_test_data()

if connected?(socket) do
Phoenix.PubSub.subscribe(Fullstack.PubSub, "centralized_counter")
Process.send_after(self(), :tick, socket.assigns.chart_options.refresh_rate)
end

{:ok, socket}
Expand All @@ -22,4 +34,40 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do

{:noreply, socket}
end

@impl true
def handle_info(:tick, socket) do
Process.send_after(self(), :tick, socket.assigns.chart_options.refresh_rate)

socket =
socket
|> make_test_data()

{:noreply, socket}
end

defp make_plot(data) do
Sparkline.new(data)
|> Map.update!(:height, fn _ -> 300 end)
|> Map.update!(:width, fn _ -> 600 end)
|> Sparkline.draw()
end

defp make_red_plot(data) do
Sparkline.new(data)
|> Sparkline.colours("#fad48e", "#ff9838")
|> Map.update!(:height, fn _ -> 300 end)
|> Map.update!(:width, fn _ -> 600 end)
|> Sparkline.draw()
end

defp make_test_data(socket) do
number_of_points = socket.assigns.chart_options.number_of_points

result =
1..number_of_points
|> Enum.map(fn _ -> :rand.uniform(50) - 100 end)

assign(socket, test_data: result)
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<LiveToast.toast_group flash={@flash} connected={assigns[:socket] != nil} corner={:top_right} />
<div class="bg-white h-screen font-sans flex flex-col items-center justify-start">
<h1>Transactions</h1>
<h1>Sales</h1>
<%= make_plot(@test_data) %>
<%= make_red_plot(@test_data) %>
<pre>
<%= inspect(@info, pretty: true ) %>
</pre>
Expand Down
3 changes: 2 additions & 1 deletion fullstack/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ defmodule Fullstack.MixProject do
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:vega_lite, "~> 0.1.9"},
{:cachex, "~> 3.6.0"},
{:live_toast, "~> 0.6.4"}
{:live_toast, "~> 0.6.4"},
{:contex, "0.3.0"}
]
end

Expand Down
2 changes: 2 additions & 0 deletions fullstack/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"cachex": {:hex, :cachex, "3.6.0", "14a1bfbeee060dd9bec25a5b6f4e4691e3670ebda28c8ba2884b12fe30b36bf8", [:mix], [{:eternal, "~> 1.2", [hex: :eternal, repo: "hexpm", optional: false]}, {:jumper, "~> 1.0", [hex: :jumper, repo: "hexpm", optional: false]}, {:sleeplocks, "~> 1.1", [hex: :sleeplocks, repo: "hexpm", optional: false]}, {:unsafe, "~> 1.0", [hex: :unsafe, repo: "hexpm", optional: false]}], "hexpm", "ebf24e373883bc8e0c8d894a63bbe102ae13d918f790121f5cfe6e485cc8e2e2"},
"castore": {:hex, :castore, "1.0.8", "dedcf20ea746694647f883590b82d9e96014057aff1d44d03ec90f36a5c0dc6e", [:mix], [], "hexpm", "0b2b66d2ee742cb1d9cb8c8be3b43c3a70ee8651f37b75a8b982e036752983f1"},
"comeonin": {:hex, :comeonin, "5.4.0", "246a56ca3f41d404380fc6465650ddaa532c7f98be4bda1b4656b3a37cc13abe", [:mix], [], "hexpm", "796393a9e50d01999d56b7b8420ab0481a7538d0caf80919da493b4a6e51faf1"},
"contex": {:hex, :contex, "0.3.0", "d390713efee604702600ba801a481bcb8534a9af43e118b29d9d37fe4495fcba", [:mix], [{:nimble_strftime, "~> 0.1.0", [hex: :nimble_strftime, repo: "hexpm", optional: false]}], "hexpm", "3fa7535cc3b265691a4eabc2707fe8622aa60a2565145a14da9aebd613817652"},
"cowboy": {:hex, :cowboy, "2.12.0", "f276d521a1ff88b2b9b4c54d0e753da6c66dd7be6c9fca3d9418b561828a3731", [:make, :rebar3], [{:cowlib, "2.13.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "8a7abe6d183372ceb21caa2709bec928ab2b72e18a3911aa1771639bef82651e"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.13.0", "db8f7505d8332d98ef50a3ef34b34c1afddec7506e4ee4dd4a3a266285d282ca", [:make, :rebar3], [], "hexpm", "e1e1284dc3fc030a64b1ad0d8382ae7e99da46c3246b815318a4b848873800a4"},
Expand Down Expand Up @@ -35,6 +36,7 @@
"money": {:hex, :money, "1.12.4", "9d9817aa79d1317871f6b006721c264bf1910fb28ba2af50746514f0d7e8ddbe", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.0 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "87e4bb907df1da184cb4640569d8df99ee6d88c84ce4f5da03cb2fab8d433eb9"},
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"nimble_strftime": {:hex, :nimble_strftime, "0.1.1", "b988184d1bd945bc139b2c27dd00a6c0774ec94f6b0b580083abd62d5d07818b", [:mix], [], "hexpm", "89e599c9b8b4d1203b7bb5c79eb51ef7c6a28fbc6228230b312f8b796310d755"},
"number": {:hex, :number, "1.0.5", "d92136f9b9382aeb50145782f116112078b3465b7be58df1f85952b8bb399b0f", [:mix], [{:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "c0733a0a90773a66582b9e92a3f01290987f395c972cb7d685f51dd927cd5169"},
"phoenix": {:hex, :phoenix, "1.7.14", "a7d0b3f1bc95987044ddada111e77bd7f75646a08518942c72a8440278ae7825", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "c7859bc56cc5dfef19ecfc240775dae358cbaa530231118a9e014df392ace61a"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.6.2", "3b83b24ab5a2eb071a20372f740d7118767c272db386831b2e77638c4dcc606d", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.1", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "3f94d025f59de86be00f5f8c5dd7b5965a3298458d21ab1c328488be3b5fcd59"},
Expand Down
2,405 changes: 2,404 additions & 1 deletion fullstack/priv/static/assets/app.css

Large diffs are not rendered by default.

Loading

0 comments on commit bf96024

Please sign in to comment.