add lookback query param; add rangeselector buttons

This commit is contained in:
crflynn
2018-10-14 01:38:45 -04:00
parent 7b1cd8193b
commit 782283258e
5 changed files with 322 additions and 260 deletions

View File

@@ -68,37 +68,5 @@
}
}
]
},
"percentages_fill":{
"data":[
{
"x":[
"2017-05-01",
"2017-05-02",
"2017-05-03"
],
"y":[
"2",
"5",
"4"
],
"text":[
"2",
"5",
"4"
],
"hoverinfo": "x+text+name",
"name":"Downloads",
"type":"scatter",
"mode":"lines",
"connectgaps":false,
"line":{
"shape":"linear",
"smoothing":1,
"width":2
},
"fill":"tonexty"
}
]
}
}

View File

@@ -5,7 +5,7 @@
"height": 400,
"margin": {
"r": 100,
"t":40,
"t": 80,
"autoexpand": true,
"b": 80,
"l": 100,
@@ -36,14 +36,42 @@
],
"title": "Date",
"titlefont": {
"family":"'Geneva', Verdana, Geneva, sans-serif",
"family": "Geneva, Verdana, Geneva, sans-serif",
"size": 16,
"color": "#7f7f7f"
},
"showline": true,
"linecolor": "rgba(148, 148, 148, 1)",
"linewidth": 2,
"tickangle":-45
"tickangle": -45,
"rangeselector": {
"buttons": [
{
"step": "day",
"stepmode": "backward",
"count": 31,
"label": "30d"
},
{
"step": "day",
"stepmode": "backward",
"count": 61,
"label": "60d"
},
{
"step": "day",
"stepmode": "backward",
"count": 91,
"label": "90d"
},
{
"step": "day",
"stepmode": "backward",
"count": 181,
"label": "all"
}
]
}
},
"yaxis": {
"hoverformat": ",.0",
@@ -57,7 +85,35 @@
"ticksuffix": "",
"tickmode": "auto",
"linecolor": "rgba(148, 148, 148, 1)",
"linewidth":2
"linewidth": 2,
"rangeselector": {
"buttons": [
{
"step": "day",
"stepmode": "backward",
"count": 31,
"label": "30d"
},
{
"step": "day",
"stepmode": "backward",
"count": 61,
"label": "60d"
},
{
"step": "day",
"stepmode": "backward",
"count": 91,
"label": "90d"
},
{
"step": "day",
"stepmode": "backward",
"count": 181,
"label": "all"
}
]
}
}
},
"config": {
@@ -81,7 +137,7 @@
"height": 400,
"margin": {
"r": 100,
"t":40,
"t": 80,
"autoexpand": true,
"b": 80,
"l": 100,
@@ -112,7 +168,7 @@
],
"title": "Date",
"titlefont": {
"family":"'Geneva', Verdana, Geneva, sans-serif",
"family": "Geneva, Verdana, Geneva, sans-serif",
"size": 16,
"color": "#7f7f7f"
},

View File

@@ -415,7 +415,7 @@ def etl():
if __name__ == "__main__":
date = "2018-07-28"
date = "2018-10-13"
env = "prod"
print(date, env)
print(get_daily_download_stats(env, date))

View File

@@ -14,7 +14,7 @@ from pypistats.models.download import RecentDownloadCount
from pypistats.models.download import SystemDownloadCount
blueprint = Blueprint('api', __name__, url_prefix='/api')
blueprint = Blueprint("api", __name__, url_prefix="/api")
@blueprint.route("/")
@@ -27,7 +27,7 @@ def api():
def api_downloads_recent(package):
"""Get the recent downloads of a package."""
package = package.replace(".", "-")
category = request.args.get('period')
category = request.args.get("period")
if category is None:
downloads = RecentDownloadCount.query.\
filter_by(package=package).all()
@@ -55,12 +55,12 @@ def api_downloads_recent(package):
def api_downloads_overall(package):
"""Get the overall download time series of a package."""
package = package.replace(".", "-")
mirrors = request.args.get('mirrors')
if mirrors == 'true':
mirrors = request.args.get("mirrors")
if mirrors == "true":
downloads = OverallDownloadCount.query.\
filter_by(package=package, category="with_mirrors").\
order_by(OverallDownloadCount.date).all()
elif mirrors == 'false':
elif mirrors == "false":
downloads = OverallDownloadCount.query.\
filter_by(package=package, category="without_mirrors").\
order_by(OverallDownloadCount.date).all()

View File

@@ -12,6 +12,7 @@ from flask import g
from flask import json
from flask import redirect
from flask import render_template
from flask import request
from flask_wtf import FlaskForm
import requests
from wtforms import StringField
@@ -97,8 +98,17 @@ def package_page(package):
"""Render the package page."""
package = package.replace(".", "-")
# Recent download stats
try:
# Take the min of the lookback and 180
lookback = min(abs(int(request.args.get("lookback", 180))), 180)
except ValueError:
lookback = 180
start_date = str(datetime.date.today() - datetime.timedelta(lookback))
recent_downloads = RecentDownloadCount.query.\
filter_by(package=package).all()
if len(recent_downloads) == 0:
return redirect(f"/search/{package}")
recent = {r: 0 for r in RECENT_CATEGORIES}
@@ -125,8 +135,8 @@ def package_page(package):
model_data = []
for model in MODELS:
records = model.query.filter_by(package=package).\
order_by(model.date,
model.category).all()
filter(model.date >= start_date).\
order_by(model.date, model.category).all()
if model == OverallDownloadCount:
metrics = ["downloads"]
@@ -144,6 +154,8 @@ def package_page(package):
plots = []
for model in model_data:
plot = deepcopy(current_app.config["PLOT_BASE"])[model["metric"]]
# Set data
data = []
for category, values in model["data"].items():
base = deepcopy(current_app.config["DATA_BASE"][model["metric"]]["data"][0])
@@ -154,12 +166,38 @@ def package_page(package):
base["name"] = category.title()
data.append(base)
plot["data"] = data
# Add titles
if model["metric"] == "percentages":
plot["layout"]["title"] = \
f"Daily Download Proportions of {package} package - {model['name'].title().replace('_', ' ')}" # noqa
else:
plot["layout"]["title"] = \
f"Daily Download Quantity of {package} package - {model['name'].title().replace('_', ' ')}" # noqa
# Explicitly set range
plot["layout"]["xaxis"]["range"] = [str(records[0].date - datetime.timedelta(1)), str(datetime.date.today())]
# Add range buttons
plot["layout"]["xaxis"]["rangeselector"] = {"buttons": []}
drange = (datetime.date.today() - records[0].date).days
for k in [30, 60, 90, 120, 9999]:
if k <= drange:
plot["layout"]["xaxis"]["rangeselector"]["buttons"].append({
"step": "day",
"stepmode": "backward",
"count": k+1,
"label": f"{k}d"
})
else:
plot["layout"]["xaxis"]["rangeselector"]["buttons"].append({
"step": "day",
"stepmode": "backward",
"count": drange + 1,
"label": "all"
})
break
plots.append(plot)
return render_template(