owner_common = $redis_cache.hgetall("v2-owner-common:#{item[0]}")
deleted_data = $redis_cache.smembers("v2-project-rank-deleted")
$redis_cache.zrem("v2-user-project-rank:#{item[0]}", deleted_data) unless deleted_data.blank?
popular_project = $redis_cache.zrevrange("v2-user-project-rank:#{item[0]}", 0, 1, withscores: true)[0]
json.id item[0]
json.score item[1]
json.name owner_common["name"]
json.type owner_common["type"]
json.login owner_common["login"]
if owner_common["tmp_projects"].nil? 
  normal_projects = Project.members_projects(item[0]).to_sql
  org_projects = Project.joins(team_projects: [team: :team_users]).where(team_users: {user_id: item[0]}).to_sql
  issue_project = Project.joins(:issues).where(issues: {author_id: item[0]}).to_sql
  pull_project = Project.joins(:pull_requests).where(pull_requests: {user_id: item[0]}).to_sql
  commit_project = Project.joins(:commit_logs).where(commit_logs: {user_id: item[0]}).to_sql
  project_count =   Project.from("( #{normal_projects} UNION #{org_projects} UNION #{issue_project} UNION #{pull_project} UNION #{issue_project} ) AS projects").distinct.size

  $redis_cache.hset("v2-owner-common:#{item[0]}", 'tmp_projects', project_count)
  json.projects_count project_count
else 
  json.projects_count owner_common["tmp_projects"]
end
json.avatar_url owner_common["avatar_url"]
if popular_project.blank?
  json.project nil
else
  popular_project_common = $redis_cache.hgetall("v2-project-common:#{popular_project[0]}")
  if popular_project_common["owner_id"].to_s == item[0].to_s
    json.project do
      json.id popular_project[0]
      json.name popular_project_common["name"]
      json.identifier popular_project_common["identifier"]
      json.description popular_project_common["description"]
    end
  else
    # owner changed remove rank
    $redis_cache.zrem("v2-user-project-rank:#{item[0]}", popular_project[0])
    json.project nil
  end
end

ids = $redis_cache.zrevrange("v2-user-project-rank:#{item[0]}", 0, 999, withscores: true).map{|a|a[0]}
visits = 0
forks = 0
watchers = 0
praises = 0
issues = 0
pulls = 0
commits = 0
issues = 0
ids.each do |pid|
  project_common = $redis_cache.hgetall("v2-project-common:#{pid}")
  visits = visits + project_common["visits"].to_i
  forks = forks + project_common["forks"].to_i
  watchers = watchers + project_common["watchers"].to_i
  praises = praises + project_common["praises"].to_i
  issues = issues + project_common["issues"].to_i
  pulls = pulls + project_common["pullrequests"].to_i
  commits = commits + project_common["commits"].to_i
  issues = issues + project_common["issues"].to_i
end

json.visits visits
json.forks forks
json.watchers watchers
json.praises praises
json.issues issues
json.pulls pulls
json.commits commits
json.issues issues