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

Add statistic service and increment usage count #75

Merged
merged 10 commits into from Oct 5, 2018
Merged

Conversation

alexzerah
Copy link
Contributor

@alexzerah alexzerah commented Oct 2, 2018

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, ...)

pyrech
pyrech previously requested changes Oct 2, 2018
@@ -90,6 +93,8 @@ public function sendAdminMessage(SecretSanta $secretSanta, string $code, string

public function finish(SecretSanta $secretSanta)
{
$this->statistic->incrementUsageCount();
Copy link
Member

@pyrech pyrech Oct 2, 2018

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing dot

{
/**
* @var Client
*/
Copy link
Member

@pyrech pyrech Oct 2, 2018

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 😉

public function incrementUsageCount()
{
//If the key does not exist, it is set to 0 before performing the operation
$this->client->incr('usageCount');
Copy link
Member

@pyrech pyrech Oct 2, 2018

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

@@ -37,6 +37,9 @@ services:
- '@JoliCode\SecretSanta\Application\SlackApplication'
- '@JoliCode\SecretSanta\Application\DiscordApplication'

Jolicode\SecretSanta\Statistics:
public: true
Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Are you sure it needs to be public ?
  2. Are you sure you need to declare it ? (because there is autodiscovery in this project)

/**
* @var Statistic
*/
private $statistic;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pas besoin de PHPDoc ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:old:

@@ -37,6 +37,9 @@ services:
- '@JoliCode\SecretSanta\Application\SlackApplication'
- '@JoliCode\SecretSanta\Application\DiscordApplication'

Jolicode\SecretSanta\Statistics:
public: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Are you sure it needs to be public ?
  2. Are you sure you need to declare it ? (because there is autodiscovery in this project)


use Predis\Client;

class Statistic
Copy link
Member

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

Copy link
Member

@pyrech pyrech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top 👍

@@ -31,13 +32,15 @@ class SantaController extends AbstractController
private $twig;
private $logger;
private $applications;
private $statistic;
Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

$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);
Copy link
Member

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)

Copy link
Contributor Author

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"

{
$statisticValues = $this->statisticCollector->getCounterValues();

// dump($statisticValues);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dump to remove 🙂

@@ -50,6 +55,8 @@ public function run(MessageDispatcher $messageDispatcher, Rudolph $rudolph, Requ

$allUsers = $application->getUsers();

dump($clientApplication);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to remove

}

public function run(MessageDispatcher $messageDispatcher, Rudolph $rudolph, Request $request, string $application): Response
{
$clientApplication = $application;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why renaming this var?

Copy link
Contributor Author

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

$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);
Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

Copy link
Contributor Author

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


public function getCounterValues()
{
$counterByDate = $this->getCounter();
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<div class="graph-noData">Sorry, there is no statistic for this section 🙈</div>
{% endif %}
{% if statisticValues.hashesMonth is not empty %}
{% set values = statisticValues.hashesMonth %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation issue

{% else %}
<div class="graph-noData">Sorry, there is no statistic for this section 🙈</div>
{% endif %}
{% if statisticValues.hashesMonth is not empty %}
Copy link
Member

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

$content = $this->twig->render('content/stats.html.twig', [
'statisticValues' => $statisticValues,
'statisticValues' => $this->statisticCollector->getCounters(),
Copy link
Member

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)

$this->client = $client;
}

public function incrementUsageCount($clientApplication)
Copy link
Member

@pyrech pyrech Oct 5, 2018

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?

Copy link
Contributor Author

@alexzerah alexzerah Oct 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done with applicationCode name

$megaHashes = [];

$allHashes = [];
$allHashes['hashesTotal'] = $this->client->keys('date:total*');
Copy link
Member

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

Copy link
Contributor Author

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

{% if statisticsCounter.hashesYear %}
{% set values = statisticsCounter.hashesYear %}
{% for value, key in values %}
{% if value matches '/date:\\d+$/' %}
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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.

<div class="chart chart-month">
{% if statisticsCounter.month %}
{% set values = statisticsCounter.month %}
{% for value, key in values %}
Copy link
Member

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 ?

Copy link
Member

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.

Copy link
Member

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

Copy link
Contributor Author

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

$this->client->hincrby("date:total-$ApplicationCode", 'usageCount', 1);
}

public function getCounters()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add return typehint

</div>
</div>

{% endblock content %}
Copy link
Member

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)

@damienalexandre damienalexandre merged commit ffc7b38 into master Oct 5, 2018
@damienalexandre damienalexandre deleted the feature/11 branch October 5, 2018 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants