Skip to content

Commit

Permalink
Merge pull request #383 from codeforjapan/feature/382-add-new-taks
Browse files Browse the repository at this point in the history
add rake task
  • Loading branch information
ayuki-joto authored May 25, 2022
2 parents 07027e1 + a0ced26 commit d1035a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
38 changes: 2 additions & 36 deletions bin/get_user_data.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
#!/bin/bash
#
# A script for retrieving user data from the decidim database
# You need to set below environment variables before running this script
# This script is obsolated. please use rake download:users task
#
# export RDS_DB_NAME=ebdb
# export RDS_PASSWORD=password
# export RDS_HOSTNAME=database-host
# export RDS_USERNAME=decidimapp
#

function print_orgs() {
export PGPASSWORD=$RDS_PASSWORD; psql -h $RDS_HOSTNAME -U $RDS_USERNAME -c "SELECT id, name FROM decidim_organizations" $RDS_DB_NAME
}

if [ "$1" = "" ]; then
print_orgs
echo "-------------------"
echo "please specify ORG_ID ; get_user_data.sh ORG_ID"
exit 1
fi

echo ORG_ID is $1
ORG_ID=$1

out_file=$(mktemp)
json_file=$(mktemp)


echo "[" > ${json_file}
export PGPASSWORD=$RDS_PASSWORD; psql -h $RDS_HOSTNAME -U $RDS_USERNAME -c "SELECT json_build_object('id', B.id, 'nickname',nickname,'email', email, 'created_at', to_char(B.created_at, 'YYYY/MM/DD'),'sign_in_count', sign_in_count, 'last_sign_in_at', to_char(last_sign_in_at, 'YYYY/MM/DD'), 'data', metadata) FROM public.decidim_authorizations as A RIGHT OUTER JOIN decidim_users as B on A.decidim_user_id = B.id where decidim_organization_id = ${ORG_ID} and B.deleted_at is null;" -A -t $RDS_DB_NAME > ${out_file}

awk -v eof=`wc -l ${out_file} | awk '{print $1}'` 'BEGIN{ORS = ",\n"}{if (NR==eof) ORS=""; print $0}' ${out_file} >> ${json_file}
echo "]" >> ${json_file}


echo "id, created_at, sign_in_count, last_sign_in, nickname, real_name, email, gender, address, birth_year, occupation" > /tmp/metadata.csv

cat ${json_file} | jq -r '.[] | [.id,.created_at, .sign_in_count,.last_sign_in_at,.nickname,.data.real_name, .email, .data.gender, .data.address, .data.birth_year, .data.occupation] | @csv' >> /tmp/metadata.csv
echo "This script is obsolated. Please use: rake download:users"

echo "/tmp/metadata.csv is created"
47 changes: 47 additions & 0 deletions lib/tasks/download.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "csv"
namespace :download do
desc "Download users list including extended attirubtion"
task :users, ["org_id"] => :environment do |_task, args|
file = Rails.root.join("tmp/user_data.csv")
if args.org_id
download_users(file, args.org_id)
else
puts "*********************"
puts "[ERROR] please specify org_id"
puts "example: rake download:users[1]"
puts "*********************"
Decidim::Organization.all.order(:id).each do |org|
puts "#{org.id}:#{org.name}"
end
end
end

def format_date(datevalue)
return nil unless datevalue

datevalue.strftime("%Y/%m/%d %H:%M:%S")
end

def download_users(file, id)
puts "[INFO] Creating csv files. It will take few minutes"
headers = %w(id created_at sign_in_count last_sign_in nickname name email real_name gender address birth_year occupation)
organization = Decidim::Organization.find(id)
Time.zone = organization.time_zone
CSV.open(file, "w", write_headers: true, headers: headers, force_quotes: true) do |writer|
organization.users.where(deleted_at: [nil, ""]).each do |user|
auth = Decidim::Authorization.find_by(decidim_user_id: user.id)
metadata = [nil, nil, nil, nil, nil]
if auth
metadata = [auth.metadata["real_name"], auth.metadata["gender"],
auth.metadata["address"], auth.metadata["birth_year"],
auth.metadata["occupation"]]
end
writer << [user.id, format_date(user.created_at), user.sign_in_count,
format_date(user.last_sign_in_at), user.nickname, user.name, user.email] + metadata
end
end
puts "[INFO] success: #{file} was created."
end
end

0 comments on commit d1035a9

Please sign in to comment.