14
14
from web3 import Web3
15
15
16
16
from aleph import __version__
17
+ from aleph .cache import cache
17
18
from aleph .config import get_config
18
19
from aleph .db .accessors .chains import get_last_height
19
20
from aleph .db .models import FilePinDb , MessageDb , PeerDb , PendingMessageDb , PendingTxDb
@@ -146,15 +147,30 @@ async def fetch_eth_height() -> Optional[int]:
146
147
async def get_metrics (session : DbSession , node_cache : NodeCache ) -> Metrics :
147
148
sync_messages_reference_total = await fetch_reference_total_messages ()
148
149
eth_reference_height = await fetch_eth_height ()
150
+ cache_duration = 10
149
151
150
152
# select count(*) can be slow but estimates are not refreshed often enough to give
151
153
# 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 )
154
160
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 )
158
174
159
175
if not (sync_messages_reference_total is None or sync_messages_total is None ):
160
176
sync_messages_remaining_total = (
@@ -173,18 +189,34 @@ async def get_metrics(session: DbSession, node_cache: NodeCache) -> Metrics:
173
189
retry_message_job_tasks = await node_cache .get ("retry_messages_job_tasks" )
174
190
nb_message_jobs = int (retry_message_job_tasks ) if retry_message_job_tasks else 0
175
191
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
+
176
210
return Metrics (
177
211
pyaleph_build_info = pyaleph_build_info ,
178
212
pyaleph_status_peers_total = peers_count ,
179
213
pyaleph_processing_pending_messages_tasks_total = nb_message_jobs ,
180
214
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 ,
182
216
pyaleph_status_sync_messages_reference_total = sync_messages_reference_total ,
183
217
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 ,
188
220
pyaleph_status_chain_eth_last_committed_height = eth_last_committed_height ,
189
221
pyaleph_status_chain_eth_height_reference_total = eth_reference_height ,
190
222
pyaleph_status_chain_eth_height_remaining_total = eth_remaining_height ,
0 commit comments