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/services/jupyter_service.rb

134 lines
4.0 KiB

5 years ago
#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
5 years ago
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级99")
end
5 years ago
5 years ago
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
5 years ago
5 years ago
def _open_game_jupyter(myshixun)
5 years ago
## 打开tpi
5 years ago
shixun = myshixun.shixun
5 years ago
if shixun.is_jupyter?
shixun_tomcat = edu_setting('cloud_bridge')
uri = "#{shixun_tomcat}/bridge/jupyter/get"
5 years ago
tpiID = myshixun.id
5 years ago
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
5 years ago
@game_jupyter_port = res['port']
5 years ago
repo_save_path = myshixun.repo_save_path
5 years ago
"https://#{res['port']}.jupyter.educoder.net/notebooks/data/workspace/myshixun_#{tpiID}/#{repo_save_path}/01.ipynb"
end
end
5 years ago
def jupyter_url_with_game(myshixun)
_open_game_jupyter(myshixun)
5 years ago
end
5 years ago
def jupyter_port_with_game(myshixun)
5 years ago
if @game_jupyter_port.to_i <=0
5 years ago
_open_game_jupyter(myshixun)
5 years ago
end
@game_jupyter_port
end
5 years ago
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
5 years ago
src_url = "https://#{jupyter_port}.jupyter.educoder.net/nbconvert/notebook/data/workspace/myshixun_#{tpiID}/01.ipynb?download=true"
response = Faraday.get(src_url)
5 years ago
5 years ago
if response.status.to_i != 200
raise("获取文件内容失败:#{response.status}")
5 years ago
end
content = response.body
5 years ago
c = GitService.update_file(repo_path: shixun.repo_path,
5 years ago
file_path: "01.ipynb",
message: message,
content: content,
author_name: author_name,
author_email: author_email)
return c.size
end
5 years ago
def jupyter_save_with_game(myshixun,jupyter_port)
5 years ago
author_name = current_user.real_name
author_email = current_user.git_mail
message = "User submitted"
5 years ago
tpiID = myshixun.id
5 years ago
5 years ago
repo_save_path = myshixun.repo_save_path
5 years ago
5 years ago
src_url = "https://#{jupyter_port}.jupyter.educoder.net/nbconvert/notebook/data/workspace/myshixun_#{tpiID}/#{repo_save_path}/01.ipynb?download=true"
response = Faraday.get(src_url)
5 years ago
5 years ago
if response.status.to_i != 200
raise("获取文件内容失败:#{response.status}")
5 years ago
end
content = response.body
5 years ago
c = GitService.update_file(repo_path: myshixun.repo_path,
5 years ago
file_path: "01.ipynb",
message: message,
content: content,
author_name: author_name,
author_email: author_email)
return c.size
end
5 years ago
end