### A Pluto.jl notebook ###
# v0.20.4

using Markdown
using InteractiveUtils

# ╔═╡ 255850f9-3984-4422-8710-6ab497904d4f
begin
    using Pkg
    Pkg.activate("..")
    Pkg.status()
    true || include("../src/PSBoardDataBase.jl")
end

# ╔═╡ 6861385c-d9a1-4115-9354-3b0bbbbf433c
begin
    using PSBoardDataBase
    using DataFrames
    using SQLite
    using DBInterface
    # using CairoMakie
    using PlutoUI
end

# ╔═╡ c639141c-48a1-424f-be33-026ab04a7d33
TableOfContents()

# ╔═╡ ad5e90ba-fa29-4ae1-b365-2e3dbb9dbe72
const SLAVELOGS_DIR = "../test/input/slavelogs/main/"

# ╔═╡ 2f5c2984-ae32-467a-ba94-cbff9b954857
db = SQLite.DB("../psboard_qaqc.db")

# ╔═╡ d3ee0e2e-ff6c-4cb3-8985-53516a9d2b8f
qaqc_single_run_results =
    DBInterface.execute(db, sql"select * from qaqc_single_run_results") |> DataFrame

# ╔═╡ 7b6caa7a-d565-4b1d-baed-9f0fa18712ec
let
    PSBoardDataBase.SlaveLogParser.parse_slavelog_file(
        joinpath(SLAVELOGS_DIR, "164_89.txt"),
    )
end

# ╔═╡ f4fc10fa-03ca-4468-b3d3-918f39ad9e35
qaqc_single_run_results_with_ppconfig_error = transform(
    qaqc_single_run_results,
    [:psboard_id, :runid] =>
        ByRow(
            (psbid, runid) -> begin
                if psbid == 999999
                    return missing
                end
                logfilepath = joinpath(SLAVELOGS_DIR, "$(psbid)_$(runid).txt")
                if !ispath(logfilepath)
                    return missing
                end
				if psbid == 723 && runid == 434
					return missing
				end
                slavelog =
                    PSBoardDataBase.SlaveLogParser.parse_slavelog_file(logfilepath)
                result_asdtp = slavelog.asdtp
                if isempty(result_asdtp)
                    return missing
                end
                result_asdtp[1].ppconfig_error |> UInt32
            end,
        ) => :ppconfig_error,
)

# ╔═╡ e8933a41-7500-409a-8650-ec52be37e483
md"""
### 1回試験でQSPIpをパスしてPPConfig_errorが0ではなかったもの
"""

# ╔═╡ 81a242ac-ac64-4794-8c8b-4ec439db4e90
let
    df = filter(
        :ppconfig_error => (x -> !ismissing(x) && x != 0),
        qaqc_single_run_results_with_ppconfig_error,
    )
    filter!(:qspip => ==(1), df)
    df
end

# ╔═╡ cc16f82d-8c45-4e75-8b5b-5abe362a3526
slave_logs = let
    entries = readdir(SLAVELOGS_DIR, join = true)
    Iterators.filter(entries) do entry_name
        contains(r"^\d+_\d+\.txt")(basename(entry_name)) ||
            contains(r"^\d+_\d+_longrun\.txt")(basename(entry_name))
    end |> collect
end

# ╔═╡ Cell order:
# ╠═255850f9-3984-4422-8710-6ab497904d4f
# ╠═6861385c-d9a1-4115-9354-3b0bbbbf433c
# ╠═c639141c-48a1-424f-be33-026ab04a7d33
# ╠═ad5e90ba-fa29-4ae1-b365-2e3dbb9dbe72
# ╠═2f5c2984-ae32-467a-ba94-cbff9b954857
# ╠═d3ee0e2e-ff6c-4cb3-8985-53516a9d2b8f
# ╠═7b6caa7a-d565-4b1d-baed-9f0fa18712ec
# ╠═f4fc10fa-03ca-4468-b3d3-918f39ad9e35
# ╠═e8933a41-7500-409a-8650-ec52be37e483
# ╠═81a242ac-ac64-4794-8c8b-4ec439db4e90
# ╠═cc16f82d-8c45-4e75-8b5b-5abe362a3526
