Merge pull request 'version format and coingecko caching' (#13) from semver-and-cache into master

Reviewed-on: https://git.wownero.com/wownero/wowlet-backend/pulls/13
This commit is contained in:
wowario 2021-05-14 20:01:18 +00:00
commit 98b36d0c6f
4 changed files with 26 additions and 23 deletions

View file

@ -10,4 +10,3 @@ psutil
psutil
pillow-simd
python-magic
semver

View file

@ -2,11 +2,13 @@
# Copyright (c) 2020, The Monero Project.
# Copyright (c) 2020, dsc@xmr.pm
import json
from typing import List, Union
import settings
from wowlet_backend.utils import httpget
from wowlet_backend.tasks import WowletTask
from wowlet_backend.factory import cache
class CryptoRatesTask(WowletTask):
@ -69,6 +71,16 @@ class CryptoRatesTask(WowletTask):
except Exception as ex:
app.logger.error(f"extra coin: {coin}; {ex}")
# use cache if present
data = await cache.get(self._cache_key)
if data:
data = json.loads(data)
extra_coin = [e for e in data if e['symbol'] == symbol]
if extra_coin:
app.logger.warning(f"using cache for extra coin: {coin}")
rates.append(extra_coin[0])
continue
try:
# additional call to fetch 24h pct change
url = f"{self._http_api_gecko}/coins/{coin}?tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false"
@ -78,5 +90,4 @@ class CryptoRatesTask(WowletTask):
pass
rates.append(obj)
return rates

View file

@ -2,7 +2,6 @@
# Copyright (c) 2020, The Monero Project.
# Copyright (c) 2020, dsc@xmr.pm
import semver
from dateutil.parser import parse
import settings
@ -16,7 +15,7 @@ class WowletReleasesTask(WowletTask):
super(WowletReleasesTask, self).__init__(interval)
self._cache_key = "wowlet_releases"
self._cache_expiry = self.interval * 10
self._cache_expiry = self.interval
self._websocket_cmd = "wowlet_releases"
@ -45,25 +44,19 @@ class WowletReleasesTask(WowletTask):
"size": asset['size']
}
_semver = self.parse_semver(blob)
tag = blob['tag_name']
if tag.startswith("v"):
tag = tag[1:]
try:
t = [int(z) for z in tag.split(".")]
if len(t) != 3:
raise Exception()
except:
raise Exception(f"invalid tag: {tag}")
return {
"assets": data,
"body": blob['body'],
"version": {
"major": _semver.major,
"minor": _semver.minor,
"patch": _semver.patch
"version": tag
}
}
def parse_semver(self, blob):
tag = blob['tag_name'] # valid tag name example: v0.2.0.0
if tag.startswith("v"):
tag = tag[1:]
tag = tag.split(".")
tag = [t for t in tag if t.isdigit()]
if tag[0] == '0':
tag = tag[1:]
return semver.VersionInfo.parse(f"{tag[0]}.{tag[1]}.{tag[2]}")

View file

@ -85,7 +85,7 @@ async def feather_data():
data = json.loads(data)
return data
keys = ["blockheights", "funding_proposals", "crypto_rates", "fiat_rates", "reddit", "rpc_nodes", "xmrig", "xmrto_rates", "suchwow", "forum"]
keys = ["blockheights", "funding_proposals", "crypto_rates", "fiat_rates", "reddit", "rpc_nodes", "xmrig", "xmrto_rates", "suchwow", "forum", "wowlet_releases"]
data = {keys[i]: json.loads(val) if val else None for i, val in enumerate(await cache.mget(*keys))}
# @TODO: for backward-compat reasons we're including some legacy keys which can be removed after 1.0 release