Skip to content

Commit

Permalink
Merge branch 'NAStool:dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurelol authored May 7, 2023
2 parents aac99cd + 2d95b97 commit 1fe7edd
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 89 deletions.
16 changes: 8 additions & 8 deletions web/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __init__(self):
"get_site_seeding_info": self.__get_site_seeding_info,
"clear_tmdb_cache": self.__clear_tmdb_cache,
"check_site_attr": self.__check_site_attr,
"refresh_process": self.__refresh_process,
"refresh_process": self.refresh_process,
"restory_backup": self.__restory_backup,
"start_mediasync": self.__start_mediasync,
"mediasync_state": self.__mediasync_state,
Expand Down Expand Up @@ -1495,9 +1495,9 @@ def re_identification(data):
if not path:
return {"retcode": -1, "retmsg": "未识别路径有误"}
succ_flag, msg = _filetransfer.transfer_media(in_from=SyncType.MAN,
rmt_mode=rmt_mode,
in_path=path,
target_dir=dest_dir)
rmt_mode=rmt_mode,
in_path=path,
target_dir=dest_dir)
if succ_flag:
_filetransfer.update_transfer_unknown_state(path)
else:
Expand All @@ -1520,9 +1520,9 @@ def re_identification(data):
if not path:
return {"retcode": -1, "retmsg": "未识别路径有误"}
succ_flag, msg = _filetransfer.transfer_media(in_from=SyncType.MAN,
rmt_mode=rmt_mode,
in_path=path,
target_dir=dest_dir)
rmt_mode=rmt_mode,
in_path=path,
target_dir=dest_dir)
if not succ_flag:
ret_flag = False
if msg not in ret_msg:
Expand Down Expand Up @@ -2538,7 +2538,7 @@ def __check_site_attr(data):
return {"code": 0, "site_free": site_free, "site_2xfree": site_2xfree, "site_hr": site_hr}

@staticmethod
def __refresh_process(data):
def refresh_process(data):
"""
刷新进度条
"""
Expand Down
27 changes: 24 additions & 3 deletions web/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1670,7 +1670,6 @@ def stream_logging():
"""
实时日志EventSources响应
"""

