Files
ubicloud/serializers/github_cache_entry.rb
Enes Cakir f209d1e8a0 Add cache entry list to UI
I added the list of cache entries as a subpage to the GitHub runners
page. It shows data sizes and timestamps in humanized format. To
accomplish this, I wrote two simple helper functions.

GitHub Actions cache has similar page
(https://github.com/ubicloud/ubicloud/actions/caches). There are several
use cases for this UI:
  - We give 10GB of free cache storage and purge any caches that haven't
    been accessed for 7 days or that exceed the storage limit. This new
    UI allows users to monitor their cache storage usage and see future
    needs. They can choose to clear some caches to free up space and
    prevent cache eviction.
  - Sometimes, users might need to delete certain caches to force their
    regeneration, especially if the caches are corrupted.
2024-10-01 14:55:49 +03:00

38 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class Serializers::GithubCacheEntry < Serializers::Base
def self.serialize_internal(entry, options = {})
{
id: entry.ubid,
key: entry.key,
repository_name: entry.repository.name,
scope: entry.scope,
created_at: entry.created_at.strftime("%Y-%m-%d %H:%M UTC"),
created_at_human: humanize_time(entry.created_at),
last_accessed_at: entry.last_accessed_at&.strftime("%Y-%m-%d %H:%M UTC"),
last_accessed_at_human: humanize_time(entry.last_accessed_at),
size: entry.size,
size_human: humanize_size(entry.size)
}
end
def self.humanize_size(bytes)
return nil if bytes.nil? || bytes.zero?
units = %w[B KB MB GB]
exp = (Math.log(bytes) / Math.log(1024)).to_i
exp = [exp, units.size - 1].min
return "%d %s" % [bytes.to_f / 1024**exp, units[exp]] if exp == 0
"%.1f %s" % [bytes.to_f / 1024**exp, units[exp]]
end
def self.humanize_time(time)
return nil unless time
seconds = Time.now - time
return "just now" if seconds < 60
return "#{(seconds / 60).to_i} minutes ago" if seconds < 3600
return "#{(seconds / 3600).to_i} hours ago" if seconds < 86400
"#{(seconds / 86400).to_i} days ago"
end
end