Aggregate hours and show in table

This commit is contained in:
Кокос Артем Николаевич
2026-01-20 17:01:35 +07:00
parent 910cc31ecf
commit 7f1018a2d4
4 changed files with 38 additions and 25 deletions

View File

@@ -1,12 +1,14 @@
from typing import List, Optional, Set
from typing import List, Optional, Dict, Tuple
from redminelib import Redmine
from redminelib.resources import Issue
from .config import Config
def fetch_issues_by_time_entries(from_date: str, to_date: str) -> Optional[List[Issue]]:
def fetch_issues_with_spent_time(from_date: str, to_date: str) -> Optional[List[Tuple[Issue, float]]]:
"""
Fetch unique issues linked to time entries of the current user in given date range.
Fetch unique issues linked to time entries of the current user in given date range,
along with total spent hours per issue.
Returns list of (issue, total_hours) tuples.
"""
redmine = Redmine(
@@ -23,15 +25,19 @@ def fetch_issues_by_time_entries(from_date: str, to_date: str) -> Optional[List[
to_date=to_date
)
issue_ids: Set[int] = set()
# Агрегируем часы по issue.id
spent_time: Dict[int, float] = {}
issue_ids = set()
for entry in time_entries:
if hasattr(entry, 'issue') and entry.issue:
issue_ids.add(entry.issue.id)
if hasattr(entry, 'issue') and entry.issue and hasattr(entry, 'hours'):
iid = entry.issue.id
issue_ids.add(iid)
spent_time[iid] = spent_time.get(iid, 0.0) + float(entry.hours)
if not issue_ids:
return None
# Fetch full issue objects with project/version/status
# Загружаем полные объекты задач
issue_list_str = ','.join(str(i) for i in issue_ids)
issues = redmine.issue.filter(
issue_id=issue_list_str,
@@ -39,4 +45,12 @@ def fetch_issues_by_time_entries(from_date: str, to_date: str) -> Optional[List[
sort='project:asc'
)
return list(issues)
# Сопоставляем задачи с суммарным временем
result = []
for issue in issues:
total_hours = spent_time.get(issue.id, 0.0)
result.append((issue, total_hours))
# Сортируем по проекту (Redmine API уже сортирует, но для надёжности)
result.sort(key=lambda x: str(x[0].project))
return result