class DailyProjectStatisticsJob < ApplicationJob
  queue_as :cache

  def perform
    date = (Date.today - 1.days).to_s
    daily_data_keys = $redis_cache.keys("v2-project-statistic:*-#{date}")
    daily_data_keys.each do |key| 
      result = $redis_cache.hgetall(key)
      project_id = key.gsub('v2-project-statistic:', '').gsub("-#{date}", '')
      next unless Project.find_by_id(project_id).present?
      visits = result["visits"].to_i 
      watchers = result["watchers"].to_i 
      praises = result["praises"].to_i
      forks = result["forks"].to_i 
      issues = result["issues"].to_i 
      closed_issues = result["closed_issues"].to_i
      pullrequests = result["pullrequests"].to_i 
      commits = result["commits"].to_i 
      score = visits *1 + watchers *5 + praises * 5 + forks * 10 + issues *5 + pullrequests * 10 + commits * 5 
      DailyProjectStatistic.create!(
        project_id: project_id,
        date: date,
        score: score ,
        visits: visits,
        watchers: watchers,
        praises: praises,
        forks: forks, 
        issues: issues,
        closed_issues: closed_issues,
        pullrequests: pullrequests,
        commits: commits
      )
    end
  end
end