Skip to content

Commit

Permalink
caching
Browse files Browse the repository at this point in the history
  • Loading branch information
smk762 committed Jun 6, 2024
1 parent 3b7652c commit 651b894
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 150 deletions.
7 changes: 4 additions & 3 deletions code/kmd_ntx_api/cache_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def get_from_memcache(key, path=None, url=None, expire=600):
logger.cached(f"Getting {key} from cache...")
# logger.cached(f"Getting {key} from cache...")
key = key.lower()
data = MEMCACHE.get(key)
if data is None or len(data) == 0:
Expand All @@ -24,9 +24,10 @@ def get_from_memcache(key, path=None, url=None, expire=600):
data = get_cache_file(path)
if data is None or len(data) == 0:
data = refresh_cache(path, url, force=True, expire=expire)
logger.cached(f"Returning {key} from {path}")
# logger.cached(f"Returning {key} from {path}")
else:
logger.cached(f"Returning {key} from memcache")
# logger.cached(f"Returning {key} from memcache")
pass
return data


Expand Down
129 changes: 68 additions & 61 deletions code/kmd_ntx_api/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def get_base_context(request):
notary = get_or_none(request, "notary", get_random_notary(notary_list))
epoch = get_or_none(request, "epoch", "Epoch_0")
coin = get_or_none(request, "coin", "KMD")
logger.info("base_context")
dpow_coins_dict = get_dpow_coins_dict(season)
logger.info(dpow_coins_dict)
dpow_coins_dict["Main"] += ["KMD", "LTC"]
Expand Down Expand Up @@ -78,77 +77,84 @@ def get_base_context(request):
return context


def get_notary_profile_index_context(request, season, context):
def get_notary_profile_index_context(request, context):
logger.merge("get_notary_profile_index_context")
return {
context.update({
"page_title": "Notary Profile Index",
"buttons": buttons.get_region_buttons(),
"nn_social": get_nn_social_info(request),
"nn_regions": get_regions_info(season)
}
"nn_regions": get_regions_info(context['season'])
})
return context


def get_notary_profile_context(request, season, notary, context):
logger.merge("get_notary_profile_context")
notary_profile_summary_table = get_notary_ntx_season_table_data(request, notary)
if len(notary_profile_summary_table["notary_ntx_summary_table"]) == 1:

last_ntx_time = 0
last_ntx_coin = ""
last_ltc_ntx_time = 0
notary_ntx_summary_table = notary_profile_summary_table["notary_ntx_summary_table"][0]

for coin in notary_ntx_summary_table:
if "last_ntx_blocktime" in notary_ntx_summary_table[coin]:
if coin == "KMD":
last_ltc_ntx_time = notary_ntx_summary_table[coin]["last_ntx_blocktime"]
if notary_ntx_summary_table[coin]["last_ntx_blocktime"] > last_ntx_time:
last_ntx_time = notary_ntx_summary_table[coin]["last_ntx_blocktime"]
last_ntx_coin = coin

context = {
"notary_ntx_summary_table": notary_ntx_summary_table,
"last_ltc_ntx_time": get_time_since(last_ltc_ntx_time)[1],
"last_ntx_time": get_time_since(last_ntx_time)[1],
"last_ntx_coin": last_ntx_coin
}

seed_scores = get_seednode_version_score_total(request)
notarised_data_24hr = get_notarised_date(season, None, None, notary, True)
region = get_notary_region(notary)
season_stats_sorted = get_season_stats_sorted(season, context['notaries'])

