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

feat(pages): patrons page #317

Merged
merged 16 commits into from
Nov 22, 2023
Merged

feat(pages): patrons page #317

merged 16 commits into from
Nov 22, 2023

Conversation

wJoenn
Copy link
Collaborator

@wJoenn wJoenn commented Nov 12, 2023

Summary of changes and context

closes #304

Can probably be refactored a bit but I'm proud of that one query to get everything 👀

    @users = User
             .select("users.uid AS uid, users.username AS username, COUNT(referees.id) AS referrals")
             .select("CASE WHEN COUNT(referees.id) > 0 THEN CEIL(100 * LN(4)) ELSE 0 END AS aura")
             .joins("LEFT JOIN users referees ON users.id = referees.referrer_id")
             .group("users.id")
             .order(aura: :desc)

A User::aura method has been added to the model in case we wanna access the aura somewhere else at some point

  def self.aura(referrals)
    (100 * Math.log(1 + referrals, Math::E)).ceil
  end

I made it a class method because the #order(aura: :desc) in the afore mentioned Active::Record query would call the User#aura instead of the SELECT aura otherwise for some reason 🤔
I've trying doing #order("aura DESC") but it was doing the same.

The introduction text for the /patrons page is missing atm, waiting for @pil0u
image

Sanity checks

  • Linters pass
  • Tests pass
  • Related GitHub issues are linked in the description

@pil0u
Copy link
Owner

pil0u commented Nov 15, 2023

Je review vendredi 👀

Copy link
Collaborator

@Aquaj Aquaj left a comment

Choose a reason for hiding this comment

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

Manque des specs. (Ça aurait chopé l'erreur du LN(4) par exemple).

Comment on lines 56 to 61
@users = User
.select("users.uid AS uid, users.username AS username, COUNT(referees.id) AS referrals")
.select("CASE WHEN COUNT(referees.id) > 0 THEN CEIL(100 * LN(4)) ELSE 0 END AS aura")
.joins("LEFT JOIN users referees ON users.id = referees.referrer_id")
.group("users.id")
.order(aura: :desc)
Copy link
Collaborator

Choose a reason for hiding this comment

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

La logique de calcul de l'aura n'a pas sa place dans le controller. Plutôt à mettre dans une classe dédiée, ou au pire dans le modèle.

Copy link
Collaborator Author

@wJoenn wJoenn Nov 22, 2023

Choose a reason for hiding this comment

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

@Aquaj Je suis en train de reflechir a la remarque que tu fais et je comprend mais j'ai pas d'idée sur comment appliquer ca d'une meilleure facon sans perdre en performance.

La ce que la query me permet de faire c'est récupéré, en une seule query SQL, tous les Users, avec le nombre de referees qu'ils ont, et leur aura.

SELECT
  users.uid AS uid,
  users.username AS username,
  COUNT(referees.id) AS referrals,
  CASE WHEN COUNT(referees.id) > 0 THEN CEIL(100 * LN(COUNT(referees.id) + 1)) ELSE 0 END AS aura
FROM "users"
LEFT JOIN users referees ON users.id = referees.referrer_id
GROUP BY "users"."id"
ORDER BY "aura" DESC

J'ai l'impression que isolé le calcul de l'aura va nous forcer a répéter plusieurs queries au leiu d'en avoir qu'une seule comme ici.
Avec plus de 1000 users queried a chaques fois on a un besoin de faire au plus performant sur cette requete je penses, et donc de la maintenant en un bloque comme ici

app/controllers/pages_controller.rb Outdated Show resolved Hide resolved
app/models/user.rb Outdated Show resolved Hide resolved
@wJoenn
Copy link
Collaborator Author

wJoenn commented Nov 20, 2023

@Aquaj C'est pour ca qu'on fait +1 dans la formule non ?

(100 * Math.log(1 + referrals, Math::E)).ceil

Comme ca si tu as 1 referee alors tu as 100 * ln(1 + 1)

@Aquaj
Copy link
Collaborator

Aquaj commented Nov 20, 2023

Tout à fait. Elle manque juste à l'implem SQL en place. En allant revoir l'issue j'ai vu que le +1 était bien là donc j'ai supprimé mon message (j'avais espoir que personne l'ait vu 😄) pour aller édit ma review à la place.

@wJoenn wJoenn force-pushed the feat/pages/patrons branch from 1f0d6f2 to 381265e Compare November 22, 2023 19:15
Procfile.dev Outdated Show resolved Hide resolved
@pil0u pil0u force-pushed the feat/pages/patrons branch from 5c7264c to d9b1b2f Compare November 22, 2023 21:35
@pil0u pil0u requested a review from Aquaj November 22, 2023 22:17
@pil0u pil0u merged commit f48e0a3 into main Nov 22, 2023
5 checks passed
@pil0u pil0u deleted the feat/pages/patrons branch November 22, 2023 22:42
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.

Parrainage : Créer la page de leaderboard des parrains
3 participants