Skip to content

Commit

Permalink
Cache period queries when loading indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Sep 2, 2024
1 parent 3bed9e7 commit 114b9a8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 1 addition & 4 deletions cpi/defaults.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#! /usr/bin/env python
"""
Default values.
"""
"""Default values."""

DEFAULT_SERIES_ID = "CUUR0000SA0"
DEFAULTS_SERIES_ATTRS = {
Expand Down
11 changes: 8 additions & 3 deletions cpi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,18 @@ def get_by_id(cls, value: str):

# Get the indexes
dict_list = query("SELECT * FROM 'indexes' WHERE series=?", (value,))

# Cache the periods to reduce queries
period_cache = {p.id: p for p in Period.all()}

# Load the indexes one by one
d["indexes"] = []
for i in dict_list:
obj = Index(
series_id=d["id"],
year=i["year"],
period=Period.get_by_id(i["period"]),
value=i["value"],
year=int(i["year"]),
period=period_cache[i["period"]],
value=float(i["value"]),
)
d["indexes"].append(obj)

Expand Down

0 comments on commit 114b9a8

Please sign in to comment.