Class: ReportWorker
- Inherits:
-
Object
- Object
- ReportWorker
- Includes:
- Sidekiq::Worker
- Defined in:
- app/workers/report_worker.rb
Instance Method Summary collapse
- #delete_file(file_path) ⇒ Object
-
#generate_pdf(rendered_template, orientation, footer = true) ⇒ Object
Generates PDF with wicked_pdf.
- #handle_defect_picture_report(report_request, user_id, property_id, report_type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) ⇒ Object
-
#manage_file(report, rendered_template, report_type) ⇒ Object
Creates file.
- #perform(report_id) ⇒ Object
- #render_template(user_id, property_id, report_type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) ⇒ Object
-
#upload_to_s3(report_name, file_path) ⇒ Object
Upload report file to s3.
Instance Method Details
#delete_file(file_path) ⇒ Object
380 381 382 383 384 |
# File 'app/workers/report_worker.rb', line 380 def delete_file(file_path) if File.exist?(file_path) File.delete(file_path) end end |
#generate_pdf(rendered_template, orientation, footer = true) ⇒ Object
Generates PDF with wicked_pdf
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'app/workers/report_worker.rb', line 334 def generate_pdf(rendered_template, orientation, = true) ac = ActionController::Base.new report_file = if header_content = ac.render_to_string('layouts/header', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data }) = ac.render_to_string('layouts/footer', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data }) WickedPdf.new.pdf_from_string( rendered_template, header: { content: header_content, line: true }, footer: { content: , line: true }, page_size: 'Letter', orientation: orientation ) else WickedPdf.new.pdf_from_string( rendered_template, page_size: 'Letter', orientation: orientation ) end end |
#handle_defect_picture_report(report_request, user_id, property_id, report_type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/workers/report_worker.rb', line 88 def handle_defect_picture_report(report_request, user_id, property_id, report_type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) # Get the data for the defect picture report @data = ::ReportsManager::DefectPictureReport.call(user_id, property_id, 'PDF', inspection_details, inspectable_buildings, property_units, items, defects) if @data['split_reports'] file_list = [] @data['defect_batches'].each_with_index do |batch_data, index| ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/picture_defect', formats: [:pdf], layout: 'stylingpdf', locals: { data: batch_data } ) batch_report_type = "#{report_type}_batch_#{index + 1}_of_#{@data['batch_count']}" pdf_path = manage_file(report_request, rendered_template, batch_report_type) file_list.push(pdf_path) end # Create a zip file containing all PDFs property = Property.find_by_id(property_id) zip_file_name = property.property_name.gsub(' ', '-') + '-' + report_type + "-" + Time.now.getutc.to_s + '.zip' zip_file = File.new('/tmp/' + zip_file_name, 'w') Zip::File.open(zip_file.path, Zip::File::CREATE) do |zip| file_list.each do |file_path| zip.add(File.basename(file_path), file_path) end end zip_file.close # Clean up individual PDFs file_list.each do |file_path| delete_file(file_path) end return zip_file.path else ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/picture_defect', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) return manage_file(report_request, rendered_template, report_type) end end |
#manage_file(report, rendered_template, report_type) ⇒ Object
Creates file
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'app/workers/report_worker.rb', line 285 def manage_file(report, rendered_template, report_type) print "\n" print "here inside manage file" print "\n" file_ext = nil report_file = rendered_template if report.export_type == 'zip' report_exts = ApplicationHelper.get_report_ext.as_json export_type = report_exts[report_type] else export_type = report.export_type end orientation = report_type == 'defect_report' || report_type == 'lt_report' || report_type == 'priority_defect_report' ? 'Landscape' : 'Portrait' if export_type == 'excel' file_ext = '.xlsx' else report_file = if report.report_type == 'cover_report' generate_pdf(rendered_template, orientation, false) else generate_pdf(rendered_template, orientation) end file_ext = '.pdf' end property = Property.find_by_id(report.property_id) if report_type == 'house_keeping_report' report_name = property.property_name.gsub(' ', '-') + '-Housekeeping, Pets and Smoking' + file_ext elsif report_type == 'curb_appeal_report' report_name = property.property_name.gsub(' ', '-') + '-Curb Appeal' + file_ext elsif report_type == 'lbp_visual_assessment_report' report_name = property.property_name.gsub(' ', '-') + '-LBP-Visual-Assessment' + file_ext else report_name = property.property_name.gsub(' ', '-') + '-' + report_type.titleize + file_ext end pdf_path = '/tmp/' + report_name File.open(pdf_path, 'wb') do |file| file << report_file end pdf_path end |
#perform(report_id) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/workers/report_worker.rb', line 6 def perform(report_id) report_request = ReportRequest.find_by_id(report_id) unless report_request Rails.logger.error("ReportRequest with ID #{report_id} not found!") return end user_id = report_request.created_by_id property_id = report_request.property_id report_type = report_request.report_type ApplicationHelper.remove_duplicate_defects(property_id) buildings = Building.where(property_id: property_id) property_buildings = Building.get_property_buildings(property_id) inspectable_buildings = Building.get_inspectable_buildings(property_id) property_units = Unit.get_property_units(property_id) inspectable_units = Unit.get_inspectable_units(property_id) defects = DeficiencyDefinition.get_all items = InspectableItem.get_all inspection_details = InspectionDetail.get_details(property_id) file_path = '' file_list = [] if report_type == 'defect_picture_report' file_path = handle_defect_picture_report(report_request, user_id, property_id, report_type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) elsif report_type == 'all_nspire' report_list = ApplicationHelper.nspire_report_list report_list.each do |type| if type == 'defect_picture_report' file_path = handle_defect_picture_report(report_request, user_id, property_id, type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) else rendered_template = render_template(user_id, property_id, type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) file_path = manage_file(report_request, rendered_template, type) end file_list.push(file_path) end property = Property.find_by_id(property_id) zip_file_name = property.property_name.gsub(' ', '-') + '-' + report_type + "-" + Time.now.getutc.to_s + '.zip' zip_file = File.new('/tmp/' + zip_file_name, 'w') Zip::File.open(zip_file.path, Zip::File::CREATE) do |zip| file_list.each do |file_path| zip.add(File.basename(file_path), file_path) end end zip_file.close file_list.each do |file_path| delete_file(file_path) end file_path = zip_file.path else rendered_template = render_template(user_id, property_id, report_request.report_type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) file_path = manage_file(report_request, rendered_template, report_type) unless rendered_template.nil? end filename = File.basename(file_path) public_url = upload_to_s3(filename, file_path) report_request.report_url = public_url report_request.status = 'completed' report_request.save delete_file(file_path) end |
#render_template(user_id, property_id, report_type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'app/workers/report_worker.rb', line 144 def render_template(user_id, property_id, report_type, buildings, property_buildings, inspectable_buildings, property_units, inspectable_units, defects, items, inspection_details) rendered_template = nil case report_type when 'mass_certificate' @data = ::ReportsManager::CertificateReport.call(user_id, property_id, 'PDF',property_buildings,property_units) ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/certificate', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'lt_report' @data = ::ReportsManager::NspireLtReport.call(user_id, property_id, inspection_details, items, inspectable_buildings, inspectable_units, 'PDF', '') ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/lt_defect', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'scoring_report' @data = ::ReportsManager::NspireScoreReport.call(user_id, property_id, 'PDF',buildings, inspection_details, inspectable_units, inspectable_buildings, defects, items) ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/score', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'house_keeping_report' @data = ::ReportsManager::ItemBasedInspectionReport.call(user_id, property_id, 'PDF', inspectable_buildings, inspectable_units, 'house_keeping') ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/house_keeping', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'curb_appeal_report' @data = ::ReportsManager::ItemBasedInspectionReport.call(user_id, property_id, 'PDF', inspectable_buildings, inspectable_units, 'curb_appeal') ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/curb_appeal', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'defect_report' @data = ::ReportsManager::NspireDefectReport.call(user_id, property_id, 'PDF', inspection_details, inspectable_buildings, inspectable_units, items, defects) ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/defect', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'defect_report_excel' @data = ::ReportsManager::NspireDefectExcelReport.call(user_id, property_id, 'excel', inspection_details, inspectable_buildings, inspectable_units, items, defects) ac = ActionController::Base.new rendered_template = ac.render_to_string('admin/reports/excel/defect', formats: [:xlsx], locals: { data: @data }) when 'single_unit_report' @data = ::ReportsManager::SingleUnitReport.call(user_id, property_id, 'PDF', items, defects, property_buildings) ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/single_unit', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'cover_report' @data = ::ReportsManager::CoverReport.call(user_id, property_id, 'PDF') ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/cover', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'priority_repair_report' @data = ::ReportsManager::PriorityRepairReport.call(user_id, property_id, 'PDF') ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/defect_priority', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'building_report' @data = ::ReportsManager::BuildingReport.call(user_id, property_id, 'PDF') ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/building', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'fixed_assets_excel' @data = ::ReportsManager::PropertyFixedAssetsReport.call(user_id, property_id, 'excel') ac = ActionController::Base.new rendered_template = ac.render_to_string('admin/reports/excel/property_fixed_assets', formats: [:xlsx], locals: { data: @data }) when 'unit_defect_count' @data = ::ReportsManager::DefectStatisticsReport.call(user_id, property_id, 'excel', property_buildings, property_units) ac = ActionController::Base.new rendered_template = ac.render_to_string('admin/reports/excel/defect_statistics', formats: [:xlsx], locals: { data: @data }) when 'property_photos_excel' @data = ::ReportsManager::PropertyPhotosExcelReport.call(user_id, property_id, 'excel') ac = ActionController::Base.new rendered_template = ac.render_to_string('admin/reports/excel/upcs_form', formats: [:xlsx], locals: { data: @data }) when 'priority_defect_report' @data = ::ReportsManager::NspireDefectReport.call(user_id, property_id, 'PDF', false, false, 'defect-priority-report', inspection_details, inspectable_buildings, inspectable_units, items, defects) ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/defect', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) when 'lbp_visual_assessment_report' @data = ::ReportsManager::LbpVisualAssessmentReport.call(user_id, property_id, 'PDF', inspectable_buildings, inspectable_units) ac = ActionController::Base.new rendered_template = ac.render_to_string( 'admin/reports/pdf/nspire/lbp_visual_assessment', formats: [:pdf], layout: 'stylingpdf', locals: { data: @data } ) end rendered_template end |
#upload_to_s3(report_name, file_path) ⇒ Object
Upload report file to s3
369 370 371 372 373 374 375 376 377 378 |
# File 'app/workers/report_worker.rb', line 369 def upload_to_s3(report_name, file_path) folder = SecureRandom.hex(16) filename = 'test/' + folder + '/' + report_name s3 = Aws::S3::Resource.new bucket = s3.bucket('usig.reports') obj = bucket.object(filename) obj.upload_file(file_path, acl: 'public-read') obj.public_url end |