Class: Kotoshu::Cli::StatusReport

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/cli/status_report.rb

Overview

Pure data model describing what the ‘kotoshu status` command reports.

Knows nothing about presentation — the CLI command formats it as text or JSON. Construction is split from presentation so both outputs share one source of truth (MECE).

Defined Under Namespace

Classes: ResourceStatus

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version:, languages_setup:, resources:, cache_path:, cache_size_bytes:, audit_log_path:, audit_log_size_bytes:, onnx_loaded:, default_language:, offline:) ⇒ StatusReport

Returns a new instance of StatusReport.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kotoshu/cli/status_report.rb', line 17

def initialize(version:, languages_setup:, resources:, cache_path:, cache_size_bytes:,
               audit_log_path:, audit_log_size_bytes:, onnx_loaded:, default_language:, offline:)
  @version = version
  @languages_setup = languages_setup
  @resources = resources
  @cache_path = cache_path
  @cache_size_bytes = cache_size_bytes
  @audit_log_path = audit_log_path
  @audit_log_size_bytes = audit_log_size_bytes
  @onnx_loaded = onnx_loaded
  @default_language = default_language
  @offline = offline
end

Instance Attribute Details

#audit_log_pathObject (readonly)

Returns the value of attribute audit_log_path.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def audit_log_path
  @audit_log_path
end

#audit_log_size_bytesObject (readonly)

Returns the value of attribute audit_log_size_bytes.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def audit_log_size_bytes
  @audit_log_size_bytes
end

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def cache_path
  @cache_path
end

#cache_size_bytesObject (readonly)

Returns the value of attribute cache_size_bytes.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def cache_size_bytes
  @cache_size_bytes
end

#default_languageObject (readonly)

Returns the value of attribute default_language.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def default_language
  @default_language
end

#languages_setupObject (readonly)

Returns the value of attribute languages_setup.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def languages_setup
  @languages_setup
end

#offlineObject (readonly)

Returns the value of attribute offline.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def offline
  @offline
end

#onnx_loadedObject (readonly)

Returns the value of attribute onnx_loaded.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def onnx_loaded
  @onnx_loaded
end

#resourcesObject (readonly)

Returns the value of attribute resources.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def resources
  @resources
end

#versionObject (readonly)

Returns the value of attribute version.



13
14
15
# File 'lib/kotoshu/cli/status_report.rb', line 13

def version
  @version
end

Class Method Details

.build(version:, resource_manager: Kotoshu::ResourceManager, paths: Kotoshu::Paths, configuration: Kotoshu.configuration, onnx_loaded: Kotoshu::Models::OnnxModel::ONNX_LOADED) ⇒ Object

Probe the live system and produce a report. Each collaborator is injectable for tests; defaults pull from the live configuration.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kotoshu/cli/status_report.rb', line 37

def self.build(version:, resource_manager: Kotoshu::ResourceManager,
               paths: Kotoshu::Paths, configuration: Kotoshu.configuration,
               onnx_loaded: Kotoshu::Models::OnnxModel::ONNX_LOADED)
  langs = resource_manager.languages_setup
  cache_path = paths.cache_path
  cache_size = directory_size(cache_path)
  audit = audit_info(paths.audit_log_path)

  new(
    version: version,
    languages_setup: langs,
    resources: langs.flat_map { |lang| statuses_for(lang, resource_manager, cache_path) },
    cache_path: cache_path,
    cache_size_bytes: cache_size,
    audit_log_path: audit[:path],
    audit_log_size_bytes: audit[:size],
    onnx_loaded: onnx_loaded,
    default_language: configuration.default_language,
    offline: configuration.offline
  )
end

.directory_size(dir) ⇒ Object

Sum every regular file’s size under ‘dir`. Returns 0 if missing.



60
61
62
63
64
65
66
# File 'lib/kotoshu/cli/status_report.rb', line 60

def self.directory_size(dir)
  return 0 unless File.directory?(dir)

  Dir.glob(File.join(dir, "**", "*"))
    .select { |path| File.file?(path) }
    .sum { |path| File.size(path) }
end

.format_bytes(bytes) ⇒ String

Human-readable byte format (KB / MB / GB).

Parameters:

  • bytes (Integer, nil)

Returns:

  • (String)


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kotoshu/cli/status_report.rb', line 71

def self.format_bytes(bytes)
  return "0 B" if bytes.nil? || bytes.zero?

  units = %w[B KB MB GB TB]
  size = bytes.to_f
  i = 0
  while size >= 1024 && i < units.length - 1
    size /= 1024
    i += 1
  end
  template = i.zero? ? "%.0f" : "%.1f"
  "#{template % size} #{units[i]}"
end

Instance Method Details

#languages_with_modelObject



31
32
33
# File 'lib/kotoshu/cli/status_report.rb', line 31

def languages_with_model
  resources.select { |r| r.resource == :model && r.available }.map(&:language).uniq.sort
end