From 913333c134d4305b6df08e795d386c09f2cf2846 Mon Sep 17 00:00:00 2001 From: anon Date: Mon, 10 Oct 2022 11:50:15 -0300 Subject: [PATCH] fixed pageviews --- charts/urls.py | 1 + charts/views.py | 477 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 473 insertions(+), 5 deletions(-) diff --git a/charts/urls.py b/charts/urls.py index ed9952b..da1e2c5 100644 --- a/charts/urls.py +++ b/charts/urls.py @@ -109,6 +109,7 @@ urlpatterns = [ path('importer/', views.importer, name='importer'), path('reset//', views.reset, name='reset'), path('update///', views.update_database_admin, name='update'), + path('update_pageviews///', views.update_pageviews, name='update_pageviews'), path('add_coin/', views.add_coin, name="add_coin"), ] \ No newline at end of file diff --git a/charts/views.py b/charts/views.py index cbdf1bc..773ed51 100644 --- a/charts/views.py +++ b/charts/views.py @@ -882,6 +882,477 @@ def update_database_admin(request, date_from, date_to): message = 'Database updated from ' + str(date_from) + ' to ' + str(date_to) context = {'message': message} return render(request, 'charts/maintenance.html', context) + if request.user.username != "Administrador" and request.user.username != "Morpheus": + return render(request, 'users/error.html') + count = 0 + + ################################################################### + # SF model charts + ################################################################### + print('Populating database for sfmodel.html, sfmodelin.html and pricesats.html, wait a moment...') + Sfmodel.objects.all().delete() + timevar = 1283 + v0 = 0.002 + delta = (0.015 - 0.002)/(6*365) + previous_supply = 0 + supply = 0 + sf_aux = 0 + count_aux = 0 + + coins = Coin.objects.order_by('date').filter(name='xmr') + for coin in coins: + if coin.priceusd < 0.1: + coin.priceusd = 0.1 + coin.pricebtc = 0.000001 + if coin.stocktoflow > sf_aux*1.3+100: + coin.stocktoflow = sf_aux + + sf_aux = coin.stocktoflow + if coin.supply > 0: + supply = int(coin.supply)*10**12 + else: + supply = previous_supply + count += 1 + count_aux += 1 + + data = Sfmodel() + data.date = coin.date + data.priceusd = coin.priceusd + data.pricebtc = coin.pricebtc + data.stocktoflow = coin.stocktoflow + data.color = 30*coin.pricebtc/(count*delta + v0) + date_aux1 = datetime.datetime.strptime('2017-12-29', '%Y-%m-%d') + date_aux2 = datetime.datetime.strftime(coin.date, '%Y-%m-%d') + date_aux2 = datetime.datetime.strptime(date_aux2, '%Y-%m-%d') + if date_aux2 < date_aux1: + lastprice = coin.priceusd + current_inflation = coin.inflation + data.greyline = 0 + count_aux = 0 + else: + day = date_aux2 - timedelta(timevar) + coin_aux1 = Coin.objects.filter(name='xmr').get(date=day) + day = date_aux2 - timedelta(timevar+1) + coin_aux2 = Coin.objects.filter(name='xmr').get(date=day) + date_aux3 = datetime.datetime.strptime('2017-12-29', '%Y-%m-%d') + + if date_aux3 + timedelta(int(count_aux*2)) < datetime.datetime.strptime('2021-07-03', '%Y-%m-%d'): + day = date_aux3 + timedelta(int(count_aux*2)) + coin_aux3 = Coin.objects.filter(name='xmr').get(date=day) + if coin_aux3: + if (coin_aux3.inflation/current_inflation) > 1.2 or (coin_aux3.inflation/current_inflation) < 0.8: + coin_aux3.inflation = current_inflation + else: + current_inflation = coin_aux3.inflation + supply2 = supply + else: + reward2 = (2**64 -1 - supply2) >> 19 + if reward2 < 0.6*(10**12): + reward2 = 0.6*(10**12) + supply2 += int(720*reward2) + current_inflation = 100*reward2*720*365/supply2 + + if coin_aux1 and coin_aux2: + lastprice += (coin_aux1.priceusd/coin_aux2.priceusd-1)*lastprice + actualprice = lastprice*(math.sqrt(coin.inflation/current_inflation)) + data.greyline = actualprice + else: + data.greyline = 0 + previous_supply = supply + data.save() + + count_aux = 0 + for count_aux in range(700): + date_now = date.today() + timedelta(count_aux) + reward = (2**64 -1 - supply) >> 19 + if reward < 0.6*(10**12): + reward = 0.6*(10**12) + supply += int(720*reward) + + data = Sfmodel() + data.date = datetime.datetime.strftime(date_now, '%Y-%m-%d') + data.stocktoflow = (100/(100*reward*720*365/supply))**1.65 + data.priceusd = 0 + data.pricebtc = 0 + data.greyline = 0 + data.color = 0 + data.priceusd = 0 + data.greyline = 0 + data.save() + count += 1 + + ################################################################### + # Daily Emissions, inflation charts and coins against bitcoin + ################################################################### + print('Populating database for dailyemission.html and dailyemissionntv.html, wait a moment...') + DailyData.objects.all().delete() + supply_btc = 0 + supply_xmr = 0 + count_aux = 0 + coins_btc = Coin.objects.order_by('date').filter(name='btc') + + for coin_btc in coins_btc: + count_aux += 1 + data = DailyData() + data.date = datetime.datetime.strftime(coin_btc.date, '%Y-%m-%d') + + if coin_btc.blocksize > 0: + data.btc_blocksize = coin_btc.blocksize + data.btc_transactions = coin_btc.transactions + else: + data.btc_blocksize = 0 + data.btc_transactions = 0 + + if coin_btc.difficulty > 0: + data.btc_difficulty = coin_btc.difficulty + else: + data.btc_difficulty = 0 + + if coin_btc.transactions == 0: + data.btc_transcostusd = 0 + data.btc_transcostntv = 0 + else: + if coin_btc.fee*coin_btc.priceusd/coin_btc.transactions < 0.0001: + data.btc_transcostusd = 0 + data.btc_transcostntv = 0 + else: + data.btc_transcostusd = coin_btc.fee*coin_btc.priceusd/coin_btc.transactions + data.btc_transcostntv = coin_btc.fee/coin_btc.transactions + + if coin_btc.revenue < 0.000001 or coin_btc.priceusd < 0.001: + data.btc_minerrevntv = 0 + data.btc_minerrevusd = 0 + data.btc_commitntv = 0 + data.btc_commitusd = 0 + data.btc_priceusd = 0 + data.btc_marketcap = 0 + else: + data.btc_minerrevntv = coin_btc.revenue + data.btc_minerrevusd = coin_btc.revenue*coin_btc.priceusd + data.btc_commitntv = coin_btc.hashrate/(coin_btc.revenue) + data.btc_commitusd = coin_btc.hashrate/(coin_btc.revenue*coin_btc.priceusd) + data.btc_priceusd = coin_btc.priceusd + data.btc_marketcap = coin_btc.priceusd*coin_btc.supply + + if coin_btc.supply == 0: + data.btc_minerrevcap = 0 + else: + data.btc_minerrevcap = 365*100*coin_btc.revenue/coin_btc.supply + + if coin_btc.priceusd: + if coin_btc.priceusd/30 > 0.02: + data.btc_return = coin_btc.priceusd/30 + else: + data.btc_return = 0 + else: + data.btc_return = 0 + + if coin_btc.inflation > 0: + data.btc_inflation = coin_btc.inflation + else: + data.btc_inflation = 0 + if coin_btc.supply > 0: + data.btc_supply = coin_btc.supply + else: + data.btc_supply = 0 + + if coin_btc.supply - supply_btc < 0.000001: + data.btc_minerfeesntv = 0 + data.btc_minerfeesusd = 0 + data.btc_emissionntv = 0 + else: + data.btc_minerfeesntv = coin_btc.revenue - coin_btc.supply + supply_btc + data.btc_minerfeesusd = (coin_btc.revenue - coin_btc.supply + supply_btc)*coin_btc.priceusd + data.btc_emissionntv = coin_btc.supply - supply_btc + + if (coin_btc.supply - supply_btc)*coin_btc.priceusd < 1000: + data.btc_emissionusd = 0 + else: + data.btc_emissionusd = (coin_btc.supply - supply_btc)*coin_btc.priceusd + supply_btc = coin_btc.supply + + if count_aux > 1750: + coins_xmr = Coin.objects.filter(name='xmr').filter(date=coin_btc.date) + if coins_xmr: + for coin_xmr in coins_xmr: + if coin_xmr.blocksize > 0: + data.xmr_blocksize = coin_xmr.blocksize + else: + data.xmr_blocksize = 0 + + if coin_xmr.difficulty > 0: + data.xmr_difficulty = coin_xmr.difficulty + else: + data.xmr_difficulty = 0 + + if coin_xmr.priceusd < 0.001: + data.xmr_pricebtc = 0 + data.xmr_priceusd = 0 + data.xmr_marketcap = 0 + else: + data.xmr_pricebtc = coin_xmr.pricebtc + data.xmr_priceusd = coin_xmr.priceusd + data.xmr_marketcap = coin_xmr.priceusd*coin_xmr.supply + + if coin_btc.supply > 0 and coin_btc.transactions > 0: + data.xmr_transactions = coin_xmr.transactions + data.xmr_metcalfeusd = coin_btc.priceusd*coin_xmr.transactions*coin_xmr.supply/(coin_btc.supply*coin_btc.transactions) + data.xmr_metcalfebtc = coin_xmr.transactions*coin_xmr.supply/(coin_btc.supply*coin_btc.transactions) + else: + data.xmr_metcalfeusd = 0 + data.xmr_metcalfebtc = 0 + data.xmr_transactions = 0 + if data.xmr_metcalfeusd < 0.23: + data.xmr_metcalfeusd = 0 + data.xmr_metcalfebtc = 0 + + if coin_xmr.transactions == 0: + data.xmr_transacpercentage = 0 + data.xmr_transcostusd = 0 + data.xmr_transcostntv = 0 + else: + if coin_xmr.fee*coin_xmr.priceusd/coin_xmr.transactions < 0.0001: + data.xmr_transcostusd = 0 + data.xmr_transcostntv = 0 + else: + data.xmr_transcostusd = coin_xmr.fee*coin_xmr.priceusd/coin_xmr.transactions + data.xmr_transcostntv = coin_xmr.fee/coin_xmr.transactions + if coin_btc.transactions == 0: + data.xmr_transacpercentage = 0 + else: + data.xmr_transacpercentage = coin_xmr.transactions/coin_btc.transactions + + if coin_xmr.revenue < 0.000001 or coin_xmr.priceusd < 0.001: + data.xmr_minerrevntv = 0 + data.xmr_minerrevusd = 0 + data.xmr_commitntv = 0 + data.xmr_commitusd = 0 + else: + data.xmr_minerrevntv = coin_xmr.revenue + data.xmr_minerrevusd = coin_xmr.revenue*coin_xmr.priceusd + data.xmr_commitntv = coin_xmr.hashrate/(coin_xmr.revenue) + data.xmr_commitusd = coin_xmr.hashrate/(coin_xmr.revenue*coin_xmr.priceusd) + + if coin_xmr.supply == 0: + data.xmr_minerrevcap = 0 + else: + data.xmr_minerrevcap = 365*100*coin_xmr.revenue/coin_xmr.supply + + if coin_xmr.priceusd/5.01 > 0.02: + data.xmr_return = coin_xmr.priceusd/5.01 + else: + data.xmr_return = 0 + + if coin_xmr.inflation > 0: + data.xmr_inflation = coin_xmr.inflation + else: + data.xmr_inflation = 0 + + if coin_xmr.supply > 0: + data.xmr_supply = coin_xmr.supply + else: + data.xmr_supply = 0 + + if coin_xmr.supply - supply_xmr < 0.000001: + data.xmr_minerfeesntv = 0 + data.xmr_minerfeesusd = 0 + data.xmr_emissionntv = 0 + else: + data.xmr_minerfeesntv = coin_xmr.revenue - coin_xmr.supply + supply_xmr + data.xmr_minerfeesusd = (coin_xmr.revenue - coin_xmr.supply + supply_xmr)*coin_xmr.priceusd + data.xmr_emissionntv = coin_xmr.supply - supply_xmr + + if (coin_xmr.supply - supply_xmr)*coin_xmr.priceusd < 1000: + data.xmr_emissionusd = 0 + else: + data.xmr_emissionusd = (coin_xmr.supply - supply_xmr)*coin_xmr.priceusd + supply_xmr = coin_xmr.supply + else: + data.xmr_emissionntv = 0 + data.xmr_emissionusd = 0 + data.xmr_inflation = 0 + data.xmr_supply = 0 + data.xmr_return = 0 + data.xmr_minerrevntv = 0 + data.xmr_minerrevusd = 0 + data.xmr_minerfeesntv = 0 + data.xmr_minerfeesusd = 0 + data.xmr_transcostntv = 0 + data.xmr_transcostusd = 0 + data.xmr_commitntv = 0 + data.xmr_commitusd = 0 + data.xmr_metcalfeusd = 0 + data.xmr_metcalfebtc = 0 + data.xmr_pricebtc = 0 + data.xmr_priceusd = 0 + data.xmr_transacpercentage = 0 + data.xmr_marketcap = 0 + data.xmr_minerrevcap = 0 + data.xmr_transactions = 0 + data.xmr_blocksize = 0 + data.xmr_difficulty = 0 + + coins_dash = Coin.objects.filter(name='dash').filter(date=coin_btc.date) + if coins_dash: + for coin_dash in coins_dash: + if coin_dash.transactions > 0: + data.dash_transactions = coin_dash.transactions + else: + data.dash_transactions = 0 + if coin_dash.inflation > 0: + data.dash_inflation = coin_dash.inflation + else: + data.dash_inflation = 0 + + if coin_dash.priceusd > 0: + data.dash_marketcap = coin_dash.priceusd*coin_dash.supply + else: + data.dash_marketcap = 0 + else: + data.dash_inflation = 0 + data.dash_marketcap = 0 + data.dash_transactions = 0 + else: + data.xmr_emissionntv = 0 + data.xmr_emissionusd = 0 + data.xmr_inflation = 0 + data.xmr_supply = 0 + data.xmr_return = 0 + data.dash_inflation = 0 + data.dash_marketcap = 0 + data.dash_transactions = 0 + data.xmr_marketcap = 0 + data.xmr_minerrevntv = 0 + data.xmr_minerrevusd = 0 + data.xmr_minerfeesntv = 0 + data.xmr_minerfeesusd = 0 + data.xmr_transcostntv = 0 + data.xmr_transcostusd = 0 + data.xmr_commitntv = 0 + data.xmr_commitusd = 0 + data.xmr_metcalfeusd = 0 + data.xmr_metcalfebtc = 0 + data.xmr_pricebtc = 0 + data.xmr_priceusd = 0 + data.xmr_transacpercentage = 0 + data.xmr_minerrevcap = 0 + data.xmr_transactions = 0 + data.xmr_blocksize = 0 + data.xmr_difficulty = 0 + + if count_aux > 2800: + coins_zcash = Coin.objects.filter(name='zec').filter(date=coin_btc.date) + if coins_zcash: + for coin_zcash in coins_zcash: + if coin_zcash.transactions > 0: + data.zcash_transactions = coin_zcash.transactions + else: + data.zcash_transactions = 0 + if coin_zcash.inflation > 0: + data.zcash_inflation = coin_zcash.inflation + else: + data.zcash_inflation = 0 + + if coin_zcash.priceusd > 0: + data.zcash_marketcap = coin_zcash.priceusd*coin_zcash.supply + else: + data.zcash_marketcap = 0 + else: + data.zcash_inflation = 0 + data.zcash_marketcap = 0 + data.zcash_transactions = 0 + else: + data.zcash_inflation = 0 + data.zcash_marketcap = 0 + data.zcash_transactions = 0 + + if count_aux > 3600: + coins_grin = Coin.objects.filter(name='grin').filter(date=coin_btc.date) + if coins_grin: + for coin_grin in coins_grin: + if coin_grin.transactions > 0: + data.grin_transactions = coin_grin.transactions + else: + data.grin_transactions = 0 + if coin_grin.inflation > 0: + data.grin_inflation = coin_grin.inflation + else: + data.grin_inflation = 0 + + if coin_grin.priceusd > 0: + data.grin_marketcap = coin_grin.priceusd*coin_grin.supply + else: + data.grin_marketcap = 0 + else: + data.grin_inflation = 0 + data.grin_marketcap = 0 + data.grin_transactions = 0 + else: + data.grin_inflation = 0 + data.grin_marketcap = 0 + data.grin_transactions = 0 + + socials = Social.objects.filter(name='Bitcoin').filter(date=coin_btc.date) + if socials: + for social in socials: + data.btc_subscriberCount = social.subscriberCount + data.btc_commentsPerHour = social.commentsPerHour + data.btc_postsPerHour = social.postsPerHour + else: + data.btc_subscriberCount = 0 + data.btc_commentsPerHour = 0 + data.btc_postsPerHour = 0 + + socials = Social.objects.filter(name='Monero').filter(date=coin_btc.date) + if socials: + for social in socials: + data.xmr_subscriberCount = social.subscriberCount + data.xmr_commentsPerHour = social.commentsPerHour + data.xmr_postsPerHour = social.postsPerHour + else: + data.xmr_subscriberCount = 0 + data.xmr_commentsPerHour = 0 + data.xmr_postsPerHour = 0 + + socials = Social.objects.filter(name='CryptoCurrency').filter(date=coin_btc.date) + if socials: + for social in socials: + data.crypto_subscriberCount = social.subscriberCount + data.crypto_commentsPerHour = social.commentsPerHour + data.crypto_postsPerHour = social.postsPerHour + else: + data.crypto_subscriberCount = 0 + data.crypto_commentsPerHour = 0 + data.crypto_postsPerHour = 0 + + data.save() + count += 1 + + message = 'Total of ' + str(count) + ' data generated' + context = {'message': message} + return render(request, 'charts/maintenance.html', context) + +# Update database with between certain dates +# Only authorized users can do this +@login_required +def update_pageviews(request, date, value): + if request.user.username != "Administrador" and request.user.username != "Morpheus": + return render(request, 'users/error.html') + + try: + pageview = PageViews.objects.filter(date=date).delete() + date = datetime.datetime.strptime(date, '%Y-%m-%d') + pageview = PageViews() + pageview.date = date + pageview.unique_visitors = int(value/3) + pageview.total_pageviews = value + pageview.save() + except: + date = 'None' + + message = 'Pageviews updated on ' + str(date) + context = {'message': message} + return render(request, 'charts/maintenance.html', context) #################################################################################### # Views @@ -2314,11 +2785,7 @@ def pageviews(request): pageviews.append(user.total_pageviews) unique.append(user.unique_visitors) dates.append(datetime.datetime.strftime(user.date, '%Y-%m-%d')) - - print(dates) - print(unique) - print(pageviews) - + context = {'pageviews': pageviews, 'dates': dates, 'unique': unique} return render(request, 'charts/pageviews.html', context)