Class: Fontist::IndexStats

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/system_index.rb

Overview

Statistics tracking for index building (thread-safe)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndexStats

Returns a new instance of IndexStats.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fontist/system_index.rb', line 9

def initialize
  @cache_hits = 0
  @cache_misses = 0
  @total_fonts = 0
  @parsed_fonts = 0
  @skipped_fonts = 0
  @errors = 0
  @validation_failures = 0
  @start_time = Time.now
  @mutex = Mutex.new
end

Instance Attribute Details

#cache_hitsObject (readonly)

Returns the value of attribute cache_hits.



6
7
8
# File 'lib/fontist/system_index.rb', line 6

def cache_hits
  @cache_hits
end

#cache_missesObject (readonly)

Returns the value of attribute cache_misses.



6
7
8
# File 'lib/fontist/system_index.rb', line 6

def cache_misses
  @cache_misses
end

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/fontist/system_index.rb', line 6

def errors
  @errors
end

#parsed_fontsObject (readonly)

Returns the value of attribute parsed_fonts.



6
7
8
# File 'lib/fontist/system_index.rb', line 6

def parsed_fonts
  @parsed_fonts
end

#skipped_fontsObject (readonly)

Returns the value of attribute skipped_fonts.



6
7
8
# File 'lib/fontist/system_index.rb', line 6

def skipped_fonts
  @skipped_fonts
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



6
7
8
# File 'lib/fontist/system_index.rb', line 6

def start_time
  @start_time
end

#total_fontsObject

Returns the value of attribute total_fonts.



6
7
8
# File 'lib/fontist/system_index.rb', line 6

def total_fonts
  @total_fonts
end

#validation_failuresObject (readonly)

Returns the value of attribute validation_failures.



6
7
8
# File 'lib/fontist/system_index.rb', line 6

def validation_failures
  @validation_failures
end

Instance Method Details

#avg_time_per_fontObject



54
55
56
57
58
# File 'lib/fontist/system_index.rb', line 54

def avg_time_per_font
  return 0 if @parsed_fonts.zero?

  elapsed_time / @parsed_fonts
end

#cache_hit_rateObject



60
61
62
63
64
# File 'lib/fontist/system_index.rb', line 60

def cache_hit_rate
  return 0 if @total_fonts.zero?

  (@cache_hits.to_f / @total_fonts * 100).round(1)
end

#elapsed_timeObject



50
51
52
# File 'lib/fontist/system_index.rb', line 50

def elapsed_time
  Time.now - @start_time
end


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fontist/system_index.rb', line 79

def print_summary(verbose: false)
  return unless verbose

  s = summary
  puts "\n#{Paint['=' * 80, :cyan]}"
  puts Paint["Index Build Statistics:", :cyan, :bright]
  puts Paint["=" * 80, :cyan]
  puts "  Total time:          #{Paint[format('%.2f', s[:total_time]),
                                       :green]} seconds"
  puts "  Total fonts:         #{Paint[s[:total_fonts], :yellow]}"
  puts "  Parsed fonts:        #{Paint[s[:parsed_fonts], :yellow]}"
  puts "  Cached fonts:        #{Paint[s[:cached_fonts], :green]}"
  puts "  Cache hit rate:      #{Paint[s[:cache_hit_rate], :green]}"
  puts "  Errors:              #{Paint[s[:errors],
                                       s[:errors].zero? ? :green : :red]}"
  puts "  Validation failures: #{Paint[s[:validation_failures],
                                       s[:validation_failures].zero? ? :green : :yellow]}"
  puts "  Avg time per font:   #{Paint[format('%.4f', s[:avg_time_per_font]),
                                       :green]} seconds"
  puts Paint["=" * 80, :cyan]
end

#record_cache_hitObject



25
26
27
28
29
30
# File 'lib/fontist/system_index.rb', line 25

def record_cache_hit
  @mutex.synchronize do
    @cache_hits += 1
    @skipped_fonts += 1
  end
end

#record_cache_missObject



32
33
34
35
36
37
# File 'lib/fontist/system_index.rb', line 32

def record_cache_miss
  @mutex.synchronize do
    @cache_misses += 1
    @parsed_fonts += 1
  end
end

#record_errorObject



39
40
41
# File 'lib/fontist/system_index.rb', line 39

def record_error
  @mutex.synchronize { @errors += 1 }
end

#record_validation_failureObject



43
44
45
46
47
48
# File 'lib/fontist/system_index.rb', line 43

def record_validation_failure
  @mutex.synchronize do
    @validation_failures += 1
    @errors += 1
  end
end

#summaryObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fontist/system_index.rb', line 66

def summary
  {
    total_time: elapsed_time.round(2),
    total_fonts: @total_fonts,
    parsed_fonts: @parsed_fonts,
    cached_fonts: @cache_hits,
    errors: @errors,
    validation_failures: @validation_failures,
    cache_hit_rate: "#{cache_hit_rate}%",
    avg_time_per_font: avg_time_per_font.round(4),
  }
end