Permalink
Browse files

Show size of database file next to download link, closes #172

  • Loading branch information...
simonw committed Feb 6, 2019
1 parent 195a5b3 commit 4462a5ab2817ac0d9ffe20dafbbf27c5c5b81466
@@ -305,6 +305,7 @@ def inspect(self):
self._inspect[name] = {
"hash": inspect_hash(path),
"file": str(path),
"size": path.stat().st_size,
"views": inspect_views(conn),
"tables": inspect_tables(conn, (self.metadata("databases") or {}).get(name, {}))
}
@@ -286,3 +286,8 @@ a.not-underlined {
display: inline-block;
box-shadow: 1px 2px 8px 2px rgba(0,0,0,0.08);
}

.download-sqlite em {
font-style: normal;
font-size: 0.8em;
}
@@ -57,7 +57,7 @@ <h2>Queries</h2>
{% endif %}

{% if config.allow_download %}
<p>Download SQLite DB: <a href="/{{ database }}-{{ database_hash }}.db">{{ database }}.db</a></p>
<p class="download-sqlite">Download SQLite DB: <a href="/{{ database }}-{{ database_hash }}.db">{{ database }}.db</a> <em>{{ format_bytes(size) }}</em></p>
{% endif %}

{% include "_codemirror_foot.html" %}
@@ -901,3 +901,15 @@ def convert(self, value, param, ctx):
if not os.path.exists(dirpath) or not os.path.isdir(dirpath):
self.fail("%s is not a valid directory path" % value, param, ctx)
return path, dirpath


def format_bytes(bytes):
current = float(bytes)
for unit in ("bytes", "KB", "MB", "GB", "TB"):
if current < 1024:
break
current = current / 1024
if unit == "bytes":
return "{} {}".format(int(current), unit)
else:
return "{:.1f} {}".format(current, unit)
@@ -19,6 +19,7 @@
InterruptedError,
InvalidSql,
LimitedWriter,
format_bytes,
is_url,
path_from_row_pks,
path_with_added_args,
@@ -102,6 +103,7 @@ def render(self, templates, **context):
"extra_js_urls": self._asset_urls(
"extra_js_urls", template, context
),
"format_bytes": format_bytes,
}
}
)
@@ -24,6 +24,7 @@ class DatabaseView(BaseView):
tables.sort(key=lambda t: (t["hidden"], t["name"]))
return {
"database": database,
"size": info["size"],
"tables": tables,
"hidden_count": len([t for t in tables if t["hidden"]]),
"views": info["views"],
@@ -374,3 +374,18 @@ def test_path_with_format(path, format, extra_qs, expected):
)
actual = utils.path_with_format(request, format, extra_qs)
assert expected == actual


@pytest.mark.parametrize(
"bytes,expected",
[
(120, '120 bytes'),
(1024, '1.0 KB'),
(1024 * 1024, '1.0 MB'),
(1024 * 1024 * 1024, '1.0 GB'),
(1024 * 1024 * 1024 * 1.3, '1.3 GB'),
(1024 * 1024 * 1024 * 1024, '1.0 TB'),
]
)
def test_format_bytes(bytes, expected):
assert expected == utils.format_bytes(bytes)

0 comments on commit 4462a5a

Please sign in to comment.