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
Add statistic service and increment usage count #75
Conversation
src/Application/SlackApplication.php
Outdated
@@ -90,6 +93,8 @@ public function sendAdminMessage(SecretSanta $secretSanta, string $code, string | |||
|
|||
public function finish(SecretSanta $secretSanta) | |||
{ | |||
$this->statistic->incrementUsageCount(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this logic where the finish
method is called instead of duplicating the call and the dependency in each application.
README.md
Outdated
@@ -28,6 +28,7 @@ Then launch this command: | |||
|
|||
The application should now be running on http://127.0.0.1:8000. | |||
|
|||
Tests are made with PHPUnit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing dot
src/Statistic.php
Outdated
{ | ||
/** | ||
* @var Client | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this phpdoc 😉
src/Statistic.php
Outdated
public function incrementUsageCount() | ||
{ | ||
//If the key does not exist, it is set to 0 before performing the operation | ||
$this->client->incr('usageCount'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also add a monthly/yearly counter
config/services.yaml
Outdated
@@ -37,6 +37,9 @@ services: | |||
- '@JoliCode\SecretSanta\Application\SlackApplication' | |||
- '@JoliCode\SecretSanta\Application\DiscordApplication' | |||
|
|||
Jolicode\SecretSanta\Statistics: | |||
public: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Il y a un niveau d'indentation de trop il me semble
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Are you sure it needs to be public ?
- Are you sure you need to declare it ? (because there is autodiscovery in this project)
/** | ||
* @var Statistic | ||
*/ | ||
private $statistic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pas besoin de PHPDoc ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:old:
config/services.yaml
Outdated
@@ -37,6 +37,9 @@ services: | |||
- '@JoliCode\SecretSanta\Application\SlackApplication' | |||
- '@JoliCode\SecretSanta\Application\DiscordApplication' | |||
|
|||
Jolicode\SecretSanta\Statistics: | |||
public: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Are you sure it needs to be public ?
- Are you sure you need to declare it ? (because there is autodiscovery in this project)
src/Statistic.php
Outdated
|
||
use Predis\Client; | ||
|
||
class Statistic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name does not really reflect the intent.
I would name it StatisticCollector
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Top 👍
src/Controller/SantaController.php
Outdated
@@ -31,13 +32,15 @@ class SantaController extends AbstractController | |||
private $twig; | |||
private $logger; | |||
private $applications; | |||
private $statistic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
statisticCollector
(Consistency is really important - everywhere, everytime)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done !
src/StatisticCollector.php
Outdated
$currentMonth = date('m'); | ||
|
||
//If the key does not exist, it is set to 0 before performing the operation | ||
$this->client->hincrby('date:' . $currentYear . '-' . $currentMonth, 'usageCount', 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"date: $currentYear - $currentMonth"
is more readable than concatenation (same on next line)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the change but I need that there is no space so it looks like "date:$currentYear-$currentMonth"
src/Controller/ContentController.php
Outdated
{ | ||
$statisticValues = $this->statisticCollector->getCounterValues(); | ||
|
||
// dump($statisticValues); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dump to remove 🙂
src/Controller/SantaController.php
Outdated
@@ -50,6 +55,8 @@ public function run(MessageDispatcher $messageDispatcher, Rudolph $rudolph, Requ | |||
|
|||
$allUsers = $application->getUsers(); | |||
|
|||
dump($clientApplication); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to remove
src/Controller/SantaController.php
Outdated
} | ||
|
||
public function run(MessageDispatcher $messageDispatcher, Rudolph $rudolph, Request $request, string $application): Response | ||
{ | ||
$clientApplication = $application; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why renaming this var?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I delete the $clientApplication
variable and I use $application->getCode
instead
src/StatisticCollector.php
Outdated
$currentMonth = date('m'); | ||
|
||
//If the key does not exist, it is set to 0 before performing the operation | ||
$this->client->hincrby('date:$currentYear-$currentMonth', 'usageCount', 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doest it really work with single quote?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my mistake, It's done now
src/StatisticCollector.php
Outdated
|
||
public function getCounterValues() | ||
{ | ||
$counterByDate = $this->getCounter(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return the counter without storing into a temp variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
templates/content/stats.html.twig
Outdated
<div class="graph-noData">Sorry, there is no statistic for this section 🙈</div> | ||
{% endif %} | ||
{% if statisticValues.hashesMonth is not empty %} | ||
{% set values = statisticValues.hashesMonth %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation issue
templates/content/stats.html.twig
Outdated
{% else %} | ||
<div class="graph-noData">Sorry, there is no statistic for this section 🙈</div> | ||
{% endif %} | ||
{% if statisticValues.hashesMonth is not empty %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{% if statisticValues.hashesMonth %} is enought
src/Controller/ContentController.php
Outdated
$content = $this->twig->render('content/stats.html.twig', [ | ||
'statisticValues' => $statisticValues, | ||
'statisticValues' => $this->statisticCollector->getCounters(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
naming matching: values VS counter
(consistancy)
src/StatisticCollector.php
Outdated
$this->client = $client; | ||
} | ||
|
||
public function incrementUsageCount($clientApplication) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a string
typehint and name the parameter application
or applicationCode
to be accurate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done with applicationCode
name
src/StatisticCollector.php
Outdated
$megaHashes = []; | ||
|
||
$allHashes = []; | ||
$allHashes['hashesTotal'] = $this->client->keys('date:total*'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would simply name the keys total
, year
, month
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it, it's clearer
templates/content/stats.html.twig
Outdated
{% if statisticsCounter.hashesYear %} | ||
{% set values = statisticsCounter.hashesYear %} | ||
{% for value, key in values %} | ||
{% if value matches '/date:\\d+$/' %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need this condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This serves to check if there is any stats.
If not it shows some message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the condition.
templates/content/stats.html.twig
Outdated
<div class="chart chart-month"> | ||
{% if statisticsCounter.month %} | ||
{% set values = statisticsCounter.month %} | ||
{% for value, key in values %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the naming seeming really odd to me. the regular syntaxt of for is for key, value in values
. Why are you using the opposite ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, values
is a really bad name. It does not mean anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW I don't think you need an additional variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I made a mistake.
I change the name of key
, value
and values
src/StatisticCollector.php
Outdated
$this->client->hincrby("date:total-$ApplicationCode", 'usageCount', 1); | ||
} | ||
|
||
public function getCounters() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add return typehint
templates/content/stats.html.twig
Outdated
</div> | ||
</div> | ||
|
||
{% endblock content %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing return line (you can configure your IDE to automatically fix that)
This commit fixes the issue #11
I choose to add incrementing at the finish function so the incrementUsageCount method needs to be add to all Apps (i.e. Slack, Discord, ...)