|
| 1 | +import aiohttp |
| 2 | + |
1 | 3 | from middlewared.api import api_method
|
2 | 4 | from middlewared.api.current import CatalogSyncArgs, CatalogSyncResult
|
3 | 5 | from middlewared.service import job, private, Service
|
|
6 | 8 | from .utils import OFFICIAL_LABEL, OFFICIAL_CATALOG_REPO, OFFICIAL_CATALOG_BRANCH
|
7 | 9 |
|
8 | 10 |
|
| 11 | +STATS_URL = 'https://telemetry.sys.truenas.net/apps/truenas-apps-stats.json' |
| 12 | + |
| 13 | + |
9 | 14 | class CatalogService(Service):
|
10 | 15 |
|
| 16 | + POPULARITY_INFO = {} |
11 | 17 | SYNCED = False
|
12 | 18 |
|
| 19 | + @private |
| 20 | + async def update_popularity_cache(self): |
| 21 | + async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as session: |
| 22 | + try: |
| 23 | + async with session.get(STATS_URL) as response: |
| 24 | + response.raise_for_status() |
| 25 | + self.POPULARITY_INFO = { |
| 26 | + k.lower(): v for k, v in (await response.json()).items() |
| 27 | + # Making sure we have a consistent format as for trains we see capitalized |
| 28 | + # entries in the file |
| 29 | + } |
| 30 | + except Exception as e: |
| 31 | + self.logger.error('Failed to fetch popularity stats for apps: %r', e) |
| 32 | + |
| 33 | + @private |
| 34 | + async def popularity_cache(self): |
| 35 | + return self.POPULARITY_INFO |
| 36 | + |
13 | 37 | @private
|
14 | 38 | async def synced(self):
|
15 | 39 | return self.SYNCED
|
@@ -38,6 +62,7 @@ async def sync(self, job):
|
38 | 62 | 'retrieve_all_trains': True,
|
39 | 63 | 'trains': [],
|
40 | 64 | })
|
| 65 | + await self.update_popularity_cache() |
41 | 66 | except Exception as e:
|
42 | 67 | await self.middleware.call(
|
43 | 68 | 'alert.oneshot_create', 'CatalogSyncFailed', {'catalog': OFFICIAL_LABEL, 'error': str(e)}
|
|
0 commit comments