Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf : Save ratings count in notices entities #408

Merged
merged 1 commit into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
perf/save_ratings_count_in_notices_entities
  • Loading branch information
JalilArfaoui committed Jul 6, 2021
commit 560429a0c282eae1931a069aacce223098f44e2a
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=275fee27732fdd71221f5cbe115f889d
###< lexik/jwt-authentication-bundle ###

###> symfony/messenger ###
# Choose one of the transports below
# MESSENGER_TRANSPORT_DSN=doctrine://default
# MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages
# MESSENGER_TRANSPORT_DSN=redis://localhost:6379/messages
###< symfony/messenger ###
27 changes: 14 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,39 @@
"symfony/validator": "4.4.*",
"symfony/twig-bundle": "4.4.*",
"symfony/yaml": "4.4.*",
"symfony/apache-pack": "^1.0",
"symfony/expression-language": "4.4.*",
"symfony/proxy-manager-bridge": "4.4.*",
"symfony/property-access": "4.4.*",
"symfony/property-info": "4.4.*",
"symfony/serializer": "4.4.*",
"symfony/messenger": "4.4.*",
"doctrine/annotations": "^1.0",
"doctrine/orm": "^2.3",
"doctrine/doctrine-bundle": "^1.2",
"doctrine/doctrine-cache-bundle": "^1.4",
"doctrine/doctrine-migrations-bundle": "^v1.3.2",
"twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0",
"sensio/framework-extra-bundle": "^5.6",
"friendsofsymfony/user-bundle": "^2.1.2",
"friendsofsymfony/jsrouting-bundle": "^2.6",
"beberlei/doctrineextensions": "^1.2",
"knplabs/doctrine-behaviors": "1.4",
"api-platform/core": "^2.6",
"easycorp/easyadmin-bundle": "^2",
"composer/package-versions-deprecated": "^1.11",
"ocramius/proxy-manager": "^2.0.2",
"marc-mabe/php-enum": "^2.2",
"beberlei/doctrineextensions": "^1.2",
"vich/uploader-bundle": "^1.7",
"liip/imagine-bundle": "^2.0",
"youthweb/urllinker": "^1.2",
"sentry/sentry-symfony": "^3.0",
"knplabs/doctrine-behaviors": "1.4",
"nelmio/cors-bundle": "1.5.6",
"league/uri": "^6.2",
"league/uri-components": "^2.2",
"composer/package-versions-deprecated": "^1.11",
"twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0",
"symfony/apache-pack": "^1.0",
"ezyang/htmlpurifier": "^4.13",
"api-platform/core": "^2.6",
"symfony/expression-language": "4.4.*",
"symfony/proxy-manager-bridge": "4.4.*",
"doctrine/annotations": "^1.0",
"phpdocumentor/reflection-docblock": "^5.2",
"symfony/property-access": "4.4.*",
"symfony/property-info": "4.4.*",
"symfony/serializer": "4.4.*",
"lexik/jwt-authentication-bundle": "^2.11"
},
"require-dev": {
Expand Down
91 changes: 89 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions config/packages/easy_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ easy_admin:
- 'js/message_validator.js'
- 'js/filter_table_row.js'
- 'js/select2.js'
formats:
datetime: 'd/m/y'
entities:
Notice:
class: App\Entity\Notice
Expand All @@ -32,17 +34,17 @@ easy_admin:
fields: &noticeListFields
- { property: id, label: notices.id, css_class: 'column-id' }
- { property: contributor, label: notices.contributor, css_class: 'column-contributor' }
- { property: relayersCount, label: notices.relayers, css_class: 'column-relayers' }
- { property: matchingContexts, label: notices.matchingContexts, css_class: 'column-matchingContexts' }
- { property: message, label: notices.message, css_class: 'column-message' }
- { property: relayersCount, label: notices.relayers, css_class: 'column-relayers' }
- { property: visibility, label: notices.visibility, css_class: 'column-visibility' }
- { property: matchingContexts, label: notices.matchingContexts, css_class: 'column-matchingContexts' }
- { property: badgedRatingCount, label: notices.badgedRatingCount, css_class: 'graphable column-badgedRatingCount' }
- { property: displayedRatingCount, label: notices.displayedRatingCount, css_class: 'graphable column-displayedRatingCount' }
- { property: unfoldedRatingCount, label: notices.unfoldedRatingCount, css_class: 'graphable column-unfoldedRatingCount' }
- { property: clickedRatingCount, label: notices.clickedRatingCount, css_class: 'graphable column-clickedRatingCount' }
- { property: likedRatingCount, label: notices.likedRatingCount, css_class: 'graphable column-likedRatingCount' }
- { property: dislikedRatingCount, label: notices.dislikedRatingCount, css_class: 'graphable column-dislikedRatingCount' }
- { property: dismissedRatingCount, label: notices.dismissedRatingCount, css_class: 'graphable column-dismissedRatingCount' }
- { property: badgedCount, label: notices.badgedCount, css_class: 'graphable column-badgedCount' }
- { property: displayedCount, label: notices.displayedCount, css_class: 'graphable column-displayedCount' }
- { property: unfoldedCount, label: notices.unfoldedCount, css_class: 'graphable column-unfoldedCount' }
- { property: clickedCount, label: notices.clickedCount, css_class: 'graphable column-clickedCount' }
- { property: likedCount, label: notices.likedCount, css_class: 'graphable column-likedCount' }
- { property: dislikedCount, label: notices.dislikedCount, css_class: 'graphable column-dislikedCount' }
- { property: dismissedCount, label: notices.dismissedCount, css_class: 'graphable column-dismissedCount' }
- { property: locale, label: contributors.locale }
- { property: expires, label: notices.expires, css_class: 'column-expires' }
- { property: created, label: notices.created, css_class: 'column-created' }
Expand Down
14 changes: 14 additions & 0 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
framework:
messenger:
# Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
# failure_transport: failed

transports:
# https://symfony.com/doc/current/messenger.html#transport-configuration
# async: '%env(MESSENGER_TRANSPORT_DSN)%'
# failed: 'doctrine://default?queue_name=failed'
# sync: 'sync://'

routing:
# Route your messages to the transports
# 'App\Message\YourMessage': async
54 changes: 54 additions & 0 deletions migrations/Version20210704210627.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210704210627 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE notice
ADD COLUMN badged_count int DEFAULT 0 NOT NULL,
ADD COLUMN displayed_count int DEFAULT 0 NOT NULL,
ADD COLUMN unfolded_count int DEFAULT 0 NOT NULL,
ADD COLUMN clicked_count int DEFAULT 0 NOT NULL,
ADD COLUMN liked_count int DEFAULT 0 NOT NULL,
ADD COLUMN disliked_count int DEFAULT 0 NOT NULL,
ADD COLUMN dismissed_count int DEFAULT 0 NOT NULL;
');

$this->addSql('CREATE INDEX rating_notice_type_index ON rating (notice_id, type);');

$this->addSql('UPDATE notice n
SET n.badged_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'badge\'),
n.displayed_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'display\'),
n.unfolded_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'unfold\'),
n.clicked_count = (SELECT COUNT(*) FROM rating r WHERE r.notice_id = n.id AND r.type = \'outbound-click\'),
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\'),
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\'),
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\');
');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE rating DROP INDEX rating_notice_type_index');

$this->addSql('ALTER TABLE notice
DROP COLUMN badged_count,
DROP COLUMN displayed_count,
DROP COLUMN unfolded_count,
DROP COLUMN clicked_count,
DROP COLUMN liked_count,
DROP COLUMN disliked_count,
DROP COLUMN dismissed_count
');
}
}
47 changes: 0 additions & 47 deletions src/Controller/AdminApiController.php

This file was deleted.

9 changes: 9 additions & 0 deletions src/Controller/Api/V3/PostNoticeRating.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
POST http://localhost:8088/api/v3/notices/1028/ratings
Content-Type: application/json

{
"ratingType": "badge"
}


###
6 changes: 5 additions & 1 deletion src/Controller/Api/V3/PostNoticeRatingAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Controller\Api\V3;

use App\Entity\Rating;
use App\Message\NoticeRated;
use App\Repository\NoticeRepository;
use App\Serializer\V3\NormalizerOptions;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -14,6 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Serializer\SerializerInterface;

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

$bus->dispatch(new NoticeRated($notice));

return new JsonResponse('', 204, [], true);
}
}
1 change: 1 addition & 0 deletions src/DataFixtures/NoticeFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function load(ObjectManager $manager): void
<a href="http://link.com?foo=bar">foo</a>
with https://bulles.fr.');
$notice->setVisibility(NoticeVisibility::PUBLIC_VISIBILITY());
$notice->setLikedCount(2);
$this->addReference('notice_type_ecology', $notice);
$manager->persist($notice);

Expand Down
Loading