Skip to content

Commit f1e3111

Browse files
committed
perf/save_ratings_count_in_notices_entities
1 parent c252437 commit f1e3111

18 files changed

+480
-182
lines changed

.env

+7
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
4545
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
4646
JWT_PASSPHRASE=275fee27732fdd71221f5cbe115f889d
4747
###< lexik/jwt-authentication-bundle ###
48+
49+
###> symfony/messenger ###
50+
# Choose one of the transports below
51+
# MESSENGER_TRANSPORT_DSN=doctrine://default
52+
# MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages
53+
# MESSENGER_TRANSPORT_DSN=redis://localhost:6379/messages
54+
###< symfony/messenger ###

composer.json

+14-13
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,39 @@
3434
"symfony/validator": "4.4.*",
3535
"symfony/twig-bundle": "4.4.*",
3636
"symfony/yaml": "4.4.*",
37+
"symfony/apache-pack": "^1.0",
38+
"symfony/expression-language": "4.4.*",
39+
"symfony/proxy-manager-bridge": "4.4.*",
40+
"symfony/property-access": "4.4.*",
41+
"symfony/property-info": "4.4.*",
42+
"symfony/serializer": "4.4.*",
43+
"symfony/messenger": "4.4.*",
44+
"doctrine/annotations": "^1.0",
3745
"doctrine/orm": "^2.3",
3846
"doctrine/doctrine-bundle": "^1.2",
3947
"doctrine/doctrine-cache-bundle": "^1.4",
4048
"doctrine/doctrine-migrations-bundle": "^v1.3.2",
49+
"twig/extra-bundle": "^2.12|^3.0",
50+
"twig/twig": "^2.12|^3.0",
4151
"sensio/framework-extra-bundle": "^5.6",
4252
"friendsofsymfony/user-bundle": "^2.1.2",
4353
"friendsofsymfony/jsrouting-bundle": "^2.6",
54+
"beberlei/doctrineextensions": "^1.2",
55+
"knplabs/doctrine-behaviors": "1.4",
56+
"api-platform/core": "^2.6",
4457
"easycorp/easyadmin-bundle": "^2",
58+
"composer/package-versions-deprecated": "^1.11",
4559
"ocramius/proxy-manager": "^2.0.2",
4660
"marc-mabe/php-enum": "^2.2",
47-
"beberlei/doctrineextensions": "^1.2",
4861
"vich/uploader-bundle": "^1.7",
4962
"liip/imagine-bundle": "^2.0",
5063
"youthweb/urllinker": "^1.2",
5164
"sentry/sentry-symfony": "^3.0",
52-
"knplabs/doctrine-behaviors": "1.4",
5365
"nelmio/cors-bundle": "1.5.6",
5466
"league/uri": "^6.2",
5567
"league/uri-components": "^2.2",
56-
"composer/package-versions-deprecated": "^1.11",
57-
"twig/extra-bundle": "^2.12|^3.0",
58-
"twig/twig": "^2.12|^3.0",
59-
"symfony/apache-pack": "^1.0",
6068
"ezyang/htmlpurifier": "^4.13",
61-
"api-platform/core": "^2.6",
62-
"symfony/expression-language": "4.4.*",
63-
"symfony/proxy-manager-bridge": "4.4.*",
64-
"doctrine/annotations": "^1.0",
6569
"phpdocumentor/reflection-docblock": "^5.2",
66-
"symfony/property-access": "4.4.*",
67-
"symfony/property-info": "4.4.*",
68-
"symfony/serializer": "4.4.*",
6970
"lexik/jwt-authentication-bundle": "^2.11"
7071
},
7172
"require-dev": {

composer.lock

+89-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/packages/easy_admin.yaml

+11-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ easy_admin:
2121
- 'js/message_validator.js'
2222
- 'js/filter_table_row.js'
2323
- 'js/select2.js'
24+
formats:
25+
datetime: 'd/m/y'
2426
entities:
2527
Notice:
2628
class: App\Entity\Notice
@@ -32,17 +34,17 @@ easy_admin:
3234
fields: &noticeListFields
3335
- { property: id, label: notices.id, css_class: 'column-id' }
3436
- { property: contributor, label: notices.contributor, css_class: 'column-contributor' }
35-
- { property: relayersCount, label: notices.relayers, css_class: 'column-relayers' }
37+
- { property: matchingContexts, label: notices.matchingContexts, css_class: 'column-matchingContexts' }
3638
- { property: message, label: notices.message, css_class: 'column-message' }
39+
- { property: relayersCount, label: notices.relayers, css_class: 'column-relayers' }
3740
- { property: visibility, label: notices.visibility, css_class: 'column-visibility' }
38-
- { property: matchingContexts, label: notices.matchingContexts, css_class: 'column-matchingContexts' }
39-
- { property: badgedRatingCount, label: notices.badgedRatingCount, css_class: 'graphable column-badgedRatingCount' }
40-
- { property: displayedRatingCount, label: notices.displayedRatingCount, css_class: 'graphable column-displayedRatingCount' }
41-
- { property: unfoldedRatingCount, label: notices.unfoldedRatingCount, css_class: 'graphable column-unfoldedRatingCount' }
42-
- { property: clickedRatingCount, label: notices.clickedRatingCount, css_class: 'graphable column-clickedRatingCount' }
43-
- { property: likedRatingCount, label: notices.likedRatingCount, css_class: 'graphable column-likedRatingCount' }
44-
- { property: dislikedRatingCount, label: notices.dislikedRatingCount, css_class: 'graphable column-dislikedRatingCount' }
45-
- { property: dismissedRatingCount, label: notices.dismissedRatingCount, css_class: 'graphable column-dismissedRatingCount' }
41+
- { property: badgedCount, label: notices.badgedCount, css_class: 'graphable column-badgedCount' }
42+
- { property: displayedCount, label: notices.displayedCount, css_class: 'graphable column-displayedCount' }
43+
- { property: unfoldedCount, label: notices.unfoldedCount, css_class: 'graphable column-unfoldedCount' }
44+
- { property: clickedCount, label: notices.clickedCount, css_class: 'graphable column-clickedCount' }
45+
- { property: likedCount, label: notices.likedCount, css_class: 'graphable column-likedCount' }
46+
- { property: dislikedCount, label: notices.dislikedCount, css_class: 'graphable column-dislikedCount' }
47+
- { property: dismissedCount, label: notices.dismissedCount, css_class: 'graphable column-dismissedCount' }
4648
- { property: locale, label: contributors.locale }
4749
- { property: expires, label: notices.expires, css_class: 'column-expires' }
4850
- { property: created, label: notices.created, css_class: 'column-created' }

config/packages/messenger.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
framework:
2+
messenger:
3+
# Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
4+
# failure_transport: failed
5+
6+
transports:
7+
# https://symfony.com/doc/current/messenger.html#transport-configuration
8+
# async: '%env(MESSENGER_TRANSPORT_DSN)%'
9+
# failed: 'doctrine://default?queue_name=failed'
10+
# sync: 'sync://'
11+
12+
routing:
13+
# Route your messages to the transports
14+
# 'App\Message\YourMessage': async

migrations/Version20210704210627.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Application\Migrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20210704210627 extends AbstractMigration
14+
{
15+
public function up(Schema $schema): void
16+
{
17+
$this->addSql('ALTER TABLE notice
18+
ADD COLUMN badged_count int DEFAULT 0 NOT NULL,
19+
ADD COLUMN displayed_count int DEFAULT 0 NOT NULL,
20+
ADD COLUMN unfolded_count int DEFAULT 0 NOT NULL,
21+
ADD COLUMN clicked_count int DEFAULT 0 NOT NULL,
22+
ADD COLUMN liked_count int DEFAULT 0 NOT NULL,
23+
ADD COLUMN disliked_count int DEFAULT 0 NOT NULL,
24+
ADD COLUMN dismissed_count int DEFAULT 0 NOT NULL;
25+
');
26+
27+
$this->addSql('CREATE INDEX rating_notice_type_index ON rating (notice_id, type);');
28+
29+
$this->addSql('UPDATE notice n
30+
SET n.badged_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'badge\'),
31+
n.displayed_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'display\'),
32+
n.unfolded_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'unfold\'),
33+
n.clicked_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'outbound-click\'),
34+
n.liked_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'like\') - (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'unlike\'),
35+
n.disliked_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'dislike\') - (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'undislike\'),
36+
n.dismissed_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'dismiss\') - (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'undismiss\');
37+
');
38+
}
39+
40+
public function down(Schema $schema): void
41+
{
42+
$this->addSql('ALTER TABLE rating DROP INDEX rating_notice_type_index');
43+
44+
$this->addSql('ALTER TABLE notice
45+
DROP COLUMN badged_count,
46+
DROP COLUMN displayed_count,
47+
DROP COLUMN unfolded_count,
48+
DROP COLUMN clicked_count,
49+
DROP COLUMN liked_count,
50+
DROP COLUMN disliked_count,
51+
DROP COLUMN dismissed_count
52+
');
53+
}
54+
}

src/Controller/AdminApiController.php

-47
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
POST http://localhost:8088/api/v3/notices/1028/ratings
2+
Content-Type: application/json
3+
4+
{
5+
"ratingType": "badge"
6+
}
7+
8+
9+
###

src/Controller/Api/V3/PostNoticeRatingAction.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Controller\Api\V3;
66

77
use App\Entity\Rating;
8+
use App\Message\NoticeRated;
89
use App\Repository\NoticeRepository;
910
use App\Serializer\V3\NormalizerOptions;
1011
use Doctrine\ORM\EntityManagerInterface;
@@ -14,6 +15,7 @@
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1617
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
18+
use Symfony\Component\Messenger\MessageBusInterface;
1719
use Symfony\Component\Serializer\SerializerInterface;
1820

1921
class PostNoticeRatingAction extends BaseAction
@@ -39,7 +41,7 @@ public function __construct(SerializerInterface $serializer, NoticeRepository $n
3941
* @Route("/notices/{id}/ratings")
4042
* @Method("POST")
4143
*/
42-
public function __invoke(Request $request): JsonResponse
44+
public function __invoke(Request $request, MessageBusInterface $bus): JsonResponse
4345
{
4446
$id = $request->get('id', null);
4547
$notice = $this->noticeRepository->getOne((int) $id);
@@ -60,6 +62,8 @@ public function __invoke(Request $request): JsonResponse
6062
$this->entityManager->persist($rating);
6163
$this->entityManager->flush();
6264

65+
$bus->dispatch(new NoticeRated($notice));
66+
6367
return new JsonResponse('', 204, [], true);
6468
}
6569
}

src/DataFixtures/NoticeFixtures.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function load(ObjectManager $manager): void
2929
<a href="http://link.com?foo=bar">foo</a>
3030
with https://bulles.fr.');
3131
$notice->setVisibility(NoticeVisibility::PUBLIC_VISIBILITY());
32+
$notice->setLikedCount(2);
3233
$this->addReference('notice_type_ecology', $notice);
3334
$manager->persist($notice);
3435

0 commit comments

Comments
 (0)