-
Notifications
You must be signed in to change notification settings - Fork 4
824 Fix currency and league issues #827
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
Merged
bogadisa
merged 16 commits into
main
from
824-optimize-downtime-between-data-retrieval-cycles
Jul 18, 2026
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
911e5ef
#824 Maybe working once per hour currency update
bogadisa 9b8c49c
Merge branch 'main' of https://github.com/Path-of-Modifiers/pathofmod…
bogadisa 84f5e9c
Working new league table and migration, no backend data retrieval
bogadisa acb3513
Added league api endpoint and valid to/from
bogadisa 983326e
#824 Added league data deposit and changed active league api route
bogadisa c22eea9
#824 Small bug fix for update league
bogadisa 8bb755a
Fixed unidentified items and cleaned up thread exiting
bogadisa f390d9d
Fixed tests, added tests for league
bogadisa 2dca75a
Fixed plotting (at least tests), still missing frontend
bogadisa 1905f83
#824 Updated the frontend api
bogadisa d36e38d
#824 Restructured frontend using leagues from database
bogadisa 57c3bbe
#824 Removed league .env backend and fixed different hours per league
bogadisa eb2252f
#824 Backend changes
bogadisa 119c63f
#824 Frontend changes
bogadisa 0417cf5
#824 Fixed format of latest currencies
bogadisa 0e260a7
#824 Fixed problem with aggregating items
bogadisa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 267 additions & 0 deletions
267
src/backend_api/app/alembic/versions/965e766db0a0_added_league_table.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| """Added league table | ||
|
|
||
| Revision ID: 965e766db0a0 | ||
| Revises: 17daa1c96438 | ||
| Create Date: 2026-06-30 15:49:45.173827 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
| from app.alembic.replaceable_objects.main import ReplaceableTrigger | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "965e766db0a0" | ||
| down_revision: Union[str, None] = "17daa1c96438" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
| unidentified_aggregation_trigger_old = ReplaceableTrigger( | ||
| "aggregate_unidentified", | ||
| "unidentified_item", | ||
| """ | ||
| RETURNS TRIGGER AS ${name}$ | ||
| DECLARE | ||
| current_hour INT; | ||
| divine_id INT; | ||
| divine_value FLOAT; | ||
| BEGIN | ||
| IF EXISTS (SELECT 1 FROM unidentified_item AS unid WHERE unid."createdHoursSinceLaunch" = NEW."createdHoursSinceLaunch") THEN | ||
| RETURN NEW; | ||
| END IF; | ||
|
|
||
| current_hour := (SELECT MAX(unid."createdHoursSinceLaunch") FROM unidentified_item AS unid); | ||
| divine_id := (SELECT MAX(cur."currencyId") FROM currency AS cur WHERE cur."tradeName"='divine'); | ||
| divine_value := (SELECT cur."valueInChaos" FROM currency AS cur WHERE cur."currencyId"=divine_id); | ||
|
|
||
|
|
||
| WITH aggregates AS ( | ||
| SELECT name, unid."itemBaseTypeId", unid."createdHoursSinceLaunch", league, ilvl, stddev(unid."currencyAmount" * cur."valueInChaos") AS std, AVG(unid."currencyAmount" * cur."valueInChaos") AS calc_avg, COUNT(unid."itemId") AS calc_count | ||
| FROM unidentified_item AS unid | ||
| NATURAL JOIN currency as cur | ||
| WHERE unid."createdHoursSinceLaunch" = current_hour | ||
| AND NOT aggregated | ||
| GROUP BY name, unid."itemBaseTypeId", unid."createdHoursSinceLaunch", league, ilvl | ||
| ), affected_item_ids AS ( | ||
| SELECT unid."itemId" FROM unidentified_item AS unid WHERE NOT aggregated AND unid."createdHoursSinceLaunch"=current_hour | ||
| ) | ||
|
|
||
| INSERT INTO unidentified_item (name, "itemBaseTypeId", "createdHoursSinceLaunch", league, "currencyId", ilvl, "currencyAmount", "nItems", identified, rarity, aggregated) | ||
| SELECT name, unid."itemBaseTypeId", unid."createdHoursSinceLaunch", league, divine_id, ilvl, AVG(unid."currencyAmount" * cur."valueInChaos") / divine_value, calc_count, identified, rarity, TRUE | ||
| FROM unidentified_item AS unid | ||
| NATURAL JOIN aggregates | ||
| NATURAL JOIN currency AS cur | ||
| WHERE unid."currencyAmount" * cur."valueInChaos" <= calc_avg + 1.97 * std | ||
| AND unid."currencyAmount" * cur."valueInChaos" >= calc_avg - 1.97 * std | ||
| GROUP BY name, unid."itemBaseTypeId", unid."createdHoursSinceLaunch", league, ilvl, identified, rarity, calc_count; | ||
|
|
||
| DELETE FROM unidentified_item as unid | ||
| WHERE NOT aggregated AND unid."createdHoursSinceLaunch"=current_hour; | ||
|
|
||
| RETURN NEW; | ||
| END; | ||
| ${name}$ LANGUAGE plpgsql; | ||
| """, | ||
| """ | ||
| BEFORE INSERT ON {table} | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION {name}(); | ||
| """, | ||
| ) | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.create_table( | ||
| "league", | ||
| sa.Column( | ||
| "leagueId", sa.SmallInteger(), sa.Identity(always=False), nullable=False | ||
| ), | ||
| sa.Column("name", sa.Text(), nullable=False), | ||
| sa.Column("validFrom", sa.DateTime(timezone=True), nullable=False), | ||
| sa.Column("validTo", sa.DateTime(timezone=True), nullable=True), | ||
| sa.Column("version", sa.Float(), nullable=False), | ||
| sa.PrimaryKeyConstraint("leagueId"), | ||
| ) | ||
| op.execute(""" | ||
| INSERT INTO league (name, "validFrom", version) | ||
| SELECT DISTINCT | ||
| league, | ||
| TIMESTAMP '1970-01-01 00:00:00', | ||
| 0.0 | ||
| FROM item | ||
| WHERE league IS NOT NULL | ||
| """) | ||
|
|
||
| # ---- Item table ---- | ||
| op.add_column("item", sa.Column("leagueId", sa.SmallInteger())) | ||
| op.execute(""" | ||
| UPDATE item | ||
| SET "leagueId" = l."leagueId" | ||
| FROM league l | ||
| WHERE item.league = l.name | ||
| """) | ||
| op.alter_column("item", "leagueId", nullable=False) | ||
| op.create_foreign_key( | ||
| "fk_item_league", | ||
| "item", | ||
| "league", | ||
| ["leagueId"], | ||
| ["leagueId"], | ||
| ondelete="RESTRICT", | ||
| onupdate="CASCADE", | ||
| ) | ||
| op.drop_column("item", "league") | ||
|
|
||
| # ---- Unidentified item table ---- | ||
| op.add_column("unidentified_item", sa.Column("leagueId", sa.SmallInteger())) | ||
| op.execute(""" | ||
| UPDATE unidentified_item | ||
| SET "leagueId" = l."leagueId" | ||
| FROM league l | ||
| WHERE league = l.name | ||
| """) | ||
| op.alter_column("unidentified_item", "leagueId", nullable=False) | ||
| op.create_foreign_key( | ||
| "fk_unidentified_item_league", | ||
| "unidentified_item", | ||
| "league", | ||
| ["leagueId"], | ||
| ["leagueId"], | ||
| ondelete="RESTRICT", | ||
| onupdate="CASCADE", | ||
| ) | ||
| op.drop_column("unidentified_item", "league") | ||
|
|
||
| # ---- Currency table ---- | ||
| op.add_column("currency", sa.Column("leagueId", sa.SmallInteger())) | ||
| # Deletes all currency ids that are not used | ||
| op.execute(""" | ||
| DELETE FROM currency c | ||
| WHERE NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM item i | ||
| WHERE i."currencyId" = c."currencyId" | ||
| ) | ||
| """) | ||
| op.execute(""" | ||
| UPDATE currency c | ||
| SET "leagueId" = i.leagueId | ||
| FROM ( | ||
| SELECT item."currencyId", MIN(item."leagueId") AS leagueId | ||
| FROM item | ||
| GROUP BY item."currencyId" | ||
| ) i | ||
| WHERE c."currencyId" = i."currencyId" | ||
| """) | ||
| op.alter_column("currency", "leagueId", nullable=False) | ||
| op.create_foreign_key( | ||
| "fk_currency_league", | ||
| "currency", | ||
| "league", | ||
| ["leagueId"], | ||
| ["leagueId"], | ||
| ondelete="RESTRICT", | ||
| onupdate="CASCADE", | ||
| ) | ||
|
|
||
| # ---- Indexes ---- | ||
| # Manual SQL to drop query instead of `op.drop_index` is needed due to hypertables | ||
| op.execute(""" | ||
| DROP INDEX IF EXISTS ix_item_name_itemBaseTypeId_createdHoursSinceLaunch_league | ||
| """) | ||
| op.create_index( | ||
| "ix_item_name_itemBaseTypeId_createdHoursSinceLaunch_leagueId", | ||
| "item", | ||
| [ | ||
| "name", | ||
| "itemBaseTypeId", | ||
| "createdHoursSinceLaunch", | ||
| "leagueId", | ||
| ], | ||
| unique=False, | ||
| ) | ||
|
|
||
| op.execute(""" | ||
| DROP INDEX IF EXISTS ix_unid_item_name_itemBaseTypeId_createdHoursSinceLaunch_league | ||
| """) | ||
| op.create_index( | ||
| "ix_unid_name_itemBaseTypeId_createdHoursSinceLaunch_leagueId", | ||
| "unidentified_item", | ||
| [ | ||
| "name", | ||
| "itemBaseTypeId", | ||
| "createdHoursSinceLaunch", | ||
| "leagueId", | ||
| ], | ||
| unique=False, | ||
| ) | ||
|
|
||
| # ---- triggers ---- | ||
| op.drop_trigger(unidentified_aggregation_trigger_old) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| # ---- Item table ---- | ||
| op.add_column( | ||
| "item", | ||
| sa.Column("league", sa.TEXT(), autoincrement=False), | ||
| ) | ||
| op.execute(""" | ||
| UPDATE item i | ||
| SET league = l.name | ||
| FROM league l | ||
| WHERE i."leagueId" = l."leagueId" | ||
| """) | ||
| op.alter_column("item", "league", nullable=False) | ||
| op.drop_constraint("fk_item_league", "item", type_="foreignkey") | ||
| op.execute(""" | ||
| DROP INDEX IF EXISTS ix_item_name_itemBaseTypeId_createdHoursSinceLaunch_leagueId | ||
| """) | ||
| op.create_index( | ||
| "ix_item_name_itemBaseTypeId_createdHoursSinceLaunch_league", | ||
| "item", | ||
| ["name", "itemBaseTypeId", "createdHoursSinceLaunch", "league"], | ||
| unique=False, | ||
| ) | ||
| op.drop_column("item", "leagueId") | ||
|
|
||
| # ---- Unidentified item table ---- | ||
| op.add_column( | ||
| "unidentified_item", | ||
| sa.Column("league", sa.TEXT(), autoincrement=False), | ||
| ) | ||
| op.execute(""" | ||
| UPDATE unidentified_item ui | ||
| SET league = l.name | ||
| FROM league l | ||
| WHERE ui."leagueId" = l."leagueId" | ||
| """) | ||
| op.alter_column("unidentified_item", "league", nullable=False) | ||
| op.drop_constraint( | ||
| "fk_unidentified_item_league", "unidentified_item", type_="foreignkey" | ||
| ) | ||
| op.execute(""" | ||
| DROP INDEX IF EXISTS ix_unid_name_itemBaseTypeId_createdHoursSinceLaunch_leagueId | ||
| """) | ||
| op.create_index( | ||
| "ix_unid_item_name_itemBaseTypeId_createdHoursSinceLaunch_league", | ||
| "unidentified_item", | ||
| ["name", "itemBaseTypeId", "createdHoursSinceLaunch", "league"], | ||
| unique=False, | ||
| ) | ||
| op.drop_column("unidentified_item", "leagueId") | ||
|
|
||
| # ---- Currency table ---- | ||
| op.drop_column("currency", "leagueId") | ||
|
|
||
| op.drop_table("league") | ||
|
|
||
| # ---- triggers ---- | ||
| op.create_trigger(unidentified_aggregation_trigger_old) | ||
| # ### end Alembic commands ### | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.