### A Pluto.jl notebook ###
# v0.20.2

using Markdown
using InteractiveUtils

# ╔═╡ cba1228a-b95e-11ef-0e06-5df9120a08ad
begin
    using Pkg
    Pkg.activate(".")
end

# ╔═╡ 0f44ab6d-8f68-495e-95b9-9168cdf22004
begin
    using SQLite
    using DataFrames
    using CairoMakie
    using Dates
end

# ╔═╡ 436f0b91-4cc8-4c22-b625-cfd4779e20ec
db = SQLite.DB("../psboard_qaqc.db")

# ╔═╡ 085176e5-c0f9-4cf2-8416-581a8d20c941
SQLite.tables(db)

# ╔═╡ 363afd96-5b0d-4b09-b480-64a40ddce8cd
qaqc_single_run_results =
    DBInterface.execute(db, "select * from qaqc_single_run_results") |> DataFrame

# ╔═╡ 0467fb45-6b97-4148-8343-0e4b06a7c2ec
qaqc_runs = let
    df = DBInterface.execute(db, "select * from qaqc_runs") |> DataFrame
    transform!(df, :run_datetime => ByRow(passmissing(DateTime)) => :run_datetime)
    df
end

# ╔═╡ 4c30b33a-e15e-4b3c-a0f5-50184e7a711f
qaqc_campaigns = let
    df = DBInterface.execute(db, "select * from qaqc_campaigns") |> DataFrame
    transform!(
        df,
        :start_date => ByRow(DateTime) => :start_date,
        :end_date => ByRow(DateTime) => :end_date,
    )
    df
end

# ╔═╡ 560bbdc3-26a4-4fe9-8459-e5ecaad04082
qaqc_dispatch = let
    df = DBInterface.execute(db, "select * from qaqc_dispatch") |> DataFrame
    transform!(df, :qaqc_campaign_id => ByRow(x -> begin
            if x == 0
                x + 1
            else
                x
            end
        end) => :qaqc_campaign_id)
    df
end

# ╔═╡ 89d90098-0324-4bc0-8881-819cd8685f5c
df_num_tested_boards = let
    df = leftjoin(
        select(qaqc_single_run_results, [:psboard_id, :runid]),
        qaqc_runs,
        on = :runid => :id,
    )
    sort!(df, :runid)
    df = combine(groupby(df, :psboard_id), first)
    df = combine(
        groupby(df, :campaign_id),
        :psboard_id => length ∘ unique => :num_tested_boards,
    )
    df = leftjoin(df, qaqc_campaigns, on = :campaign_id => :id)
    df.num_total_tested = accumulate(+, df.num_tested_boards)
    disallowmissing!(df, :end_date)
    df
end

# ╔═╡ fa7565e0-2662-4f2c-8e1c-8f6158557d58
df_num_dispatched = let
    df = combine(groupby(qaqc_dispatch, :qaqc_campaign_id), nrow => :num_dispatched)
    df.num_total_dispatched = accumulate(+, df.num_dispatched)
    df = leftjoin(df, qaqc_campaigns, on = :qaqc_campaign_id => :id)
    disallowmissing!(df, :end_date)
    df
end

# ╔═╡ 8a2ae739-0da0-49cc-9614-74ac7e133599
let
    fig = Figure(size = (600, 400))
    ax = Axis(fig[1, 1], limits = (nothing, (0, 1500)), title = "PS-Board QAQC progress")

    n_campaings = nrow(df_num_dispatched)
    num_testeds = let
        v = df_num_tested_boards.num_total_tested |> copy
        hcat([0, v[1:(end - 1)]...], v) |> transpose |> (a -> reshape(a, 2 * n_campaings))
    end
    num_dispatched = let
        v = df_num_dispatched.num_total_dispatched |> copy
        hcat([0, v[1:(end - 1)]...], v) |> transpose |> (a -> reshape(a, 2 * n_campaings))
    end
    start_end_dates =
        vcat(
            reshape(df_num_dispatched.start_date, (1, n_campaings)),
            reshape(df_num_dispatched.end_date, (1, n_campaings)),
        ) |>
        (a -> reshape(a, 12)) |>
        skipmissing |>
        collect

    @info "" num_testeds num_dispatched
    lines!(ax, start_end_dates, num_testeds, label = "tested")
    lines!(ax, start_end_dates, num_dispatched, label = "passed")
	middle_dates = let
		starts = df_num_dispatched.start_date
		@. (df_num_dispatched.end_date - starts) / 2 + starts
	end
	text!(
		ax,
		middle_dates,
		df_num_tested_boards.num_total_tested,
		text = ["1st", "2nd", "3rd", ("$(i)th" for i in 4:n_campaings)...],
		align = (:center, :bottom),
	)
    axislegend(position = :rb)
	save("qaqc_progress.svg", fig)

    fig
end

# ╔═╡ Cell order:
# ╠═cba1228a-b95e-11ef-0e06-5df9120a08ad
# ╠═0f44ab6d-8f68-495e-95b9-9168cdf22004
# ╠═436f0b91-4cc8-4c22-b625-cfd4779e20ec
# ╠═085176e5-c0f9-4cf2-8416-581a8d20c941
# ╠═363afd96-5b0d-4b09-b480-64a40ddce8cd
# ╠═0467fb45-6b97-4148-8343-0e4b06a7c2ec
# ╠═4c30b33a-e15e-4b3c-a0f5-50184e7a711f
# ╠═560bbdc3-26a4-4fe9-8459-e5ecaad04082
# ╠═89d90098-0324-4bc0-8881-819cd8685f5c
# ╠═fa7565e0-2662-4f2c-8e1c-8f6158557d58
# ╠═8a2ae739-0da0-49cc-9614-74ac7e133599
