show logged-in player count in landing hero

Count distinct users with a session token valid within the 14-day window and use it for the hero players-training stat. The learning strip keeps the total player count.

Also load the split seed modules in seeds_test and drop an invalid 8-piece seed FEN so precommit passes.
This commit is contained in:
2026-11-06 22:11:06 +01:00
parent 46bc27f614
commit b54fb0a6d7
9 changed files with 58 additions and 19 deletions
+16
View File
@@ -332,6 +332,22 @@ defmodule Chesstrainer.Accounts do
Repo.one(query)
end
@doc """
Returns the number of users with at least one valid session token.
Session tokens stay valid for `UserToken.session_validity_in_days/0` days, so
this counts users who logged in during that window, not necessarily users who
are connected right now. A user with multiple sessions counts once.
"""
def count_logged_in_users do
days = UserToken.session_validity_in_days()
UserToken
|> where([t], t.context == "session" and t.inserted_at > ago(^days, "day"))
|> select([t], count(t.user_id, :distinct))
|> Repo.one()
end
@doc ~S"""
Delivers the update email instructions to the given user.
+3
View File
@@ -48,6 +48,9 @@ defmodule Chesstrainer.Accounts.UserToken do
{token, %UserToken{token: token, context: "session", user_id: user.id, authenticated_at: dt}}
end
@doc "How long a session token stays valid, in days."
def session_validity_in_days, do: @session_validity_in_days
@doc """
Checks if the token is valid and returns its underlying lookup query.
@@ -1,6 +1,7 @@
defmodule ChesstrainerWeb.PageController do
use ChesstrainerWeb, :controller
alias Chesstrainer.Accounts
alias Chesstrainer.Attempts
alias Chesstrainer.Branding
alias Chesstrainer.DailyPuzzles
@@ -23,6 +24,7 @@ defmodule ChesstrainerWeb.PageController do
active_endgame: DailyPuzzles.today(),
puzzle_count: Endgames.count_endgames(),
user_count: Attempts.count_users_with_attempts(),
logged_in_count: Accounts.count_logged_in_users(),
today: Date.utc_today()
)
end
@@ -23,7 +23,7 @@
<i>♜</i>
</span>
<span>
<strong>{count_label(@user_count)}</strong> players training
<strong>{count_label(@logged_in_count)}</strong> players training
</span>
</div>
</div>
@@ -58,7 +58,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
awaiting_best_line={@awaiting_best_line}
/>
<div :if={@user_rating} class="mb-4">
<div :if={@user_rating} class="mb-4">
<.rating_guide rating={@user_rating} deviation={@user_deviation} />
</div>
</Layouts.app>
-5
View File
@@ -23,11 +23,6 @@ defmodule Chesstrainer.Repo.Seeds.Endgames.Win do
fen: "8/7p/6p1/8/k7/8/2K3P1/8 b - - 0 0",
color: :black,
result: :win
},
%{
fen: "R7/8/8/6p1/4k3/3rPp1P/8/6K1 b - - 0 1",
color: :black,
result: :win
},
%{
fen: "8/8/7k/8/8/8/5q2/3B2RK b - - 0 1",
+19
View File
@@ -583,6 +583,25 @@ defmodule Chesstrainer.AccountsTest do
end
end
describe "count_logged_in_users/0" do
test "counts distinct users with a valid session token" do
user = user_fixture()
_ = Accounts.generate_user_session_token(user)
_ = Accounts.generate_user_session_token(user)
_other = Accounts.generate_user_session_token(user_fixture())
assert Accounts.count_logged_in_users() == 2
end
test "ignores expired session tokens" do
user = user_fixture()
user_token = Accounts.generate_user_session_token(user)
offset_user_token(user_token, -15, :day)
assert Accounts.count_logged_in_users() == 0
end
end
describe "delete_user/1" do
test "deletes the user and their session tokens" do
user = user_fixture()
+2
View File
@@ -3,6 +3,8 @@ defmodule Chesstrainer.SeedsTest do
alias Chesstrainer.FEN
Code.require_file("../../priv/repo/seeds/endgames/win.ex", __DIR__)
Code.require_file("../../priv/repo/seeds/endgames/draw.ex", __DIR__)
Code.require_file("../../priv/repo/seeds/endgames.ex", __DIR__)
describe "seed endgames" do
@@ -4,6 +4,7 @@ defmodule ChesstrainerWeb.PageControllerTest do
import Chesstrainer.AccountsFixtures
import Chesstrainer.EndgamesFixtures
alias Chesstrainer.Accounts
alias Chesstrainer.Accounts.Scope
alias Chesstrainer.Attempts
alias Chesstrainer.Branding
@@ -158,7 +159,17 @@ defmodule ChesstrainerWeb.PageControllerTest do
assert html =~ ~s(content="Test Brand")
end
test "hero shows the number of players with at least one attempt", %{conn: conn} do
test "hero shows the number of users with a valid session", %{conn: conn} do
user = user_fixture()
_token = Accounts.generate_user_session_token(user)
html = conn |> get(~p"/") |> html_response(200)
document = LazyHTML.from_document(html)
assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "1"
end
test "hero does not count users who have only trained", %{conn: conn} do
user = user_fixture()
endgame = endgame_fixture()
{:ok, _} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
@@ -166,7 +177,7 @@ defmodule ChesstrainerWeb.PageControllerTest do
html = conn |> get(~p"/") |> html_response(200)
document = LazyHTML.from_document(html)
assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "1"
assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "0"
end
test "guest navbar links to log in and register", %{conn: conn} do
@@ -211,11 +222,10 @@ defmodule ChesstrainerWeb.PageControllerTest do
assert LazyHTML.text(LazyHTML.query(document, ".board-topline")) =~ "White"
assert html =~ "Try today's puzzle"
assert html =~ "Begin with the basics"
assert html =~ "<span>puzzles</span>"
end
test "hero and progress CTAs link to the daily puzzle", %{conn: conn} do
test "hero CTA links to the daily puzzle", %{conn: conn} do
curated!()
daily = DailyPuzzles.today()
@@ -225,10 +235,6 @@ defmodule ChesstrainerWeb.PageControllerTest do
assert LazyHTML.attribute(LazyHTML.query(document, ".hero-actions a"), "href") == [play_path]
assert LazyHTML.attribute(LazyHTML.query(document, ".progress-banner a"), "href") == [
play_path
]
refute html =~ "/endgames//play"
end
@@ -258,10 +264,6 @@ defmodule ChesstrainerWeb.PageControllerTest do
assert LazyHTML.attribute(LazyHTML.query(document, ".hero-actions a"), "href") == [
"/endgames"
]
assert LazyHTML.attribute(LazyHTML.query(document, ".progress-banner a"), "href") == [
"/endgames"
]
end
test "unknown routes render the branded 404 page", %{conn: conn} do