<commit_before>require 'json'
require 'sitemap_generator'


namespace :data do
  
  task :environment do
    require_relative '../../config/environment'
  end
  
  desc 'Generate top file'
  task :generate_top => :environment do
  	File.open("/www/putin.io/data/top.json","w") do |f|
  		top = Models::Vote.top
  		top.each do |key, array|
  			top[key]  = array.map { |e|  
  				{:name => e.candidate.name.force_encoding("UTF-8"), :count => e.ammount_sum, :id => e.candidate.id}
  			} 
        end 
      f.write(top.to_json)  
		end
  end

  desc 'Generate candidate each  stat file'
  task :generate_all => :environment do
    agg = Models::Vote.aggregate_by_date
    result = {}
    agg.each do |item|
      unless result[item['candidate_id']].present?
        result[item['candidate_id']] = {}
      end

      unless result[item['candidate_id']]['pro'].present?
        result[item['candidate_id']]['pro'] = {}
      end

        unless result[item['candidate_id']]['contra'].present?
        result[item['candidate_id']]['contra'] = {}
      end

        unless result[item['candidate_id']]['who'].present?
        result[item['candidate_id']]['who'] = {}
      end

      unless result[item['candidate_id']]['pro'][item['day']].present?
        result[item['candidate_id']]['pro'][item['day']] = 0
      end 

      unless result[item['candidate_id']]['contra'][item['day']].present?
        result[item['candidate_id']]['contra'][item['day']] = 0
      end

      unless result[item['candidate_id']]['who'][item['day']].present?
        result[item['candidate_id']]['who'][item['day']] = 0
      end
        
      result[item['candidate_id']][item['subject']][item['day']] = item['ammount_sum']
      result[item['candidate_id']][:candidate] =   {:name => item.candidate.name.force_encoding("UTF-8"), :description => item.candidate.description.force_encoding("UTF-8"), :image => item.candidate.image, :id => item.candidate.id}

    end 

    result.each do |candidate_id, content|
      File.open("/www/putin.io/data/candidate/#{candidate_id}.json","w") do |f|
        f.write(content.to_json) 
      end 
    end
  end

  desc 'Generate sitemap'
  task :generate_sitemap => :environment do
    SitemapGenerator::Sitemap.default_host = 'http://putin.io'
    SitemapGenerator::Sitemap.public_path = '/www/putin.io/'
    SitemapGenerator::Sitemap.create do
      add '/', :changefreq => 'daily', :priority => 0.9<commit_msg>add stitemap<commit_after>require 'json'
require 'sitemap_generator'


namespace :data do
  
  task :environment do
    require_relative '../../config/environment'
  end
  
  desc 'Generate top file'
  task :generate_top => :environment do
  	File.open("/www/putin.io/data/top.json","w") do |f|
  		top = Models::Vote.top
  		top.each do |key, array|
  			top[key]  = array.map { |e|  
  				{:name => e.candidate.name.force_encoding("UTF-8"), :count => e.ammount_sum, :id => e.candidate.id}
  			} 
        end 
      f.write(top.to_json)  
		end
  end

  desc 'Generate candidate each  stat file'
  task :generate_all => :environment do
    agg = Models::Vote.aggregate_by_date
    result = {}
    agg.each do |item|
      unless result[item['candidate_id']].present?
        result[item['candidate_id']] = {}
      end

      unless result[item['candidate_id']]['pro'].present?
        result[item['candidate_id']]['pro'] = {}
      end

        unless result[item['candidate_id']]['contra'].present?
        result[item['candidate_id']]['contra'] = {}
      end

        unless result[item['candidate_id']]['who'].present?
        result[item['candidate_id']]['who'] = {}
      end

      unless result[item['candidate_id']]['pro'][item['day']].present?
        result[item['candidate_id']]['pro'][item['day']] = 0
      end 

      unless result[item['candidate_id']]['contra'][item['day']].present?
        result[item['candidate_id']]['contra'][item['day']] = 0
      end

      unless result[item['candidate_id']]['who'][item['day']].present?
        result[item['candidate_id']]['who'][item['day']] = 0
      end
        
      result[item['candidate_id']][item['subject']][item['day']] = item['ammount_sum']
      result[item['candidate_id']][:candidate] =   {:name => item.candidate.name.force_encoding("UTF-8"), :description => item.candidate.description.force_encoding("UTF-8"), :image => item.candidate.image, :id => item.candidate.id}

    end 

    result.each do |candidate_id, content|
      File.open("/www/putin.io/data/candidate/#{candidate_id}.json","w") do |f|
        f.write(content.to_json) 
      end 
    end
  end

  desc 'Generate sitemap'
  task :generate_sitemap => :environment do
    SitemapGenerator::Sitemap.default_host = 'http://putin.io'
    SitemapGenerator::Sitemap.public_path = '/www/putin.io/'
    SitemapGenerator::Sitemap.compress = false
    SitemapGenerator::Sitemap.create do
      add '/', :changefreq => 'daily', :priority => 0.9
{'fraction_non_alphanumeric': 0.1790801790801791, 'fraction_numerical': 0.003256003256003256, 'mean_word_length': 3.341872791519435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '64037', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1665, 'n_words': 264}