Skip to content

Commit

Permalink
Move statistics UI to ConnectFour (#265)
Browse files Browse the repository at this point in the history
Move statistics UI to ConnectFour for higher cohesion. Additionally,
the content is cached for 10 seconds #263.

The service sending updates to the UI is also moved to ConnectFour,
and the BrowserNotifier is now shared library.
  • Loading branch information
marein authored Feb 28, 2025
1 parent fde9d52 commit abcfcf6
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ APP_ENV='dev'
APP_KERNEL_SECRET='ThisTokenIsNotSoSecretChangeIt'
APP_WAIT_FOR='unix:///var/run/proxysql/proxysql.sock,mysql:3306,redis:6379,rabbitmq:5672,nchan:81'
APP_RUN_MIGRATIONS='1'
APP_NCHAN_BASE_URL='http://nchan:81'
APP_RABBIT_MQ_DSN='amqp://guest:guest@rabbitmq:5672?heartbeat=60&prefetchCount=30'

############################
Expand All @@ -31,5 +32,4 @@ APP_IDENTITY_DOCTRINE_DBAL_URL='mysqli://root:password@localhost/identity?persis
# Web Interface Context #
############################
APP_WEB_INTERFACE_PREDIS_CLIENT_URL='redis://redis:6379?persistent=1'
APP_WEB_INTERFACE_NCHAN_BASE_URL='http://nchan:81'
APP_WEB_INTERFACE_PUBLISH_TO_BROWSER_SHARDS='1'
11 changes: 11 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ services:
gaming.scheduler:
class: Gaming\Common\Scheduler\PcntlScheduler

gaming.browser-notifier:
class: Gaming\Common\BrowserNotifier\Integration\NchanBrowserNotifier
arguments:
- !service
class: Marein\Nchan\Nchan
arguments:
- '%env(APP_NCHAN_BASE_URL)%'
- !service
class: Marein\Nchan\HttpAdapter\Psr18ClientAdapter
arguments: ['@psr18.http_client', '@psr18.http_client', '@psr18.http_client']

# Custom exception listener for the gaming domain.
# Lower priority so that the profiler is respected.
# todo: This can be removed as soon as https://github.com/marein/php-gaming-website/issues/34 is done.
Expand Down
3 changes: 3 additions & 0 deletions config/connect-four/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ gaming_platform_bus:
connect_four_command: ~
connect_four_query: ~

twig:
paths: { '%kernel.project_dir%/src/ConnectFour/Port/Adapter/Http/View': connect-four }

doctrine:
dbal:
connections:
Expand Down
7 changes: 7 additions & 0 deletions config/connect-four/services/console.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ services:
- name: console.command
command: connect-four:follow-event-store
description: 'Publish events to subscribers.'

Gaming\ConnectFour\Port\Adapter\Console\PublishRunningGamesCountToNchanCommand:
arguments: ['@connect-four.query-bus', '@gaming.browser-notifier']
tags:
- name: console.command
command: connect-four:publish-running-games-count-to-nchan
description: 'Publish number of running games to the browser.'
6 changes: 6 additions & 0 deletions config/connect-four/services/controller.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
connect-four.fragment-controller:
class: Gaming\ConnectFour\Port\Adapter\Http\FragmentController
arguments: ['@connect-four.query-bus']
calls: [[setContainer, ['@Psr\Container\ContainerInterface']]]
tags: ['controller.service_arguments', 'container.service_subscriber']
7 changes: 0 additions & 7 deletions config/web-interface/services/console.yml

This file was deleted.

2 changes: 1 addition & 1 deletion config/web-interface/services/consumer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
arguments:
- !service
class: Gaming\WebInterface\Infrastructure\Messaging\PublishMessageBrokerEventsToBrowserMessageHandler
arguments: ['@web-interface.browser-notifier']
arguments: ['@gaming.browser-notifier']
- !service
class: Gaming\Common\MessageBroker\Integration\AmqpLib\QueueConsumer\ConsumeQueueWithLeastConsumers
arguments:
Expand Down
12 changes: 0 additions & 12 deletions config/web-interface/services/integration.yml

This file was deleted.

2 changes: 1 addition & 1 deletion deploy/load-test/stack/app.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ APP_ENV=prod
APP_KERNEL_SECRET=ThisTokenIsNotSoSecretChangeIt
APP_WAIT_FOR=unix:///var/run/proxysql/proxysql.sock,chat-mysql:3306,connect-four-mysql-1:3306,connect-four-mysql-2:3306,connect-four-mysql-3:3306,connect-four-mysql-4:3306,connect-four-mysql-5:3306,identity-mysql:3306,chat-redis:6379,connect-four-redis:6379,web-interface-redis:6379,rabbitmq:5672,nchan:81
APP_RUN_MIGRATIONS=1
APP_NCHAN_BASE_URL=http://nchan:81
APP_RABBIT_MQ_DSN=amqp://guest:guest@rabbitmq:5672?heartbeat=60&prefetchCount=30

APP_CHAT_DOCTRINE_DBAL_URL=mysqli://root:password@localhost/chat?persistent=1&unix_socket=/var/run/proxysql/proxysql.sock
Expand All @@ -15,7 +16,6 @@ APP_CONNECT_FOUR_PREDIS_CLIENT_URL=redis://connect-four-redis:6379?persistent=1
APP_IDENTITY_DOCTRINE_DBAL_URL=mysqli://root:password@localhost/identity?persistent=1&unix_socket=/var/run/proxysql/proxysql.sock

APP_WEB_INTERFACE_PREDIS_CLIENT_URL=redis://web-interface-redis:6379?persistent=1
APP_WEB_INTERFACE_NCHAN_BASE_URL=http://nchan:81
APP_WEB_INTERFACE_PUBLISH_TO_BROWSER_SHARDS=5

PHP_FPM_PM=static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ services:
placement:
constraints:
- "node.labels.long-running==1"
connect-four-publish-running-games-count-to-nchan:
image: marein/php-gaming-website:php-fpm
command: bin/console connect-four:publish-running-games-count-to-nchan
env_file: ../app.env
volumes:
- proxysql.sock:/var/run/proxysql
deploy:
placement:
constraints:
- "node.labels.long-running==1"

volumes:
proxysql.sock:
10 changes: 0 additions & 10 deletions deploy/load-test/stack/web-interface/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ services:
placement:
constraints:
- "node.labels.long-running==1"
web-interface-publish-running-games-count-to-nchan:
image: marein/php-gaming-website:php-fpm
command: bin/console web-interface:publish-running-games-count-to-nchan
env_file: ../app.env
volumes:
- proxysql.sock:/var/run/proxysql
deploy:
placement:
constraints:
- "node.labels.long-running==1"

volumes:
proxysql.sock:
8 changes: 4 additions & 4 deletions deploy/single-server/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ x-php-container:
APP_CONNECT_FOUR_PREDIS_CLIENT_URL: "redis://redis:6379?persistent=1"
APP_IDENTITY_DOCTRINE_DBAL_URL: "mysqli://root:password@localhost/identity?persistent=1&unix_socket=/var/run/proxysql/proxysql.sock"
APP_WEB_INTERFACE_PREDIS_CLIENT_URL: "redis://redis:6379?persistent=1"
APP_WEB_INTERFACE_NCHAN_BASE_URL: "http://nchan:81"
APP_NCHAN_BASE_URL: "http://nchan:81"
APP_WEB_INTERFACE_PUBLISH_TO_BROWSER_SHARDS: "1"
depends_on:
- mysql
Expand Down Expand Up @@ -150,6 +150,9 @@ services:
connect-four-follow-event-store:
<<: *php-container
command: bin/console connect-four:follow-event-store pointer all
connect-four-publish-running-games-count-to-nchan:
<<: *php-container
command: bin/console connect-four:publish-running-games-count-to-nchan
chat-follow-event-store:
<<: *php-container
command: bin/console chat:follow-event-store pointer all
Expand All @@ -159,9 +162,6 @@ services:
consume-messages:
<<: *php-container
command: bin/console gaming:consume-messages all
web-interface-publish-running-games-count-to-nchan:
<<: *php-container
command: bin/console web-interface:publish-running-games-count-to-nchan

volumes:
mysql:
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ services:
php-connect-four-follow-event-store:
<<: *php-container
command: bin/console connect-four:follow-event-store pointer all
php-connect-four-publish-running-games-count-to-nchan:
<<: *php-container
command: bin/console connect-four:publish-running-games-count-to-nchan
php-chat-follow-event-store:
<<: *php-container
command: bin/console chat:follow-event-store pointer all
Expand All @@ -104,9 +107,6 @@ services:
php-consume-messages:
<<: *php-container
command: bin/console gaming:consume-messages all
php-web-interface-publish-running-games-count-to-nchan:
<<: *php-container
command: bin/console web-interface:publish-running-games-count-to-nchan

##############################
# CI helper #
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ services:
php-connect-four-follow-event-store:
<<: *php-container
command: bin/restartOnChange bin/console connect-four:follow-event-store pointer all
php-connect-four-publish-running-games-count-to-nchan:
<<: *php-container
command: bin/restartOnChange bin/console connect-four:publish-running-games-count-to-nchan
php-chat-follow-event-store:
<<: *php-container
command: bin/restartOnChange bin/console chat:follow-event-store pointer all
Expand All @@ -159,9 +162,6 @@ services:
php-consume-messages:
<<: *php-container
command: bin/restartOnChange bin/console gaming:consume-messages all
php-web-interface-publish-running-games-count-to-nchan:
<<: *php-container
command: bin/restartOnChange bin/console web-interface:publish-running-games-count-to-nchan

##############################
# Development helper #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Gaming\WebInterface\Application;
namespace Gaming\Common\BrowserNotifier;

interface BrowserNotifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Gaming\WebInterface\Infrastructure;
namespace Gaming\Common\BrowserNotifier\Integration;

use Gaming\WebInterface\Application\BrowserNotifier;
use Gaming\Common\BrowserNotifier\BrowserNotifier;
use Marein\Nchan\Api\Model\PlainTextMessage;
use Marein\Nchan\Nchan;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Gaming\WebInterface\Presentation\Console;
namespace Gaming\ConnectFour\Port\Adapter\Console;

use Gaming\Common\BrowserNotifier\BrowserNotifier;
use Gaming\Common\Bus\Bus;
use Gaming\ConnectFour\Application\Game\Query\RunningGamesQuery;
use Gaming\WebInterface\Application\BrowserNotifier;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
27 changes: 27 additions & 0 deletions src/ConnectFour/Port/Adapter/Http/FragmentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Gaming\ConnectFour\Port\Adapter\Http;

use Gaming\Common\Bus\Bus;
use Gaming\ConnectFour\Application\Game\Query\RunningGamesQuery;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\Cache;

final class FragmentController extends AbstractController
{
public function __construct(
private readonly Bus $queryBus
) {
}

#[Cache(public: true, maxage: 10)]
public function statisticsAction(): Response
{
return $this->render('@connect-four/statistics.html.twig', [
'runningGames' => $this->queryBus->handle(new RunningGamesQuery())
]);
}
}
8 changes: 8 additions & 0 deletions src/ConnectFour/Port/Adapter/Http/View/statistics.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>
<span class="badge bg-blue text-blue-fg">
<connect-four-running-games>
{{ runningGames.count }}
</connect-four-running-games>
</span>
running games
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Gaming\WebInterface\Infrastructure\Messaging;

use Gaming\Common\BrowserNotifier\BrowserNotifier;
use Gaming\Common\MessageBroker\Context;
use Gaming\Common\MessageBroker\Message;
use Gaming\Common\MessageBroker\MessageHandler;
use Gaming\WebInterface\Application\BrowserNotifier;
use GamingPlatform\Api\Chat\V1\ChatV1Factory;

final class PublishMessageBrokerEventsToBrowserMessageHandler implements MessageHandler
Expand Down
4 changes: 1 addition & 3 deletions src/WebInterface/Presentation/Http/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Gaming\ConnectFour\Application\Game\Query\GameQuery;
use Gaming\ConnectFour\Application\Game\Query\GamesByPlayerQuery;
use Gaming\ConnectFour\Application\Game\Query\OpenGamesQuery;
use Gaming\ConnectFour\Application\Game\Query\RunningGamesQuery;
use Gaming\WebInterface\Infrastructure\Security\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -28,8 +27,7 @@ public function lobbyAction(): Response
return new Response(
$this->twig->render('@web-interface/lobby.html.twig', [
'maximumNumberOfGamesInList' => 10,
'openGames' => $this->connectFourQueryBus->handle(new OpenGamesQuery()),
'runningGames' => $this->connectFourQueryBus->handle(new RunningGamesQuery())
'openGames' => $this->connectFourQueryBus->handle(new OpenGamesQuery())
])
);
}
Expand Down
9 changes: 1 addition & 8 deletions src/WebInterface/Presentation/Http/View/lobby.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@
<div class="col-lg-3 order-first">
<div class="card">
<div class="card-body">
<p>
<span class="badge bg-blue text-blue-fg">
<connect-four-running-games>
{{ runningGames.count }}
</connect-four-running-games>
</span>
running games
</p>
{{ render_ssi(controller('connect-four.fragment-controller::statisticsAction')) }}
</div>
</div>
</div>
Expand Down

0 comments on commit abcfcf6

Please sign in to comment.