Skip to content

Commit

Permalink
Judgement day
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Dec 6, 2021
1 parent af8de51 commit 001d96d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Actions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function process(
@warn "Notebook file does not have desired hash. This probably means that the file changed too quickly. Continuing and hoping for the best!" s.path new_hash s.desired_hash
end

keep_running = settings.SliderServer.enabled || settings.Export.static_export_state
keep_running = settings.SliderServer.enabled || settings.Precompute.enabled
skip_cache = keep_running || path settings.Export.ignore_cache

cached_state = skip_cache ? nothing : try_fromcache(settings.Export.cache_dir, new_hash)
Expand Down Expand Up @@ -122,7 +122,7 @@ function process(
desired_hash=s.desired_hash,
run=run,
)
if settings.Export.static_export_state
if settings.Precompute.enabled
generate_precomputed_staterequests(
new_session;
settings,
Expand Down
14 changes: 12 additions & 2 deletions src/Configuration.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Configurations
import TOML
import Pluto
export SliderServerSettings, ExportSettings, PlutoDeploySettings, get_configuration
export SliderServerSettings,
ExportSettings, PrecomputeSettings, PlutoDeploySettings, get_configuration
using TerminalLoggers: TerminalLogger
using Logging: global_logger
using FromFile
Expand All @@ -23,10 +24,18 @@ using FromFile
simulated_lag::Real = 0
end


@extract_docs @option struct PrecomputeSettings
"Precompute slider server requests?"
enabled::Bool = false
"List of notebook files to skip precomputation. Provide paths relative to `start_dir`."
exclude::Vector{String} = String[]
max_filesize_per_group::Integer = 10_000_000
end

@extract_docs @option struct ExportSettings
"Generate static HTML files? This setting can only be `false` if you are also running a slider server."
enabled::Bool = true
static_export_state::Bool = false
"Folder to write generated HTML files to (will create directories to preserve the input folder structure). The behaviour of the default value depends on whether you are running the slider server, or just exporting. If running the slider server, we use a temporary directory; otherwise, we use `start_dir` (i.e. we generate each HTML file in the same folder as the notebook file)."
output_dir::Union{Nothing,String} = nothing
"List of notebook files to skip. Provide paths relative to `start_dir`."
Expand Down Expand Up @@ -54,6 +63,7 @@ end
@option struct PlutoDeploySettings
SliderServer::SliderServerSettings = SliderServerSettings()
Export::ExportSettings = ExportSettings()
Precompute::PrecomputeSettings = PrecomputeSettings()
Pluto::Pluto.Configuration.Options = Pluto.Configuration.Options()
end

Expand Down
7 changes: 4 additions & 3 deletions src/precomputed/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ function variable_groups(

VariableGroupPossibilities(;
names=names,
num_possibilities=prod(Int64.(length.(possible_values))),
possible_values=possible_values,
not_available=not_available,
num_possibilities=prod(Int64.(length.(possible_values))),
)
end
end
Expand Down Expand Up @@ -102,6 +102,7 @@ function generate_precomputed_staterequests_report(
num_possibilities=group.num_possibilities,
not_available=group.not_available,
file_size_sample_distribution=stat,
settings,
)
end |> PrecomputedSampleReport
end
Expand All @@ -127,7 +128,7 @@ function generate_precomputed_staterequests(
bondconnections_path =
joinpath(output_dir, "bondconnections", URIs.escapeuri(current_hash))
write(bondconnections_path, Pluto.pack(run.bond_connections))
@info "Written bond connections to " bondconnections_path
@debug "Written bond connections to " bondconnections_path

groups = variable_groups(connections; pluto_session, notebook=run.notebook)

Expand All @@ -154,7 +155,7 @@ function generate_precomputed_staterequests(

write(write_path, Pluto.pack(result))

@info "Written state request to " write_path values =
@debug "Written state request to " write_path values =
(; (zip(group.names, combination))...)
end
end
Expand Down
84 changes: 78 additions & 6 deletions src/precomputed/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,74 @@ import Statistics
using Distributions
import Markdown

@from "../Configuration.jl" import PlutoDeploySettings

const Reason = Symbol

Base.@kwdef struct VariableGroupPossibilities
Base.@kwdef struct Judgement
should_precompute::Bool = false
not_available::Bool = false
close_to_filesize_limit::Bool = false
exceeds_filesize_limit::Bool = false
end

struct VariableGroupPossibilities
names::Vector{Symbol}
possible_values::Vector{Any}
possible_values::Vector
not_available::Dict{Symbol,Reason}
# size info:
file_size_sample_distribution::Union{Nothing,Distribution} = nothing
file_size_sample_distribution::Union{Nothing,Distribution}
num_possibilities::Int64

judgement::Judgement
end

function VariableGroupPossibilities(;
names::Vector{Symbol},
possible_values::Vector,
not_available::Dict{Symbol,Reason},
# size info:
num_possibilities::Int64,
file_size_sample_distribution::Union{Nothing,Distribution}=nothing,
settings::Union{Nothing,PlutoDeploySettings}=nothing,
)
is_not_available = !isempty(not_available)

if !isa(file_size_sample_distribution, Nothing)
@assert settings isa PlutoDeploySettings

limit = settings.Precompute.max_filesize_per_group
current = mean(file_size_sample_distribution)

exceeds_filesize_limit = current > limit
close_to_filesize_limit = current > limit * 0.7
else
exceeds_filesize_limit = close_to_filesize_limit = false
end

j = Judgement(;
should_precompute=!is_not_available && !exceeds_filesize_limit,
exceeds_filesize_limit,
close_to_filesize_limit,
not_available=is_not_available,
)

VariableGroupPossibilities(
names,
possible_values,
not_available,
file_size_sample_distribution,
num_possibilities,
j,
)
end

Base.@kwdef struct PrecomputedSampleReport
groups::Vector{VariableGroupPossibilities}
# size info:
file_size_sample_distribution::Union{Nothing,Distribution} = nothing
num_possibilities::Int64
judgement::Judgement
end

function PrecomputedSampleReport(groups::Vector{VariableGroupPossibilities})
Expand All @@ -32,15 +83,28 @@ function PrecomputedSampleReport(groups::Vector{VariableGroupPossibilities})
group.file_size_sample_distribution
end |> sum_distributions

PrecomputedSampleReport(; groups, num_possibilities, file_size_sample_distribution)
judgement = Judgement(;
should_precompute=any(g.judgement.should_precompute for g in groups),
not_available=any(g.judgement.not_available for g in groups),
exceeds_filesize_limit=any(g.judgement.exceeds_filesize_limit for g in groups),
close_to_filesize_limit=any(g.judgement.close_to_filesize_limit for g in groups),
)

PrecomputedSampleReport(;
groups,
num_possibilities,
file_size_sample_distribution,
judgement,
)
end


function Base.show(io::IO, m::MIME"text/plain", p::PrecomputedSampleReport)
groups = sort(p.groups; by=g -> mean(g.file_size_sample_distribution), rev=true)

r = Markdown.parse(
"""
# Precomputed state summary
# $(pretty(p.judgement)) Precomputed state summary
Total size estimate: $(p.num_possibilities) files, $(
p.file_size_sample_distribution |> format_filesize
Expand All @@ -50,7 +114,7 @@ $(map(groups) do group
total_size_dist = group.file_size_sample_distribution
"""
## Group: $(join(["`$(n)`" for n in group.names], ", "))
## $(pretty(group.judgement)) Group: $(join(["`$(n)`" for n in group.names], ", "))
$(if isempty(group.not_available)
"""
Expand Down Expand Up @@ -131,3 +195,11 @@ end

sum_distributions(ds; init=Normal(0, 0)) =
any(isnothing, ds) ? nothing : reduce(convolve, ds; init=init)


pretty(j::Judgement) =
if j.should_precompute
j.close_to_filesize_limit ? "⚠️" : ""
else
""
end
14 changes: 8 additions & 6 deletions test/staterequest static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ using Base64
end
end

PlutoSliderServer.export_directory(
test_dir;
Export_static_export_state=true,
notebook_paths,
on_ready,
)
withenv("JULIA_DEBUG" => nothing) do
PlutoSliderServer.export_directory(
test_dir;
Precompute_enabled=true,
notebook_paths,
on_ready,
)
end

@test isdir(joinpath(test_dir, "staterequest"))
@test isdir(joinpath(test_dir, "bondconnections"))
Expand Down

0 comments on commit 001d96d

Please sign in to comment.