log-search: Auto-limit to the correct logfile when searching datetimes.

This commit is contained in:
Alex Vandiver 2024-02-26 15:44:27 +00:00 committed by Tim Abbott
parent 9eaaacba52
commit 175e61486e
1 changed files with 23 additions and 1 deletions

View File

@ -8,7 +8,7 @@ import os
import re import re
import signal import signal
import sys import sys
from datetime import datetime, timedelta from datetime import date, datetime, timedelta, timezone
from enum import Enum, auto from enum import Enum, auto
from typing import List, Match, Optional, Set, TextIO, Tuple from typing import List, Match, Optional, Set, TextIO, Tuple
@ -248,6 +248,28 @@ def parse_logfile_names(args: argparse.Namespace) -> List[str]:
else: else:
base_path = "/var/log/zulip/server.log" base_path = "/var/log/zulip/server.log"
for term in args.filter_terms:
date_term = re.match(r"2\d\d\d-\d\d-\d\d", term)
if not date_term:
continue
# They're limiting to a specific day; find the right logfile
# which is going to have any matches
rotations = int(
(datetime.now(tz=timezone.utc).date() - date.fromisoformat(date_term[0]))
/ timedelta(days=1)
)
access_log_retention_days = int(
get_config(get_config_file(), "application_server", "access_log_retention_days", "14")
)
if rotations > access_log_retention_days:
raise RuntimeError(f"Date is too old (more than {access_log_retention_days} days ago)")
if rotations == 0:
return [base_path]
if rotations == 1:
return [f"{base_path}.1"]
else:
return [f"{base_path}.{rotations}.gz"]
logfile_names = [base_path] logfile_names = [base_path]
if args.all_logs: if args.all_logs:
logfile_count = 15 logfile_count = 15