Skip to content

Commit 7d28f02

Browse files
committed
refactor: switched cache size calculation from SQL to MBTiles file metrics
- removed `SUM(length(tile_data))` from `ViewHelpers#get_tiles_size`; size is now computed via route_storage_files` - added `DB_SIDECAR_SUFFIXES`, `mbtiles_related_paths`, `resolve_mbtiles_path`, `progress_json_path`, and `format_bytes` to handle `.mbtiles`, `-wal`, `-shm`, and `.progress.json` - updated "Cache Size" in `database.slim` to use `format_bytes(get_tiles_size(@route))` - added a `Storage Files` section in `database.slim` showing file list, `exists/missing` status, and per-file size - switched progress JSON lookup in `database.slim` to `progress_json_path(@route)` with support for both absolute and relative `mbtiles_file` paths
1 parent 10d28db commit 7d28f02

2 files changed

Lines changed: 77 additions & 5 deletions

File tree

src/view_helpers.rb

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,39 @@
33
require_relative 'geometry_tile_calculator'
44

55
module ViewHelpers
6-
def get_tiles_size(route)
7-
db = route[:db]
8-
db.table_exists?(:tiles) ? (db[:tiles].sum(Sequel.function(:length, :tile_data)) || 0) : 0
6+
DB_SIDECAR_SUFFIXES = ['-wal', '-shm'].freeze
7+
8+
def get_tiles_size(route) = route_storage_files(route).sum { _1[:size] }
9+
10+
def route_storage_files(route)
11+
mbtiles_related_paths(route[:mbtiles_file]).map do |path|
12+
exists = File.exist?(path)
13+
{
14+
path: path,
15+
name: File.basename(path),
16+
exists: exists,
17+
size: exists ? File.size(path) : 0
18+
}
19+
end
20+
end
21+
22+
def progress_json_path(route)
23+
mbtiles_path = resolve_mbtiles_path(route[:mbtiles_file])
24+
mbtiles_path&.end_with?('.mbtiles') ? mbtiles_path.sub(/\.mbtiles$/, '.progress.json') : nil
25+
end
26+
27+
def format_bytes(bytes)
28+
units = %w[B KB MB GB TB]
29+
size = bytes.to_f
30+
unit_index = 0
31+
32+
while size >= 1024 && unit_index < units.length - 1
33+
size /= 1024.0
34+
unit_index += 1
35+
end
36+
37+
precision = unit_index.zero? ? 0 : 2
38+
"#{size.round(precision)} #{units[unit_index]}"
939
end
1040

1141
def tiles_per_zoom(z, route = nil)
@@ -305,4 +335,24 @@ def generate_single_source_style(route, source_name, debug_mode = false)
305335

306336
style.to_json
307337
end
338+
339+
private
340+
341+
def mbtiles_related_paths(mbtiles_file)
342+
mbtiles_path = resolve_mbtiles_path(mbtiles_file)
343+
return [] unless mbtiles_path
344+
return [mbtiles_path] unless mbtiles_path.end_with?('.mbtiles')
345+
346+
[mbtiles_path, *DB_SIDECAR_SUFFIXES.map { "#{mbtiles_path}#{_1}" }, mbtiles_path.sub(/\.mbtiles$/, '.progress.json')]
347+
end
348+
349+
def resolve_mbtiles_path(mbtiles_file)
350+
return nil if mbtiles_file.nil? || mbtiles_file.to_s.strip.empty?
351+
352+
path = mbtiles_file.to_s
353+
return path if path.start_with?('/')
354+
355+
candidates = [File.expand_path(path, Dir.pwd), File.expand_path(path, __dir__)].uniq
356+
candidates.find { |candidate| File.exist?(candidate) } || candidates.first
357+
end
308358
end

src/views/database.slim

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ html
134134

135135
.section
136136
.section-title Statistics
137+
- storage_files = route_storage_files(@route)
137138
.stats-grid
138139
.stat-box
139140
.stat-number = @route[:db][:tiles].count
@@ -142,9 +143,30 @@ html
142143
.stat-number = @route[:db][:misses].count
143144
.stat-desc Cache Misses
144145
.stat-box
145-
.stat-number = "#{((@route[:db][:tiles].sum(Sequel.function(:length, :tile_data)) || 0) / 1024.0 / 1024.0).round(2)} MB"
146+
.stat-number = format_bytes(get_tiles_size(@route))
146147
.stat-desc Cache Size
147148

149+
.section
150+
.section-title Storage Files
151+
- if storage_files.empty?
152+
.json-missing mbtiles_file is not configured.
153+
- else
154+
.table-container
155+
table
156+
thead
157+
tr
158+
th File
159+
th Path
160+
th Size
161+
th Status
162+
tbody
163+
- storage_files.each do |file|
164+
tr
165+
td = file[:name]
166+
td = file[:path]
167+
td = format_bytes(file[:size])
168+
td = file[:exists] ? 'exists' : 'missing'
169+
148170
.main-layout
149171
.sidebar
150172
.table-list
@@ -187,7 +209,7 @@ html
187209
- if page < total_pages
188210
a.pagination-btn href=url_for("/db?source=#{@source}&table=#{current_table}&page=#{page+1}") Next →
189211

190-
- json_path = @route[:mbtiles_file]&.sub(/\.mbtiles$/, '.progress.json')
212+
- json_path = progress_json_path(@route)
191213
- json_content = json_path && File.exist?(json_path) ? File.read(json_path) : nil
192214
.section
193215
.section-title Progress JSON

0 commit comments

Comments
 (0)