ntx_season_data = notary_profile_summary_table["ntx_season_data"][0]
seed_score = seed_scores[notary]
total_ntx_score = ntx_season_data["total_ntx_score"]
total_score = total_ntx_score + seed_score
context.update({
"page_title": f"{notary} Notary Profile",
"notary": notary,
"notary_clean": notary.replace("_", " "),
"seed_score": seed_score,
"ntx_score": total_score,
"season_score": total_score,
"master_server_count": ntx_season_data['master_server_count'],
"main_server_count": ntx_season_data['main_server_count'],
"third_party_server_count": ntx_season_data['third_party_server_count'],
"rank": get_region_rank(season_stats_sorted[region], notary),
"buttons": buttons.get_ntx_buttons(),
"nn_social": get_nn_social_info(request), # Social Media Links
"mining_summary": get_nn_mining_summary(notary), # Mining Summary
"master_notarised_24hr": notarised_data_24hr.filter(server='KMD').count(),
"main_notarised_24hr": notarised_data_24hr.filter(server='Main').count(),
"third_notarised_24hr": notarised_data_24hr.filter(server='Third_Party').count()
})
def get_notary_profile_context(request, notary, context):
logger.info("get_notary_profile_context")
try:
notary_profile_summary_table = get_notary_ntx_season_table_data(request, notary)
if len(notary_profile_summary_table["notary_ntx_summary_table"]) == 1:

last_ntx_time = 0
last_ntx_coin = ""
last_ltc_ntx_time = 0
notary_ntx_summary_table = notary_profile_summary_table["notary_ntx_summary_table"][0]

for coin in notary_ntx_summary_table:
if "last_ntx_blocktime" in notary_ntx_summary_table[coin]:
if coin == "KMD":
last_ltc_ntx_time = notary_ntx_summary_table[coin]["last_ntx_blocktime"]
if notary_ntx_summary_table[coin]["last_ntx_blocktime"] > last_ntx_time:
last_ntx_time = notary_ntx_summary_table[coin]["last_ntx_blocktime"]
last_ntx_coin = coin

context.update({
"notary_ntx_summary_table": notary_ntx_summary_table,
"last_ltc_ntx_time": get_time_since(last_ltc_ntx_time)[1],
"last_ntx_time": get_time_since(last_ntx_time)[1],
"last_ntx_coin": last_ntx_coin
})

seed_scores = get_seednode_version_score_total(request)
notarised_data_24hr = get_notarised_date(context['season'], None, None, notary, True)
region = get_notary_region(notary)
season_stats_sorted = get_season_stats_sorted(context['season'], context['notaries'])

ntx_season_data = notary_profile_summary_table["ntx_season_data"][0]
seed_score = seed_scores[notary]
total_ntx_score = ntx_season_data["total_ntx_score"]
total_score = total_ntx_score + seed_score
context.update({
"page_title": f"{notary} Notary Profile",
"notary": notary,
"notary_clean": notary.replace("_", " "),
"seed_score": seed_score,
"ntx_score": total_score,
"season_score": total_score,
"master_server_count": ntx_season_data['master_server_count'],
"main_server_count": ntx_season_data['main_server_count'],
"third_party_server_count": ntx_season_data['third_party_server_count'],
"rank": get_region_rank(season_stats_sorted[region], notary),
"buttons": buttons.get_ntx_buttons(),
"nn_social": get_nn_social_info(request), # Social Media Links
"mining_summary": get_nn_mining_summary(notary), # Mining Summary
"master_notarised_24hr": notarised_data_24hr.filter(server='KMD').count(),
"main_notarised_24hr": notarised_data_24hr.filter(server='Main').count(),
"third_notarised_24hr": notarised_data_24hr.filter(server='Third_Party').count()
})
except Exception as e:
logger.warning(e)
logger.info("g0t_notary_profile_context")
return context


def get_coin_profile_index_context(request, season, context):
logger.merge("get_coin_profile_index_context")
return {
context.update({
"buttons": buttons.get_server_buttons(),
"coin_social": get_coin_social_info(request)
}
})
return context



def get_coin_profile_context(request, season, coin, context):
Expand Down Expand Up @@ -177,7 +183,7 @@ def get_coin_profile_context(request, season, coin, context):
top_region_notarisers, coin, context["notary_icons"]
)

return {
context.update({
"page_title": f"{coin} Profile",
"server": get_dpow_coin_server(season, coin),
"coin": coin,
Expand All @@ -190,5 +196,6 @@ def get_coin_profile_context(request, season, coin, context):
"coin_notariser_ranks": coin_notariser_ranks,
"top_region_notarisers": top_region_notarisers,
"top_coin_notarisers": top_coin_notarisers
}
})
return context

