本地化数据初始化

dev_local_2
daiao 5 years ago
parent 29b486c112
commit aa62b7b416

@ -1,42 +1,75 @@
class InitializationDataController < ApplicationController class InitializationDataController < ApplicationController
# GET: /api/initialization_data.json?paths=169,170,171
def index def index
raise("实践课程id不能为空") if params[:paths].blank? raise("实践课程id不能为空") if params[:paths].blank?
paths = params[:paths].split(",") paths = params[:paths].split(",")
origin_database = EduSetting.where(name: origin_database)&.value origin_database = EduSetting.find_by(name: "origin_database")&.value
database = EduSetting.where(name: database)&.value database = EduSetting.find_by(name: "database")&.value
logger.info("#########{origin_database}")
raise("请配置原始数据库") if origin_database.blank?
raise("请配置本地数据库") if database.blank?
# 获取原始数据库导入数据 # 获取原始数据库导入数据
get_origin_data origin_database, paths get_origin_data origin_database, paths
# 创建初始数据 # 创建初始数据
create_init_data database create_init_data database
normal_status("初始化数据成功!")
end end
private
def get_origin_data origin_database, paths def get_origin_data origin_database, paths
connect_to_origin_date(origin_database) connect_to_origin_date(origin_database)
@subjects = get_subject_data(paths) get_subject_data(paths)
@stages = get_stages_data(@subjects) get_stages_data(@subjects)
@stage_shixuns = get_stage_shixuns_data(@stages) get_stage_shixuns_data(@stages)
@shixuns = get_shixun_data(@stage_shixuns) get_shixun_data(@stage_shixuns)
@shixun_mirror_repositories = get_shixun_mirror_repositories_data(@shixuns) get_shixun_info_data(@shixuns)
@shixun_tag_repertoires = get_shixun_tag_repertoires_data(@shixuns) get_shixun_mirror_repositories_data(@shixuns)
@shixun_service_configs = get_shixun_service_configs_data(@shixuns) get_shixun_tag_repertoires_data(@shixuns)
@challenges = get_challenges_data(@shixuns) get_shixun_service_configs_data(@shixuns)
@challenge_answers = get_challenge_answers_data(@challenges) get_challenges_data(@shixuns)
@challenge_tags = get_challenge_tags_data(@challenges) get_challenge_answers_data(@challenges)
@test_sets = get_test_sets_data(@challenges) get_challenge_tags_data(@challenges)
@challenge_chooses = get_challenge_chooses_data(@challenges) get_test_sets_data(@challenges)
@challenge_questions = get_challenge_questions_data(@challenge_chooses) get_challenge_chooses_data(@challenges)
get_challenge_questions_data(@challenge_chooses)
end end
def create_init_data database def create_init_data database
logger.info("#########subjects: #{@subjects.map(&:id)}")
logger.info("#########stage: #{@stages.map(&:id)}")
logger.info("#########stage_shixuns: #{@stage_shixuns.map(&:id)}")
logger.info("#########shixuns: #{@shixuns.map(&:id)}")
logger.info("#########shixun_infos: #{@shixun_infos.map(&:id)}")
logger.info("#########shixun_mirror_repositories: #{@shixun_mirror_repositories.map(&:id)}")
logger.info("#########shixun_tag_repertoires: #{@shixun_tag_repertoires.map(&:id)}")
logger.info("#########shixun_service_configs: #{@shixun_service_configs.map(&:id)}")
logger.info("#########challenges: #{@challenges.map(&:id)}")
logger.info("#########challenge_answers: #{@challenge_answers.map(&:id)}")
logger.info("#########challenge_tags: #{@challenge_tags.map(&:id)}")
logger.info("#########test_sets: #{@test_sets.map(&:id)}")
logger.info("#########challenge_chooses: #{@challenge_chooses.map(&:id)}")
logger.info("#########challenge_questions: #{@challenge_questions.map(&:id)}")
connect_to_origin_date database connect_to_origin_date database
logger.info("#########subjects: #{@subjects.count}")
logger.info("#########stage: #{@stages.count}")
logger.info("#########stage_shixuns: #{@stage_shixuns.count}")
logger.info("#########shixuns: #{@shixuns.count}")
logger.info("#########shixun_infos: #{@shixun_infos.count}")
logger.info("#########shixun_mirror_repositories: #{@shixun_mirror_repositories.count}")
logger.info("#########shixun_tag_repertoires: #{@shixun_tag_repertoires.count}")
logger.info("#########shixun_service_configs: #{@shixun_service_configs.count}")
logger.info("#########challenges: #{@challenges.count}")
logger.info("#########challenge_answers: #{@challenge_answers.count}")
logger.info("#########challenge_tags: #{@challenge_tags.count}")
logger.info("#########test_sets: #{@test_sets.count}")
logger.info("#########challenge_chooses: #{@challenge_chooses.count}")
logger.info("#########challenge_questions: #{@challenge_questions.count}")
ActiveRecord::Base.transaction do
copy_subject_data copy_subject_data
copy_stages_data copy_stages_data
copy_stage_shixuns_data copy_stage_shixuns_data
copy_shixun_data copy_shixun_data
copy_shixun_info_data
copy_shixun_mirror_repositories_data copy_shixun_mirror_repositories_data
copy_shixun_tag_repertoires_data copy_shixun_tag_repertoires_data
copy_shixun_service_configs_data copy_shixun_service_configs_data
@ -46,7 +79,9 @@ class InitializationDataController < ApplicationController
copy_test_sets_data copy_test_sets_data
copy_challenge_chooses_data copy_challenge_chooses_data
copy_challenge_questions_data copy_challenge_questions_data
copy_shixun_members_data
copy_subject_members_data
end
end end
# 链接库 # 链接库
@ -56,37 +91,39 @@ class InitializationDataController < ApplicationController
host: "localhost", host: "localhost",
username: "root", username: "root",
password: "123456", password: "123456",
database: "#{database}" database: "#{database}",
encoding: "utf8"
) )
end end
# 获取需要的路径 # 获取需要的路径
def get_subject_data paths def get_subject_data paths
Subject.where(id: paths) @subjects = Subject.where(id: paths)
end end
# 创建需要的路径 # 创建需要的路径
def copy_subject_data def copy_subject_data
logger.info("##############subjects: #{@subjects.map(&:id)}")
subject_attrs = %i[id name description status major_id learning_notes introduction homepage_show user_id subject_attrs = %i[id name description status major_id learning_notes introduction homepage_show user_id
repertoire_id score_count publish_time subject_level_system_id excellent] repertoire_id score_count publish_time subject_level_system_id excellent created_at updated_at stages_count
stage_shixuns_count shixuns_count]
Subject.bulk_insert(*subject_attrs) do |worker| Subject.bulk_insert(*subject_attrs) do |worker|
@subjects.each do |subject| @subjects.each do |subject|
base_attr = subject.attributes.dup.except("user_id", "stages_count", "stage_shixuns_count", "shixuns_count") base_attr = subject.attributes.dup.except("user_id")
worker.add(base_attr.merge(user_id: 1)) worker.add(base_attr.merge(user_id: 1))
# 复制成员
SubjectMember.create(subject_id: subject.id, user_id: 1, role: 1)
end end
end end
end end
# 获取实践课程的章节 # 获取实践课程的章节
def get_stages_data subjects def get_stages_data subjects
Stage.where(subject_id: subjects) @stages = Stage.where(subject_id: subjects)
end end
# 创建需要的章节 # 创建需要的章节
def copy_stages_data def copy_stages_data
stage_attrs = %i[id subject_id name description user_id position ] logger.info("##############stages: #{@stages.map(&:id)}")
stage_attrs = %i[id subject_id name description user_id position created_at updated_at]
Stage.bulk_insert(*stage_attrs) do |worker| Stage.bulk_insert(*stage_attrs) do |worker|
@stages.each do |stage| @stages.each do |stage|
base_attr = stage.attributes.dup.except("user_id", "shixuns_count") base_attr = stage.attributes.dup.except("user_id", "shixuns_count")
@ -97,12 +134,12 @@ class InitializationDataController < ApplicationController
# 获取实践课程的关联实训表 # 获取实践课程的关联实训表
def get_stage_shixuns_data stages def get_stage_shixuns_data stages
StageShixun.where(stage_id: stages) @stage_shixuns = StageShixun.where(stage_id: stages)
end end
# 创建实践课程关联实训表 # 创建实践课程关联实训表
def copy_stage_shixuns_data def copy_stage_shixuns_data
stage_shixun_attrs = %i[id subject_id stage_id shixun_id position] stage_shixun_attrs = %i[id subject_id stage_id shixun_id position created_at updated_at]
StageShixun.bulk_insert(*stage_shixun_attrs) do |worker| StageShixun.bulk_insert(*stage_shixun_attrs) do |worker|
@stage_shixuns.each do |stage_shixun| @stage_shixuns.each do |stage_shixun|
base_attr = stage_shixun.attributes.dup base_attr = stage_shixun.attributes.dup
@ -113,37 +150,34 @@ class InitializationDataController < ApplicationController
# 获取实训数据 # 获取实训数据
def get_shixun_data stage_shixuns def get_shixun_data stage_shixuns
Shixun.where(id: stage_shixuns.map(&:shixun_id)) @shixuns = Shixun.where(id: stage_shixuns.map(&:shixun_id))
end end
# 创建实训数据 # 创建实训数据
def copy_shixun_data def copy_shixun_data
shixun_attrs = %i[id name user_id gpid status authentication identifier trainee major_id webssh homepage_show hidden shixun_attrs = %i[id name user_id gpid status authentication identifier trainee major_id webssh homepage_show hidden
fork_from can_copy modify_time reset_time publish_time closer_id end_time git_url vnc fork_from can_copy modify_time reset_time publish_time closer_id end_time git_url vnc created_at updated_at
mirror_script_id image_text code_hidden task_pass exec_time test_set_permission sigle_training hide_code mirror_script_id image_text code_hidden task_pass exec_time test_set_permission sigle_training hide_code
multi_webssh excute_time repo_name averge_star opening_time forbid_copy pod_life local_status evaluate_script multi_webssh excute_time repo_name averge_star opening_time forbid_copy pod_life]
description propaedeutics]
Shixun.bulk_insert(*shixun_attrs) do |worker| Shixun.bulk_insert(*shixun_attrs) do |worker|
@shixuns.each do |shixun| @shixuns.each do |shixun|
base_attr = shixun.attributes.dup.except("user_id", "challenges_count", "myshixuns_count", "use_scope") base_attr = shixun.attributes.dup.except("user_id", "challenges_count", "myshixuns_count", "use_scope")
logger.info("#######shixun:#{base_attr}")
worker.add(base_attr.merge(user_id: 1)) worker.add(base_attr.merge(user_id: 1))
# 创建实训成员
ShixunMember.create(user_id: 1, shixun_id: shixun.id, role: 1)
end end
end end
end end
# 获取关卡数据 # 获取关卡数据
def get_challenges_data shixuns def get_challenges_data shixuns
Challenge.where(shixun_id: shixuns) @challenges = Challenge.where(shixun_id: shixuns)
end end
# 创建关卡数据 # 创建关卡数据
def copy_challenges_data def copy_challenges_data
challenge_attrs = %i[id shixun_id subject user_id status position task_pass score path evaluation_way difficulty challenge_attrs = %i[id shixun_id subject user_id status position task_pass score path evaluation_way difficulty
exec_path code_line st web_route picture_path expect_picture_path modify_time challenge_tags_count original_picture_path exec_path code_line st web_route picture_path expect_picture_path modify_time challenge_tags_count original_picture_path
show_type test_set_score test_set_average exec_time] show_type test_set_score test_set_average exec_time created_at updated_at]
Challenge.bulk_insert(*challenge_attrs) do |worker| Challenge.bulk_insert(*challenge_attrs) do |worker|
@challenges.each do |challenge| @challenges.each do |challenge|
base_attr = challenge.attributes.dup.except("user_id", "praises_count", "memory_limit", "request_limit", "time_limit", base_attr = challenge.attributes.dup.except("user_id", "praises_count", "memory_limit", "request_limit", "time_limit",
@ -155,12 +189,12 @@ class InitializationDataController < ApplicationController
# 获取参考答案 # 获取参考答案
def get_challenge_answers_data challenges def get_challenge_answers_data challenges
ChallengeAnswer.where(challenge_id: challenges) @challenge_answers = ChallengeAnswer.where(challenge_id: challenges)
end end
# 复制参考答案 # 复制参考答案
def copy_challenge_answers_data def copy_challenge_answers_data
answer_attrs = %i[id name contents score level challenge_id] answer_attrs = %i[id name contents score level challenge_id created_at updated_at]
ChallengeAnswer.bulk_insert(*answer_attrs) do |worker| ChallengeAnswer.bulk_insert(*answer_attrs) do |worker|
@challenge_answers.each do |answer| @challenge_answers.each do |answer|
base_attr = answer.attributes.dup base_attr = answer.attributes.dup
@ -171,12 +205,12 @@ class InitializationDataController < ApplicationController
# 获取关卡标签数据 # 获取关卡标签数据
def get_challenge_tags_data challenges def get_challenge_tags_data challenges
ChallengeTag.where(challenge_id: challenges) @challenge_tags = ChallengeTag.where(challenge_id: challenges)
end end
# 创建关卡标签数据 # 创建关卡标签数据
def copy_challenge_tags_data def copy_challenge_tags_data
tag_attrs = %i[id name challenge_id challenge_choose_id] tag_attrs = %i[id name challenge_id challenge_choose_id created_at updated_at]
ChallengeTag.bulk_insert(*tag_attrs) do |worker| ChallengeTag.bulk_insert(*tag_attrs) do |worker|
@challenge_tags.each do |tag| @challenge_tags.each do |tag|
base_attr = tag.attributes.dup base_attr = tag.attributes.dup
@ -187,12 +221,12 @@ class InitializationDataController < ApplicationController
# 获取实训镜像标签数据 # 获取实训镜像标签数据
def get_shixun_mirror_repositories_data shixuns def get_shixun_mirror_repositories_data shixuns
ShixunMirrorRepository.where(shixun_id: shixuns) @shixun_mirror_repositories = ShixunMirrorRepository.where(shixun_id: shixuns)
end end
# 创建实训镜像标签 # 创建实训镜像标签
def copy_shixun_mirror_repositories_data def copy_shixun_mirror_repositories_data
repository_attrs = %i[id shixun_id mirror_repository_id] repository_attrs = %i[id shixun_id mirror_repository_id created_at updated_at]
ShixunMirrorRepository.bulk_insert(*repository_attrs) do |worker| ShixunMirrorRepository.bulk_insert(*repository_attrs) do |worker|
@shixun_mirror_repositories.each do |repository| @shixun_mirror_repositories.each do |repository|
base_attr = repository.attributes.dup base_attr = repository.attributes.dup
@ -203,12 +237,12 @@ class InitializationDataController < ApplicationController
# 获取实训标签 # 获取实训标签
def get_shixun_tag_repertoires_data shixuns def get_shixun_tag_repertoires_data shixuns
ShixunTagRepertoire.where(shixun_id: shixuns) @shixun_tag_repertoires = ShixunTagRepertoire.where(shixun_id: shixuns)
end end
# 创建实训标签 # 创建实训标签
def copy_shixun_tag_repertoires_data def copy_shixun_tag_repertoires_data
repository_attrs = %i[id shixun_id tag_repertoire_id] repository_attrs = %i[id shixun_id tag_repertoire_id created_at updated_at]
ShixunTagRepertoire.bulk_insert(*repository_attrs) do |worker| ShixunTagRepertoire.bulk_insert(*repository_attrs) do |worker|
@shixun_tag_repertoires.each do |repository| @shixun_tag_repertoires.each do |repository|
base_attr = repository.attributes.dup base_attr = repository.attributes.dup
@ -219,12 +253,13 @@ class InitializationDataController < ApplicationController
# 获取实训镜像配置 # 获取实训镜像配置
def get_shixun_service_configs_data shixuns def get_shixun_service_configs_data shixuns
ShixunServiceConfig.where(shixun_id: shixuns) @shixun_service_configs = ShixunServiceConfig.where(shixun_id: shixuns)
end end
# 创建镜像配置 # 创建镜像配置
def copy_shixun_service_configs_data def copy_shixun_service_configs_data
config_attrs = %i[id shixun_id cpu_limit memory_limit request_limit lower_cpu_limit resource_limit mirror_repository_id] config_attrs = %i[id shixun_id cpu_limit memory_limit request_limit lower_cpu_limit resource_limit
mirror_repository_id created_at updated_at]
ShixunServiceConfig.bulk_insert(*config_attrs) do |worker| ShixunServiceConfig.bulk_insert(*config_attrs) do |worker|
@shixun_service_configs.each do |config| @shixun_service_configs.each do |config|
base_attr = config.attributes.dup base_attr = config.attributes.dup
@ -235,12 +270,12 @@ class InitializationDataController < ApplicationController
# 获取测试集数据 # 获取测试集数据
def get_test_sets_data challenges def get_test_sets_data challenges
TestSet.where(challenge_id: challenges) @test_sets = TestSet.where(challenge_id: challenges)
end end
# 创建测试集数据 # 创建测试集数据
def copy_test_sets_data def copy_test_sets_data
set_attrs = %i[id input output challenge_id is_public result position score] set_attrs = %i[id input output challenge_id is_public result position score created_at updated_at]
TestSet.bulk_insert(*set_attrs) do |worker| TestSet.bulk_insert(*set_attrs) do |worker|
@test_sets.each do |set| @test_sets.each do |set|
base_attr = set.attributes.dup base_attr = set.attributes.dup
@ -251,12 +286,12 @@ class InitializationDataController < ApplicationController
# 获取选择题题目 # 获取选择题题目
def get_challenge_chooses_data challenges def get_challenge_chooses_data challenges
ChallengeChoose.where(challenge_id: challenges) @challenge_chooses = ChallengeChoose.where(challenge_id: challenges)
end end
# 创建选择题题目数据 # 创建选择题题目数据
def copy_challenge_chooses_data def copy_challenge_chooses_data
choose_attrs = %i[id subject challenge_id standard_answer answer score difficult category position] choose_attrs = %i[id subject challenge_id standard_answer answer score difficult category position created_at updated_at]
ChallengeChoose.bulk_insert(*choose_attrs) do |worker| ChallengeChoose.bulk_insert(*choose_attrs) do |worker|
@challenge_chooses.each do |choose| @challenge_chooses.each do |choose|
base_attr = choose.attributes.dup base_attr = choose.attributes.dup
@ -267,12 +302,12 @@ class InitializationDataController < ApplicationController
# 复制选择题选项 # 复制选择题选项
def get_challenge_questions_data challenge_chooses def get_challenge_questions_data challenge_chooses
ChallengeQuestion.where(challenge_choose_id: challenge_chooses) @challenge_questions = ChallengeQuestion.where(challenge_choose_id: challenge_chooses)
end end
# 创建选择题选项数据 # 创建选择题选项数据
def copy_challenge_questions_data def copy_challenge_questions_data
question_attrs = %i[id option_name challenge_choose_id right_key position] question_attrs = %i[id option_name challenge_choose_id right_key position created_at updated_at]
ChallengeQuestion.bulk_insert(*question_attrs) do |worker| ChallengeQuestion.bulk_insert(*question_attrs) do |worker|
@challenge_questions.each do |question| @challenge_questions.each do |question|
base_attr = question.attributes.dup base_attr = question.attributes.dup
@ -281,5 +316,43 @@ class InitializationDataController < ApplicationController
end end
end end
# 获取实训长字段内容
def get_shixun_info_data shixuns
@shixun_infos = ShixunInfo.where(shixun_id: shixuns)
end
# 创建实训长字段内容
def copy_shixun_info_data
info_attrs = %i[id propaedeutics description evaluate_script shixun_id created_at updated_at]
ShixunInfo.bulk_insert(*info_attrs) do |worker|
@shixun_infos.each do |info|
base_attr = info.attributes.dup
worker.add(base_attr)
end
end
end
# 创建实训成员
def copy_shixun_members_data
member_attrs = %i[user_id shixun_id role created_at updated_at]
ShixunMember.bulk_insert(*member_attrs) do |worker|
@shixuns.each do |shixun|
base_attr = {shixun_id: shixun.id, user_id: 1, role: 1}
worker.add(base_attr)
end
end
end
# 创建实践课程成员
def copy_subject_members_data
member_attrs = %i[user_id subject_id role created_at updated_at position]
SubjectMember.bulk_insert(*member_attrs) do |worker|
@subjects.each do |subject|
base_attr = {subject_id: subject.id, user_id: 1, role: 1, position: 1}
worker.add(base_attr)
end
end
end
end end

Loading…
Cancel
Save