From 3f3f27d4c867b7df5b431e38be6a84f6b9a13ac0 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 14 Aug 2024 19:48:58 +0100 Subject: [PATCH] Major refactor of charts --- backend/app.py | 75 ++++++++++++++++++++-- content/data-downloads/singapore-srtm.md | 2 +- content/data-lab/global-business-growth.md | 5 +- hugo.toml | 5 +- layouts/partials/chart.html | 2 +- layouts/shortcodes/bitcoin-price.html | 2 +- layouts/shortcodes/chart.html | 7 +- layouts/shortcodes/download-data.html | 2 +- layouts/shortcodes/dropdown-filter.html | 7 -- static/css/charts.css | 5 ++ static/js/bitcoin-business-growth-chart.js | 43 ++++++++----- static/js/bitcoin-business-growth-table.js | 12 ++-- static/js/bitcoin-price.js | 27 ++++++-- static/js/chart-params.js | 29 +++++++++ static/js/feerate-percentile.js | 28 ++++++-- static/js/hashrate.js | 27 ++++++-- static/js/miner-rewards.js | 67 +++++++++++++++---- 17 files changed, 274 insertions(+), 71 deletions(-) delete mode 100644 layouts/shortcodes/dropdown-filter.html diff --git a/backend/app.py b/backend/app.py index 8229f49..a45ecf7 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,11 +1,42 @@ -from flask import Flask, jsonify, request, json, Response, send_from_directory, abort +from flask import Flask, g, jsonify, request, json, Response, send_from_directory, abort from flask_cors import CORS import orjson, os +import datetime +import time + app = Flask(__name__) CORS(app) FILES_DIRECTORY = '../data/' +today = datetime.datetime.today() + +@app.before_request +def start_timer(): + g.start = time.time() + +@app.after_request +def log(response): + now = time.time() + duration = round(now - g.start, 4) + dt = datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S') + + log_entry = { + 'timestamp': dt, + 'duration': duration, + 'method': request.method, + 'url': request.url, + 'status': response.status_code, + 'remote_addr': request.remote_addr, + 'user_agent': request.user_agent.string, + } + + log_line = ','.join(f'{key}={value}' for key, value in log_entry.items()) + + with open('api_logs.txt', 'a') as f: + f.write(log_line + '\n') + + return response @app.route('/bitcoin_business_growth_by_country', methods=['GET']) def business_growth(): @@ -29,8 +60,18 @@ def business_growth(): countries = [name.strip() for name in country_names.split(",")] filtered_data = [item for item in filtered_data if item['country_name'] in countries] - if cumulative_period_type: - filtered_data = [item for item in filtered_data if item['cumulative_period_type'] == cumulative_period_type] + if cumulative_period_type == "1 day": + delta = today - datetime.timedelta(days=2) + filtered_data = [item for item in filtered_data if item['cumulative_period_type'] == cumulative_period_type and delta <= datetime.datetime.strptime(item['date'], '%Y-%m-%d')] + elif cumulative_period_type == "7 day": + delta = today - datetime.timedelta(days=7) + filtered_data = [item for item in filtered_data if item['cumulative_period_type'] == cumulative_period_type and delta <= datetime.datetime.strptime(item['date'], '%Y-%m-%d')] + elif cumulative_period_type == "28 day": + delta = today - datetime.timedelta(days=28) + filtered_data = [item for item in filtered_data if item['cumulative_period_type'] == cumulative_period_type and delta <= datetime.datetime.strptime(item['date'], '%Y-%m-%d')] + elif cumulative_period_type == "365 day": + delta = today - datetime.timedelta(days=365) + filtered_data = [item for item in filtered_data if item['cumulative_period_type'] == cumulative_period_type and delta <= datetime.datetime.strptime(item['date'], '%Y-%m-%d')] # Sort by date sorted_data = sorted(filtered_data, key=lambda x: x['date'], reverse=True) @@ -40,14 +81,36 @@ def business_growth(): @app.route('/get_json/', methods=['GET']) def get_json(filename): + + period = request.args.get('period') + file_path = os.path.join(FILES_DIRECTORY, filename) if not os.path.isfile(file_path): abort(404) - with open(file_path, 'r') as file: - data = json.load(file) + with open(file_path, 'r') as f: + data = orjson.loads(f.read()) - return jsonify(data) + if period == "last 7 days": + delta = today - datetime.timedelta(days=7) + filtered_data = [item for item in data if delta <= datetime.datetime.strptime(item['date'], '%Y-%m-%d') <= today] + sorted_data = sorted(filtered_data, key=lambda x: x['date']) + elif period == "last 28 days": + delta = today - datetime.timedelta(days=28) + filtered_data = [item for item in data if delta <= datetime.datetime.strptime(item['date'], '%Y-%m-%d') <= today] + sorted_data = sorted(filtered_data, key=lambda x: x['date']) + elif period == "last 365 days": + delta = today - datetime.timedelta(days=365) + filtered_data = [item for item in data if delta <= datetime.datetime.strptime(item['date'], '%Y-%m-%d') <= today] + sorted_data = sorted(filtered_data, key=lambda x: x['date']) + elif period == "last 2 years": + delta = today - datetime.timedelta(days=730) + filtered_data = [item for item in data if delta <= datetime.datetime.strptime(item['date'], '%Y-%m-%d') <= today] + sorted_data = sorted(filtered_data, key=lambda x: x['date']) + else: + sorted_data = sorted(data, key=lambda x: x['date']) + + return jsonify(sorted_data) @app.route('/download/', methods=['GET']) def download_file(filename): diff --git a/content/data-downloads/singapore-srtm.md b/content/data-downloads/singapore-srtm.md index fb9c1ae..2022215 100644 --- a/content/data-downloads/singapore-srtm.md +++ b/content/data-downloads/singapore-srtm.md @@ -11,7 +11,7 @@ tags: ["QGIS", "SRTM", "DEM", "Raster", "download"] {{< figure src="/pics/blog/batch-import-postgis-rasters/singapore-final.webp" width="300">}} Download the Digital Elevation Model featured in [this](/blog/batch-import-postgis-rasters/) blog. -{{< download-data src="https://api.baseddata.io/download/singapore-srtm-dem.tif" name="singapore-srtm-dem.tif" >}} +{{< download-data src="/singapore-srtm-dem.tif" name="singapore-srtm-dem.tif" >}} #### Citations NASA Shuttle Radar Topography Mission (SRTM)(2013). Shuttle Radar Topography Mission (SRTM) Global. Distributed by OpenTopography. https://doi.org/10.5069/G9445JDF. Accessed: 2024-08-06 diff --git a/content/data-lab/global-business-growth.md b/content/data-lab/global-business-growth.md index 860f4ed..9333eb3 100644 --- a/content/data-lab/global-business-growth.md +++ b/content/data-lab/global-business-growth.md @@ -8,13 +8,12 @@ summary: "This analysis uses OpenStreetMaps data to chart the yearly growth of b tags: ["Bitcoin", "Stats", "OpenStreetMaps"] --- -The table below illustrates growth of businesses worldwide that accept bitcoin as payment for products or services. The accompanying chart displays the yearly cumulative number of bitcoin-accepting businesses for the countries selected in the table. The data is sourced from OpenStreetMaps and is updated approximately every 2 hours. +The table below illustrates growth of businesses worldwide that accept bitcoin as payment for products or services. The accompanying chart displays the cumulative number of bitcoin-accepting businesses for the countries selected in the table. The data is sourced from OpenStreetMaps and is updated approximately every 2 hours. You can select the growth period of interest from the drop-down, which updates the values in the table. The table shows the ***Previous*** value, which was the number of businesses *n* days ago specified in the drop-down. The ***Current*** value is the number of businesses as of the latest update. The table also shows the ***Absolute Difference***, and the ***Percent Difference*** between this period. -The chart always reflects the countries selected in the table. There is a zoom feature on the chart to focus in on a period of interest. +The chart always reflects the countries selected in the table.
-Select growth period: {{< dropdown-filter id=cumulative_period_type select="365 day,28 day,7 day,1 day" >}} {{< chart src="/js/bitcoin-business-growth-chart.js" >}} {{< table src="/js/bitcoin-business-growth-table.js" >}} diff --git a/hugo.toml b/hugo.toml index 0bb6ca7..1a2f0c4 100644 --- a/hugo.toml +++ b/hugo.toml @@ -1,7 +1,10 @@ -baseURL = 'https://baseddata.io/' +baseURL = 'baseddata.io' languageCode = 'en-gb' title = 'Based Data' +[params] + apiURL = 'http://localhost:5000' + [markup.highlight] pygmentsUseClasses = false codeFences = true diff --git a/layouts/partials/chart.html b/layouts/partials/chart.html index 1655e08..c663914 100644 --- a/layouts/partials/chart.html +++ b/layouts/partials/chart.html @@ -1,7 +1,7 @@
+
-
diff --git a/layouts/shortcodes/bitcoin-price.html b/layouts/shortcodes/bitcoin-price.html index 388a395..9e8a52e 100644 --- a/layouts/shortcodes/bitcoin-price.html +++ b/layouts/shortcodes/bitcoin-price.html @@ -4,7 +4,7 @@

+{{ $id := .Get "src" | md5 }} {{ partial "chart.html" (dict "src" (.Get "src") +"id" $id) }} diff --git a/layouts/shortcodes/download-data.html b/layouts/shortcodes/download-data.html index 84457f3..894e09b 100644 --- a/layouts/shortcodes/download-data.html +++ b/layouts/shortcodes/download-data.html @@ -5,7 +5,7 @@