The code in this notebook will be executed by JavaScript using the Jupyter API. The docs for Jupyter itself aren't easy to navigate, but the API is identical to the older IPython project, so refer to https://github.com/ipython/ipython/wiki/Dev:-URL-mapping-of-IPython-notebook for details on all that's possible.
In [1]:
## We IF-GUARD this block because we do not want to include and load the data multiple times
if !isdefined(:df)
using DataFrames
using JSON
df = readtable("data.csv");
# Function to set histogram thresholds after dropping outliers based on IQR
function getSymmetricThresholds(results::DataFrame; timer::Symbol=:timers_t_done)
summary = summarystats(results[timer])
fw = (summary.q75-summary.q25)*1.5
low = round(Int64, max(summary.min, summary.q25-fw))
high = round(Int64, min(summary.max, summary.q75+fw))+1
thresholds::Array{Int64, 1} = []
nthresholds=25
range = high - low
for i in 0:nthresholds-1
push!(thresholds, round(Int64, low + i * range/nthresholds))
end
push!(thresholds, high)
if high < round(Int64, summary.max)
push!(thresholds, round(Int64, summary.max))
end
return thresholds
end
thresholds = getSymmetricThresholds(df)
groups = by(
df,
:user_agent_family,
rows -> DataFrame(
count = size(rows, 1),
median = median(rows[:timers_t_done]),
hist = JSON.json(hist(rows[:timers_t_done], thresholds)[2])
)
)
sort!(groups, rev=true, cols=[:count]);
end
Out[1]:
In [5]:
# browser-list-widget
JSON.json(groups[:user_agent_family])
Out[5]:
In [23]:
function getBrowserData(;browser="%browser%")
data = groups[groups[:user_agent_family] .== browser, :]
if size(data, 1) == 0
return "{}"
end
out = Dict(
"browser" => data[1, :user_agent_family],
"count" => data[1, :count],
"median" => data[1, :median],
"hist" => JSON.parse(data[1, :hist])
)
JSON.json(out)
end
Out[23]:
A DataFrame is great for passing data between Julia functions, but because Julia data structures are columnar, converting this to JSON gives us a terribly unwieldy structure. It's better to convert to a Dict and then JSON encode that.
As part of our token comment, we also indicate which part of the cell can be replaced with text from JavaScript. In this case, the %browser% token can be replaced with the actual name of the browser. Note that this is a convention we've just invented, so feel free to use your own convention.
In [25]:
# browser-data-widget param=%browser%
getBrowserData(browser="%browser%")
Out[25]:
In [ ]: