#!/usr/bin/env ruby
# this will loop through each year and for each CVE-ID and count the assigners
require 'json'
require 'pry'

files = Dir['../cvelist/*/*/*.json']
results = files.map do |fp|
  f = File.read(fp);
  json = JSON.parse(f);
  assigner = json.dig('CVE_data_meta', 'ASSIGNER')
  id = json.dig('CVE_data_meta', 'ID')
  [id, assigner]
end

sorted_and_grouped_cnas = results.map { |cve_id, cna_email| cna_email}.sort.group_by(&:itself) 
cna_count_per_cves = sorted_and_grouped_cnas.map {|k,v| [k, v.count]}.sort_by{|a,b|b}.reverse

result_count = cna_count_per_cves.map {|cna, count| {:cna => cna, :count => count}}
binding.pry
json_data = JSON.pretty_generate(result_count)

File.write("./cna_count_for_all_cves.json", json_data)
