diff --git a/app/controllers/jupyters_controller.rb b/app/controllers/jupyters_controller.rb index 9e137946f..5d3f05cb4 100644 --- a/app/controllers/jupyters_controller.rb +++ b/app/controllers/jupyters_controller.rb @@ -2,6 +2,8 @@ require 'net/http' class JupytersController < ApplicationController + include JupyterService + before_action :shixun def open diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index b410db0e2..ccb8ee511 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -3,6 +3,7 @@ class ShixunsController < ApplicationController include ApplicationHelper include ElasticsearchAble include CoursesHelper + include JupyterService before_action :require_login, :check_auth, except: [:download_file, :index, :menus, :show, :show_right, :ranking_list, :discusses, :collaborators, :fork_list, :propaedeutics] @@ -26,7 +27,7 @@ class ShixunsController < ApplicationController - helper_method :jupyter_url + helper_method :jupyter_url_with_shixun, :jupyter_port_with_shixun ## 获取课程列表 def index @@ -532,22 +533,7 @@ class ShixunsController < ApplicationController # @evaluate_scirpt = @shixun.evaluate_script || "无" end - def jupyter_url(shixun) - #打开tpm - juypter接口 - if shixun.is_jupyter? - shixun_tomcat = edu_setting('cloud_bridge') - uri = "#{shixun_tomcat}/bridge/jupyter/get" - tpiID = "tpm#{shixun.id}" - params = {tpiID: tpiID, :containers => "#{Base64.urlsafe_encode64(shixun_container_limit(shixun))}"} - logger.info "test_juypter: uri->#{uri}, params->#{params}" - res = uri_post uri, params - logger.info "test_juypter: #{res}" - - return "https://#{res['port']}.jupyter.educoder.net/notebooks/data/workspace/myshixun_#{tpiID}/01.ipynb" - end - - end # 获取脚本内容 def get_script_contents diff --git a/app/services/jupyter_service.rb b/app/services/jupyter_service.rb new file mode 100644 index 000000000..2c1e07c08 --- /dev/null +++ b/app/services/jupyter_service.rb @@ -0,0 +1,115 @@ +#coding=utf-8 + +module JupyterService + + def _open_shixun_jupyter(shixun) + if shixun.is_jupyter? + shixun_tomcat = edu_setting('cloud_bridge') + uri = "#{shixun_tomcat}/bridge/jupyter/get" + tpiID = "tpm#{shixun.id}" + params = {tpiID: tpiID, :containers => "#{Base64.urlsafe_encode64(shixun_container_limit(shixun))}"} + + logger.info "test_juypter: uri->#{uri}, params->#{params}" + res = uri_post uri, params + logger.info "test_juypter: #{res}" + + @shixun_jupyter_port = res['port'] + + return "https://#{res['port']}.jupyter.educoder.net/notebooks/data/workspace/myshixun_#{tpiID}/01.ipynb" + end + end + + def jupyter_url_with_shixun(shixun) + #打开tpm - juypter接口 + _open_shixun_jupyter(shixun) + end + + def jupyter_port_with_shixun(shixun) + if @shixun_jupyter_port.to_i <=0 + _open_shixun_jupyter(shixun) + end + @shixun_jupyter_port + end + + def jupyter_url_with_game(game) + ## 打开tpi + shixun = game.myshixun.shixun + + if shixun.is_jupyter? + shixun_tomcat = edu_setting('cloud_bridge') + uri = "#{shixun_tomcat}/bridge/jupyter/get" + + tpiID = game.myshixun.id + params = {tpiID: tpiID, :containers => "#{Base64.urlsafe_encode64(shixun_container_limit(shixun))}"} + res = uri_post uri, params + + logger.info "test_juypter: #{res}" + + if res && res['code'].to_i != 0 + raise("实训云平台繁忙(繁忙等级:99)") + end + + repo_save_path = game.myshixun.repo_save_path + + "https://#{res['port']}.jupyter.educoder.net/notebooks/data/workspace/myshixun_#{tpiID}/#{repo_save_path}/01.ipynb" + end + end + + + def jupyter_save_with_shixun(shixun,jupyter_port) + author_name = current_user.real_name + author_email = current_user.git_mail + message = "User submitted" + + tpiID = "tpm#{shixun.id}" + + #https://47526.jupyter.educoder.net/nbconvert/notebook/data/workspace/myshixun_570461/f2ef5p798r20191210163135/01.ipynb?download=true + src_url = URI("https://#{jupyter_port}.jupyter.educoder.net/nbconvert/notebook/data/workspace/myshixun_#{tpiID}/01.ipynb?download=true") + response = Net::HTTP.get_response(src_url) + + if response.code.to_i != 200 + raise("获取文件内容失败:#{response.code}") + end + + content = response.body + + c = GitService.update_file(repo_path: @shixun.repo_path, + file_path: "01.ipynb", + message: message, + content: content, + author_name: author_name, + author_email: author_email) + + return c.size + end + + def jupyter_save_with_game(game,jupyter_port) + author_name = current_user.real_name + author_email = current_user.git_mail + message = "User submitted" + + tpiID = game.myshixun.id + + repo_save_path = game.myshixun.repo_save_path + + src_url = URI("https://#{jupyter_port}.jupyter.educoder.net/nbconvert/notebook/data/workspace/myshixun_#{tpiID}/#{repo_save_path}/01.ipynb?download=true") + response = Net::HTTP.get_response(src_url) + + if response.code.to_i != 200 + raise("获取文件内容失败:#{response.code}") + end + + content = response.body + + c = GitService.update_file(repo_path: game.myshixun.repo_path, + file_path: "01.ipynb", + message: message, + content: content, + author_name: author_name, + author_email: author_email) + + return c.size + end + + +end \ No newline at end of file diff --git a/app/views/shixuns/show.json.jbuilder b/app/views/shixuns/show.json.jbuilder index 4a0c3ebd6..93647cda7 100644 --- a/app/views/shixuns/show.json.jbuilder +++ b/app/views/shixuns/show.json.jbuilder @@ -3,3 +3,5 @@ json.identity User.current.shixun_identity(@shixun) json.power @power json.partial! 'shixuns/top', locals: { shixun: @shixun, current_myshixun: @current_myshixun } json.secret_repository @shixun.shixun_secret_repository.present? +json.jupyter_url jupyter_url_with_shixun(@shixun) +json.jupyter_port jupyter_port_with_shixun(@shixun) \ No newline at end of file