<commit_before>namespace :load_apps_gallery_data do

  desc "actually publish"
  task :publish, [:file] => :environment do |t,args|
    PublicActivity.enabled = false
    puts "publish items that might not be actually published"
    Outlet.where("draft_id IS NULL AND status = ?", Outlet.statuses[:published]).each { |outlet| outlet.published!}
    MobileApp.where("draft_id IS NULL AND status = ?", MobileApp.statuses[:published]).each { |ma| ma.published!}
    Gallery.where("draft_id IS NULL AND status = ?", Gallery.statuses[:published]).each { |ga| ga.published!}
    
    puts "outlets updates"
    Sponsorship.all.each do |sponsorship|
      sponsorship.update_counter_cache
    end
    OutletOfficialTag.all.each do |oot|
      oot.update_counter_cache
    end

    puts "mobile apps updates"
    MobileAppAgency.all.each do |maa|
      maa.update_counter_cache
    end
    MobileAppOfficialTag.all.each do |maot|
      maot.update_counter_cache
    end

    puts "gallery updates"
    GalleryAgency.all.each do |ga|
      ga.update_counter_cache
    end
    GalleryOfficialTag.all.each do |got|
      got.update_counter_cache
    end
  end

	desc "update all"
	task :update_all, [:file] => :environment do |t,args|
		PublicActivity.enabled = false
		Outlet.update_all(status: 1)
    Outlet.where(status: 1).each do |outlet|
      outlet.published!
    end
    MobileApp.update_all(status: 1)
    MobileApp.where(status: 1).each do |mobile_app|
      mobile_app.published!
    end
    Gallery.update_all(status: 1)
	end
  
  desc "Fill agencies"
  task :agencies, [:file] => :environment do |t, args|
  	filepath= args[:file] || "data/current/agencies.json"
  	PublicActivity.enabled = false
  	if filepath

		json = nil
	    File.readlines(filepath).each do |f|
	        item = JSON.load( f )
		    	agency = nil
		    	agencies = Agency.where("name LIKE ?", "%#{item["Name"]}%")
		    	if agencies.count == 0
		    		agency = Agency.find_or_create_by(name: item["Name"])
		    	else
		    		agency = agencies.first
		    	end
		    	agency.mongo_id = item["Id"]
		    	agency.parent_mongo_id = item["ParentId"]
		    	agency.shortname = item["Acronym"] ? item["Acronym"].downcase : nil
		    	agency.save!
	    end

	    Agency.all.each do |agency|
	    	if(agency.parent_mongo_id)
	    		parent = Agency.find_by(mongo_id: agency.parent_mongo_id)
		    	agency.parent_id = parent.id if parent
		    	
		    	agency.save!
		    end
	    end
  	else
  		puts "provide a file path relative to current working directory (or absolute)"
  	end
  end

  desc "Fill contacts"
  task :contacts, [:file] => :environment do |t, args|
  	filepath= args[:file] || "data/current/contacts.json"
  	PublicActivity.enabled = false
  	if filepath

  		json = nil
  	    File.readlines(filepath).each do |f|
  	      item = JSON.load( f )
  	    	user = nil
  	    	user = User.find_by(email: item["Email1"])
  	    	if user == nil
  	    		user = User.new(user: item["Id"])
  	    	end
  	    	user.email = item["Email1"] ? item["Email1"] : "<EMAIL>#{rand(100000)}<EMAIL>"
  	    	user.first_name = item["FirstName"]
  	    	user.last_name = item["LastName"]
  	    	if item["Agency"] && item["Agency"] != []
  	    		user.agency = Agency.find_by(mongo_id: item["Agency"][0]["Id"])
  	    	end
  	    	user.phone = item["Phone1"]
  	    	user.save!
  	    end

  	else
  		puts "provide a file path relative to current working directory (or absolute)"
  	end
  end

  desc "Fill mobile apps"
  task :mobile_apps, [:file] => :environment do |t, args|
  	filepath= args[:file] || "data/current/registrations.json"
  	PublicActivity.enabled = false
    Gallery.skip_callback(:save)
    MobileApp.skip_callback(:save)
    MobileAppVersion.skip_callback(:save)
    Gallery.skip_callback(:create)
    MobileApp.skip_callback(:create)
    MobileAppVersion.skip_callback(:create)
  	if filepath
  	    File.readlines(filepath).each do |f|
  	      item = JSON.load( f )
  	    	app = MobileApp.find_or_create_by(mongo_id: item["Id"])
  	    	app.name = item["Name"]
  	    	app.short_description = item["Short_Description"]
  	    	app.long_description = item["Long_Description"]
  	    	if item["Icon"]
  		    	app.icon_url = item["Icon"].join(",")
  		    end
  	    	app.language = item["Language"] == "EN" ? "English" : "Spanish"
  	    	if item["Agency"] && item["Agency"] != []
  		    	item["Agency"].each do |agency|
  		    		agency = Agency.find_by(mongo_id: agency["Id"])
  		    		if agency
  		    			app.agencies << agency
  		    		else
  		    			puts "Couldn't find agency - #{agency.inspect}"
  		    		end
  		    	end
  		    end
  	    	case item["Status"]
  		    	when "PUBLIC"<commit_msg>spanish fix<commit_after>namespace :load_apps_gallery_data do

  desc "actually publish"
  task :publish, [:file] => :environment do |t,args|
    PublicActivity.enabled = false
    puts "publish items that might not be actually published"
    Outlet.where("draft_id IS NULL AND status = ?", Outlet.statuses[:published]).each { |outlet| outlet.published!}
    MobileApp.where("draft_id IS NULL AND status = ?", MobileApp.statuses[:published]).each { |ma| ma.published!}
    Gallery.where("draft_id IS NULL AND status = ?", Gallery.statuses[:published]).each { |ga| ga.published!}
    
    puts "outlets updates"
    Sponsorship.all.each do |sponsorship|
      sponsorship.update_counter_cache
    end
    OutletOfficialTag.all.each do |oot|
      oot.update_counter_cache
    end

    puts "mobile apps updates"
    MobileAppAgency.all.each do |maa|
      maa.update_counter_cache
    end
    MobileAppOfficialTag.all.each do |maot|
      maot.update_counter_cache
    end

    puts "gallery updates"
    GalleryAgency.all.each do |ga|
      ga.update_counter_cache
    end
    GalleryOfficialTag.all.each do |got|
      got.update_counter_cache
    end
  end

	desc "update all"
	task :update_all, [:file] => :environment do |t,args|
		PublicActivity.enabled = false
		Outlet.update_all(status: 1)
    Outlet.where(status: 1).each do |outlet|
      outlet.published!
    end
    MobileApp.update_all(status: 1)
    MobileApp.where(status: 1).each do |mobile_app|
      mobile_app.published!
    end
    Gallery.update_all(status: 1)
	end
  
  desc "Fill agencies"
  task :agencies, [:file] => :environment do |t, args|
  	filepath= args[:file] || "data/current/agencies.json"
  	PublicActivity.enabled = false
  	if filepath

		json = nil
	    File.readlines(filepath).each do |f|
	        item = JSON.load( f )
		    	agency = nil
		    	agencies = Agency.where("name LIKE ?", "%#{item["Name"]}%")
		    	if agencies.count == 0
		    		agency = Agency.find_or_create_by(name: item["Name"])
		    	else
		    		agency = agencies.first
		    	end
		    	agency.mongo_id = item["Id"]
		    	agency.parent_mongo_id = item["ParentId"]
		    	agency.shortname = item["Acronym"] ? item["Acronym"].downcase : nil
		    	agency.save!
	    end

	    Agency.all.each do |agency|
	    	if(agency.parent_mongo_id)
	    		parent = Agency.find_by(mongo_id: agency.parent_mongo_id)
		    	agency.parent_id = parent.id if parent
		    	
		    	agency.save!
		    end
	    end
  	else
  		puts "provide a file path relative to current working directory (or absolute)"
  	end
  end

  desc "Fill contacts"
  task :contacts, [:file] => :environment do |t, args|
  	filepath= args[:file] || "data/current/contacts.json"
  	PublicActivity.enabled = false
  	if filepath

  		json = nil
  	    File.readlines(filepath).each do |f|
  	      item = JSON.load( f )
  	    	user = nil
  	    	user = User.find_by(email: item["Email1"])
  	    	if user == nil
  	    		user = User.new(user: item["Id"])
  	    	end
  	    	user.email = item["Email1"] ? item["Email1"] : "replace#{rand(100000)}<EMAIL>"
  	    	user.first_name = item["FirstName"]
  	    	user.last_name = item["LastName"]
  	    	if item["Agency"] && item["Agency"] != []
  	    		user.agency = Agency.find_by(mongo_id: item["Agency"][0]["Id"])
  	    	end
  	    	user.phone = item["Phone1"]
  	    	user.save!
  	    end

  	else
  		puts "provide a file path relative to current working directory (or absolute)"
  	end
  end


  desc "Fill mobile apps"
  task :fix_languages, [:file] => :environment do |t, args|
    filepath= args[:file] || "data/current/registrations.json"
    PublicActivity.enabled = false
    Gallery.skip_callback(:save)
    MobileApp.skip_callback(:save)
    MobileAppVersion.skip_callback(:save)
    Gallery.skip_callback(:create)
    MobileApp.skip_callback(:create)
    MobileAppVersion.skip_callback(:create)
    if filepath
        File.readlines(filepath).each do |f|
          item = JSON.load( f )
          app = MobileApp.find_by(mongo_id: item["Id"])
          if app
            app.language = item["Language"] == "English" ? "English" : "Spanish"
          end
        end
        app.save(:validate => false)
        end

    else
      puts "provide a file path relative to current working directory (or absolute)"
    end
  end

  desc "Fill mobile apps"
  task :mobile_apps, [:file] => :environment do |t, args|
  	filepath= args[:file] || "data/current/registrations.json"
  	PublicActivity.enabled = false
    Gallery.skip_callback(:save)
    MobileApp.skip_callback(:save)
    MobileAppVersion.skip_callback(:save)
    Gallery.skip_callback(:create)
    MobileApp.skip_callback(:create)
    MobileAppVersion.skip_callback(:create)
  	if filepath
  	    File.readlines(filepath).each do |f|
  	      item = JSON.load( f )
  	    	app = MobileApp.find_or_create_by(mongo_id: item["Id"])
  	    	app.name = item["Name"]
  	    	app.short_description = item["Short_Description"]
  	    	app.long_description = item["Long_Description"]
  	    	if item["Icon"]
  		    	app.icon_url = item["Icon"].join(",")
  		    end
  	    	app.language = item["Language"] == "English" ? "English" : "Spanish"
  	    	if item["Agency"] && item["Agency"] != []
  		    	item["Agency"].each do |agency|
  		    		agency = Agency.find_by(mongo_id: agency["Id"])
  		    		if agency
  		    			app.agencies << agency
  		    		else
  		    			puts "Couldn't find agency - #{agency.inspect}"
  		    		end
  		    	end
  		    end
  	    	case item["Status"]
  		    	when "PUBLIC"
{'fraction_non_alphanumeric': 0.12236328125, 'fraction_numerical': 0.0033203125, 'mean_word_length': 2.697111913357401, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '113917', 'n_tokens_mistral': 3696, 'n_tokens_neox': 3503, 'n_words': 748}