using Plots
using Distributions

μfast = 0
σfast = 1
dfast = LogNormal(μfast, σfast)
x = rand(dfast, 1000)
histogram(x, dpi=600, xlabel="Page Load Time (s)", ylabel="Number of Loads", title="Fast User Experience", legend = false)
png("images/goodpageload.png")

dslow = LogNormal(3, 0.4)
x = rand(dslow, 1000)
histogram(x, dpi=600, xlabel="Page Load Time (s)", ylabel="Number of Loads", title="Slow User Experience", legend = false)
png("images/badpageload.png")

users = zeros(100)
usercount = zeros(100)

for i in 1:100
    while true
        if i < 90
            users[i] += rand(dfast)
        else
            users[i] += rand(dslow)
        end
        usercount[i] += 1.0
        if users[i] > 3600
            break            
        end
    end
end

pageloads = rand(dfast, Int64(sum(usercount[1:90])))
append!(pageloads, rand(dslow, Int64(sum(usercount[91:100]))))
pageloads = log.(pageloads)
histogram(pageloads, label = "Simulated Page Loads", xlabel="Logarithm of Page Load Times", ylabel="Number of Page Loads", dpi=600)
vline!([μfast], label="Metric 1", linewidth=2)
vline!([log(mean(exp.(pageloads)))], label="Metric 2", linewidth=2)
vline!([log(median(users./usercount))], label="Metric 3", linewidth=2)
vline!([log(mean(users./usercount))], label="Metric 4", linewidth=2)
png("images/examplesimulation.png")

for k in 75:99
    users = zeros(100)
    usercount = zeros(100)

    for i in 1:100
        while true
            if i < k
                users[i] += rand(dfast)
            else
                users[i] += rand(dslow)
            end
            usercount[i] += 1.0
            if users[i] > 3600
                break            
            end
        end
    end

    pageloads = rand(dfast, Int64(sum(usercount[1:k])))
    append!(pageloads, rand(dslow, Int64(sum(usercount[k+1:100]))))
    pageloads = log.(pageloads)
    if k==75
        scatter([k], [exp(μfast)], label="Metric 1", linewidth=2, color="red", dpi=600, xlabel="Percentage of Users with fast experience", ylabel="Metric Calculated Page Load Time (s)")
        scatter!([k], [mean(exp.(pageloads))], label="Metric 2", linewidth=2, color="black")
        scatter!([k], [median(users./usercount)], label="Metric 3", linewidth=2, color="green")
        scatter!([k], [mean(users./usercount)], label="Metric 4", linewidth=2, color="blue")
    else
        scatter!([k], [exp(μfast)], linewidth=2, color="red", label=false)
        scatter!([k], [mean(exp.(pageloads))], linewidth=2, color="black", label=false)
        scatter!([k], [median(users./usercount)], linewidth=2, color="green", label=false)
        scatter!([k], [mean(users./usercount)], linewidth=2, color="blue", label=false)
    end
end    
png("images/varyingusers.png")