Skip to content

Commit

Permalink
cf better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
CroMarmot committed Jul 29, 2023
1 parent c8d9729 commit 0e628f0
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 28 deletions.
1 change: 1 addition & 0 deletions docs/dev/publish.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ config `~/.pypirc` follow https://packaging.python.org/en/latest/specifications/

.. code-block::
pip install .[dev]
rm -rf dist/
python -m build
twine check dist/*
Expand Down
2 changes: 1 addition & 1 deletion oi_cli2/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.2.2"
__version__ = "0.2.2.4"
4 changes: 2 additions & 2 deletions oi_cli2/cli/adaptor/AtCoderAdaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def transform_Result(res: CORE_SUB_RES) -> SubmissionResult:
quick_key=res.url, # for refetch result
url=res.url, # TODO change to webpage url
state_note=str(res.score),
time_note=str(res.time_cost_ms / 1000),
mem_note=str(res.mem_cost_kb),
time_note=str(res.time_cost_ms / 1000) + ' ms',
mem_note=str(res.mem_cost_kb) + ' kb',
msg_txt=res.msg_txt,
)

Expand Down
2 changes: 2 additions & 0 deletions oi_cli2/cli/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
COOKIES_FILE = '_cookies.json'
ACCT_CONFIG_FILE = '_acctConfig.json' # TODO split config json
TEMP_CONFIG_FILE = '_tempConfig.json'
# LOG
APP_NAME='oiTerminal'

IN_SUFFIX = 'in.'
OUT_SUFFIX = 'out.'
Expand Down
5 changes: 2 additions & 3 deletions oi_cli2/cli/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ def generate_submission_table(res: SubmissionResult) -> Table:
table = Table().grid()
table.add_column(min_width=12)
table.add_column()
table.add_row("Source", "web page")
table.add_row("Result ID", f"{res.id}")
# "[red]ERROR" if value < 50 else "[green]SUCCESS"
table.add_row("Status", Text.from_ansi(f"{status_string(res)}"))
table.add_row("Time(s)", f"{res.time_note}")
table.add_row("Memory(KB)", f"{res.mem_note}")
table.add_row("Time", f"{res.time_note}")
table.add_row("Memory", f"{res.mem_note}")
if res.msg_txt:
table.add_row("MSG", f"{res.msg_txt}")
if res.url:
Expand Down
54 changes: 37 additions & 17 deletions oi_cli2/custom/Codeforces/Codeforces.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from requests.exceptions import ReadTimeout, ConnectTimeout

from oi_cli2.cli.constant import CIPHER_KEY, GREEN, DEFAULT
from oi_cli2.cli.constant import APP_NAME, CIPHER_KEY, GREEN, DEFAULT
from oi_cli2.model.BaseOj import BaseOj
from oi_cli2.model.ParseProblemResult import ParsedProblemResult
from oi_cli2.model.LangKV import LangKV
Expand Down Expand Up @@ -38,6 +38,7 @@ def __init__(self, http_util: AioHttpHelperInterface, logger: logging.Logger, ac
self.logger: logging.Logger = logger
self.account: Account = account
self.http = http_util
self.api_sub_logger:logging.Logger = logging.getLogger(f'{APP_NAME}.yxr-cf-core')

async def init(self) -> None:
await self.http.open_session()
Expand Down Expand Up @@ -148,19 +149,14 @@ async def async_submit_code(self, sid: str, language_id: str, code_path: str) ->
contest_id=contest_id,
level=problem_key,
file_path=code_path,
lang_id=language_id)
lang_id=language_id,
logger=self.api_sub_logger)
self.logger.debug(f'submit_id = {submit_id}')
return bool(submit_id)

async def async_get_result_yield(self, problem_url: str, time_gap: float = 2) -> AsyncIterator[SubmissionResult]:
contest_id, problem_key = problem_url_parse(problem_url)

# return (end watch?, transform result)
def custom_handler(result: Any) -> Tuple[bool, SubmissionWSResult]:
parsed_data = transform_submission(result)
if parsed_data.msg != 'TESTING':
return True, parsed_data
return False, parsed_data

# TODO move more parse inside codeforces-core ? cf是出错中断形式,状态+数量
def page_result_transform(res: SubmissionPageResult) -> SubmissionResult:
Expand All @@ -176,21 +172,28 @@ def page_result_transform(res: SubmissionPageResult) -> SubmissionResult:
cur_status = SubmissionResult.Status.AC
elif res.verdict.startswith('Wrong answer'):
cur_status = SubmissionResult.Status.WA
elif res.verdict.startswith('Time limit exceeded'):
cur_status = SubmissionResult.Status.TLE
elif res.verdict.startswith('Runtime error'): # Runtime error on pretest 2
cur_status = SubmissionResult.Status.RE
else:
self.logger.error('NOT HANDLE PAGE:'+str(res.verdict))

if res.url.startswith('/'):
res.url = 'https://codeforces.com' + res.url

return SubmissionResult(id=res.id,
cur_status=cur_status,
time_note=res.time_ms,
mem_note=res.mem_bytes,
time_note=res.time_ms + ' ms',
mem_note=str(int(res.mem_bytes)/1000) + ' kb',
url=res.url,
msg_txt=res.verdict)




# TODO move more parse inside codeforces-core ?
def ws_result_transform(res: SubmissionWSResult) -> SubmissionResult:
self.logger.debug('ws res:'+str(res))
cur_status = SubmissionResult.Status.PENDING
if res.msg == 'TESTING':
cur_status = SubmissionResult.Status.RUNNING
Expand All @@ -202,30 +205,47 @@ def ws_result_transform(res: SubmissionWSResult) -> SubmissionResult:
self.logger.error('NOT HANDLE WS:' + str(res.msg))

msg_txt = str(res.testcases)
if cur_status == SubmissionResult.Status.AC:
if cur_status in [SubmissionResult.Status.AC, SubmissionResult.Status.WA]:
msg_txt = f'{res.passed}/{res.testcases}'

return SubmissionResult(
id=str(res.submit_id),
cur_status=cur_status,
time_note=str(res.ms),
mem_note=str(res.mem),
url=f'/contest/{res.contest_id}/submission/{res.submit_id}',
time_note=str(res.ms) + ' ms',
mem_note=str(int(res.mem)/1000) + ' kb',
url=f'https://codeforces.com/contest/{res.contest_id}/submission/{res.submit_id}',
msg_txt=msg_txt,
)

# TODO visit page without ws first
results = await async_fetch_submission_page(http=self.http, problem_url=problem_url)
results = await async_fetch_submission_page(http=self.http, problem_url=problem_url,logger=self.api_sub_logger)
fix_submit_id = ''
if len(results) > 0:
result = page_result_transform(results[0])
fix_submit_id = result.id
self.logger.debug(f"fix submit_id = {fix_submit_id}");
yield result
if result.cur_status not in [SubmissionResult.Status.PENDING, SubmissionResult.Status.RUNNING]:
return

self.logger.debug('after page result, enter ws result')

# return (end watch?, transform result)
def custom_handler(result: Any) -> Tuple[bool, SubmissionWSResult]:
parsed_data = transform_submission(result)
if fix_submit_id and fix_submit_id != parsed_data.contest_id: # submit id not match, dont end watch ws
return False, parsed_data
if parsed_data.msg != 'TESTING':
return True, parsed_data
return False, parsed_data

# TODO add timeout for ws
async for wsresult in create_contest_ws_task_yield(http=self.http, contest_id=contest_id, ws_handler=custom_handler):
# TODO 可能有别人的? pc/cc?
async for wsresult in create_contest_ws_task_yield(http=self.http, contest_id=contest_id, ws_handler=custom_handler,logger=self.api_sub_logger):
self.logger.debug('ws res:'+str(wsresult))
if fix_submit_id and wsresult.submit_id != fix_submit_id:
self.logger.debug('[skip]fixed id not match! continue')
continue
data = ws_result_transform(wsresult)
yield data
if data.cur_status not in [SubmissionResult.Status.PENDING, SubmissionResult.Status.RUNNING]:
Expand Down
4 changes: 2 additions & 2 deletions oi_cli2/model/Result.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Status(Enum):
url: str = '' # print for user
quick_key: str = '' # for refetch result
state_note: str = '0'
time_note: str = '0/0'
mem_note: str = '0/0'
time_note: str = ''
mem_note: str = ''
msg_txt: str = ''


Expand Down
7 changes: 5 additions & 2 deletions oi_cli2/utils/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from oi_cli2.utils.Singleton import Singleton

from oi_cli2.cli.constant import APP_NAME


@Singleton
class LogConfig:
Expand All @@ -29,15 +31,16 @@ def getLogger(logger_path):
print(e)
traceback.print_exc()

logger = logging.getLogger(__name__)
# 调用库的 logging 名是 {APP_NAME}.xxx
logger = logging.getLogger(APP_NAME)
# basic level shoud not larger than handler
logger.setLevel(logging.DEBUG)
# remove default handler
logging.getLogger().handlers.clear()

# file
fh = logging.FileHandler(logger_path)
fileformatter = logging.Formatter('[%(asctime)s %(levelname)s %(filename)s %(funcName)s %(lineno)d]: %(message)s')
fileformatter = logging.Formatter('[%(asctime)s %(name)s %(levelname)s %(filename)s %(funcName)s %(lineno)d]: %(message)s')
fh.setFormatter(fileformatter)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies = [
"click >= 8.1.3",
"rich >= 11.0.0",
"yxr-atcoder-core == 0.0.3.3",
"yxr-codeforces-core == 0.0.2.1",
"yxr-codeforces-core == 0.0.2.2",
]
readme = "README.md"
requires-python = ">=3.8"
Expand All @@ -34,6 +34,7 @@ license = { file = "LICENSE" }
oi = "oi_cli2.cli.main:main"

[project.optional-dependencies]
dev = ['build']
tests = ['build', 'coverage >= 6', 'pytest >= 7']

[tool.hatch.build]
Expand Down

0 comments on commit 0e628f0

Please sign in to comment.