6 changes: 3 additions & 3 deletions code/kmd_ntx_api/notary_seasons.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def get_season(timestamp: int=int(time.time()), notary_seasons=None) -> str:
def get_page_season(request, seasons_info=None):
logger.calc("get_page_season")
if "season" in request.GET:
if seasons_info is None:
seasons_info = get_seasons_info()
season = request.GET["season"]
if season.isnumeric():
season = f"Season_{request.GET['season']}"
return f"Season_{request.GET['season']}"
if seasons_info is None:
seasons_info = get_seasons_info()
if season.title() in seasons_info:
return season.title()
return get_season()
Expand Down
12 changes: 6 additions & 6 deletions code/kmd_ntx_api/ntx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from kmd_ntx_api.query import get_notarised_data, get_notarised_tenure_data
from kmd_ntx_api.cron import days_ago
from kmd_ntx_api.cache_data import get_from_memcache, refresh_cache
from kmd_ntx_api.logger import logger

def get_notarised_date(season=None, server=None, coin=None, notary=None, last_24hrs=True):
return get_notarised_data(
season, server, None, coin,
notary
).filter(
block_time__gt=str(days_ago(1))
).order_by('-block_time')
logger.info("get_notarised_date")
try:
return get_notarised_data(season, server, None, coin, notary).filter(block_time__gt=str(days_ago(1))).order_by('-block_time')
except Exception as e:
logger.warning(e)

def get_ntx_tenure_table(request):
tenure_data = get_notarised_tenure_data().values()
Expand Down
26 changes: 14 additions & 12 deletions code/kmd_ntx_api/ntx_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@


def notary_profile_view(request, notary=None):
logger.info("notary_profile_view")
context = get_base_context(request)
# TODO: This is not currently used, but can be added for prior season stats given fully populated databases
context.update({
"notary_seasons": get_notary_seasons(),
"notary": notary
})
# Base context will return a random notary if one is not specified. For this view, we prefer 'None'.
season = context["season"]
if notary in context["notaries"]:
context.update(get_notary_profile_context(request, season, notary, context))
context = get_notary_profile_context(request, notary, context)
return render(request, 'views/notarisation/notary_profile.html', context)
context.update(get_notary_profile_index_context(request, season, context))
context = get_notary_profile_index_context(request, context)
return render(request, 'views/notarisation/notary_profile_index.html', context)


def coin_profile_view(request, coin=None): # TODO: REVIEW and ALIGN with NOTARY PROFILE
logger.info("coin_profile_view")
context = get_base_context(request)
season = context["season"]
coin = get_or_none(request, "coin", coin)
Expand All @@ -49,17 +50,14 @@ def coin_profile_view(request, coin=None): # TODO: REVIEW and ALIGN with NOTARY
})

if coin:
profile_data = get_coin_profile_context(request, season, coin, context)
if profile_data is not None:
context.update(profile_data)
return render(request, 'views/coin/coin_profile.html', context)
else:
messages.error(request, f"Coin {coin} not found.")
context.update(get_coin_profile_index_context(request, season, context))
context = get_coin_profile_context(request, season, coin, context)
return render(request, 'views/coin/coin_profile.html', context)
context = get_coin_profile_index_context(request, season, context)
return render(request, 'views/coin/coin_profile_index.html', context)


def ntx_scoreboard_view(request, region=None):
logger.info("ntx_scoreboard_view")
context = get_base_context(request)
context["region"] = get_or_none(request, "region", region)
season_stats_sorted = get_season_stats_sorted(context["season"], context["notaries"])
Expand All @@ -74,6 +72,7 @@ def ntx_scoreboard_view(request, region=None):


