Permalink
Browse files

Calculate rolling average stats.

Signed-off-by: Laura <l@veriny.tf>
  • Loading branch information...
Fuyukai committed Aug 3, 2017
1 parent a67d453 commit 8f9f4815ab5b82cfc389db590c3b9cba6acec549
Showing with 29 additions and 15 deletions.
  1. +9 −4 api.md
  2. +16 −8 owapi/v3/parsing.py
  3. +4 −3 owapi/v3/v3_util.py
View
13 api.md
@@ -1,5 +1,9 @@
# OWAPI API Docs
## Changelog
- 2017-08-03 - **Added new `rolling_averages` key. This contains averages over 10 minutes.**
## Notes
Regions and platforms can be overridden with URL parameters.
@@ -15,14 +19,15 @@ You can override the platform with `?platform=<pc|xbl|psn>`. This defaults to `p
### Formatting
The default output is standard JSON, but you can prettify it with `?format=json_pretty"` to have it in a more readable
format. (The other option is installing a browser plugin, such as [JSONView](https://jsonview.com/).
The default output is standard JSON, but you can prettify it with `?format=json_pretty"` to have it
in a more readable format. (The other option is installing a browser plugin, such as
[JSONView](https://jsonview.com/).
#### Field formatting
Field names inside the inner dict will correspond to all of the cards shown inside the
[PlayOverwatch](https://playoverwatch.com/en-us/career/pc/eu/Downy-2877) pages corresponding to the users, with a
slight formatting tweak:
[PlayOverwatch](https://playoverwatch.com/en-us/career/pc/eu/Downy-2877) pages corresponding to
the users, with a slight formatting tweak:
- All nonalphanumeric characters are removed
- All spaces are replaced with underscores
View
@@ -204,8 +204,10 @@ def bl_parse_stats(parsed, mode="quickplay"):
built_dict["overall_stats"]["wins"] = wins
# Build a dict using the stats.
_t_d = {}
_a_d = {}
game_stats = {}
average_stats = {}
rolling_average_stats = {}
for subbox in stat_groups:
trs = subbox.findall(".//tbody/tr")
# Update the dict with [0]: [1]
@@ -214,22 +216,28 @@ def bl_parse_stats(parsed, mode="quickplay"):
# Try and parse out the value. It might be a time!
# If so, try and extract the time.
nvl = util.try_extract(value)
if 'average' in name.lower():
_a_d[name.replace("_average", "_avg")] = nvl
average_stats[name.replace("_average", "_avg")] = nvl
elif '_avg_per_10_min' in name.lower():
# 2017-08-03 - calculate rolling averages.
name = name.lower().replace("_avg_per_10_min", "")
rolling_average_stats[name] = nvl
else:
_t_d[name] = nvl
game_stats[name] = nvl
# Manually add the KPD.
try:
_t_d["kpd"] = round(_t_d["eliminations"] / _t_d["deaths"], 2)
game_stats["kpd"] = round(game_stats["eliminations"] / game_stats["deaths"], 2)
except KeyError:
# They don't have any eliminations/deaths.
# Set the KPD to 0.0.
# See: #106
_t_d["kpd"] = 0
game_stats["kpd"] = 0
built_dict["game_stats"] = _t_d
built_dict["average_stats"] = _a_d
built_dict["game_stats"] = game_stats
built_dict["average_stats"] = average_stats
built_dict["rolling_average_stats"] = rolling_average_stats
built_dict["competitive"] = mode == "competitive"
if "games" not in built_dict["overall_stats"]:
View
@@ -18,7 +18,8 @@
# Bad useragent response text.
BAD_USERAGENT = {
"error": 400,
"msg": "Hi! To prevent abuse of this service, it is required that you customize your user agent."
"msg": "Hi! To prevent abuse of this service, it is required that you "
"customize your user agent."
}, 400, {"Content-Type": "application/json"}
if not os.path.exists(RATES_PATH):
@@ -44,7 +45,7 @@ def check_default_useragents(useragent: str):
return DISALLOW_AGENTS.match(useragent)
def with_ratelimit(bucket: str, timelimit: int=None, max_reqs: int=0):
def with_ratelimit(bucket: str, timelimit: int = None, max_reqs: int = 0):
"""
Defines a function to rate limit for.
@@ -69,7 +70,7 @@ def _rl_inner1(func):
assert isinstance(ctx.redis, aioredis.Redis)
# Get the IP.
ip = ctx.request.headers.get("X-Real-IP") or \
ctx.request.headers.get("X-Forwarded-For") or ctx.request.remote_addr
ctx.request.headers.get("X-Forwarded-For") or ctx.request.remote_addr
# Build the ratelimit string.
built = "{bucket}:{ip}:ratelimit".format(bucket=bucket, ip=ip)

0 comments on commit 8f9f481

Please sign in to comment.