def __logging(_source=""):
"""
实时日志
Expand All @@ -1679,15 +1678,14 @@ def __logging(_source=""):

while True:
with LoggingLock:
print(f"{LoggingSource} {_source} {log.LOG_INDEX}")
if _source != LoggingSource:
LoggingSource = _source
log.LOG_INDEX = len(log.LOG_QUEUE)
if log.LOG_INDEX > 0:
logs = list(log.LOG_QUEUE)[-log.LOG_INDEX:]
log.LOG_INDEX = 0
if _source:
logs = [l for l in logs if l.get("source") == _source]
logs = [lg for lg in logs if lg.get("source") == _source]
else:
logs = []
time.sleep(1)
Expand All @@ -1698,6 +1696,29 @@ def __logging(_source=""):
mimetype='text/event-stream'
)


@App.route('/stream-progress')
@login_required
def stream_progress():
"""
实时日志EventSources响应
"""
def __progress(_type):
"""
实时日志
"""
WA = WebAction()
while True:
time.sleep(0.2)
detail = WA.refresh_process({"type": _type})
yield 'data: %s\n\n' % json.dumps(detail)

return Response(
__progress(request.args.get("type")),
mimetype='text/event-stream'
)


@Sock.route('/message')
@login_required
def message_handler(ws):
Expand Down
63 changes: 36 additions & 27 deletions web/static/js/functions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* 公共变量区
*/
// 刷新进度
let RefreshProcessFlag = false;
let RefreshFailCount = 0;

// 刷新订阅站点列表
let RssSitesLength = 0;
// 刷新搜索站点列表
Expand All @@ -20,6 +18,8 @@ let NavPageLoading = false;
let NavPageXhr;
// 是否允许打断弹窗
let GlobalModalAbort = true;
// 进度刷新EventSource
let ProgressES;
// 日志来源筛选时关掉之前的刷新日志计时器
let LoggingSource = "";
// 日志EventSource
Expand Down Expand Up @@ -101,7 +101,7 @@ function navmenu(page, newflag = false) {
// 搜索
function media_search(tmdbid, title, type) {
const param = {"tmdbid": tmdbid, "search_word": title, "media_type": type};
show_refresh_process("正在搜索 " + title + " ...", "search");
show_refresh_progress("正在搜索 " + title + " ...", "search");
ajax_post("search", param, function (ret) {
hide_refresh_process();
if (ret.code === 0) {
Expand Down Expand Up @@ -131,6 +131,7 @@ function hide_wait_modal() {
function stop_logging() {
if (LoggingES) {
LoggingES.close();
LoggingES = undefined;
}
}

Expand Down Expand Up @@ -398,26 +399,37 @@ function switch_cooperation_sites(obj) {
$(`#user_auth_${siteid}_params`).show();
}

// 停止刷新进度条
function stop_progress() {
if (ProgressES) {
ProgressES.close();
ProgressES = undefined;
}
}

// 刷新进度条
function refresh_process(type) {
if (!RefreshProcessFlag) {
return;
function start_progress(type) {
stop_progress();
ProgressES = new EventSource(`stream-progress?type=${type}`);
ProgressES.onmessage = function (event) {
render_progress(JSON.parse(event.data))
};
}

// 渲染进度条
function render_progress(ret) {
if (ret.code === 0 && ret.value <= 100) {
$("#modal_process_bar").attr("style", "width: " + ret.value + "%").attr("aria-valuenow", ret.value);
$("#modal_process_text").text(ret.text);
}
if ($("#modal-process").is(":hidden")) {
stop_progress();
}
ajax_post("refresh_process", {type: type}, function (ret) {
if (ret.code === 0 && ret.value <= 100) {
$("#modal_process_bar").attr("style", "width: " + ret.value + "%").attr("aria-valuenow", ret.value);
$("#modal_process_text").text(ret.text);
} else {
RefreshFailCount = RefreshFailCount + 1;
}
if (RefreshFailCount < 5) {
setTimeout("refresh_process('" + type + "')", 200);
}
}, true, false);
}

// 显示全局进度框
function show_refresh_process(title, type) {
function show_refresh_progress(title, type) {
// 显示对话框
if (title) {
$("#modal_process_title").text(title);
} else {
Expand All @@ -426,15 +438,12 @@ function show_refresh_process(title, type) {
$("#modal_process_bar").attr("style", "width: 0%").attr("aria-valuenow", 0);
$("#modal_process_text").text("请稍候...");
$("#modal-process").modal("show");
//刷新进度
RefreshProcessFlag = true;
RefreshFailCount = 0;
refresh_process(type);
// 开始刷新进度条
setTimeout(`start_progress('${type}')`, 1000);
}

// 关闭全局进度框
function hide_refresh_process() {
RefreshProcessFlag = false;
$("#modal-process").modal("hide");
}

Expand Down Expand Up @@ -689,7 +698,7 @@ function show_rss_seasons_modal(name, year, type, mediaid, seasons, func) {
function search_mediainfo_media(tmdbid, title, typestr) {
hide_mediainfo_modal();
const param = {"tmdbid": tmdbid, "search_word": title, "media_type": typestr};
show_refresh_process("正在搜索 " + title + " ...", "search");
show_refresh_progress("正在搜索 " + title + " ...", "search");
ajax_post("search", param, function (ret) {
hide_refresh_process();
if (ret.code === 0) {
Expand Down Expand Up @@ -1352,7 +1361,7 @@ function search_media_advanced() {
};
const param = {"search_word": keyword, "filters": filters, "unident": true};
$("#modal-search-advanced").modal("hide");
show_refresh_process(`正在搜索 ${keyword} ...`, "search");
show_refresh_progress(`正在搜索 ${keyword} ...`, "search");
ajax_post("search", param, function (ret) {
hide_refresh_process();
if (ret.code === 0) {
Expand Down Expand Up @@ -1674,7 +1683,7 @@ function manual_media_transfer() {
"logid": logid
};
$('#modal-media-identification').modal('hide');
show_refresh_process("手动转移 " + inpath, "filetransfer");
show_refresh_progress("手动转移 " + inpath, "filetransfer");
let cmd = (manual_type === '3') ? "rename_udf" : "rename"
ajax_post(cmd, data, function (ret) {
hide_refresh_process();
Expand Down
33 changes: 19 additions & 14 deletions web/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ <h5 class="modal-title">统计数据</h5>
//关闭媒体库同步框
function close_mediasync_modal() {
$("#index-mediasync-modal").modal('hide');
refresh_sync_process_flag = false;
}

//开始媒体库同步
Expand All @@ -350,12 +349,10 @@ <h5 class="modal-title">统计数据</h5>
ajax_post("start_mediasync", {
"librarys": select_GetSelectedVAL("sync_library")
}, function (ret) {
refresh_sync_process_flag = true;
refresh_sync_process();
setTimeout("start_mediasync_progress()", 1000);
}, true, false);
} else {
refresh_sync_process_flag = true;
refresh_sync_process();
setTimeout("start_mediasync_progress()", 1000);
}
}

Expand All @@ -371,22 +368,30 @@ <h5 class="modal-title">统计数据</h5>
});
}

//刷新进度及文件
var refresh_sync_process_flag = false;
function refresh_sync_process() {
if (!refresh_sync_process_flag) {
return;
// 停止刷新进度
function stop_mediasync_progress() {
if (MediaSyncProgressEs) {
MediaSyncProgressEs.close();
MediaSyncProgressEs = undefined;
}
ajax_post("refresh_process", {type: "mediasync"}, function (ret) {
}

//刷新进度
var MediaSyncProgressEs;
function start_mediasync_progress() {
stop_mediasync_progress();
MediaSyncProgressEs = new EventSource(`stream-progress?type=mediasync`);
MediaSyncProgressEs.onmessage = function (event) {
let ret = JSON.parse(event.data);
if (ret.code === 0) {
$("#mediasync_process_bar").attr("style", "width: " + ret.value + "%")
.attr("aria-valuenow", ret.value);
$("#mediasync_status").text(ret.text);
}
if (ret.value < 100) {
setTimeout("refresh_sync_process()", 200);
if ($("#index-mediasync-modal").is(":hidden")) {
stop_mediasync_progress();
}
}, true, false);
}
}

</script>
Expand Down
Loading

0 comments on commit 1fe7edd

Please sign in to comment.