fix: add models and utils modules
This commit is contained in:
@@ -1,65 +1,32 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Tuple
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
def get_date_range(days: int) -> Tuple[datetime, datetime]:
|
||||
"""Get date range for the last N days."""
|
||||
end_date = datetime.now()
|
||||
start_date = end_date - timedelta(days=days)
|
||||
return start_date, end_date
|
||||
def parse_date(date_str: Optional[str]) -> Optional[datetime]:
|
||||
"""Parse a date string into a datetime object."""
|
||||
if not date_str:
|
||||
return None
|
||||
|
||||
|
||||
def parse_timestamp(timestamp_str: str) -> datetime:
|
||||
"""Parse a timestamp string to datetime."""
|
||||
formats = [
|
||||
"%Y-%m-%d",
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
"%Y-%m-%dT%H:%M:%S",
|
||||
"%Y-%m-%d %H:%M:%S%z",
|
||||
"%Y-%m-%dT%H:%M:%SZ",
|
||||
"%Y/%m/%d",
|
||||
"%m/%d/%Y",
|
||||
"%d/%m/%Y",
|
||||
]
|
||||
|
||||
for fmt in formats:
|
||||
try:
|
||||
return datetime.strptime(timestamp_str, fmt)
|
||||
return datetime.strptime(date_str, fmt)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
raise ValueError(f"Cannot parse timestamp: {timestamp_str}")
|
||||
return None
|
||||
|
||||
|
||||
def format_duration(seconds: float) -> str:
|
||||
"""Format duration in human-readable format."""
|
||||
if seconds < 60:
|
||||
return f"{seconds:.1f}s"
|
||||
elif seconds < 3600:
|
||||
minutes = seconds / 60
|
||||
return f"{minutes:.1f}m"
|
||||
elif seconds < 86400:
|
||||
hours = seconds / 3600
|
||||
return f"{hours:.1f}h"
|
||||
else:
|
||||
days = seconds / 86400
|
||||
return f"{days:.1f}d"
|
||||
|
||||
|
||||
def group_by_period(
|
||||
commits: list,
|
||||
period: str = "day",
|
||||
) -> dict:
|
||||
"""Group commits by time period."""
|
||||
result = {}
|
||||
for commit in commits:
|
||||
if period == "hour":
|
||||
key = commit.timestamp.strftime("%Y-%m-%d %H:00")
|
||||
elif period == "day":
|
||||
key = commit.timestamp.strftime("%Y-%m-%d")
|
||||
elif period == "week":
|
||||
key = commit.timestamp.strftime("%Y-%W")
|
||||
elif period == "month":
|
||||
key = commit.timestamp.strftime("%Y-%m")
|
||||
else:
|
||||
key = commit.timestamp.strftime("%Y-%m-%d")
|
||||
|
||||
result[key] = result.get(key, 0) + 1
|
||||
|
||||
return result
|
||||
def get_date_range(days: int) -> Tuple[datetime, datetime]:
|
||||
"""Get a date range for the last N days."""
|
||||
end_date = datetime.now()
|
||||
start_date = end_date - timedelta(days=days)
|
||||
return start_date, end_date
|
||||
|
||||
Reference in New Issue
Block a user