def ntx_scoreboard_24hrs_view(request, region=None):
logger.info("ntx_scoreboard_24hrs_view")
context = get_base_context(request)
context["region"] = get_or_none(request, "region", region)
context.update({
Expand All @@ -86,12 +85,14 @@ def ntx_scoreboard_24hrs_view(request, region=None):


def seednode_version_view(request):
logger.info("seednode_version_view")
context = get_base_context(request)
context.update(seednode_version_context(request))
return render(request, 'views/atomicdex/seednode_version_stats.html', context)


def coins_last_ntx_view(request):
logger.info("coins_last_ntx_view")
season = get_page_season(request)
context = get_base_context(request)
server = get_page_server(request)
Expand All @@ -105,6 +106,7 @@ def coins_last_ntx_view(request):
return render(request, 'views/ntx/coins_last_ntx.html', context)

def notarised_tenure_view(request):
logger.info("notarised_tenure_view")
context = get_base_context(request)
context.update({
"page_title":f"Coin Notarisation Tenure"
Expand Down Expand Up @@ -224,9 +226,9 @@ def coin_profile_view(request, coin=None): # TODO: REVIEW and ALIGN with NOTARY
})

if coin:
context.update(get_coin_profile_context(request, season, coin, context))
context = get_coin_profile_context(request, season, coin, context)
return render(request, 'views/coin/coin_profile.html', context)
context.update(get_coin_profile_index_context(request, season, context))
context = get_coin_profile_index_context(request, season, context)
return render(request, 'views/coin/coin_profile_index.html', context)


Expand Down
44 changes: 11 additions & 33 deletions code/kmd_ntx_api/page_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,21 @@


def dash_view(request):
season = get_page_season(request)
context = get_base_context(request)
logger.info("dash_view")

ntx_data = get_notarised_data(season=season, exclude_epoch="Unofficial")
logger.info("got_notarised_data")
ntx_season = get_ntx_count_season(ntx_data)
logger.info("got_notarised_data count")
ntx_24hr = get_ntx_count_24hr(ntx_data)
logger.info("got_notarised_data count 24")

# Get Mining Stats
mined_data = get_mined_data(season=season)
mined_season = get_mined_count_season(mined_data)
logger.info("got mined data")
mined_24hr = get_mined_count_24hr(mined_data)
logger.info("got mined 24 data")
biggest_block = get_biggest_block_season(mined_data)
logger.info("biggest_block")
daily_stats_sorted = get_daily_stats_sorted(context["notaries"], context["dpow_coins_dict"])
logger.info("daily_stats_sorted")
season_stats_sorted = get_season_stats_sorted(season, context["notaries"])
logger.info("season_stats_sorted")
region_score_stats = get_region_score_stats(season_stats_sorted)
logger.info("region_score_stats")

ntx_data = get_notarised_data(season=context["season"], exclude_epoch="Unofficial")
mined_data = get_mined_data(season=context["season"])
season_stats_sorted = get_season_stats_sorted(context["season"], context["notaries"])
context.update({
"show_ticker": True,
"page_title": "Index",
"ntx_24hr": ntx_24hr,
"ntx_season": ntx_season,
"mined_24hr": mined_24hr,
"mined_season": mined_season,
"biggest_block": biggest_block,
"ntx_24hr": get_ntx_count_24hr(ntx_data),
"ntx_season": get_ntx_count_season(ntx_data),
"mined_24hr": get_mined_count_24hr(mined_data),
"mined_season": get_mined_count_season(mined_data),
"biggest_block": get_biggest_block_season(mined_data),
"season_stats_sorted": season_stats_sorted,
"region_score_stats": region_score_stats,
"show_ticker": True,
"daily_stats_sorted": daily_stats_sorted,
"region_score_stats": get_region_score_stats(season_stats_sorted),
"daily_stats_sorted": get_daily_stats_sorted(context["notaries"], context["dpow_coins_dict"]),
"nn_social": get_nn_social_info(request)
})
return render(request, 'views/dash_index.html', context)
1 change: 1 addition & 0 deletions code/kmd_ntx_api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def filter_data_basic(data, **kwargs):
def get_notarised_data(season=None, server=None, epoch=None, coin=None,
notary=None, address=None, txid=None, exclude_epoch=None,
min_blocktime=None, max_blocktime=None):
logger.info("get_notarised_data")
if txid:
return notarised.objects.filter(txid=txid)
else:
Expand Down
2 changes: 0 additions & 2 deletions code/kmd_ntx_api/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ def get_notary_ntx_24hr_summary(ntx_24hr, notary, dpow_coins_dict):
return notary_ntx_24hr




def get_seednode_version_season_stats_data(season, notary=None):
return get_seednode_version_stats_data(season=season, name=notary, score=0.2).values('name').annotate(sum_score=Sum('score'))

Expand Down
Loading

0 comments on commit 651b894

Please sign in to comment.