Skip to content

Commit

Permalink
Worker 在完成时打印平均速度
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-cq committed Mar 3, 2024
1 parent f81b07a commit 57a630e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
16 changes: 16 additions & 0 deletions alist_sync/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import builtins
import datetime
import hashlib
import logging
import os
Expand Down Expand Up @@ -94,6 +95,21 @@ def timeout_input(msg, default, timeout=3):
return default


def transfer_speed(size, start: datetime.datetime, end: datetime.datetime) -> str:
"""转换速度"""
speed = size * 2 / (end - start).seconds
if speed < 1024:
return f"{speed}B/s"
speed /= 1024
if speed < 1024:
return f"{speed:.2f}KB/s"
speed /= 1024
if speed < 1024:
return f"{speed:.2f}MB/s"
speed /= 1024
return f"{speed:.2f}GB/s"


if __name__ == "__main__":
from pydantic import BaseModel

Expand Down
6 changes: 1 addition & 5 deletions alist_sync/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,5 @@ def dump_to_mongodb(self):


if __name__ == "__main__":
# config = create_config()
# print(config)
# print(config.cache_dir)
# print(config.mongodb)
# print(config.notify)

print(Config.model_json_schema())
9 changes: 8 additions & 1 deletion alist_sync/d_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from alist_sdk.path_lib import AbsAlistPathType, AlistPath

from alist_sync.config import create_config
from alist_sync.common import sha1, prefix_in_threads
from alist_sync.common import sha1, prefix_in_threads, transfer_speed
from alist_sync.err import WorkerError, RetryError
from alist_sync.thread_pool import MyThreadPoolExecutor
from alist_sync.version import __version__
Expand Down Expand Up @@ -116,6 +116,13 @@ def update(self, **field: Any):
logger.info(f"Worker[{self.short_id}] is {self.status}.")
self.done_at = datetime.datetime.now()
sync_config.handle.create_log(self)
if self.status == "done":
logger.info(
f"Worker[{self.short_id}] "
f"{self.source_path} -> {self.target_path} "
f"平均传输速度: "
f"{transfer_speed(self.file_size, self.done_at, self.created_at)}"
)
return sync_config.handle.delete_worker(self.id)

return sync_config.handle.update_worker(self, *field.keys())
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions alist_sync/notice/_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@File Name : _webhook.py
@Author : LeeCQ
@Date-Time : 2024/3/3 21:47
"""
import os

from httpx import Client


class Webhook:
def __init__(self, url: str, headers: dict[str, str] = None):
self.client = Client(base_url=url, headers=headers)

def send(self, data: str):
return self.client.post(
"",
json={
"msg_type": "text",
"content": {"text":data},
},
)


if __name__ == "__main__":
webhook = Webhook(os.getenv("WEBHOOK_URL", ""))
print(webhook.send("test message.").text)
Empty file removed alist_sync/notice/webhook.py
Empty file.
2 changes: 1 addition & 1 deletion alist_sync/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"""
0.1.0-a*:
[] 使用Workers架构重构,使用多线程,不再使用协程
[*] 使用Workers架构重构,使用多线程,不再使用协程
[] New 通知 - email
[] New 通知 - webhook
Expand Down

0 comments on commit 57a630e

Please sign in to comment.