From 1d08a930212bfaf1f44747c9f2f15b8de57e1013 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Tue, 28 Jan 2025 18:22:50 +0000 Subject: [PATCH] expose request count in LiveView to visualise #226 --- lib/app/api_manager.ex | 6 +++++- lib/app/reqlog.ex | 1 - lib/app_web/live/app_live.ex | 5 +++-- lib/app_web/live/app_live.html.heex | 1 + 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/app/api_manager.ex b/lib/app/api_manager.ex index cb0af2f..275e665 100644 --- a/lib/app/api_manager.ex +++ b/lib/app/api_manager.ex @@ -22,11 +22,15 @@ defmodule App.ApiManager do end defp schedule_work() do - Process.send_after(self(), :work, 2 * 60 * 60 * 1000) # In 2 hours + Process.send_after(self(), :work, 60 * 1000) # check again in 1 min end def get_users() do # Check how many requests have been made in the last hour: + count = App.Reqlog.req_count_last_hour() + if count < 4920 do + # Get the top 80 users that need to be queried + end end end diff --git a/lib/app/reqlog.ex b/lib/app/reqlog.ex index b195ef1..81796be 100644 --- a/lib/app/reqlog.ex +++ b/lib/app/reqlog.ex @@ -39,7 +39,6 @@ defmodule App.Reqlog do # Using `DateTime.add/4` with a negative number to subtract. ;-) # via: https://elixirforum.com/t/create-time-with-one-hour-plus/3666/5 one_hour_ago = DateTime.utc_now(:second) |> DateTime.add(-3600) - Repo.one(from r in Reqlog, select: count("*"), where: r.inserted_at > ^one_hour_ago) end diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index add18a4..04e57a9 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -15,8 +15,9 @@ defmodule AppWeb.AppLive do created_at: "2010-02-02T08:44:49Z", company: "ideaq"} # NEXT: prepend avatars to list ... - - {:ok, assign(socket, %{data: p, ids: App.User.list_users_avatars()})} + {:ok, assign(socket, %{data: p, + ids: App.User.list_users_avatars(), + count: App.Reqlog.req_count_last_hour()})} end def handle_event("sync", value, socket) do diff --git a/lib/app_web/live/app_live.html.heex b/lib/app_web/live/app_live.html.heex index 4e64c05..4775d3b 100644 --- a/lib/app_web/live/app_live.html.heex +++ b/lib/app_web/live/app_live.html.heex @@ -2,6 +2,7 @@ class="w-20 bg-green-600 hover:bg-green-800 text-white font-bold py-2 px-4 rounded"> Sync +

{@count}

Newest person: