#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 if res && res['code'].to_i != 0 raise("实训云平台繁忙(繁忙等级:99)") end 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 _open_game_jupyter(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 @game_jupyter_port = res['port'] 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_url_with_game(game) _open_game_jupyter(game) end def jupyter_port_with_game(game) if @game_jupyter_port.to_i <=0 _open_game_jupyter(shixun) end @game_jupyter_port 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