Collect and report statistics about usage of the api server

This commit is contained in:
Christine Dodrill 2015-08-27 20:32:07 -07:00
parent 5d3a94bffa
commit dc24fa6b48
2 changed files with 49 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import jester
import json import json
import os import os
import random import random
import stats
import strutils import strutils
import times import times
@ -66,6 +67,7 @@ routes:
"http://github.com/Xe/PonyAPI".uri.redirect "http://github.com/Xe/PonyAPI".uri.redirect
get "/all": get "/all":
stats.all.success.inc
resp Http200, myHeaders, pretty(%%episodes, 4) resp Http200, myHeaders, pretty(%%episodes, 4)
get "/newest": get "/newest":
@ -80,9 +82,11 @@ routes:
ep = episode ep = episode
break break
stats.newest.success.inc
resp Http200, myHeaders, pretty(%%ep, 4) resp Http200, myHeaders, pretty(%%ep, 4)
get "/random": get "/random":
stats.newest.success.inc
resp Http200, myHeaders, pretty(%%episodes.randomChoice(), 4) resp Http200, myHeaders, pretty(%%episodes.randomChoice(), 4)
get "/last_aired": get "/last_aired":
@ -97,6 +101,7 @@ routes:
ep = episodes[epid-1] ep = episodes[epid-1]
break break
stats.lastAired.success.inc
resp Http200, myHeaders, pretty(%%ep, 4) resp Http200, myHeaders, pretty(%%ep, 4)
get "/season/@snumber": get "/season/@snumber":
@ -105,8 +110,10 @@ routes:
eps: seq[Episode] = lc[x | (x <- episodes, x.season == season), Episode] eps: seq[Episode] = lc[x | (x <- episodes, x.season == season), Episode]
if eps.len == 0: if eps.len == 0:
stats.seasonLookup.fails.inc
resp Http404, myHeaders, pretty(%!"No episodes found", 4) resp Http404, myHeaders, pretty(%!"No episodes found", 4)
else: else:
stats.seasonLookup.success.inc
resp Http200, myHeaders, pretty(%%eps, 4) resp Http200, myHeaders, pretty(%%eps, 4)
get "/season/@snumber/episode/@epnumber": get "/season/@snumber/episode/@epnumber":
@ -121,8 +128,10 @@ routes:
ep = episode ep = episode
if ep.air_date == 0: if ep.air_date == 0:
stats.episodeLookup.fails.inc
resp Http404, myHeaders, pretty(%!"Not found", 4) resp Http404, myHeaders, pretty(%!"Not found", 4)
else: else:
stats.episodeLookup.success.inc
resp Http200, myHeaders, pretty(%%ep, 4) resp Http200, myHeaders, pretty(%%ep, 4)
get "/search": get "/search":
@ -130,6 +139,7 @@ routes:
query = @"q".toLower query = @"q".toLower
if query == "": if query == "":
stats.search.fails.inc
halt Http406, myHeaders, pretty(%!"Need to specify a query", 4) halt Http406, myHeaders, pretty(%!"Need to specify a query", 4)
var var
@ -137,8 +147,22 @@ routes:
lc[x | (x <- episodes, x.name.toLower.contains query), Episode] lc[x | (x <- episodes, x.name.toLower.contains query), Episode]
if eps.len == 0: if eps.len == 0:
stats.search.fails.inc
resp Http404, myHeaders, pretty(%!"No episodes found", 4) resp Http404, myHeaders, pretty(%!"No episodes found", 4)
else: else:
stats.search.success.inc
resp Http200, myHeaders, pretty(%%eps, 4) resp Http200, myHeaders, pretty(%%eps, 4)
get "/_stats":
resp Http200, myHeaders, pretty(%*
[
stats.all,
stats.newest,
stats.random,
stats.lastAired,
stats.seasonLookup,
stats.episodeLookup,
stats.search,
], 4)
runForever() runForever()

25
stats.nim Normal file
View File

@ -0,0 +1,25 @@
import json
type
Bucket* = object of RootObj
## A collection of stats
name*: string
fails*: int
success*: int
proc `%`*(b: Bucket): JsonNode =
%*
{
"name": b.name,
"fails": b.fails,
"success": b.success,
}
var
all* = Bucket(name: "all")
newest* = Bucket(name: "newest")
random* = Bucket(name: "random")
lastAired* = Bucket(name: "lastAired")
seasonLookup* = Bucket(name: "seasonLookup")
episodeLookup* = Bucket(name: "episodeLookup")
search* = Bucket(name: "search")