@@ -17,12 +17,11 @@ class << self
1717 . returns ( CacheResult )
1818 end
1919 def fetch_or_generate_inspection_pdf ( inspection , **options )
20- # Never cache incomplete inspections
21- unless caching_enabled? && inspection . complete?
22- return generate_pdf_result ( inspection , :inspection , **options )
20+ if caching_enabled? && inspection . complete?
21+ fetch_or_generate ( inspection , :inspection , **options )
22+ else
23+ generate_pdf_result ( inspection , :inspection , **options )
2324 end
24-
25- fetch_or_generate ( inspection , :inspection , **options )
2625 end
2726
2827 sig { params ( unit : Unit , options : T . untyped ) . returns ( CacheResult ) }
@@ -52,8 +51,14 @@ def invalidate_unit_cache(unit)
5251 ) . returns ( CacheResult )
5352 end
5453 def fetch_or_generate ( record , type , **options )
55- valid_cache = record . cached_pdf . attached? &&
56- cached_pdf_valid? ( record . cached_pdf , record )
54+ valid_cache = PdfPerformance . measure (
55+ :cache_lookup ,
56+ pdf_type : type ,
57+ record_id : record . id
58+ ) do
59+ record . cached_pdf . attached? &&
60+ cached_pdf_valid? ( record . cached_pdf , record )
61+ end
5762
5863 if valid_cache
5964 Rails . logger . info "PDF cache hit for #{ type } #{ record . id } "
@@ -79,7 +84,13 @@ def fetch_or_generate(record, type, **options)
7984 end
8085 def generate_and_cache ( record , type , **options )
8186 result = generate_pdf_result ( record , type , **options )
82- store_cached_pdf ( record , result . data )
87+ PdfPerformance . measure (
88+ :cache_store ,
89+ pdf_type : type ,
90+ record_id : record . id
91+ ) do
92+ store_cached_pdf ( record , result . data )
93+ end
8394 result
8495 end
8596
@@ -91,14 +102,28 @@ def generate_and_cache(record, type, **options)
91102 ) . returns ( CacheResult )
92103 end
93104 def generate_pdf_result ( record , type , **options )
94- pdf_document = case type
95- when :inspection
96- PdfGeneratorService . generate_inspection_report ( record , **options )
97- when :unit
98- PdfGeneratorService . generate_unit_report ( record , **options )
105+ pdf_document = PdfPerformance . measure (
106+ :document_build ,
107+ pdf_type : type ,
108+ record_id : record . id
109+ ) do
110+ case type
111+ when :inspection
112+ PdfGeneratorService . generate_inspection_report ( record , **options )
113+ when :unit
114+ PdfGeneratorService . generate_unit_report ( record , **options )
115+ end
116+ end
117+
118+ pdf_data = PdfPerformance . measure (
119+ :document_render ,
120+ pdf_type : type ,
121+ record_id : record . id
122+ ) do
123+ pdf_document . render
99124 end
100125
101- CacheResult . new ( type : :pdf_data , data : pdf_document . render )
126+ CacheResult . new ( type : :pdf_data , data : pdf_data )
102127 end
103128
104129 sig { params ( record : T . any ( Inspection , Unit ) ) . void }
0 commit comments