Skip to content

Commit 3034fdf

Browse files
committed
feat: cache all metrics data for 10 seconds for performances reasons
1 parent 4d95491 commit 3034fdf

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/aleph/cache.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from aiocache import Cache
2+
3+
4+
cache = Cache()

src/aleph/web/controllers/metrics.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from web3 import Web3
1515

1616
from aleph import __version__
17+
from aleph.cache import cache
1718
from aleph.config import get_config
1819
from aleph.db.accessors.chains import get_last_height
1920
from aleph.db.models import FilePinDb, MessageDb, PeerDb, PendingMessageDb, PendingTxDb
@@ -146,15 +147,30 @@ async def fetch_eth_height() -> Optional[int]:
146147
async def get_metrics(session: DbSession, node_cache: NodeCache) -> Metrics:
147148
sync_messages_reference_total = await fetch_reference_total_messages()
148149
eth_reference_height = await fetch_eth_height()
150+
cache_duration = 10
149151

150152
# select count(*) can be slow but estimates are not refreshed often enough to give
151153
# a meaningful sense of progress for the node main page on /.
152-
sync_messages_total: int = MessageDb.count(session=session)
153-
peers_count = PeerDb.count(session=session)
154+
sync_messages_total: int
155+
if await cache.exists("sync_messages_total"):
156+
sync_messages_total = await cache.get("sync_messages_total")
157+
else:
158+
sync_messages_total = MessageDb.count(session=session)
159+
await cache.set("sync_messages_total", sync_messages_total, ttl=cache_duration)
154160

155-
eth_last_committed_height = get_last_height(
156-
session=session, chain=Chain.ETH, sync_type=ChainEventType.SYNC
157-
)
161+
if await cache.exists("peers_count"):
162+
peers_count = await cache.get("peers_count")
163+
else:
164+
peers_count = PeerDb.count(session=session)
165+
await cache.set("peers_count", peers_count, ttl=cache_duration)
166+
167+
if await cache.exists("eth_last_committed_height"):
168+
eth_last_committed_height = await cache.get("eth_last_committed_height")
169+
else:
170+
eth_last_committed_height = get_last_height(
171+
session=session, chain=Chain.ETH, sync_type=ChainEventType.SYNC
172+
)
173+
await cache.set("eth_last_committed_height", eth_last_committed_height, ttl=cache_duration)
158174

159175
if not (sync_messages_reference_total is None or sync_messages_total is None):
160176
sync_messages_remaining_total = (
@@ -173,18 +189,34 @@ async def get_metrics(session: DbSession, node_cache: NodeCache) -> Metrics:
173189
retry_message_job_tasks = await node_cache.get("retry_messages_job_tasks")
174190
nb_message_jobs = int(retry_message_job_tasks) if retry_message_job_tasks else 0
175191

192+
if await cache.exists("sync_pending_messages_total"):
193+
sync_pending_messages_total = await cache.get("sync_pending_messages_total")
194+
else:
195+
sync_pending_messages_total = PendingMessageDb.count(session=session)
196+
await cache.set("sync_pending_messages_total", sync_pending_messages_total, ttl=cache_duration)
197+
198+
if await cache.exists("sync_permanent_files_total"):
199+
sync_permanent_files_total = await cache.get("sync_permanent_files_total")
200+
else:
201+
sync_permanent_files_total = FilePinDb.count(session=session)
202+
await cache.set("sync_permanent_files_total", sync_permanent_files_total, ttl=cache_duration)
203+
204+
if await cache.exists("sync_pending_txs_total"):
205+
sync_pending_txs_total = await cache.get("sync_pending_txs_total")
206+
else:
207+
sync_pending_txs_total = PendingTxDb.count(session=session)
208+
await cache.set("sync_pending_txs_total", sync_pending_txs_total, ttl=cache_duration)
209+
176210
return Metrics(
177211
pyaleph_build_info=pyaleph_build_info,
178212
pyaleph_status_peers_total=peers_count,
179213
pyaleph_processing_pending_messages_tasks_total=nb_message_jobs,
180214
pyaleph_status_sync_messages_total=sync_messages_total,
181-
pyaleph_status_sync_permanent_files_total=FilePinDb.count(session=session),
215+
pyaleph_status_sync_permanent_files_total=sync_permanent_files_total,
182216
pyaleph_status_sync_messages_reference_total=sync_messages_reference_total,
183217
pyaleph_status_sync_messages_remaining_total=sync_messages_remaining_total,
184-
pyaleph_status_sync_pending_messages_total=PendingMessageDb.count(
185-
session=session
186-
),
187-
pyaleph_status_sync_pending_txs_total=PendingTxDb.count(session=session),
218+
pyaleph_status_sync_pending_messages_total=sync_pending_messages_total,
219+
pyaleph_status_sync_pending_txs_total=sync_pending_txs_total,
188220
pyaleph_status_chain_eth_last_committed_height=eth_last_committed_height,
189221
pyaleph_status_chain_eth_height_reference_total=eth_reference_height,
190222
pyaleph_status_chain_eth_height_remaining_total=eth_remaining_height,

0 commit comments

Comments
 (0)