Skip to content

Commit ca5e6b8

Browse files
only keep latest usage per object
1 parent 2821f9c commit ca5e6b8

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/Types.jl

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -459,34 +459,49 @@ end
459459
function write_env_usage(source_file::AbstractString, usage_filepath::AbstractString)
460460
# Don't record ghost usage
461461
!isfile(source_file) && return
462-
463462
# Ensure that log dir exists
464463
!ispath(logdir()) && mkpath(logdir())
465464

466-
# Generate entire entry as a string first
467-
entry = sprint() do io
468-
TOML.print(io, Dict(source_file => [Dict("time" => now())]))
469-
end
465+
p = TOML.Parser()
466+
usage_file = joinpath(logdir(), usage_filepath)
467+
timestamp = now()
470468

471469
## Atomically write usage file
472-
usage_file = joinpath(logdir(), usage_filepath)
473470
while true
474471
# read existing usage file
475-
existing_usage = isfile(usage_file) ? read(usage_file, String) : ""
472+
content = read(usage_file)
473+
Base.TOML.reinit!(p, String(content); filepath=usage_file)
474+
usage = isfile(usage_file) ? Base.TOML.parse(p) : Dict()
475+
476+
# record new usage
477+
usage[source_file] = [Dict("time" => timestamp)]
478+
479+
# keep only latest usage info
480+
for kv in usage
481+
times = map(d->Dates.DateTime(d["time"]), usage[kv.first])
482+
usage[kv.first] = [Dict("time" => maximum(times))]
483+
end
476484

477485
# Write to a temp file in the same directory as the destination
478486
temp_usage_file = tempname(logdir())
479487
open(temp_usage_file, "w") do io
480-
write(io, existing_usage)
481-
write(io, entry)
488+
TOML.print(io, usage, sorted=true)
482489
end
483490

484491
# Move the temp file into place, replacing the original
485492
mv(temp_usage_file, usage_file, force = true)
486493

487494
# Check that the new file has what we want in it
488-
new_usage = read(usage_file, String)
489-
occursin(entry, new_usage) && break
495+
content = read(usage_file)
496+
Base.TOML.reinit!(p, String(content); filepath=usage_file)
497+
new_usage = Base.TOML.parse(p)
498+
if haskey(new_usage, source_file)
499+
for e in new_usage[source_file]
500+
if Dates.DateTime(e["time"]) >= timestamp
501+
return
502+
end
503+
end
504+
end
490505
# If not, try again
491506
end
492507

0 commit comments

Comments
 (0)