Last active
May 14, 2025 21:45
-
-
Save Sdy603/5f1dd5458230fcee75a720ddfca43f08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| module CustomerScripts | |
| class BeesJsmIncidentImport | |
| extend Callable | |
| def call | |
| incidents = unprocessed_incidents | |
| if incidents.empty? | |
| Rails.logger.info("No unprocessed incidents found.") | |
| else | |
| Rails.logger.info("Processing #{incidents.size} unprocessed incidents...") | |
| incidents.each do |incident| | |
| process_incident(incident) | |
| rescue StandardError => e | |
| Rails.logger.error("Failed to process incident #{incident[:reference_id]}: #{e.message}") | |
| end | |
| Rails.logger.info("Processed all incidents.") | |
| end | |
| rescue StandardError => e | |
| Rails.logger.error("Error in BeesJsmIncidentImport: #{e.message}") | |
| Rails.logger.error(e.backtrace.join("\n")) | |
| end | |
| private | |
| def unprocessed_incidents | |
| sql = <<~SQL | |
| SELECT DISTINCT | |
| ji.source_key AS reference_id, | |
| cf_service.value AS services, | |
| ji.summary AS name, | |
| COALESCE(cf_start.value::timestamp, ji.created_at) AS started_at, | |
| COALESCE(cf_end.value::timestamp, ji.resolved_at) AS resolved_at, | |
| ji.source_url, | |
| ji.priority | |
| FROM | |
| jsm_incidents ji | |
| JOIN | |
| jsm_projects jp ON ji.project_id = jp.id | |
| LEFT JOIN | |
| jsm_incident_custom_field_values cf_service ON cf_service.incident_id = ji.id AND cf_service.custom_field_id IN (358) | |
| LEFT JOIN | |
| jsm_incident_custom_field_values cf_start ON cf_start.incident_id = ji.id AND cf_start.custom_field_id IN (1098) | |
| LEFT JOIN | |
| jsm_incident_custom_field_values cf_end ON cf_end.incident_id = ji.id AND cf_end.custom_field_id IN (1105) | |
| LEFT JOIN | |
| jsm_users ju ON ji.assignee_id = ju.id | |
| LEFT JOIN | |
| jsm_incident_services jis ON jis.incident_id = ji.id | |
| LEFT JOIN | |
| jsm_services js ON js.id = jis.service_id | |
| WHERE | |
| ji.resolved_at IS NOT NULL | |
| AND cf_service IS NOT NULL | |
| AND ji.source_key NOT IN (SELECT source_id FROM incidents) | |
| ORDER BY | |
| COALESCE(cf_start.value::timestamp, ji.created_at) DESC | |
| LIMIT 300; | |
| SQL | |
| ClientRecord.connection.execute(sql).map(&:symbolize_keys) | |
| end | |
| def process_incident(incident) | |
| services = parse_services(incident[:services]) | |
| service_source_ids = services.map do |service_name| | |
| Rails.logger.info("Processing service: #{service_name}") | |
| service = Service.find_or_create_by(name: service_name) | |
| Rails.logger.info("Found or created service: #{service.inspect}") | |
| identity = ServiceIdentity.find_or_create_by( | |
| name: service_name, | |
| source: "dx_automation", | |
| source_id: service_name | |
| ) do |new_identity| | |
| new_identity.service_id = service.id | |
| end | |
| Rails.logger.info("Found or created service identity: #{identity.inspect}") | |
| identity&.source_id | |
| end.compact | |
| params = { | |
| source: "dx_automation", | |
| source_id: incident[:reference_id], | |
| name: incident[:name], | |
| source_url: incident[:source_url], | |
| started_at: incident[:started_at], | |
| resolved_at: incident[:resolved_at], | |
| service_source_ids: service_source_ids, | |
| priority: incident[:priority] | |
| } | |
| puts params[:service_source_ids] | |
| CreateIncident.call(params) | |
| Rails.logger.info("Incident created successfully for incident #{incident[:reference_id]}.") | |
| end | |
| def parse_services(services) | |
| services&.split(",")&.map(&:strip) || [] | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment