You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/controllers/initialization_data_control...

401 lines
15 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

class InitializationDataController < ApplicationController
# GET: /api/initialization_data.json?paths=169,170,171
def index
raise("实践课程id不能为空") if params[:paths].blank?
paths = params[:paths].split(",")
origin_database = EduSetting.find_by(name: "origin_database")&.value
database = EduSetting.find_by(name: "database")&.value
logger.info("#########{origin_database}")
raise("请配置原始数据库") if origin_database.blank?
raise("请配置本地数据库") if database.blank?
# 获取原始数据库导入数据 TODO: 附件同步问题。 attachemnts表
get_origin_data origin_database, paths
# 创建初始数据
create_init_data database
normal_status("初始化数据成功!")
end
def clear_test_data
# 删除测试数据.
ActiveRecord::Base.transaction do
User.where("id > 2").destroy_all
Myshixun.where(user: 1).destroy_all
Course.where(tea_id: 1).destroy_all
# 清理垃圾高校
School.where("id > 1").destroy_all
# 清理实训评论
Discuss.destroy_all
Tiding.destroy_all
ApplyAddDepartment.destroy_all
ApplyAddSchool.destroy_all
normal_status("初始化数据成功!")
end
end
def get_origin_data origin_database, paths
connect_to_origin_date(origin_database)
get_subject_data(paths)
get_stages_data(@subjects)
get_stage_shixuns_data(@stages)
get_shixun_data(@stage_shixuns)
get_shixun_info_data(@shixuns)
get_shixun_mirror_repositories_data(@shixuns)
get_shixun_tag_repertoires_data(@shixuns)
get_shixun_service_configs_data(@shixuns)
get_challenges_data(@shixuns)
get_challenge_answers_data(@challenges)
get_challenge_tags_data(@challenges)
get_test_sets_data(@challenges)
get_challenge_chooses_data(@challenges)
get_challenge_questions_data(@challenge_chooses)
end
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
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_stages_data
copy_stage_shixuns_data
copy_shixun_data
copy_shixun_info_data
copy_shixun_mirror_repositories_data
copy_shixun_tag_repertoires_data
copy_shixun_service_configs_data
copy_challenges_data
copy_challenge_answers_data
copy_challenge_tags_data
copy_test_sets_data
copy_challenge_chooses_data
copy_challenge_questions_data
copy_shixun_members_data
copy_subject_members_data
end
end
# 链接库
def connect_to_origin_date database
ActiveRecord::Base.establish_connection(
adapter: "mysql2",
host: "rm-bp13v5020p7828r5r.mysql.rds.aliyuncs.com",
username: "testeducoder",
password: "TEST@123",
database: "#{database}",
encoding: "utf8"
)
end
# 获取需要的路径
def get_subject_data paths
@subjects = Subject.where(id: paths)
end
# 创建需要的路径
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
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|
@subjects.each do |subject|
base_attr = subject.attributes.dup.except("user_id")
worker.add(base_attr.merge(user_id: 1))
end
end
end
# 获取实践课程的章节
def get_stages_data subjects
@stages = Stage.where(subject_id: subjects)
end
# 创建需要的章节
def copy_stages_data
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|
@stages.each do |stage|
base_attr = stage.attributes.dup.except("user_id", "shixuns_count")
worker.add(base_attr.merge(user_id: 1))
end
end
end
# 获取实践课程的关联实训表
def get_stage_shixuns_data stages
@stage_shixuns = StageShixun.where(stage_id: stages)
end
# 创建实践课程关联实训表
def copy_stage_shixuns_data
stage_shixun_attrs = %i[id subject_id stage_id shixun_id position created_at updated_at]
StageShixun.bulk_insert(*stage_shixun_attrs) do |worker|
@stage_shixuns.each do |stage_shixun|
base_attr = stage_shixun.attributes.dup
worker.add(base_attr)
end
end
end
# 获取实训数据
def get_shixun_data stage_shixuns
@shixuns = Shixun.where(id: stage_shixuns.map(&:shixun_id))
end
# 创建实训数据
def copy_shixun_data
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 created_at updated_at
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 challenges_count]
Shixun.bulk_insert(*shixun_attrs) do |worker|
@shixuns.each do |shixun|
base_attr = shixun.attributes.dup.except("user_id", "myshixuns_count", "use_scope")
logger.info("#######shixun:#{base_attr}")
worker.add(base_attr.merge(user_id: 1))
end
end
end
# 获取关卡数据
def get_challenges_data shixuns
@challenges = Challenge.where(shixun_id: shixuns)
end
# 创建关卡数据
def copy_challenges_data
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
show_type test_set_score test_set_average exec_time created_at updated_at]
Challenge.bulk_insert(*challenge_attrs) do |worker|
@challenges.each do |challenge|
base_attr = challenge.attributes.dup.except("user_id", "praises_count", "memory_limit", "request_limit", "time_limit",
"lower_cpu_limit", "cpu_limit", "visits")
worker.add(base_attr.merge(user_id: 1))
end
end
end
# 获取参考答案
def get_challenge_answers_data challenges
@challenge_answers = ChallengeAnswer.where(challenge_id: challenges)
end
# 复制参考答案
def copy_challenge_answers_data
answer_attrs = %i[id name contents score level challenge_id created_at updated_at]
ChallengeAnswer.bulk_insert(*answer_attrs) do |worker|
@challenge_answers.each do |answer|
base_attr = answer.attributes.dup
worker.add(base_attr)
end
end
end
# 获取关卡标签数据
def get_challenge_tags_data challenges
@challenge_tags = ChallengeTag.where(challenge_id: challenges)
end
# 创建关卡标签数据
def copy_challenge_tags_data
tag_attrs = %i[id name challenge_id challenge_choose_id created_at updated_at]
ChallengeTag.bulk_insert(*tag_attrs) do |worker|
@challenge_tags.each do |tag|
base_attr = tag.attributes.dup
worker.add(base_attr)
end
end
end
# 获取实训镜像标签数据
def get_shixun_mirror_repositories_data shixuns
@shixun_mirror_repositories = ShixunMirrorRepository.where(shixun_id: shixuns)
end
# 创建实训镜像标签
def copy_shixun_mirror_repositories_data
repository_attrs = %i[id shixun_id mirror_repository_id created_at updated_at]
ShixunMirrorRepository.bulk_insert(*repository_attrs) do |worker|
@shixun_mirror_repositories.each do |repository|
base_attr = repository.attributes.dup
worker.add(base_attr)
end
end
end
# 获取实训标签
def get_shixun_tag_repertoires_data shixuns
@shixun_tag_repertoires = ShixunTagRepertoire.where(shixun_id: shixuns)
end
# 创建实训标签
def copy_shixun_tag_repertoires_data
repository_attrs = %i[id shixun_id tag_repertoire_id created_at updated_at]
ShixunTagRepertoire.bulk_insert(*repository_attrs) do |worker|
@shixun_tag_repertoires.each do |repository|
base_attr = repository.attributes.dup
worker.add(base_attr)
end
end
end
# 获取实训镜像配置
def get_shixun_service_configs_data shixuns
@shixun_service_configs = ShixunServiceConfig.where(shixun_id: shixuns)
end
# 创建镜像配置
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 created_at updated_at]
ShixunServiceConfig.bulk_insert(*config_attrs) do |worker|
@shixun_service_configs.each do |config|
base_attr = config.attributes.dup
worker.add(base_attr)
end
end
end
# 获取测试集数据
def get_test_sets_data challenges
@test_sets = TestSet.where(challenge_id: challenges)
end
# 创建测试集数据
def copy_test_sets_data
set_attrs = %i[id input output challenge_id is_public result position score created_at updated_at]
TestSet.bulk_insert(*set_attrs) do |worker|
@test_sets.each do |set|
base_attr = set.attributes.dup
worker.add(base_attr)
end
end
end
# 获取选择题题目
def get_challenge_chooses_data challenges
@challenge_chooses = ChallengeChoose.where(challenge_id: challenges)
end
# 创建选择题题目数据
def copy_challenge_chooses_data
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|
@challenge_chooses.each do |choose|
base_attr = choose.attributes.dup
worker.add(base_attr)
end
end
end
# 复制选择题选项
def get_challenge_questions_data challenge_chooses
@challenge_questions = ChallengeQuestion.where(challenge_choose_id: challenge_chooses)
end
# 创建选择题选项数据
def copy_challenge_questions_data
question_attrs = %i[id option_name challenge_choose_id right_key position created_at updated_at]
ChallengeQuestion.bulk_insert(*question_attrs) do |worker|
@challenge_questions.each do |question|
base_attr = question.attributes.dup
worker.add(base_attr)
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
# 获取关卡附件
def get_attachment_data
task_pass = Challenge.where(id: @challenges).where("task_pass like '%/api/attachments/%'").pluck(:task_pass)
@att_ids = []
# 附件的格式为(/api/attachments/ + 附件id的形式因此下面的正文匹配是为了取出附件的id方便迁移attachments
task_pass.each do |tp|
@att_ids += tp.scan(/\(\/api\/attachments\/.+\)/).map{|s|s.match(/\d+/)[0]}
end
end
# 创建附件数据
def copy_attachment_data
return if @att_ids.blank?
attachment_attrs = %i[id container_id container_type filename disk_filename filesize content_type digest author_id created_on
downloads description disk_directory attachtype is_public copy_from quotes is_publish publish_time resource_bank_id unified_setting
cloud_url course_second_category_id]
Attachment.bulk_insert(*attachment_attrs) do |worker|
Attachment.where(id: @att_ids).find_each do |att|
base_attr = att.attributes.except("author_id", "disk_directory", "copy_from")
# 本地版将过关任务的图片迁移到files/localshixuns 目录下, copy_from记录的是他原来的目录结构
worker.add(base_attr.merge({author_id: 1, disk_directory: "localshixuns", copy_from: att.disk_directory}))
end
end
end
end