Humanizes bytesizes

This commit is contained in:
Hugo Peixoto 2023-07-28 12:00:41 +01:00
parent 1a81e55723
commit bbafcfa40f
5 changed files with 23 additions and 2 deletions

View File

@ -1,3 +1,4 @@
/videos/
/videos.db
/public/
/.gitignore

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/videos/
/videos.db
/public/

View File

@ -9,7 +9,11 @@ RUN bundle
ADD config.ru ./
ADD *.rb ./
ADD index.html.erb ./
WORKDIR /app/public
ADD saca-sapos ./
RUN tar czf saca-sapos.tar.gz saca-sapos
WORKDIR /app
EXPOSE 4567
CMD ["puma", "-p", "4567"]

View File

@ -42,8 +42,8 @@ $ PROCS=10 /caminho/para/o/saca-sapos 1000 eu@exemplo.pt
<h2>Estatísticas</h2>
<p>Número de vídeos descarregados: <%= stats[:downloads]["total"].to_i %></p>
<p>Tamanho total: <%= stats[:downloads]["bytes"].to_i / 1024 / 1024 %> MiB</p>
<p>Tamanho médio: <%= stats[:downloads]["bytes"].to_i / 1024 / 1024 / stats[:downloads]["total"].to_i %> MiB/vídeo</p>
<p>Tamanho total: <%= h(stats[:downloads]["bytes"].to_i) %></p>
<p>Tamanho médio: <%= h(stats[:downloads]["bytes"].to_i / stats[:downloads]["total"].to_i) %>/vídeo</p>
<footer>
<hr />

15
main.rb
View File

@ -6,6 +6,21 @@ require 'sinatra'
require './database.rb'
require './models.rb'
def h(bytes)
if bytes < 2**10
"#{bytes} bytes"
elsif bytes < 2**20
"%.2f KiB" % (1.0 * bytes / 2**10)
elsif bytes < 2**30
"%.2f MiB" % (1.0 * bytes / 2**20)
elsif bytes < 2**40
"%.2f GiB" % (1.0 * bytes / 2**30)
else
"%.2f TiB" % (1.0 * bytes / 2**40)
end
end
set :public_folder, __dir__ + '/public'
get '/' do
downloads = ActiveRecord::Base.connection.execute("
SELECT