Top Level Namespace

Defined Under Namespace

Modules: Avs, CisaKevApi, Service Classes: App, Asset, AssetGroup, CisaKevVulnerability, CmdbAsset, CmdbDiscoverySite, CmdbEos, CmdbVulnerabilitySite, Country, CountryDiscoverySite, Db, DiscoverySiteTarget, Domain, InsightVMApi, MailService, MicrosoftProductLifecycle, NucleusApi, OperatingSystem, Project, ProjectReport, ProjectReportDetail, ProjectReportFinding, Report, ScanEngine, ScanEnginePool, ScanSchedule, ScanTemplate, SearchCriteria, SharedCredential, Site, SiteTarget, Software, Tag, Vulnerability, VulnerabilityWithSecondStep

Constant Summary collapse

MAX_TABSHEET_NAME_LENGTH =

in Excel

31
MAX_FORMATABLE_ROWS =
500_000

Instance Method Summary collapse

Instance Method Details

#clean_value(value) ⇒ Object



278
279
280
281
282
283
284
285
286
# File 'lib/domain/vulnerability/api.rb', line 278

def clean_value(value)
  value = value.to_s.strip
  return nil if value.empty?

  value = value.gsub(/\A['"]|['"]\Z/, '') # Remove leading/trailing quotes
  value = value.gsub(/\s+/, ' ')  # Replace multiple spaces with a single space
  value = value.gsub(/\\n/, "\n") # Replace literal '\n' with actual newlines
  value.gsub(/\A\n+|\n+\Z/, '') # Remove leading/trailing newlines
end

#fetch_fixturesObject



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
# File 'lib/domain/tag/fixture.rb', line 6

def fetch_fixtures
  [
    CmdbAsset.new(
      id: 100,
      country_code: 'cd',
      business_unit: 'bu',
      sub_area: 'sa',
      application: 'ewallet',
      utr: 'UTR01966',
      fqdn: 'fqdn.co.za',
      host_name: 'fqdn',
      ip_address: '172.16.19.66',
      operating_system: 'ubuntu',
      server_environment: 'linux',
      server_category: 'linux',
      host_key: 'xxxx',
      country: 'DRC'
    ),
    CmdbAsset.new(
      id: 200,
      country_code: 'mw',
      business_unit: 'bu',
      sub_area: 'sa',
      application: 'atm',
      utr: 'UTR01966',
      fqdn: 'fqdn.co.za',
      host_name: 'fqdn',
      ip_address: '198.172.19.66',
      operating_system: 'ubuntu',
      server_environment: 'linux',
      server_category: 'linux',
      host_key: 'xxxx',
      country: 'Malawi'
    ),
    CmdbAsset.new(
      id: 300,
      country_code: 'bw',
      business_unit: 'bu',
      sub_area: 'sa',
      application: 'forex',
      utr: 'UTR01986',
      fqdn: 'fqdn.co.za',
      host_name: 'fqdn',
      ip_address: '10.12.19.66',
      operating_system: 'ubuntu',
      server_environment: 'linux',
      server_category: 'linux',
      host_key: 'xxxx',
      country: 'Botswana'
    )
  ]
end

#generate_column_names(n) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/service/csv2xlsx.rb', line 11

def generate_column_names(n)
  names = []
  while n >= 0
    names.unshift((n % 26 + 65).chr)
    n /= 26
    n -= 1
  end
  names.join('')
end

#generate_sheet(wb, filename, sheet_name) ⇒ Object



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
# File 'lib/service/csv2xlsx.rb', line 21

def generate_sheet(wb, filename, sheet_name)
  table = CSV.parse(File.open(filename),
                    headers: true,
                    quote_char: '"',
                    force_quotes: false,)
  count = table.length + 1
  # progress_bar = ProgressBar.new(sheet_name, max)
  # progress_bar = Tqdm::ProgressBar.new("#{sheet_name} [:bar] :percent", total: count)
  progress_bar = ProgressBar.create(total: count, format: '%a %B %p%% %E')
  headers = table.headers
  basename = File.basename(filename, '.*')
  column_names = headers.map.with_index { |_, i| generate_column_names(i) }
  # puts "headers #{headers}"
  # p column_names

  bottom_right_cell = "#{column_names.last}#{count}".upcase
  # puts "bottom_right_cell #{bottom_right_cell}"
  range = "A1:#{bottom_right_cell}"
  # puts "range: #{range}"

  wb.add_worksheet(name: sheet_name) do |sheet|
    sheet.add_row headers
    counter = 1
    table.each do |row|
      counter += 1
      if counter % 100 == 0
        progress_bar.progress += 100
      end
      types = row.fields.map do |f|
        if f =~ /^\d+$/
          :integer
        elsif f =~ /^\d{4}\-\d{2}\-\d{2}/
          :date
        elsif f =~ /^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}(.\d*)*/
          :datetime
        else
          :string
        end
      end

      widths = row.fields.map { |f| f.to_s.length > 40 ? 40 : nil }
      parsed_row = CSV.parse_line(row.to_csv.chomp)
      # Convert date strings to Date objects
      parsed_row = parsed_row.map do |value|
        if value =~ /^\d+$/
          value.to_i
        elsif value =~ /\A\d{4}\-\d{2}\-\d{2}\z/
          date = DateTime.strptime(value, "%Y-%m-%d")
        elsif value =~ /\A\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d{0,3})?\z/
          date = DateTime.strptime(value, "%Y-%m-%d") # TODO
        else
          value
        end
      end
      sheet.add_row parsed_row, types: types, widths: widths
    end
    progress_bar.progress = count
    if count < MAX_FORMATABLE_ROWS
      sheet.add_table range, name: basename, style_info: { name: 'TableStyleMedium23' }
    end
  end
end