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 leaderboard by campaign #100

Merged
merged 7 commits into from
May 21, 2024
Merged

Add leaderboard by campaign #100

merged 7 commits into from
May 21, 2024

Conversation

moisses89
Copy link
Member

  • Boost for the endpoint is calculated by Sum(total_boosted_points) for all Periods / Sum(total_points) for all Periods

Closes #83

@moisses89 moisses89 requested a review from a team as a code owner May 16, 2024 10:20
@coveralls
Copy link

coveralls commented May 16, 2024

Pull Request Test Coverage Report for Build 9155957672

Details

  • 157 of 157 (100.0%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.8%) to 90.346%

Totals Coverage Status
Change from base Build 9108639077: 0.8%
Covered Lines: 1750
Relevant Lines: 1937

💛 - Coveralls

@moisses89 moisses89 marked this pull request as draft May 16, 2024 10:23
@moisses89 moisses89 force-pushed the add_leaderboard_by_campaign branch from 844d162 to 73594d8 Compare May 16, 2024 10:24
@moisses89 moisses89 self-assigned this May 16, 2024
@moisses89 moisses89 marked this pull request as ready for review May 16, 2024 12:33
ON ("campaigns_period"."campaign_id" = "campaigns_campaign"."id")
WHERE "campaigns_campaign"."uuid" = %s::uuid
GROUP BY "campaigns_activity"."address"
ORDER BY 3 DESC) AS LEADERTABLE where address = %s;
Copy link
Contributor

Choose a reason for hiding this comment

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

ORDER BY 3 is referring to total_campaign_boosted_points right? Can we use the name instead as it seems to be a better reference (also if we shift the select clauses around).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 15050c3

(SELECT "campaigns_activity"."address",
SUM("campaigns_activity"."total_points") AS "total_campaign_points",
SUM("campaigns_activity"."total_boosted_points") AS "total_campaign_boosted_points",
(SUM("campaigns_activity"."total_boosted_points") / SUM("campaigns_activity"."total_points")) AS "last_boost",
Copy link
Contributor

Choose a reason for hiding this comment

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

This can potentially cause a division by zero right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we can fix it doing it in the serializer:
#100 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 15050c3

(SELECT "campaigns_activity"."address",
SUM("campaigns_activity"."total_points") AS "total_campaign_points",
SUM("campaigns_activity"."total_boosted_points") AS "total_campaign_boosted_points",
(SUM("campaigns_activity"."total_boosted_points") / SUM("campaigns_activity"."total_points")) AS "last_boost",
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: This is the boost multiplier right? What about renaming last_boost to boost_multiplier

(SELECT "campaigns_activity"."address",
SUM("campaigns_activity"."total_points") AS "total_campaign_points",
SUM("campaigns_activity"."total_boosted_points") AS "total_campaign_boosted_points",
(SUM("campaigns_activity"."total_boosted_points") / SUM("campaigns_activity"."total_points")) AS "last_boost",
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it's worth to incur on the multiplier boost calculation on the query side (versus client side). What's your opinion on this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, we can remove it from query and do it in the serializer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 15050c3

:return:
"""

query = """
Copy link
Contributor

Choose a reason for hiding this comment

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

We should consider the use case where you want to get the data for a single address (which still requires doing all the sum computation on the points and boosted points) but without the ranking.

Maybe we should split this query into two. The "core" query would be:

SELECT
    activities."address",
    SUM(activities."total_points") AS "total_campaign_points",
    SUM(activities."total_boosted_points") AS "total_campaign_boosted_points",
FROM "campaigns_activity" activities
INNER JOIN "campaigns_period" periods ON activities."period_id" = periods."id"
INNER JOIN "campaigns_campaign" campaigns ON periods."campaign_id" = campaigns."id"
WHERE campaigns."uuid" = %s::uuid
GROUP BY activities."address"

Then the leaderboard query would use this one to compute the ranks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Different endpoint?
We have currently campaigns/{campaignID}/leaderboard/{holder} that returns the mentioned data with the position, because is necessary to share with the client that this address is in that position of the leaderboard.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok. I didn't know that the ranking would have to be part of the response for the single "holder" but it makes sense 👍

@@ -13,4 +13,14 @@
views.RetrieveCampaignView.as_view(),
name="retrieve-campaign",
),
path(
"campaigns/<str:resource_id>/leaderboard/",
Copy link
Contributor

Choose a reason for hiding this comment

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

The campaigns.urls is already prefixed with campaigns/ so we should remove this part of the path.

safe_locking_service/campaigns/views.py Outdated Show resolved Hide resolved
@moisses89 moisses89 requested a review from fmrsabino May 20, 2024 08:37
@moisses89 moisses89 merged commit 0d725b3 into main May 21, 2024
5 checks passed
@moisses89 moisses89 deleted the add_leaderboard_by_campaign branch May 21, 2024 07:19
@github-actions github-actions bot locked and limited conversation to collaborators May 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add leaderboard endpoint by campaign
3 participants